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. 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. libvlc_state_t state = libvlc_media_player_get_state_(c->media_player);
  672. if (pause && state == libvlc_Playing)
  673. libvlc_media_list_player_pause_(c->media_list_player);
  674. else if (!pause && state == libvlc_Paused)
  675. libvlc_media_list_player_play_(c->media_list_player);
  676. }
  677. static void vlcs_restart(void *data)
  678. {
  679. struct vlc_source *c = data;
  680. libvlc_media_list_player_stop_(c->media_list_player);
  681. libvlc_media_list_player_play_(c->media_list_player);
  682. }
  683. static void vlcs_stop(void *data)
  684. {
  685. struct vlc_source *c = data;
  686. libvlc_media_list_player_stop_(c->media_list_player);
  687. obs_source_output_video(c->source, NULL);
  688. }
  689. static void vlcs_playlist_next(void *data)
  690. {
  691. struct vlc_source *c = data;
  692. libvlc_media_list_player_next_(c->media_list_player);
  693. }
  694. static void vlcs_playlist_prev(void *data)
  695. {
  696. struct vlc_source *c = data;
  697. libvlc_media_list_player_previous_(c->media_list_player);
  698. }
  699. static int64_t vlcs_get_duration(void *data)
  700. {
  701. struct vlc_source *c = data;
  702. return (int64_t)libvlc_media_player_get_length_(c->media_player);
  703. }
  704. static int64_t vlcs_get_time(void *data)
  705. {
  706. struct vlc_source *c = data;
  707. return (int64_t)libvlc_media_player_get_time_(c->media_player);
  708. }
  709. static void vlcs_set_time(void *data, int64_t ms)
  710. {
  711. struct vlc_source *c = data;
  712. libvlc_media_player_set_time_(c->media_player, (libvlc_time_t)ms);
  713. }
  714. static void vlcs_play_pause_hotkey(void *data, obs_hotkey_id id,
  715. obs_hotkey_t *hotkey, bool pressed)
  716. {
  717. UNUSED_PARAMETER(id);
  718. UNUSED_PARAMETER(hotkey);
  719. struct vlc_source *c = data;
  720. enum obs_media_state state = obs_source_media_get_state(c->source);
  721. if (pressed && obs_source_showing(c->source)) {
  722. if (state == OBS_MEDIA_STATE_PLAYING)
  723. obs_source_media_play_pause(c->source, true);
  724. else if (state == OBS_MEDIA_STATE_PAUSED)
  725. obs_source_media_play_pause(c->source, false);
  726. }
  727. }
  728. static void vlcs_restart_hotkey(void *data, obs_hotkey_id id,
  729. obs_hotkey_t *hotkey, bool pressed)
  730. {
  731. UNUSED_PARAMETER(id);
  732. UNUSED_PARAMETER(hotkey);
  733. struct vlc_source *c = data;
  734. if (pressed && obs_source_showing(c->source))
  735. obs_source_media_restart(c->source);
  736. }
  737. static void vlcs_stop_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  738. bool pressed)
  739. {
  740. UNUSED_PARAMETER(id);
  741. UNUSED_PARAMETER(hotkey);
  742. struct vlc_source *c = data;
  743. if (pressed && obs_source_showing(c->source))
  744. obs_source_media_stop(c->source);
  745. }
  746. static void vlcs_playlist_next_hotkey(void *data, obs_hotkey_id id,
  747. obs_hotkey_t *hotkey, bool pressed)
  748. {
  749. UNUSED_PARAMETER(id);
  750. UNUSED_PARAMETER(hotkey);
  751. struct vlc_source *c = data;
  752. if (pressed && obs_source_showing(c->source))
  753. obs_source_media_next(c->source);
  754. }
  755. static void vlcs_playlist_prev_hotkey(void *data, obs_hotkey_id id,
  756. obs_hotkey_t *hotkey, bool pressed)
  757. {
  758. UNUSED_PARAMETER(id);
  759. UNUSED_PARAMETER(hotkey);
  760. struct vlc_source *c = data;
  761. if (pressed && obs_source_showing(c->source))
  762. obs_source_media_previous(c->source);
  763. }
  764. static void *vlcs_create(obs_data_t *settings, obs_source_t *source)
  765. {
  766. struct vlc_source *c = bzalloc(sizeof(*c));
  767. c->source = source;
  768. c->play_pause_hotkey = obs_hotkey_register_source(
  769. source, "VLCSource.PlayPause", obs_module_text("PlayPause"),
  770. vlcs_play_pause_hotkey, c);
  771. c->restart_hotkey = obs_hotkey_register_source(
  772. source, "VLCSource.Restart", obs_module_text("Restart"),
  773. vlcs_restart_hotkey, c);
  774. c->stop_hotkey = obs_hotkey_register_source(source, "VLCSource.Stop",
  775. obs_module_text("Stop"),
  776. vlcs_stop_hotkey, c);
  777. c->playlist_next_hotkey = obs_hotkey_register_source(
  778. source, "VLCSource.PlaylistNext",
  779. obs_module_text("PlaylistNext"), vlcs_playlist_next_hotkey, c);
  780. c->playlist_prev_hotkey = obs_hotkey_register_source(
  781. source, "VLCSource.PlaylistPrev",
  782. obs_module_text("PlaylistPrev"), vlcs_playlist_prev_hotkey, c);
  783. pthread_mutex_init_value(&c->mutex);
  784. if (pthread_mutex_init(&c->mutex, NULL) != 0)
  785. goto error;
  786. if (!load_libvlc())
  787. goto error;
  788. c->media_list_player = libvlc_media_list_player_new_(libvlc);
  789. if (!c->media_list_player)
  790. goto error;
  791. c->media_player = libvlc_media_player_new_(libvlc);
  792. if (!c->media_player)
  793. goto error;
  794. libvlc_media_list_player_set_media_player_(c->media_list_player,
  795. c->media_player);
  796. libvlc_video_set_callbacks_(c->media_player, vlcs_video_lock, NULL,
  797. vlcs_video_display, c);
  798. libvlc_video_set_format_callbacks_(c->media_player, vlcs_video_format,
  799. NULL);
  800. libvlc_audio_set_callbacks_(c->media_player, vlcs_audio_play, NULL,
  801. NULL, NULL, NULL, c);
  802. libvlc_audio_set_format_callbacks_(c->media_player, vlcs_audio_setup,
  803. NULL);
  804. libvlc_event_manager_t *event_manager;
  805. event_manager = libvlc_media_player_event_manager_(c->media_player);
  806. libvlc_event_attach_(event_manager, libvlc_MediaPlayerEndReached,
  807. vlcs_stopped, c);
  808. libvlc_event_attach_(event_manager, libvlc_MediaPlayerOpening,
  809. vlcs_started, c);
  810. proc_handler_t *ph = obs_source_get_proc_handler(source);
  811. proc_handler_add(
  812. ph, "void get_metadata(in string tag_id out string tag_data)",
  813. vlcs_get_metadata, c);
  814. obs_source_update(source, NULL);
  815. UNUSED_PARAMETER(settings);
  816. return c;
  817. error:
  818. vlcs_destroy(c);
  819. return NULL;
  820. }
  821. static void vlcs_activate(void *data)
  822. {
  823. struct vlc_source *c = data;
  824. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  825. libvlc_media_list_player_play_(c->media_list_player);
  826. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  827. libvlc_media_list_player_play_(c->media_list_player);
  828. }
  829. }
  830. static void vlcs_deactivate(void *data)
  831. {
  832. struct vlc_source *c = data;
  833. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  834. libvlc_media_list_player_stop_(c->media_list_player);
  835. obs_source_output_video(c->source, NULL);
  836. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  837. libvlc_media_list_player_pause_(c->media_list_player);
  838. }
  839. }
  840. static void vlcs_defaults(obs_data_t *settings)
  841. {
  842. obs_data_set_default_bool(settings, S_LOOP, true);
  843. obs_data_set_default_bool(settings, S_SHUFFLE, false);
  844. obs_data_set_default_string(settings, S_BEHAVIOR,
  845. S_BEHAVIOR_STOP_RESTART);
  846. obs_data_set_default_int(settings, S_NETWORK_CACHING, 400);
  847. obs_data_set_default_int(settings, S_TRACK, 1);
  848. obs_data_set_default_bool(settings, S_SUBTITLE_ENABLE, false);
  849. obs_data_set_default_int(settings, S_SUBTITLE_TRACK, 1);
  850. }
  851. static obs_properties_t *vlcs_properties(void *data)
  852. {
  853. obs_properties_t *ppts = obs_properties_create();
  854. struct vlc_source *c = data;
  855. struct dstr filter = {0};
  856. struct dstr exts = {0};
  857. struct dstr path = {0};
  858. obs_property_t *p;
  859. obs_properties_set_flags(ppts, OBS_PROPERTIES_DEFER_UPDATE);
  860. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  861. obs_properties_add_bool(ppts, S_SHUFFLE, T_SHUFFLE);
  862. if (c) {
  863. pthread_mutex_lock(&c->mutex);
  864. if (c->files.num) {
  865. struct media_file_data *last = da_end(c->files);
  866. const char *slash;
  867. dstr_copy(&path, last->path);
  868. dstr_replace(&path, "\\", "/");
  869. slash = strrchr(path.array, '/');
  870. if (slash)
  871. dstr_resize(&path, slash - path.array + 1);
  872. }
  873. pthread_mutex_unlock(&c->mutex);
  874. }
  875. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  876. OBS_COMBO_TYPE_LIST,
  877. OBS_COMBO_FORMAT_STRING);
  878. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  879. S_BEHAVIOR_STOP_RESTART);
  880. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  881. S_BEHAVIOR_PAUSE_UNPAUSE);
  882. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  883. S_BEHAVIOR_ALWAYS_PLAY);
  884. dstr_cat(&filter, "Media Files (");
  885. dstr_copy(&exts, EXTENSIONS_MEDIA);
  886. dstr_replace(&exts, ";", " ");
  887. dstr_cat_dstr(&filter, &exts);
  888. dstr_cat(&filter, ");;Video Files (");
  889. dstr_copy(&exts, EXTENSIONS_VIDEO);
  890. dstr_replace(&exts, ";", " ");
  891. dstr_cat_dstr(&filter, &exts);
  892. dstr_cat(&filter, ");;Audio Files (");
  893. dstr_copy(&exts, EXTENSIONS_AUDIO);
  894. dstr_replace(&exts, ";", " ");
  895. dstr_cat_dstr(&filter, &exts);
  896. dstr_cat(&filter, ");;Playlist Files (");
  897. dstr_copy(&exts, EXTENSIONS_PLAYLIST);
  898. dstr_replace(&exts, ";", " ");
  899. dstr_cat_dstr(&filter, &exts);
  900. dstr_cat(&filter, ")");
  901. obs_properties_add_editable_list(ppts, S_PLAYLIST, T_PLAYLIST,
  902. OBS_EDITABLE_LIST_TYPE_FILES_AND_URLS,
  903. filter.array, path.array);
  904. dstr_free(&path);
  905. dstr_free(&filter);
  906. dstr_free(&exts);
  907. p = obs_properties_add_int(ppts, S_NETWORK_CACHING, T_NETWORK_CACHING,
  908. 100, 60000, 10);
  909. obs_property_int_set_suffix(p, " ms");
  910. obs_properties_add_int(ppts, S_TRACK, T_TRACK, 1, 10, 1);
  911. obs_properties_add_bool(ppts, S_SUBTITLE_ENABLE, T_SUBTITLE_ENABLE);
  912. obs_properties_add_int(ppts, S_SUBTITLE_TRACK, T_SUBTITLE_TRACK, 1,
  913. 1000, 1);
  914. return ppts;
  915. }
  916. static void missing_file_callback(void *src, const char *new_path, void *data)
  917. {
  918. struct vlc_source *s = src;
  919. const char *orig_path = data;
  920. obs_source_t *source = s->source;
  921. obs_data_t *settings = obs_source_get_settings(source);
  922. obs_data_array_t *files = obs_data_get_array(settings, S_PLAYLIST);
  923. size_t l = obs_data_array_count(files);
  924. for (size_t i = 0; i < l; i++) {
  925. obs_data_t *file = obs_data_array_item(files, i);
  926. const char *path = obs_data_get_string(file, "value");
  927. if (strcmp(path, orig_path) == 0) {
  928. if (new_path && *new_path)
  929. obs_data_set_string(file, "value", new_path);
  930. else
  931. obs_data_array_erase(files, i);
  932. obs_data_release(file);
  933. break;
  934. }
  935. obs_data_release(file);
  936. }
  937. obs_source_update(source, settings);
  938. obs_data_array_release(files);
  939. obs_data_release(settings);
  940. }
  941. static obs_missing_files_t *vlcs_missingfiles(void *data)
  942. {
  943. struct vlc_source *s = data;
  944. obs_missing_files_t *missing_files = obs_missing_files_create();
  945. obs_source_t *source = s->source;
  946. obs_data_t *settings = obs_source_get_settings(source);
  947. obs_data_array_t *files = obs_data_get_array(settings, S_PLAYLIST);
  948. size_t l = obs_data_array_count(files);
  949. for (size_t i = 0; i < l; i++) {
  950. obs_data_t *item = obs_data_array_item(files, i);
  951. const char *path = obs_data_get_string(item, "value");
  952. if (strcmp(path, "") != 0) {
  953. if (!os_file_exists(path) &&
  954. strstr(path, "://") == NULL) {
  955. obs_missing_file_t *file =
  956. obs_missing_file_create(
  957. path, missing_file_callback,
  958. OBS_MISSING_FILE_SOURCE, source,
  959. (void *)path);
  960. obs_missing_files_add_file(missing_files, file);
  961. }
  962. }
  963. obs_data_release(item);
  964. }
  965. obs_data_array_release(files);
  966. obs_data_release(settings);
  967. return missing_files;
  968. }
  969. struct obs_source_info vlc_source_info = {
  970. .id = "vlc_source",
  971. .type = OBS_SOURCE_TYPE_INPUT,
  972. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  973. OBS_SOURCE_DO_NOT_DUPLICATE |
  974. OBS_SOURCE_CONTROLLABLE_MEDIA,
  975. .get_name = vlcs_get_name,
  976. .create = vlcs_create,
  977. .destroy = vlcs_destroy,
  978. .update = vlcs_update,
  979. .get_defaults = vlcs_defaults,
  980. .get_properties = vlcs_properties,
  981. .activate = vlcs_activate,
  982. .deactivate = vlcs_deactivate,
  983. .missing_files = vlcs_missingfiles,
  984. .icon_type = OBS_ICON_TYPE_MEDIA,
  985. .media_play_pause = vlcs_play_pause,
  986. .media_restart = vlcs_restart,
  987. .media_stop = vlcs_stop,
  988. .media_next = vlcs_playlist_next,
  989. .media_previous = vlcs_playlist_prev,
  990. .media_get_duration = vlcs_get_duration,
  991. .media_get_time = vlcs_get_time,
  992. .media_set_time = vlcs_set_time,
  993. .media_get_state = vlcs_get_state,
  994. };