vlc-video-source.c 35 KB

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