vlc-video-source.c 31 KB

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