1
0

vlc-video-source.c 34 KB

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