vlc-video-source.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. #include "vlc-video-plugin.h"
  2. #include <media-io/video-frame.h>
  3. #include <util/threading.h>
  4. #include <util/platform.h>
  5. #include <util/dstr.h>
  6. #define do_log(level, format, ...) \
  7. blog(level, "[vlc_source: '%s'] " format, \
  8. obs_source_get_name(ss->source), ##__VA_ARGS__)
  9. #define warn(format, ...) do_log(LOG_WARNING, format, ##__VA_ARGS__)
  10. /* clang-format off */
  11. #define S_PLAYLIST "playlist"
  12. #define S_LOOP "loop"
  13. #define S_SHUFFLE "shuffle"
  14. #define S_BEHAVIOR "playback_behavior"
  15. #define S_BEHAVIOR_STOP_RESTART "stop_restart"
  16. #define S_BEHAVIOR_PAUSE_UNPAUSE "pause_unpause"
  17. #define S_BEHAVIOR_ALWAYS_PLAY "always_play"
  18. #define S_NETWORK_CACHING "network_caching"
  19. #define S_TRACK "track"
  20. #define S_SUBTITLE_ENABLE "subtitle_enable"
  21. #define S_SUBTITLE_TRACK "subtitle"
  22. #define T_(text) obs_module_text(text)
  23. #define T_PLAYLIST T_("Playlist")
  24. #define T_LOOP T_("LoopPlaylist")
  25. #define T_SHUFFLE T_("shuffle")
  26. #define T_BEHAVIOR T_("PlaybackBehavior")
  27. #define T_BEHAVIOR_STOP_RESTART T_("PlaybackBehavior.StopRestart")
  28. #define T_BEHAVIOR_PAUSE_UNPAUSE T_("PlaybackBehavior.PauseUnpause")
  29. #define T_BEHAVIOR_ALWAYS_PLAY T_("PlaybackBehavior.AlwaysPlay")
  30. #define T_NETWORK_CACHING T_("NetworkCaching")
  31. #define T_TRACK T_("AudioTrack")
  32. #define T_SUBTITLE_ENABLE T_("SubtitleEnable")
  33. #define T_SUBTITLE_TRACK T_("SubtitleTrack")
  34. /* clang-format on */
  35. /* ------------------------------------------------------------------------- */
  36. struct media_file_data {
  37. char *path;
  38. libvlc_media_t *media;
  39. };
  40. enum behavior {
  41. BEHAVIOR_STOP_RESTART,
  42. BEHAVIOR_PAUSE_UNPAUSE,
  43. BEHAVIOR_ALWAYS_PLAY,
  44. };
  45. struct vlc_source {
  46. obs_source_t *source;
  47. libvlc_media_player_t *media_player;
  48. libvlc_media_list_player_t *media_list_player;
  49. struct obs_source_frame frame;
  50. struct obs_source_audio audio;
  51. size_t audio_capacity;
  52. pthread_mutex_t mutex;
  53. DARRAY(struct media_file_data) files;
  54. enum behavior behavior;
  55. bool loop;
  56. bool shuffle;
  57. obs_hotkey_id play_pause_hotkey;
  58. obs_hotkey_id restart_hotkey;
  59. obs_hotkey_id stop_hotkey;
  60. obs_hotkey_id playlist_next_hotkey;
  61. obs_hotkey_id playlist_prev_hotkey;
  62. };
  63. static libvlc_media_t *get_media(struct darray *array, const char *path)
  64. {
  65. DARRAY(struct media_file_data) files;
  66. libvlc_media_t *media = NULL;
  67. files.da = *array;
  68. for (size_t i = 0; i < files.num; i++) {
  69. const char *cur_path = files.array[i].path;
  70. if (strcmp(path, cur_path) == 0) {
  71. media = files.array[i].media;
  72. libvlc_media_retain_(media);
  73. break;
  74. }
  75. }
  76. return media;
  77. }
  78. static inline libvlc_media_t *create_media_from_file(const char *file)
  79. {
  80. return (file && strstr(file, "://") != NULL)
  81. ? libvlc_media_new_location_(libvlc, file)
  82. : libvlc_media_new_path_(libvlc, file);
  83. }
  84. static void free_files(struct darray *array)
  85. {
  86. DARRAY(struct media_file_data) files;
  87. files.da = *array;
  88. for (size_t i = 0; i < files.num; i++) {
  89. bfree(files.array[i].path);
  90. libvlc_media_release_(files.array[i].media);
  91. }
  92. da_free(files);
  93. }
  94. #define MAKEFORMAT(ch0, ch1, ch2, ch3) \
  95. ((uint32_t)(uint8_t)(ch0) | ((uint32_t)(uint8_t)(ch1) << 8) | \
  96. ((uint32_t)(uint8_t)(ch2) << 16) | ((uint32_t)(uint8_t)(ch3) << 24))
  97. static inline bool chroma_is(const char *chroma, const char *val)
  98. {
  99. return *(uint32_t *)chroma == *(uint32_t *)val;
  100. }
  101. static enum video_format convert_vlc_video_format(char *chroma, bool *full)
  102. {
  103. *full = false;
  104. #define CHROMA_TEST(val, ret) \
  105. if (chroma_is(chroma, val)) \
  106. return ret
  107. #define CHROMA_CONV(val, new_val, ret) \
  108. do { \
  109. if (chroma_is(chroma, val)) { \
  110. *(uint32_t *)chroma = (uint32_t)new_val; \
  111. return ret; \
  112. } \
  113. } while (false)
  114. #define CHROMA_CONV_FULL(val, new_val, ret) \
  115. do { \
  116. *full = true; \
  117. CHROMA_CONV(val, new_val, ret); \
  118. } while (false)
  119. CHROMA_TEST("RGBA", VIDEO_FORMAT_RGBA);
  120. CHROMA_TEST("BGRA", VIDEO_FORMAT_BGRA);
  121. /* 4:2:0 formats */
  122. CHROMA_TEST("NV12", VIDEO_FORMAT_NV12);
  123. CHROMA_TEST("I420", VIDEO_FORMAT_I420);
  124. CHROMA_TEST("IYUV", VIDEO_FORMAT_I420);
  125. CHROMA_CONV("NV21", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_NV12);
  126. CHROMA_CONV("I422", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_NV12);
  127. CHROMA_CONV("Y42B", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_NV12);
  128. CHROMA_CONV("YV12", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_NV12);
  129. CHROMA_CONV("yv12", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_NV12);
  130. CHROMA_CONV_FULL("J420", MAKEFORMAT('J', '4', '2', '0'),
  131. VIDEO_FORMAT_I420);
  132. /* 4:2:2 formats */
  133. CHROMA_TEST("UYVY", VIDEO_FORMAT_UYVY);
  134. CHROMA_TEST("UYNV", VIDEO_FORMAT_UYVY);
  135. CHROMA_TEST("UYNY", VIDEO_FORMAT_UYVY);
  136. CHROMA_TEST("Y422", VIDEO_FORMAT_UYVY);
  137. CHROMA_TEST("HDYC", VIDEO_FORMAT_UYVY);
  138. CHROMA_TEST("AVUI", VIDEO_FORMAT_UYVY);
  139. CHROMA_TEST("uyv1", VIDEO_FORMAT_UYVY);
  140. CHROMA_TEST("2vuy", VIDEO_FORMAT_UYVY);
  141. CHROMA_TEST("2Vuy", VIDEO_FORMAT_UYVY);
  142. CHROMA_TEST("2Vu1", VIDEO_FORMAT_UYVY);
  143. CHROMA_TEST("YUY2", VIDEO_FORMAT_YUY2);
  144. CHROMA_TEST("YUYV", VIDEO_FORMAT_YUY2);
  145. CHROMA_TEST("YUNV", VIDEO_FORMAT_YUY2);
  146. CHROMA_TEST("V422", VIDEO_FORMAT_YUY2);
  147. CHROMA_TEST("YVYU", VIDEO_FORMAT_YVYU);
  148. CHROMA_CONV("v210", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  149. CHROMA_CONV("cyuv", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  150. CHROMA_CONV("CYUV", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  151. CHROMA_CONV("VYUY", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  152. CHROMA_CONV("NV16", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  153. CHROMA_CONV("NV61", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  154. CHROMA_CONV("I410", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  155. CHROMA_CONV("I422", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  156. CHROMA_CONV("Y42B", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  157. CHROMA_CONV("J422", MAKEFORMAT('U', 'Y', 'V', 'Y'), VIDEO_FORMAT_UYVY);
  158. /* 4:4:4 formats */
  159. CHROMA_TEST("I444", VIDEO_FORMAT_I444);
  160. CHROMA_CONV_FULL("J444", MAKEFORMAT('R', 'G', 'B', 'A'),
  161. VIDEO_FORMAT_RGBA);
  162. CHROMA_CONV("YUVA", MAKEFORMAT('R', 'G', 'B', 'A'), VIDEO_FORMAT_RGBA);
  163. /* 4:4:0 formats */
  164. CHROMA_CONV("I440", MAKEFORMAT('I', '4', '4', '4'), VIDEO_FORMAT_I444);
  165. CHROMA_CONV("J440", MAKEFORMAT('I', '4', '4', '4'), VIDEO_FORMAT_I444);
  166. /* 4:1:0 formats */
  167. CHROMA_CONV("YVU9", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_UYVY);
  168. CHROMA_CONV("I410", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_UYVY);
  169. /* 4:1:1 formats */
  170. CHROMA_CONV("I411", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_UYVY);
  171. CHROMA_CONV("Y41B", MAKEFORMAT('N', 'V', '1', '2'), VIDEO_FORMAT_UYVY);
  172. /* greyscale formats */
  173. CHROMA_TEST("GREY", VIDEO_FORMAT_Y800);
  174. CHROMA_TEST("Y800", VIDEO_FORMAT_Y800);
  175. CHROMA_TEST("Y8 ", VIDEO_FORMAT_Y800);
  176. #undef CHROMA_CONV_FULL
  177. #undef CHROMA_CONV
  178. #undef CHROMA_TEST
  179. *(uint32_t *)chroma = (uint32_t)MAKEFORMAT('B', 'G', 'R', 'A');
  180. return VIDEO_FORMAT_BGRA;
  181. }
  182. static inline unsigned get_format_lines(enum video_format format,
  183. unsigned height, size_t plane)
  184. {
  185. switch (format) {
  186. case VIDEO_FORMAT_I420:
  187. case VIDEO_FORMAT_NV12:
  188. return (plane == 0) ? height : height / 2;
  189. case VIDEO_FORMAT_YVYU:
  190. case VIDEO_FORMAT_YUY2:
  191. case VIDEO_FORMAT_UYVY:
  192. case VIDEO_FORMAT_I444:
  193. case VIDEO_FORMAT_RGBA:
  194. case VIDEO_FORMAT_BGRA:
  195. case VIDEO_FORMAT_BGRX:
  196. case VIDEO_FORMAT_Y800:
  197. return height;
  198. case VIDEO_FORMAT_NONE:
  199. default:
  200. break;
  201. }
  202. return 0;
  203. }
  204. static enum audio_format convert_vlc_audio_format(char *format)
  205. {
  206. #define AUDIO_TEST(val, ret) \
  207. if (chroma_is(format, val)) \
  208. return ret
  209. #define AUDIO_CONV(val, new_val, ret) \
  210. do { \
  211. if (chroma_is(format, val)) { \
  212. *(uint32_t *)format = (uint32_t)new_val; \
  213. return ret; \
  214. } \
  215. } while (false)
  216. AUDIO_TEST("S16N", AUDIO_FORMAT_16BIT);
  217. AUDIO_TEST("S32N", AUDIO_FORMAT_32BIT);
  218. AUDIO_TEST("FL32", AUDIO_FORMAT_FLOAT);
  219. AUDIO_CONV("U16N", MAKEFORMAT('S', '1', '6', 'N'), AUDIO_FORMAT_16BIT);
  220. AUDIO_CONV("U32N", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  221. AUDIO_CONV("S24N", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  222. AUDIO_CONV("U24N", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  223. AUDIO_CONV("FL64", MAKEFORMAT('F', 'L', '3', '2'), AUDIO_FORMAT_FLOAT);
  224. AUDIO_CONV("S16I", MAKEFORMAT('S', '1', '6', 'N'), AUDIO_FORMAT_16BIT);
  225. AUDIO_CONV("U16I", MAKEFORMAT('S', '1', '6', 'N'), AUDIO_FORMAT_16BIT);
  226. AUDIO_CONV("S24I", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  227. AUDIO_CONV("U24I", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  228. AUDIO_CONV("S32I", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  229. AUDIO_CONV("U32I", MAKEFORMAT('S', '3', '2', 'N'), AUDIO_FORMAT_32BIT);
  230. #undef AUDIO_CONV
  231. #undef AUDIO_TEST
  232. *(uint32_t *)format = (uint32_t)MAKEFORMAT('F', 'L', '3', '2');
  233. return AUDIO_FORMAT_FLOAT;
  234. }
  235. /* ------------------------------------------------------------------------- */
  236. static void vlcs_get_metadata(void *data, calldata_t *cd)
  237. {
  238. struct vlc_source *vlcs = data;
  239. const char *data_id = calldata_string(cd, "tag_id");
  240. if (!vlcs || !data_id)
  241. return;
  242. libvlc_media_t *media =
  243. libvlc_media_player_get_media_(vlcs->media_player);
  244. if (!media)
  245. return;
  246. #define VLC_META(media, cd, did, tid, tag) \
  247. else if (strcmp(did, tid) == 0) \
  248. { \
  249. calldata_set_string(cd, "tag_data", \
  250. libvlc_media_get_meta_(media, tag)); \
  251. }
  252. if (strcmp(data_id, "title") == 0)
  253. calldata_set_string(cd, "tag_data",
  254. libvlc_media_get_meta_(media,
  255. libvlc_meta_Title));
  256. VLC_META(media, cd, data_id, "artist", libvlc_meta_Artist)
  257. VLC_META(media, cd, data_id, "genre", libvlc_meta_Genre)
  258. VLC_META(media, cd, data_id, "copyright", libvlc_meta_Copyright)
  259. VLC_META(media, cd, data_id, "album", libvlc_meta_Album)
  260. VLC_META(media, cd, data_id, "track_number", libvlc_meta_TrackNumber)
  261. VLC_META(media, cd, data_id, "description", libvlc_meta_Description)
  262. VLC_META(media, cd, data_id, "rating", libvlc_meta_Rating)
  263. VLC_META(media, cd, data_id, "date", libvlc_meta_Date)
  264. VLC_META(media, cd, data_id, "setting", libvlc_meta_Setting)
  265. VLC_META(media, cd, data_id, "url", libvlc_meta_URL)
  266. VLC_META(media, cd, data_id, "language", libvlc_meta_Language)
  267. VLC_META(media, cd, data_id, "now_playing", libvlc_meta_NowPlaying)
  268. VLC_META(media, cd, data_id, "publisher", libvlc_meta_Publisher)
  269. VLC_META(media, cd, data_id, "encoded_by", libvlc_meta_EncodedBy)
  270. VLC_META(media, cd, data_id, "artwork_url", libvlc_meta_ArtworkURL)
  271. VLC_META(media, cd, data_id, "track_id", libvlc_meta_TrackID)
  272. VLC_META(media, cd, data_id, "track_total", libvlc_meta_TrackTotal)
  273. VLC_META(media, cd, data_id, "director", libvlc_meta_Director)
  274. VLC_META(media, cd, data_id, "season", libvlc_meta_Season)
  275. VLC_META(media, cd, data_id, "episode", libvlc_meta_Episode)
  276. VLC_META(media, cd, data_id, "show_name", libvlc_meta_ShowName)
  277. VLC_META(media, cd, data_id, "actors", libvlc_meta_Actors)
  278. VLC_META(media, cd, data_id, "album_artist", libvlc_meta_AlbumArtist)
  279. VLC_META(media, cd, data_id, "disc_number", libvlc_meta_DiscNumber)
  280. VLC_META(media, cd, data_id, "disc_total", libvlc_meta_DiscTotal)
  281. libvlc_media_release_(media);
  282. #undef VLC_META
  283. }
  284. /* ------------------------------------------------------------------------- */
  285. static const char *vlcs_get_name(void *unused)
  286. {
  287. UNUSED_PARAMETER(unused);
  288. return obs_module_text("VLCSource");
  289. }
  290. static void vlcs_destroy(void *data)
  291. {
  292. struct vlc_source *c = data;
  293. if (c->media_list_player) {
  294. libvlc_media_list_player_stop_(c->media_list_player);
  295. libvlc_media_list_player_release_(c->media_list_player);
  296. }
  297. if (c->media_player) {
  298. libvlc_media_player_release_(c->media_player);
  299. }
  300. bfree((void *)c->audio.data[0]);
  301. obs_source_frame_free(&c->frame);
  302. free_files(&c->files.da);
  303. pthread_mutex_destroy(&c->mutex);
  304. bfree(c);
  305. }
  306. static void *vlcs_video_lock(void *data, void **planes)
  307. {
  308. struct vlc_source *c = data;
  309. for (size_t i = 0; i < MAX_AV_PLANES && c->frame.data[i] != NULL; i++)
  310. planes[i] = c->frame.data[i];
  311. return NULL;
  312. }
  313. static void vlcs_video_display(void *data, void *picture)
  314. {
  315. struct vlc_source *c = data;
  316. c->frame.timestamp = (uint64_t)libvlc_clock_() * 1000ULL - time_start;
  317. obs_source_output_video(c->source, &c->frame);
  318. UNUSED_PARAMETER(picture);
  319. }
  320. static void calculate_display_size(struct vlc_source *c, unsigned *width,
  321. unsigned *height)
  322. {
  323. libvlc_media_t *media = libvlc_media_player_get_media_(c->media_player);
  324. if (!media)
  325. return;
  326. libvlc_media_track_t **tracks;
  327. unsigned count = libvlc_media_tracks_get_(media, &tracks);
  328. if (count > 0) {
  329. for (unsigned i = 0; i < count; i++) {
  330. libvlc_media_track_t *track = tracks[i];
  331. if (track->i_type != libvlc_track_video)
  332. continue;
  333. int display_width = track->video->i_width;
  334. int display_height = track->video->i_height;
  335. if (display_width == 0 || display_height == 0)
  336. continue;
  337. /* Adjust for Sample Aspect Ratio (SAR) */
  338. if (track->video->i_sar_num > 0 &&
  339. track->video->i_sar_den > 0) {
  340. display_width = display_width *
  341. track->video->i_sar_num /
  342. track->video->i_sar_den;
  343. }
  344. switch (track->video->i_orientation) {
  345. case libvlc_video_orient_left_top:
  346. case libvlc_video_orient_left_bottom:
  347. case libvlc_video_orient_right_top:
  348. case libvlc_video_orient_right_bottom:
  349. /* orientation swaps height and width */
  350. *width = display_height;
  351. *height = display_width;
  352. break;
  353. default:
  354. /* height and width not swapped */
  355. *width = display_width;
  356. *height = display_height;
  357. break;
  358. }
  359. }
  360. libvlc_media_tracks_release_(tracks, count);
  361. }
  362. libvlc_media_release_(media);
  363. }
  364. static unsigned vlcs_video_format(void **p_data, char *chroma, unsigned *width,
  365. unsigned *height, unsigned *pitches,
  366. unsigned *lines)
  367. {
  368. struct vlc_source *c = *p_data;
  369. enum video_format new_format;
  370. enum video_range_type range;
  371. bool new_range;
  372. size_t i = 0;
  373. new_format = convert_vlc_video_format(chroma, &new_range);
  374. /* The width and height passed from VLC are the buffer size rather than
  375. * the correct video display size, and may be the next multiple of 32
  376. * up from the original dimension, e.g. 1080 would become 1088. VLC 4.0
  377. * will pass the correct display size in *(width+1) and *(height+1) but
  378. * for now we need to calculate it ourselves. */
  379. calculate_display_size(c, width, height);
  380. /* don't allocate a new frame if format/width/height hasn't changed */
  381. if (c->frame.format != new_format || c->frame.width != *width ||
  382. c->frame.height != *height) {
  383. obs_source_frame_free(&c->frame);
  384. obs_source_frame_init(&c->frame, new_format, *width, *height);
  385. c->frame.format = new_format;
  386. c->frame.full_range = new_range;
  387. range = c->frame.full_range ? VIDEO_RANGE_FULL
  388. : VIDEO_RANGE_PARTIAL;
  389. video_format_get_parameters_for_format(
  390. VIDEO_CS_DEFAULT, range, new_format,
  391. c->frame.color_matrix, c->frame.color_range_min,
  392. c->frame.color_range_max);
  393. }
  394. while (c->frame.data[i]) {
  395. pitches[i] = (unsigned)c->frame.linesize[i];
  396. lines[i] = get_format_lines(c->frame.format, *height, i);
  397. i++;
  398. }
  399. return 1;
  400. }
  401. static void vlcs_audio_play(void *data, const void *samples, unsigned count,
  402. int64_t pts)
  403. {
  404. struct vlc_source *c = data;
  405. size_t size = get_audio_size(c->audio.format, c->audio.speakers, count);
  406. if (c->audio_capacity < count) {
  407. c->audio.data[0] = brealloc((void *)c->audio.data[0], size);
  408. c->audio_capacity = count;
  409. }
  410. memcpy((void *)c->audio.data[0], samples, size);
  411. c->audio.timestamp = (uint64_t)pts * 1000ULL - time_start;
  412. c->audio.frames = count;
  413. obs_source_output_audio(c->source, &c->audio);
  414. }
  415. static int vlcs_audio_setup(void **p_data, char *format, unsigned *rate,
  416. unsigned *channels)
  417. {
  418. struct vlc_source *c = *p_data;
  419. enum audio_format new_audio_format;
  420. struct obs_audio_info aoi;
  421. obs_get_audio_info(&aoi);
  422. uint32_t out_channels = get_audio_channels(aoi.speakers);
  423. new_audio_format = convert_vlc_audio_format(format);
  424. if (*channels > out_channels)
  425. *channels = out_channels;
  426. /* don't free audio data if the data is the same format */
  427. if (c->audio.format == new_audio_format &&
  428. c->audio.samples_per_sec == *rate &&
  429. c->audio.speakers == (enum speaker_layout) * channels)
  430. return 0;
  431. c->audio_capacity = 0;
  432. bfree((void *)c->audio.data[0]);
  433. memset(&c->audio, 0, sizeof(c->audio));
  434. c->audio.speakers = (enum speaker_layout) * channels;
  435. c->audio.samples_per_sec = *rate;
  436. c->audio.format = new_audio_format;
  437. return 0;
  438. }
  439. static void add_file(struct vlc_source *c, struct darray *array,
  440. const char *path, int network_caching, int track_index,
  441. int subtitle_index, bool subtitle_enable)
  442. {
  443. DARRAY(struct media_file_data) new_files;
  444. struct media_file_data data;
  445. struct dstr new_path = {0};
  446. libvlc_media_t *new_media;
  447. bool is_url = path && strstr(path, "://") != NULL;
  448. new_files.da = *array;
  449. dstr_copy(&new_path, path);
  450. #ifdef _WIN32
  451. if (!is_url)
  452. dstr_replace(&new_path, "/", "\\");
  453. #endif
  454. path = new_path.array;
  455. new_media = get_media(&c->files.da, path);
  456. if (!new_media)
  457. new_media = get_media(&new_files.da, path);
  458. if (!new_media)
  459. new_media = create_media_from_file(path);
  460. if (new_media) {
  461. if (is_url) {
  462. struct dstr network_caching_option = {0};
  463. dstr_catf(&network_caching_option,
  464. ":network-caching=%d", network_caching);
  465. libvlc_media_add_option_(new_media,
  466. network_caching_option.array);
  467. dstr_free(&network_caching_option);
  468. }
  469. struct dstr track_option = {0};
  470. dstr_catf(&track_option, ":audio-track=%d", track_index - 1);
  471. libvlc_media_add_option_(new_media, track_option.array);
  472. dstr_free(&track_option);
  473. struct dstr sub_option = {0};
  474. if (subtitle_enable) {
  475. dstr_catf(&sub_option, ":sub-track=%d",
  476. subtitle_index - 1);
  477. }
  478. libvlc_media_add_option_(new_media, sub_option.array);
  479. dstr_free(&sub_option);
  480. data.path = new_path.array;
  481. data.media = new_media;
  482. da_push_back(new_files, &data);
  483. } else {
  484. dstr_free(&new_path);
  485. }
  486. *array = new_files.da;
  487. }
  488. static bool valid_extension(const char *ext)
  489. {
  490. struct dstr test = {0};
  491. bool valid = false;
  492. const char *b;
  493. const char *e;
  494. if (!ext || !*ext)
  495. return false;
  496. b = &EXTENSIONS_MEDIA[1];
  497. e = strchr(b, ';');
  498. for (;;) {
  499. if (e)
  500. dstr_ncopy(&test, b, e - b);
  501. else
  502. dstr_copy(&test, b);
  503. if (dstr_cmpi(&test, ext) == 0) {
  504. valid = true;
  505. break;
  506. }
  507. if (!e)
  508. break;
  509. b = e + 2;
  510. e = strchr(b, ';');
  511. }
  512. dstr_free(&test);
  513. return valid;
  514. }
  515. static void vlcs_update(void *data, obs_data_t *settings)
  516. {
  517. DARRAY(struct media_file_data) new_files;
  518. DARRAY(struct media_file_data) old_files;
  519. libvlc_media_list_t *media_list;
  520. struct vlc_source *c = data;
  521. obs_data_array_t *array;
  522. const char *behavior;
  523. size_t count;
  524. int network_caching;
  525. int track_index;
  526. int subtitle_index;
  527. bool subtitle_enable;
  528. da_init(new_files);
  529. da_init(old_files);
  530. array = obs_data_get_array(settings, S_PLAYLIST);
  531. count = obs_data_array_count(array);
  532. c->loop = obs_data_get_bool(settings, S_LOOP);
  533. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  534. network_caching = (int)obs_data_get_int(settings, S_NETWORK_CACHING);
  535. track_index = (int)obs_data_get_int(settings, S_TRACK);
  536. subtitle_index = (int)obs_data_get_int(settings, S_SUBTITLE_TRACK);
  537. subtitle_enable = obs_data_get_bool(settings, S_SUBTITLE_ENABLE);
  538. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0) {
  539. c->behavior = BEHAVIOR_PAUSE_UNPAUSE;
  540. } else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0) {
  541. c->behavior = BEHAVIOR_ALWAYS_PLAY;
  542. } else { /* S_BEHAVIOR_STOP_RESTART */
  543. c->behavior = BEHAVIOR_STOP_RESTART;
  544. }
  545. /* ------------------------------------- */
  546. /* create new list of sources */
  547. for (size_t i = 0; i < count; i++) {
  548. obs_data_t *item = obs_data_array_item(array, i);
  549. const char *path = obs_data_get_string(item, "value");
  550. if (!path || !*path) {
  551. obs_data_release(item);
  552. continue;
  553. }
  554. os_dir_t *dir = os_opendir(path);
  555. if (dir) {
  556. struct dstr dir_path = {0};
  557. struct os_dirent *ent;
  558. for (;;) {
  559. const char *ext;
  560. ent = os_readdir(dir);
  561. if (!ent)
  562. break;
  563. if (ent->directory)
  564. continue;
  565. ext = os_get_path_extension(ent->d_name);
  566. if (!valid_extension(ext))
  567. continue;
  568. dstr_copy(&dir_path, path);
  569. dstr_cat_ch(&dir_path, '/');
  570. dstr_cat(&dir_path, ent->d_name);
  571. add_file(c, &new_files.da, dir_path.array,
  572. network_caching, track_index,
  573. subtitle_index, subtitle_enable);
  574. }
  575. dstr_free(&dir_path);
  576. os_closedir(dir);
  577. } else {
  578. add_file(c, &new_files.da, path, network_caching,
  579. track_index, subtitle_index, subtitle_enable);
  580. }
  581. obs_data_release(item);
  582. }
  583. /* ------------------------------------- */
  584. /* update settings data */
  585. libvlc_media_list_player_stop_(c->media_list_player);
  586. pthread_mutex_lock(&c->mutex);
  587. old_files.da = c->files.da;
  588. c->files.da = new_files.da;
  589. pthread_mutex_unlock(&c->mutex);
  590. /* ------------------------------------- */
  591. /* shuffle playlist */
  592. c->shuffle = obs_data_get_bool(settings, S_SHUFFLE);
  593. if (c->files.num > 1 && c->shuffle) {
  594. DARRAY(struct media_file_data) new_files;
  595. DARRAY(size_t) idxs;
  596. da_init(new_files);
  597. da_init(idxs);
  598. da_resize(idxs, c->files.num);
  599. da_reserve(new_files, c->files.num);
  600. for (size_t i = 0; i < c->files.num; i++) {
  601. idxs.array[i] = i;
  602. }
  603. for (size_t i = idxs.num; i > 0; i--) {
  604. size_t val = rand() % i;
  605. size_t idx = idxs.array[val];
  606. da_push_back(new_files, &c->files.array[idx]);
  607. da_erase(idxs, val);
  608. }
  609. da_free(c->files);
  610. da_free(idxs);
  611. c->files.da = new_files.da;
  612. }
  613. /* ------------------------------------- */
  614. /* clean up and restart playback */
  615. free_files(&old_files.da);
  616. media_list = libvlc_media_list_new_(libvlc);
  617. libvlc_media_list_lock_(media_list);
  618. for (size_t i = 0; i < c->files.num; i++)
  619. libvlc_media_list_add_media_(media_list,
  620. c->files.array[i].media);
  621. libvlc_media_list_unlock_(media_list);
  622. libvlc_media_list_player_set_media_list_(c->media_list_player,
  623. media_list);
  624. libvlc_media_list_release_(media_list);
  625. libvlc_media_list_player_set_playback_mode_(
  626. c->media_list_player, c->loop ? libvlc_playback_mode_loop
  627. : libvlc_playback_mode_default);
  628. if (c->files.num && (c->behavior == BEHAVIOR_ALWAYS_PLAY ||
  629. obs_source_active(c->source)))
  630. libvlc_media_list_player_play_(c->media_list_player);
  631. else
  632. obs_source_output_video(c->source, NULL);
  633. obs_data_array_release(array);
  634. }
  635. static void vlcs_started(const struct libvlc_event_t *event, void *data)
  636. {
  637. struct vlc_source *c = data;
  638. obs_source_media_started(c->source);
  639. UNUSED_PARAMETER(event);
  640. }
  641. static void vlcs_stopped(const struct libvlc_event_t *event, void *data)
  642. {
  643. struct vlc_source *c = data;
  644. if (!c->loop) {
  645. obs_source_output_video(c->source, NULL);
  646. }
  647. obs_source_media_ended(c->source);
  648. UNUSED_PARAMETER(event);
  649. }
  650. static enum obs_media_state vlcs_get_state(void *data)
  651. {
  652. struct vlc_source *c = data;
  653. libvlc_state_t state = libvlc_media_player_get_state_(c->media_player);
  654. switch (state) {
  655. case libvlc_NothingSpecial:
  656. return OBS_MEDIA_STATE_NONE;
  657. case libvlc_Opening:
  658. return OBS_MEDIA_STATE_OPENING;
  659. case libvlc_Buffering:
  660. return OBS_MEDIA_STATE_BUFFERING;
  661. case libvlc_Playing:
  662. return OBS_MEDIA_STATE_PLAYING;
  663. case libvlc_Paused:
  664. return OBS_MEDIA_STATE_PAUSED;
  665. case libvlc_Stopped:
  666. return OBS_MEDIA_STATE_STOPPED;
  667. case libvlc_Ended:
  668. return OBS_MEDIA_STATE_ENDED;
  669. case libvlc_Error:
  670. return OBS_MEDIA_STATE_ERROR;
  671. }
  672. return 0;
  673. }
  674. static void vlcs_play_pause(void *data, bool pause)
  675. {
  676. struct vlc_source *c = data;
  677. if (pause)
  678. libvlc_media_list_player_pause_(c->media_list_player);
  679. else
  680. libvlc_media_list_player_play_(c->media_list_player);
  681. }
  682. static void vlcs_restart(void *data)
  683. {
  684. struct vlc_source *c = data;
  685. libvlc_media_list_player_stop_(c->media_list_player);
  686. libvlc_media_list_player_play_(c->media_list_player);
  687. }
  688. static void vlcs_stop(void *data)
  689. {
  690. struct vlc_source *c = data;
  691. libvlc_media_list_player_stop_(c->media_list_player);
  692. obs_source_output_video(c->source, NULL);
  693. }
  694. static void vlcs_playlist_next(void *data)
  695. {
  696. struct vlc_source *c = data;
  697. libvlc_media_list_player_next_(c->media_list_player);
  698. }
  699. static void vlcs_playlist_prev(void *data)
  700. {
  701. struct vlc_source *c = data;
  702. libvlc_media_list_player_previous_(c->media_list_player);
  703. }
  704. static int64_t vlcs_get_duration(void *data)
  705. {
  706. struct vlc_source *c = data;
  707. return (int64_t)libvlc_media_player_get_length_(c->media_player);
  708. }
  709. static int64_t vlcs_get_time(void *data)
  710. {
  711. struct vlc_source *c = data;
  712. return (int64_t)libvlc_media_player_get_time_(c->media_player);
  713. }
  714. static void vlcs_set_time(void *data, int64_t ms)
  715. {
  716. struct vlc_source *c = data;
  717. libvlc_media_player_set_time_(c->media_player, (libvlc_time_t)ms);
  718. }
  719. static void vlcs_play_pause_hotkey(void *data, obs_hotkey_id id,
  720. obs_hotkey_t *hotkey, bool pressed)
  721. {
  722. UNUSED_PARAMETER(id);
  723. UNUSED_PARAMETER(hotkey);
  724. struct vlc_source *c = data;
  725. enum obs_media_state state = obs_source_media_get_state(c->source);
  726. if (pressed && obs_source_showing(c->source)) {
  727. if (state == OBS_MEDIA_STATE_PLAYING)
  728. obs_source_media_play_pause(c->source, true);
  729. else if (state == OBS_MEDIA_STATE_PAUSED)
  730. obs_source_media_play_pause(c->source, false);
  731. }
  732. }
  733. static void vlcs_restart_hotkey(void *data, obs_hotkey_id id,
  734. obs_hotkey_t *hotkey, bool pressed)
  735. {
  736. UNUSED_PARAMETER(id);
  737. UNUSED_PARAMETER(hotkey);
  738. struct vlc_source *c = data;
  739. if (pressed && obs_source_showing(c->source))
  740. obs_source_media_restart(c->source);
  741. }
  742. static void vlcs_stop_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  743. bool pressed)
  744. {
  745. UNUSED_PARAMETER(id);
  746. UNUSED_PARAMETER(hotkey);
  747. struct vlc_source *c = data;
  748. if (pressed && obs_source_showing(c->source))
  749. obs_source_media_stop(c->source);
  750. }
  751. static void vlcs_playlist_next_hotkey(void *data, obs_hotkey_id id,
  752. obs_hotkey_t *hotkey, bool pressed)
  753. {
  754. UNUSED_PARAMETER(id);
  755. UNUSED_PARAMETER(hotkey);
  756. struct vlc_source *c = data;
  757. if (pressed && obs_source_showing(c->source))
  758. obs_source_media_next(c->source);
  759. }
  760. static void vlcs_playlist_prev_hotkey(void *data, obs_hotkey_id id,
  761. obs_hotkey_t *hotkey, bool pressed)
  762. {
  763. UNUSED_PARAMETER(id);
  764. UNUSED_PARAMETER(hotkey);
  765. struct vlc_source *c = data;
  766. if (pressed && obs_source_showing(c->source))
  767. obs_source_media_previous(c->source);
  768. }
  769. static void *vlcs_create(obs_data_t *settings, obs_source_t *source)
  770. {
  771. struct vlc_source *c = bzalloc(sizeof(*c));
  772. c->source = source;
  773. c->play_pause_hotkey = obs_hotkey_register_source(
  774. source, "VLCSource.PlayPause", obs_module_text("PlayPause"),
  775. vlcs_play_pause_hotkey, c);
  776. c->restart_hotkey = obs_hotkey_register_source(
  777. source, "VLCSource.Restart", obs_module_text("Restart"),
  778. vlcs_restart_hotkey, c);
  779. c->stop_hotkey = obs_hotkey_register_source(source, "VLCSource.Stop",
  780. obs_module_text("Stop"),
  781. vlcs_stop_hotkey, c);
  782. c->playlist_next_hotkey = obs_hotkey_register_source(
  783. source, "VLCSource.PlaylistNext",
  784. obs_module_text("PlaylistNext"), vlcs_playlist_next_hotkey, c);
  785. c->playlist_prev_hotkey = obs_hotkey_register_source(
  786. source, "VLCSource.PlaylistPrev",
  787. obs_module_text("PlaylistPrev"), vlcs_playlist_prev_hotkey, c);
  788. pthread_mutex_init_value(&c->mutex);
  789. if (pthread_mutex_init(&c->mutex, NULL) != 0)
  790. goto error;
  791. if (!load_libvlc())
  792. goto error;
  793. c->media_list_player = libvlc_media_list_player_new_(libvlc);
  794. if (!c->media_list_player)
  795. goto error;
  796. c->media_player = libvlc_media_player_new_(libvlc);
  797. if (!c->media_player)
  798. goto error;
  799. libvlc_media_list_player_set_media_player_(c->media_list_player,
  800. c->media_player);
  801. libvlc_video_set_callbacks_(c->media_player, vlcs_video_lock, NULL,
  802. vlcs_video_display, c);
  803. libvlc_video_set_format_callbacks_(c->media_player, vlcs_video_format,
  804. NULL);
  805. libvlc_audio_set_callbacks_(c->media_player, vlcs_audio_play, NULL,
  806. NULL, NULL, NULL, c);
  807. libvlc_audio_set_format_callbacks_(c->media_player, vlcs_audio_setup,
  808. NULL);
  809. libvlc_event_manager_t *event_manager;
  810. event_manager = libvlc_media_player_event_manager_(c->media_player);
  811. libvlc_event_attach_(event_manager, libvlc_MediaPlayerEndReached,
  812. vlcs_stopped, c);
  813. libvlc_event_attach_(event_manager, libvlc_MediaPlayerOpening,
  814. vlcs_started, c);
  815. proc_handler_t *ph = obs_source_get_proc_handler(source);
  816. proc_handler_add(
  817. ph, "void get_metadata(in string tag_id out string tag_data)",
  818. vlcs_get_metadata, c);
  819. obs_source_update(source, NULL);
  820. UNUSED_PARAMETER(settings);
  821. return c;
  822. error:
  823. vlcs_destroy(c);
  824. return NULL;
  825. }
  826. static void vlcs_activate(void *data)
  827. {
  828. struct vlc_source *c = data;
  829. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  830. libvlc_media_list_player_play_(c->media_list_player);
  831. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  832. libvlc_media_list_player_play_(c->media_list_player);
  833. }
  834. }
  835. static void vlcs_deactivate(void *data)
  836. {
  837. struct vlc_source *c = data;
  838. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  839. libvlc_media_list_player_stop_(c->media_list_player);
  840. obs_source_output_video(c->source, NULL);
  841. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  842. libvlc_media_list_player_pause_(c->media_list_player);
  843. }
  844. }
  845. static void vlcs_defaults(obs_data_t *settings)
  846. {
  847. obs_data_set_default_bool(settings, S_LOOP, true);
  848. obs_data_set_default_bool(settings, S_SHUFFLE, false);
  849. obs_data_set_default_string(settings, S_BEHAVIOR,
  850. S_BEHAVIOR_STOP_RESTART);
  851. obs_data_set_default_int(settings, S_NETWORK_CACHING, 400);
  852. obs_data_set_default_int(settings, S_TRACK, 1);
  853. obs_data_set_default_bool(settings, S_SUBTITLE_ENABLE, false);
  854. obs_data_set_default_int(settings, S_SUBTITLE_TRACK, 1);
  855. }
  856. static obs_properties_t *vlcs_properties(void *data)
  857. {
  858. obs_properties_t *ppts = obs_properties_create();
  859. struct vlc_source *c = data;
  860. struct dstr filter = {0};
  861. struct dstr exts = {0};
  862. struct dstr path = {0};
  863. obs_property_t *p;
  864. obs_properties_set_flags(ppts, OBS_PROPERTIES_DEFER_UPDATE);
  865. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  866. obs_properties_add_bool(ppts, S_SHUFFLE, T_SHUFFLE);
  867. if (c) {
  868. pthread_mutex_lock(&c->mutex);
  869. if (c->files.num) {
  870. struct media_file_data *last = da_end(c->files);
  871. const char *slash;
  872. dstr_copy(&path, last->path);
  873. dstr_replace(&path, "\\", "/");
  874. slash = strrchr(path.array, '/');
  875. if (slash)
  876. dstr_resize(&path, slash - path.array + 1);
  877. }
  878. pthread_mutex_unlock(&c->mutex);
  879. }
  880. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  881. OBS_COMBO_TYPE_LIST,
  882. OBS_COMBO_FORMAT_STRING);
  883. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  884. S_BEHAVIOR_STOP_RESTART);
  885. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  886. S_BEHAVIOR_PAUSE_UNPAUSE);
  887. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  888. S_BEHAVIOR_ALWAYS_PLAY);
  889. dstr_cat(&filter, "Media Files (");
  890. dstr_copy(&exts, EXTENSIONS_MEDIA);
  891. dstr_replace(&exts, ";", " ");
  892. dstr_cat_dstr(&filter, &exts);
  893. dstr_cat(&filter, ");;Video Files (");
  894. dstr_copy(&exts, EXTENSIONS_VIDEO);
  895. dstr_replace(&exts, ";", " ");
  896. dstr_cat_dstr(&filter, &exts);
  897. dstr_cat(&filter, ");;Audio Files (");
  898. dstr_copy(&exts, EXTENSIONS_AUDIO);
  899. dstr_replace(&exts, ";", " ");
  900. dstr_cat_dstr(&filter, &exts);
  901. dstr_cat(&filter, ");;Playlist Files (");
  902. dstr_copy(&exts, EXTENSIONS_PLAYLIST);
  903. dstr_replace(&exts, ";", " ");
  904. dstr_cat_dstr(&filter, &exts);
  905. dstr_cat(&filter, ")");
  906. obs_properties_add_editable_list(ppts, S_PLAYLIST, T_PLAYLIST,
  907. OBS_EDITABLE_LIST_TYPE_FILES_AND_URLS,
  908. filter.array, path.array);
  909. dstr_free(&path);
  910. dstr_free(&filter);
  911. dstr_free(&exts);
  912. p = obs_properties_add_int(ppts, S_NETWORK_CACHING, T_NETWORK_CACHING,
  913. 100, 60000, 10);
  914. obs_property_int_set_suffix(p, " ms");
  915. obs_properties_add_int(ppts, S_TRACK, T_TRACK, 1, 10, 1);
  916. obs_properties_add_bool(ppts, S_SUBTITLE_ENABLE, T_SUBTITLE_ENABLE);
  917. obs_properties_add_int(ppts, S_SUBTITLE_TRACK, T_SUBTITLE_TRACK, 1, 10,
  918. 1);
  919. return ppts;
  920. }
  921. static void missing_file_callback(void *src, const char *new_path, void *data)
  922. {
  923. struct vlc_source *s = src;
  924. const char *orig_path = data;
  925. obs_source_t *source = s->source;
  926. obs_data_t *settings = obs_source_get_settings(source);
  927. obs_data_array_t *files = obs_data_get_array(settings, S_PLAYLIST);
  928. size_t l = obs_data_array_count(files);
  929. for (size_t i = 0; i < l; i++) {
  930. obs_data_t *file = obs_data_array_item(files, i);
  931. const char *path = obs_data_get_string(file, "value");
  932. if (strcmp(path, orig_path) == 0) {
  933. if (new_path && *new_path)
  934. obs_data_set_string(file, "value", new_path);
  935. else
  936. obs_data_array_erase(files, i);
  937. obs_data_release(file);
  938. break;
  939. }
  940. obs_data_release(file);
  941. }
  942. obs_source_update(source, settings);
  943. obs_data_array_release(files);
  944. obs_data_release(settings);
  945. }
  946. static obs_missing_files_t *vlcs_missingfiles(void *data)
  947. {
  948. struct vlc_source *s = data;
  949. obs_missing_files_t *missing_files = obs_missing_files_create();
  950. obs_source_t *source = s->source;
  951. obs_data_t *settings = obs_source_get_settings(source);
  952. obs_data_array_t *files = obs_data_get_array(settings, S_PLAYLIST);
  953. size_t l = obs_data_array_count(files);
  954. for (size_t i = 0; i < l; i++) {
  955. obs_data_t *item = obs_data_array_item(files, i);
  956. const char *path = obs_data_get_string(item, "value");
  957. if (strcmp(path, "") != 0) {
  958. if (!os_file_exists(path) &&
  959. strstr(path, "://") == NULL) {
  960. obs_missing_file_t *file =
  961. obs_missing_file_create(
  962. path, missing_file_callback,
  963. OBS_MISSING_FILE_SOURCE, source,
  964. (void *)path);
  965. obs_missing_files_add_file(missing_files, file);
  966. }
  967. }
  968. obs_data_release(item);
  969. }
  970. obs_data_array_release(files);
  971. obs_data_release(settings);
  972. return missing_files;
  973. }
  974. struct obs_source_info vlc_source_info = {
  975. .id = "vlc_source",
  976. .type = OBS_SOURCE_TYPE_INPUT,
  977. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  978. OBS_SOURCE_DO_NOT_DUPLICATE |
  979. OBS_SOURCE_CONTROLLABLE_MEDIA,
  980. .get_name = vlcs_get_name,
  981. .create = vlcs_create,
  982. .destroy = vlcs_destroy,
  983. .update = vlcs_update,
  984. .get_defaults = vlcs_defaults,
  985. .get_properties = vlcs_properties,
  986. .activate = vlcs_activate,
  987. .deactivate = vlcs_deactivate,
  988. .missing_files = vlcs_missingfiles,
  989. .icon_type = OBS_ICON_TYPE_MEDIA,
  990. .media_play_pause = vlcs_play_pause,
  991. .media_restart = vlcs_restart,
  992. .media_stop = vlcs_stop,
  993. .media_next = vlcs_playlist_next,
  994. .media_previous = vlcs_playlist_prev,
  995. .media_get_duration = vlcs_get_duration,
  996. .media_get_time = vlcs_get_time,
  997. .media_set_time = vlcs_set_time,
  998. .media_get_state = vlcs_get_state,
  999. };