vlc-video-source.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. unsigned new_width = 0;
  373. unsigned new_height = 0;
  374. size_t i = 0;
  375. new_format = convert_vlc_video_format(chroma, &new_range);
  376. /* The width and height passed from VLC are the buffer size rather than
  377. * the correct video display size, and may be the next multiple of 32
  378. * up from the original dimension, e.g. 1080 would become 1088. VLC 4.0
  379. * will pass the correct display size in *(width+1) and *(height+1) but
  380. * for now we need to calculate it ourselves. */
  381. calculate_display_size(c, width, height);
  382. /* don't allocate a new frame if format/width/height hasn't changed */
  383. if (c->frame.format != new_format || c->frame.width != *width ||
  384. c->frame.height != *height) {
  385. obs_source_frame_free(&c->frame);
  386. obs_source_frame_init(&c->frame, new_format, *width, *height);
  387. c->frame.format = new_format;
  388. c->frame.full_range = new_range;
  389. range = c->frame.full_range ? VIDEO_RANGE_FULL
  390. : VIDEO_RANGE_PARTIAL;
  391. video_format_get_parameters_for_format(
  392. VIDEO_CS_DEFAULT, range, new_format,
  393. c->frame.color_matrix, c->frame.color_range_min,
  394. c->frame.color_range_max);
  395. }
  396. while (c->frame.data[i]) {
  397. pitches[i] = (unsigned)c->frame.linesize[i];
  398. lines[i] = get_format_lines(c->frame.format, *height, i);
  399. i++;
  400. }
  401. return 1;
  402. }
  403. static void vlcs_audio_play(void *data, const void *samples, unsigned count,
  404. int64_t pts)
  405. {
  406. struct vlc_source *c = data;
  407. size_t size = get_audio_size(c->audio.format, c->audio.speakers, count);
  408. if (c->audio_capacity < count) {
  409. c->audio.data[0] = brealloc((void *)c->audio.data[0], size);
  410. c->audio_capacity = count;
  411. }
  412. memcpy((void *)c->audio.data[0], samples, size);
  413. c->audio.timestamp = (uint64_t)pts * 1000ULL - time_start;
  414. c->audio.frames = count;
  415. obs_source_output_audio(c->source, &c->audio);
  416. }
  417. static int vlcs_audio_setup(void **p_data, char *format, unsigned *rate,
  418. unsigned *channels)
  419. {
  420. struct vlc_source *c = *p_data;
  421. enum audio_format new_audio_format;
  422. struct obs_audio_info aoi;
  423. obs_get_audio_info(&aoi);
  424. int out_channels = (int)get_audio_channels(aoi.speakers);
  425. new_audio_format = convert_vlc_audio_format(format);
  426. if (*channels > out_channels)
  427. *channels = out_channels;
  428. /* don't free audio data if the data is the same format */
  429. if (c->audio.format == new_audio_format &&
  430. c->audio.samples_per_sec == *rate &&
  431. c->audio.speakers == (enum speaker_layout) * channels)
  432. return 0;
  433. c->audio_capacity = 0;
  434. bfree((void *)c->audio.data[0]);
  435. memset(&c->audio, 0, sizeof(c->audio));
  436. c->audio.speakers = (enum speaker_layout) * channels;
  437. c->audio.samples_per_sec = *rate;
  438. c->audio.format = new_audio_format;
  439. return 0;
  440. }
  441. static void add_file(struct vlc_source *c, struct darray *array,
  442. const char *path, int network_caching, int track_index,
  443. int subtitle_index, bool subtitle_enable)
  444. {
  445. DARRAY(struct media_file_data) new_files;
  446. struct media_file_data data;
  447. struct dstr new_path = {0};
  448. libvlc_media_t *new_media;
  449. bool is_url = path && strstr(path, "://") != NULL;
  450. new_files.da = *array;
  451. dstr_copy(&new_path, path);
  452. #ifdef _WIN32
  453. if (!is_url)
  454. dstr_replace(&new_path, "/", "\\");
  455. #endif
  456. path = new_path.array;
  457. new_media = get_media(&c->files.da, path);
  458. if (!new_media)
  459. new_media = get_media(&new_files.da, path);
  460. if (!new_media)
  461. new_media = create_media_from_file(path);
  462. if (new_media) {
  463. if (is_url) {
  464. struct dstr network_caching_option = {0};
  465. dstr_catf(&network_caching_option,
  466. ":network-caching=%d", network_caching);
  467. libvlc_media_add_option_(new_media,
  468. network_caching_option.array);
  469. dstr_free(&network_caching_option);
  470. }
  471. struct dstr track_option = {0};
  472. dstr_catf(&track_option, ":audio-track=%d", track_index - 1);
  473. libvlc_media_add_option_(new_media, track_option.array);
  474. dstr_free(&track_option);
  475. struct dstr sub_option = {0};
  476. if (subtitle_enable) {
  477. dstr_catf(&sub_option, ":sub-track=%d",
  478. subtitle_index - 1);
  479. }
  480. libvlc_media_add_option_(new_media, sub_option.array);
  481. dstr_free(&sub_option);
  482. data.path = new_path.array;
  483. data.media = new_media;
  484. da_push_back(new_files, &data);
  485. } else {
  486. dstr_free(&new_path);
  487. }
  488. *array = new_files.da;
  489. }
  490. static bool valid_extension(const char *ext)
  491. {
  492. struct dstr test = {0};
  493. bool valid = false;
  494. const char *b;
  495. const char *e;
  496. if (!ext || !*ext)
  497. return false;
  498. b = &EXTENSIONS_MEDIA[1];
  499. e = strchr(b, ';');
  500. for (;;) {
  501. if (e)
  502. dstr_ncopy(&test, b, e - b);
  503. else
  504. dstr_copy(&test, b);
  505. if (dstr_cmpi(&test, ext) == 0) {
  506. valid = true;
  507. break;
  508. }
  509. if (!e)
  510. break;
  511. b = e + 2;
  512. e = strchr(b, ';');
  513. }
  514. dstr_free(&test);
  515. return valid;
  516. }
  517. static void vlcs_update(void *data, obs_data_t *settings)
  518. {
  519. DARRAY(struct media_file_data) new_files;
  520. DARRAY(struct media_file_data) old_files;
  521. libvlc_media_list_t *media_list;
  522. struct vlc_source *c = data;
  523. obs_data_array_t *array;
  524. const char *behavior;
  525. size_t count;
  526. int network_caching;
  527. int track_index;
  528. int subtitle_index;
  529. bool subtitle_enable;
  530. da_init(new_files);
  531. da_init(old_files);
  532. array = obs_data_get_array(settings, S_PLAYLIST);
  533. count = obs_data_array_count(array);
  534. c->loop = obs_data_get_bool(settings, S_LOOP);
  535. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  536. network_caching = (int)obs_data_get_int(settings, S_NETWORK_CACHING);
  537. track_index = (int)obs_data_get_int(settings, S_TRACK);
  538. subtitle_index = (int)obs_data_get_int(settings, S_SUBTITLE_TRACK);
  539. subtitle_enable = obs_data_get_bool(settings, S_SUBTITLE_ENABLE);
  540. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0) {
  541. c->behavior = BEHAVIOR_PAUSE_UNPAUSE;
  542. } else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0) {
  543. c->behavior = BEHAVIOR_ALWAYS_PLAY;
  544. } else { /* S_BEHAVIOR_STOP_RESTART */
  545. c->behavior = BEHAVIOR_STOP_RESTART;
  546. }
  547. /* ------------------------------------- */
  548. /* create new list of sources */
  549. for (size_t i = 0; i < count; i++) {
  550. obs_data_t *item = obs_data_array_item(array, i);
  551. const char *path = obs_data_get_string(item, "value");
  552. os_dir_t *dir = os_opendir(path);
  553. if (dir) {
  554. struct dstr dir_path = {0};
  555. struct os_dirent *ent;
  556. for (;;) {
  557. const char *ext;
  558. ent = os_readdir(dir);
  559. if (!ent)
  560. break;
  561. if (ent->directory)
  562. continue;
  563. ext = os_get_path_extension(ent->d_name);
  564. if (!valid_extension(ext))
  565. continue;
  566. dstr_copy(&dir_path, path);
  567. dstr_cat_ch(&dir_path, '/');
  568. dstr_cat(&dir_path, ent->d_name);
  569. add_file(c, &new_files.da, dir_path.array,
  570. network_caching, track_index,
  571. subtitle_index, subtitle_enable);
  572. }
  573. dstr_free(&dir_path);
  574. os_closedir(dir);
  575. } else {
  576. add_file(c, &new_files.da, path, network_caching,
  577. track_index, subtitle_index, subtitle_enable);
  578. }
  579. obs_data_release(item);
  580. }
  581. /* ------------------------------------- */
  582. /* update settings data */
  583. libvlc_media_list_player_stop_(c->media_list_player);
  584. pthread_mutex_lock(&c->mutex);
  585. old_files.da = c->files.da;
  586. c->files.da = new_files.da;
  587. pthread_mutex_unlock(&c->mutex);
  588. /* ------------------------------------- */
  589. /* shuffle playlist */
  590. c->shuffle = obs_data_get_bool(settings, S_SHUFFLE);
  591. if (c->files.num > 1 && c->shuffle) {
  592. DARRAY(struct media_file_data) new_files;
  593. DARRAY(size_t) idxs;
  594. da_init(new_files);
  595. da_init(idxs);
  596. da_resize(idxs, c->files.num);
  597. da_reserve(new_files, c->files.num);
  598. for (size_t i = 0; i < c->files.num; i++) {
  599. idxs.array[i] = i;
  600. }
  601. for (size_t i = idxs.num; i > 0; i--) {
  602. size_t val = rand() % i;
  603. size_t idx = idxs.array[val];
  604. da_push_back(new_files, &c->files.array[idx]);
  605. da_erase(idxs, val);
  606. }
  607. da_free(c->files);
  608. da_free(idxs);
  609. c->files.da = new_files.da;
  610. }
  611. /* ------------------------------------- */
  612. /* clean up and restart playback */
  613. free_files(&old_files.da);
  614. media_list = libvlc_media_list_new_(libvlc);
  615. libvlc_media_list_lock_(media_list);
  616. for (size_t i = 0; i < c->files.num; i++)
  617. libvlc_media_list_add_media_(media_list,
  618. c->files.array[i].media);
  619. libvlc_media_list_unlock_(media_list);
  620. libvlc_media_list_player_set_media_list_(c->media_list_player,
  621. media_list);
  622. libvlc_media_list_release_(media_list);
  623. libvlc_media_list_player_set_playback_mode_(
  624. c->media_list_player, c->loop ? libvlc_playback_mode_loop
  625. : libvlc_playback_mode_default);
  626. if (c->files.num && (c->behavior == BEHAVIOR_ALWAYS_PLAY ||
  627. obs_source_active(c->source)))
  628. libvlc_media_list_player_play_(c->media_list_player);
  629. else
  630. obs_source_output_video(c->source, NULL);
  631. obs_data_array_release(array);
  632. }
  633. static void vlcs_started(const struct libvlc_event_t *event, void *data)
  634. {
  635. struct vlc_source *c = data;
  636. obs_source_media_started(c->source);
  637. UNUSED_PARAMETER(event);
  638. }
  639. static void vlcs_stopped(const struct libvlc_event_t *event, void *data)
  640. {
  641. struct vlc_source *c = data;
  642. if (!c->loop) {
  643. obs_source_output_video(c->source, NULL);
  644. }
  645. obs_source_media_ended(c->source);
  646. UNUSED_PARAMETER(event);
  647. }
  648. static enum obs_media_state vlcs_get_state(void *data)
  649. {
  650. struct vlc_source *c = data;
  651. libvlc_state_t state = libvlc_media_player_get_state_(c->media_player);
  652. switch (state) {
  653. case libvlc_NothingSpecial:
  654. return OBS_MEDIA_STATE_NONE;
  655. case libvlc_Opening:
  656. return OBS_MEDIA_STATE_OPENING;
  657. case libvlc_Buffering:
  658. return OBS_MEDIA_STATE_BUFFERING;
  659. case libvlc_Playing:
  660. return OBS_MEDIA_STATE_PLAYING;
  661. case libvlc_Paused:
  662. return OBS_MEDIA_STATE_PAUSED;
  663. case libvlc_Stopped:
  664. return OBS_MEDIA_STATE_STOPPED;
  665. case libvlc_Ended:
  666. return OBS_MEDIA_STATE_ENDED;
  667. case libvlc_Error:
  668. return OBS_MEDIA_STATE_ERROR;
  669. }
  670. return 0;
  671. }
  672. static void vlcs_play_pause(void *data, bool pause)
  673. {
  674. struct vlc_source *c = data;
  675. if (pause)
  676. libvlc_media_list_player_pause_(c->media_list_player);
  677. else
  678. libvlc_media_list_player_play_(c->media_list_player);
  679. }
  680. static void vlcs_restart(void *data)
  681. {
  682. struct vlc_source *c = data;
  683. libvlc_media_list_player_stop_(c->media_list_player);
  684. libvlc_media_list_player_play_(c->media_list_player);
  685. }
  686. static void vlcs_stop(void *data)
  687. {
  688. struct vlc_source *c = data;
  689. libvlc_media_list_player_stop_(c->media_list_player);
  690. obs_source_output_video(c->source, NULL);
  691. }
  692. static void vlcs_playlist_next(void *data)
  693. {
  694. struct vlc_source *c = data;
  695. libvlc_media_list_player_next_(c->media_list_player);
  696. }
  697. static void vlcs_playlist_prev(void *data)
  698. {
  699. struct vlc_source *c = data;
  700. libvlc_media_list_player_previous_(c->media_list_player);
  701. }
  702. static int64_t vlcs_get_duration(void *data)
  703. {
  704. struct vlc_source *c = data;
  705. return (int64_t)libvlc_media_player_get_length_(c->media_player);
  706. }
  707. static int64_t vlcs_get_time(void *data)
  708. {
  709. struct vlc_source *c = data;
  710. return (int64_t)libvlc_media_player_get_time_(c->media_player);
  711. }
  712. static void vlcs_set_time(void *data, int64_t ms)
  713. {
  714. struct vlc_source *c = data;
  715. libvlc_media_player_set_time_(c->media_player, (libvlc_time_t)ms);
  716. }
  717. static void vlcs_play_pause_hotkey(void *data, obs_hotkey_id id,
  718. obs_hotkey_t *hotkey, bool pressed)
  719. {
  720. UNUSED_PARAMETER(id);
  721. UNUSED_PARAMETER(hotkey);
  722. struct vlc_source *c = data;
  723. enum obs_media_state state = obs_source_media_get_state(c->source);
  724. if (pressed && obs_source_showing(c->source)) {
  725. if (state == OBS_MEDIA_STATE_PLAYING)
  726. obs_source_media_play_pause(c->source, true);
  727. else if (state == OBS_MEDIA_STATE_PAUSED)
  728. obs_source_media_play_pause(c->source, false);
  729. }
  730. }
  731. static void vlcs_restart_hotkey(void *data, obs_hotkey_id id,
  732. obs_hotkey_t *hotkey, bool pressed)
  733. {
  734. UNUSED_PARAMETER(id);
  735. UNUSED_PARAMETER(hotkey);
  736. struct vlc_source *c = data;
  737. if (pressed && obs_source_showing(c->source))
  738. obs_source_media_restart(c->source);
  739. }
  740. static void vlcs_stop_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  741. bool pressed)
  742. {
  743. UNUSED_PARAMETER(id);
  744. UNUSED_PARAMETER(hotkey);
  745. struct vlc_source *c = data;
  746. if (pressed && obs_source_showing(c->source))
  747. obs_source_media_stop(c->source);
  748. }
  749. static void vlcs_playlist_next_hotkey(void *data, obs_hotkey_id id,
  750. obs_hotkey_t *hotkey, bool pressed)
  751. {
  752. UNUSED_PARAMETER(id);
  753. UNUSED_PARAMETER(hotkey);
  754. struct vlc_source *c = data;
  755. if (pressed && obs_source_showing(c->source))
  756. obs_source_media_next(c->source);
  757. }
  758. static void vlcs_playlist_prev_hotkey(void *data, obs_hotkey_id id,
  759. obs_hotkey_t *hotkey, bool pressed)
  760. {
  761. UNUSED_PARAMETER(id);
  762. UNUSED_PARAMETER(hotkey);
  763. struct vlc_source *c = data;
  764. if (pressed && obs_source_showing(c->source))
  765. obs_source_media_previous(c->source);
  766. }
  767. static void *vlcs_create(obs_data_t *settings, obs_source_t *source)
  768. {
  769. struct vlc_source *c = bzalloc(sizeof(*c));
  770. c->source = source;
  771. c->play_pause_hotkey = obs_hotkey_register_source(
  772. source, "VLCSource.PlayPause", obs_module_text("PlayPause"),
  773. vlcs_play_pause_hotkey, c);
  774. c->restart_hotkey = obs_hotkey_register_source(
  775. source, "VLCSource.Restart", obs_module_text("Restart"),
  776. vlcs_restart_hotkey, c);
  777. c->stop_hotkey = obs_hotkey_register_source(source, "VLCSource.Stop",
  778. obs_module_text("Stop"),
  779. vlcs_stop_hotkey, c);
  780. c->playlist_next_hotkey = obs_hotkey_register_source(
  781. source, "VLCSource.PlaylistNext",
  782. obs_module_text("PlaylistNext"), vlcs_playlist_next_hotkey, c);
  783. c->playlist_prev_hotkey = obs_hotkey_register_source(
  784. source, "VLCSource.PlaylistPrev",
  785. obs_module_text("PlaylistPrev"), vlcs_playlist_prev_hotkey, c);
  786. pthread_mutex_init_value(&c->mutex);
  787. if (pthread_mutex_init(&c->mutex, NULL) != 0)
  788. goto error;
  789. if (!load_libvlc())
  790. goto error;
  791. c->media_list_player = libvlc_media_list_player_new_(libvlc);
  792. if (!c->media_list_player)
  793. goto error;
  794. c->media_player = libvlc_media_player_new_(libvlc);
  795. if (!c->media_player)
  796. goto error;
  797. libvlc_media_list_player_set_media_player_(c->media_list_player,
  798. c->media_player);
  799. libvlc_video_set_callbacks_(c->media_player, vlcs_video_lock, NULL,
  800. vlcs_video_display, c);
  801. libvlc_video_set_format_callbacks_(c->media_player, vlcs_video_format,
  802. NULL);
  803. libvlc_audio_set_callbacks_(c->media_player, vlcs_audio_play, NULL,
  804. NULL, NULL, NULL, c);
  805. libvlc_audio_set_format_callbacks_(c->media_player, vlcs_audio_setup,
  806. NULL);
  807. libvlc_event_manager_t *event_manager;
  808. event_manager = libvlc_media_player_event_manager_(c->media_player);
  809. libvlc_event_attach_(event_manager, libvlc_MediaPlayerEndReached,
  810. vlcs_stopped, c);
  811. libvlc_event_attach_(event_manager, libvlc_MediaPlayerOpening,
  812. vlcs_started, c);
  813. proc_handler_t *ph = obs_source_get_proc_handler(source);
  814. proc_handler_add(
  815. ph, "void get_metadata(in string tag_id out string tag_data)",
  816. vlcs_get_metadata, c);
  817. obs_source_update(source, NULL);
  818. UNUSED_PARAMETER(settings);
  819. return c;
  820. error:
  821. vlcs_destroy(c);
  822. return NULL;
  823. }
  824. static void vlcs_activate(void *data)
  825. {
  826. struct vlc_source *c = data;
  827. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  828. libvlc_media_list_player_play_(c->media_list_player);
  829. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  830. libvlc_media_list_player_play_(c->media_list_player);
  831. }
  832. }
  833. static void vlcs_deactivate(void *data)
  834. {
  835. struct vlc_source *c = data;
  836. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  837. libvlc_media_list_player_stop_(c->media_list_player);
  838. obs_source_output_video(c->source, NULL);
  839. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  840. libvlc_media_list_player_pause_(c->media_list_player);
  841. }
  842. }
  843. static void vlcs_defaults(obs_data_t *settings)
  844. {
  845. obs_data_set_default_bool(settings, S_LOOP, true);
  846. obs_data_set_default_bool(settings, S_SHUFFLE, false);
  847. obs_data_set_default_string(settings, S_BEHAVIOR,
  848. S_BEHAVIOR_STOP_RESTART);
  849. obs_data_set_default_int(settings, S_NETWORK_CACHING, 400);
  850. obs_data_set_default_int(settings, S_TRACK, 1);
  851. obs_data_set_default_bool(settings, S_SUBTITLE_ENABLE, false);
  852. obs_data_set_default_int(settings, S_SUBTITLE_TRACK, 1);
  853. }
  854. static obs_properties_t *vlcs_properties(void *data)
  855. {
  856. obs_properties_t *ppts = obs_properties_create();
  857. struct vlc_source *c = data;
  858. struct dstr filter = {0};
  859. struct dstr exts = {0};
  860. struct dstr path = {0};
  861. obs_property_t *p;
  862. obs_properties_set_flags(ppts, OBS_PROPERTIES_DEFER_UPDATE);
  863. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  864. obs_properties_add_bool(ppts, S_SHUFFLE, T_SHUFFLE);
  865. if (c) {
  866. pthread_mutex_lock(&c->mutex);
  867. if (c->files.num) {
  868. struct media_file_data *last = da_end(c->files);
  869. const char *slash;
  870. dstr_copy(&path, last->path);
  871. dstr_replace(&path, "\\", "/");
  872. slash = strrchr(path.array, '/');
  873. if (slash)
  874. dstr_resize(&path, slash - path.array + 1);
  875. }
  876. pthread_mutex_unlock(&c->mutex);
  877. }
  878. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  879. OBS_COMBO_TYPE_LIST,
  880. OBS_COMBO_FORMAT_STRING);
  881. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  882. S_BEHAVIOR_STOP_RESTART);
  883. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  884. S_BEHAVIOR_PAUSE_UNPAUSE);
  885. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  886. S_BEHAVIOR_ALWAYS_PLAY);
  887. dstr_cat(&filter, "Media Files (");
  888. dstr_copy(&exts, EXTENSIONS_MEDIA);
  889. dstr_replace(&exts, ";", " ");
  890. dstr_cat_dstr(&filter, &exts);
  891. dstr_cat(&filter, ");;Video Files (");
  892. dstr_copy(&exts, EXTENSIONS_VIDEO);
  893. dstr_replace(&exts, ";", " ");
  894. dstr_cat_dstr(&filter, &exts);
  895. dstr_cat(&filter, ");;Audio Files (");
  896. dstr_copy(&exts, EXTENSIONS_AUDIO);
  897. dstr_replace(&exts, ";", " ");
  898. dstr_cat_dstr(&filter, &exts);
  899. dstr_cat(&filter, ");;Playlist Files (");
  900. dstr_copy(&exts, EXTENSIONS_PLAYLIST);
  901. dstr_replace(&exts, ";", " ");
  902. dstr_cat_dstr(&filter, &exts);
  903. dstr_cat(&filter, ")");
  904. obs_properties_add_editable_list(ppts, S_PLAYLIST, T_PLAYLIST,
  905. OBS_EDITABLE_LIST_TYPE_FILES_AND_URLS,
  906. filter.array, path.array);
  907. dstr_free(&path);
  908. dstr_free(&filter);
  909. dstr_free(&exts);
  910. obs_properties_add_int(ppts, S_NETWORK_CACHING, T_NETWORK_CACHING, 100,
  911. 60000, 10);
  912. obs_properties_add_int(ppts, S_TRACK, T_TRACK, 1, 10, 1);
  913. obs_properties_add_bool(ppts, S_SUBTITLE_ENABLE, T_SUBTITLE_ENABLE);
  914. obs_properties_add_int(ppts, S_SUBTITLE_TRACK, T_SUBTITLE_TRACK, 1, 10,
  915. 1);
  916. return ppts;
  917. }
  918. static void missing_file_callback(void *src, const char *new_path, void *data)
  919. {
  920. struct vlc_source *s = src;
  921. const char *orig_path = data;
  922. obs_source_t *source = s->source;
  923. obs_data_t *settings = obs_source_get_settings(source);
  924. obs_data_array_t *files = obs_data_get_array(settings, S_PLAYLIST);
  925. size_t l = obs_data_array_count(files);
  926. for (size_t i = 0; i < l; i++) {
  927. obs_data_t *file = obs_data_array_item(files, i);
  928. const char *path = obs_data_get_string(file, "value");
  929. if (strcmp(path, orig_path) == 0) {
  930. obs_data_set_string(file, "value", new_path);
  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. };