vlc-video-source.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. #define S_PLAYLIST "playlist"
  11. #define S_LOOP "loop"
  12. #define S_SHUFFLE "shuffle"
  13. #define S_BEHAVIOR "playback_behavior"
  14. #define S_BEHAVIOR_STOP_RESTART "stop_restart"
  15. #define S_BEHAVIOR_PAUSE_UNPAUSE "pause_unpause"
  16. #define S_BEHAVIOR_ALWAYS_PLAY "always_play"
  17. #define S_NETWORK_CACHING "network_caching"
  18. #define S_TRACK "track"
  19. #define S_SUBTITLE_ENABLE "subtitle_enable"
  20. #define S_SUBTITLE_TRACK "subtitle"
  21. #define T_(text) obs_module_text(text)
  22. #define T_PLAYLIST T_("Playlist")
  23. #define T_LOOP T_("LoopPlaylist")
  24. #define T_SHUFFLE T_("shuffle")
  25. #define T_BEHAVIOR T_("PlaybackBehavior")
  26. #define T_BEHAVIOR_STOP_RESTART T_("PlaybackBehavior.StopRestart")
  27. #define T_BEHAVIOR_PAUSE_UNPAUSE T_("PlaybackBehavior.PauseUnpause")
  28. #define T_BEHAVIOR_ALWAYS_PLAY T_("PlaybackBehavior.AlwaysPlay")
  29. #define T_NETWORK_CACHING T_("NetworkCaching")
  30. #define T_TRACK T_("AudioTrack")
  31. #define T_SUBTITLE_ENABLE T_("SubtitleEnable")
  32. #define T_SUBTITLE_TRACK T_("SubtitleTrack")
  33. /* ------------------------------------------------------------------------- */
  34. struct media_file_data {
  35. char *path;
  36. libvlc_media_t *media;
  37. };
  38. enum behavior {
  39. BEHAVIOR_STOP_RESTART,
  40. BEHAVIOR_PAUSE_UNPAUSE,
  41. BEHAVIOR_ALWAYS_PLAY,
  42. };
  43. struct vlc_source {
  44. obs_source_t *source;
  45. libvlc_media_player_t *media_player;
  46. libvlc_media_list_player_t *media_list_player;
  47. struct obs_source_frame frame;
  48. struct obs_source_audio audio;
  49. size_t audio_capacity;
  50. pthread_mutex_t mutex;
  51. DARRAY(struct media_file_data) files;
  52. enum behavior behavior;
  53. bool loop;
  54. bool shuffle;
  55. obs_hotkey_id play_pause_hotkey;
  56. obs_hotkey_id restart_hotkey;
  57. obs_hotkey_id stop_hotkey;
  58. obs_hotkey_id playlist_next_hotkey;
  59. obs_hotkey_id playlist_prev_hotkey;
  60. };
  61. static libvlc_media_t *get_media(struct darray *array, const char *path)
  62. {
  63. DARRAY(struct media_file_data) files;
  64. libvlc_media_t *media = NULL;
  65. files.da = *array;
  66. for (size_t i = 0; i < files.num; i++) {
  67. const char *cur_path = files.array[i].path;
  68. if (strcmp(path, cur_path) == 0) {
  69. media = files.array[i].media;
  70. libvlc_media_retain_(media);
  71. break;
  72. }
  73. }
  74. return media;
  75. }
  76. static inline libvlc_media_t *create_media_from_file(const char *file)
  77. {
  78. return (file && strstr(file, "://") != NULL)
  79. ? libvlc_media_new_location_(libvlc, file)
  80. : libvlc_media_new_path_(libvlc, file);
  81. }
  82. static void free_files(struct darray *array)
  83. {
  84. DARRAY(struct media_file_data) files;
  85. files.da = *array;
  86. for (size_t i = 0; i < files.num; i++) {
  87. bfree(files.array[i].path);
  88. libvlc_media_release_(files.array[i].media);
  89. }
  90. da_free(files);
  91. }
  92. static inline bool chroma_is(const char *chroma, const char *val)
  93. {
  94. return *(uint32_t*)chroma == *(uint32_t*)val;
  95. }
  96. static enum video_format convert_vlc_video_format(char *chroma, bool *full)
  97. {
  98. *full = false;
  99. #define CHROMA_TEST(val, ret) \
  100. if (chroma_is(chroma, val)) return ret
  101. #define CHROMA_CONV(val, new_val, ret) \
  102. do { \
  103. if (chroma_is(chroma, val)) { \
  104. *(uint32_t*)chroma = *(uint32_t*)new_val; \
  105. return ret; \
  106. } \
  107. } while (false)
  108. #define CHROMA_CONV_FULL(val, new_val, ret) \
  109. do { \
  110. *full = true; \
  111. CHROMA_CONV(val, new_val, ret); \
  112. } while (false)
  113. CHROMA_TEST("RGBA", VIDEO_FORMAT_RGBA);
  114. CHROMA_TEST("BGRA", VIDEO_FORMAT_BGRA);
  115. /* 4:2:0 formats */
  116. CHROMA_TEST("NV12", VIDEO_FORMAT_NV12);
  117. CHROMA_TEST("I420", VIDEO_FORMAT_I420);
  118. CHROMA_TEST("IYUV", VIDEO_FORMAT_I420);
  119. CHROMA_CONV("NV21", "NV12", VIDEO_FORMAT_NV12);
  120. CHROMA_CONV("I422", "NV12", VIDEO_FORMAT_NV12);
  121. CHROMA_CONV("Y42B", "NV12", VIDEO_FORMAT_NV12);
  122. CHROMA_CONV("YV12", "NV12", VIDEO_FORMAT_NV12);
  123. CHROMA_CONV("yv12", "NV12", VIDEO_FORMAT_NV12);
  124. CHROMA_CONV_FULL("J420", "J420", VIDEO_FORMAT_I420);
  125. /* 4:2:2 formats */
  126. CHROMA_TEST("UYVY", VIDEO_FORMAT_UYVY);
  127. CHROMA_TEST("UYNV", VIDEO_FORMAT_UYVY);
  128. CHROMA_TEST("UYNY", VIDEO_FORMAT_UYVY);
  129. CHROMA_TEST("Y422", VIDEO_FORMAT_UYVY);
  130. CHROMA_TEST("HDYC", VIDEO_FORMAT_UYVY);
  131. CHROMA_TEST("AVUI", VIDEO_FORMAT_UYVY);
  132. CHROMA_TEST("uyv1", VIDEO_FORMAT_UYVY);
  133. CHROMA_TEST("2vuy", VIDEO_FORMAT_UYVY);
  134. CHROMA_TEST("2Vuy", VIDEO_FORMAT_UYVY);
  135. CHROMA_TEST("2Vu1", VIDEO_FORMAT_UYVY);
  136. CHROMA_TEST("YUY2", VIDEO_FORMAT_YUY2);
  137. CHROMA_TEST("YUYV", VIDEO_FORMAT_YUY2);
  138. CHROMA_TEST("YUNV", VIDEO_FORMAT_YUY2);
  139. CHROMA_TEST("V422", VIDEO_FORMAT_YUY2);
  140. CHROMA_TEST("YVYU", VIDEO_FORMAT_YVYU);
  141. CHROMA_CONV("v210", "UYVY", VIDEO_FORMAT_UYVY);
  142. CHROMA_CONV("cyuv", "UYVY", VIDEO_FORMAT_UYVY);
  143. CHROMA_CONV("CYUV", "UYVY", VIDEO_FORMAT_UYVY);
  144. CHROMA_CONV("VYUY", "UYVY", VIDEO_FORMAT_UYVY);
  145. CHROMA_CONV("NV16", "UYVY", VIDEO_FORMAT_UYVY);
  146. CHROMA_CONV("NV61", "UYVY", VIDEO_FORMAT_UYVY);
  147. CHROMA_CONV("I410", "UYVY", VIDEO_FORMAT_UYVY);
  148. CHROMA_CONV("I422", "UYVY", VIDEO_FORMAT_UYVY);
  149. CHROMA_CONV("Y42B", "UYVY", VIDEO_FORMAT_UYVY);
  150. CHROMA_CONV("J422", "UYVY", VIDEO_FORMAT_UYVY);
  151. /* 4:4:4 formats */
  152. CHROMA_TEST("I444", VIDEO_FORMAT_I444);
  153. CHROMA_CONV_FULL("J444", "RGBA", VIDEO_FORMAT_RGBA);
  154. CHROMA_CONV("YUVA", "RGBA", VIDEO_FORMAT_RGBA);
  155. /* 4:4:0 formats */
  156. CHROMA_CONV("I440", "I444", VIDEO_FORMAT_I444);
  157. CHROMA_CONV("J440", "I444", VIDEO_FORMAT_I444);
  158. /* 4:1:0 formats */
  159. CHROMA_CONV("YVU9", "NV12", VIDEO_FORMAT_UYVY);
  160. CHROMA_CONV("I410", "NV12", VIDEO_FORMAT_UYVY);
  161. /* 4:1:1 formats */
  162. CHROMA_CONV("I411", "NV12", VIDEO_FORMAT_UYVY);
  163. CHROMA_CONV("Y41B", "NV12", VIDEO_FORMAT_UYVY);
  164. /* greyscale formats */
  165. CHROMA_TEST("GREY", VIDEO_FORMAT_Y800);
  166. CHROMA_TEST("Y800", VIDEO_FORMAT_Y800);
  167. CHROMA_TEST("Y8 ", VIDEO_FORMAT_Y800);
  168. #undef CHROMA_CONV_FULL
  169. #undef CHROMA_CONV
  170. #undef CHROMA_TEST
  171. *(uint32_t*)chroma = *(uint32_t*)"BGRA";
  172. return VIDEO_FORMAT_BGRA;
  173. }
  174. static inline unsigned get_format_lines(enum video_format format,
  175. unsigned height, size_t plane)
  176. {
  177. switch (format) {
  178. case VIDEO_FORMAT_I420:
  179. case VIDEO_FORMAT_NV12:
  180. return (plane == 0) ? height : height / 2;
  181. case VIDEO_FORMAT_YVYU:
  182. case VIDEO_FORMAT_YUY2:
  183. case VIDEO_FORMAT_UYVY:
  184. case VIDEO_FORMAT_I444:
  185. case VIDEO_FORMAT_RGBA:
  186. case VIDEO_FORMAT_BGRA:
  187. case VIDEO_FORMAT_BGRX:
  188. case VIDEO_FORMAT_Y800:
  189. return height;
  190. case VIDEO_FORMAT_NONE:;
  191. }
  192. return 0;
  193. }
  194. static enum audio_format convert_vlc_audio_format(char *format)
  195. {
  196. #define AUDIO_TEST(val, ret) \
  197. if (chroma_is(format, val)) return ret
  198. #define AUDIO_CONV(val, new_val, ret) \
  199. do { \
  200. if (chroma_is(format, val)) { \
  201. *(uint32_t*)format = *(uint32_t*)new_val; \
  202. return ret; \
  203. } \
  204. } while (false)
  205. AUDIO_TEST("S16N", AUDIO_FORMAT_16BIT);
  206. AUDIO_TEST("S32N", AUDIO_FORMAT_32BIT);
  207. AUDIO_TEST("FL32", AUDIO_FORMAT_FLOAT);
  208. AUDIO_CONV("U16N", "S16N", AUDIO_FORMAT_16BIT);
  209. AUDIO_CONV("U32N", "S32N", AUDIO_FORMAT_32BIT);
  210. AUDIO_CONV("S24N", "S32N", AUDIO_FORMAT_32BIT);
  211. AUDIO_CONV("U24N", "S32N", AUDIO_FORMAT_32BIT);
  212. AUDIO_CONV("FL64", "FL32", AUDIO_FORMAT_FLOAT);
  213. AUDIO_CONV("S16I", "S16N", AUDIO_FORMAT_16BIT);
  214. AUDIO_CONV("U16I", "S16N", AUDIO_FORMAT_16BIT);
  215. AUDIO_CONV("S24I", "S32N", AUDIO_FORMAT_32BIT);
  216. AUDIO_CONV("U24I", "S32N", AUDIO_FORMAT_32BIT);
  217. AUDIO_CONV("S32I", "S32N", AUDIO_FORMAT_32BIT);
  218. AUDIO_CONV("U32I", "S32N", AUDIO_FORMAT_32BIT);
  219. #undef AUDIO_CONV
  220. #undef AUDIO_TEST
  221. *(uint32_t*)format = *(uint32_t*)"FL32";
  222. return AUDIO_FORMAT_FLOAT;
  223. }
  224. /* ------------------------------------------------------------------------- */
  225. static const char *vlcs_get_name(void *unused)
  226. {
  227. UNUSED_PARAMETER(unused);
  228. return obs_module_text("VLCSource");
  229. }
  230. static void vlcs_destroy(void *data)
  231. {
  232. struct vlc_source *c = data;
  233. if (c->media_list_player) {
  234. libvlc_media_list_player_stop_(c->media_list_player);
  235. libvlc_media_list_player_release_(c->media_list_player);
  236. }
  237. if (c->media_player) {
  238. libvlc_media_player_release_(c->media_player);
  239. }
  240. bfree((void*)c->audio.data[0]);
  241. obs_source_frame_free(&c->frame);
  242. free_files(&c->files.da);
  243. pthread_mutex_destroy(&c->mutex);
  244. bfree(c);
  245. }
  246. static void *vlcs_video_lock(void *data, void **planes)
  247. {
  248. struct vlc_source *c = data;
  249. for (size_t i = 0; i < MAX_AV_PLANES && c->frame.data[i] != NULL; i++)
  250. planes[i] = c->frame.data[i];
  251. return NULL;
  252. }
  253. static void vlcs_video_display(void *data, void *picture)
  254. {
  255. struct vlc_source *c = data;
  256. c->frame.timestamp = (uint64_t)libvlc_clock_() * 1000ULL - time_start;
  257. obs_source_output_video(c->source, &c->frame);
  258. UNUSED_PARAMETER(picture);
  259. }
  260. static unsigned vlcs_video_format(void **p_data, char *chroma, unsigned *width,
  261. unsigned *height, unsigned *pitches, unsigned *lines)
  262. {
  263. struct vlc_source *c = *p_data;
  264. enum video_format new_format;
  265. enum video_range_type range;
  266. bool new_range;
  267. unsigned new_width = 0;
  268. unsigned new_height = 0;
  269. size_t i = 0;
  270. new_format = convert_vlc_video_format(chroma, &new_range);
  271. /* This is used because VLC will by default try to use a different
  272. * scaling than what the file uses (probably for optimization reasons).
  273. * For example, if the file is 1920x1080, it will try to render it by
  274. * 1920x1088, which isn't what we want. Calling libvlc_video_get_size
  275. * gets the actual video file's size, and thus fixes the problem.
  276. * However this doesn't work with URLs, so if it returns a 0 value, it
  277. * shouldn't be used. */
  278. libvlc_video_get_size_(c->media_player, 0, &new_width, &new_height);
  279. if (new_width && new_height) {
  280. *width = new_width;
  281. *height = new_height;
  282. }
  283. /* don't allocate a new frame if format/width/height hasn't changed */
  284. if (c->frame.format != new_format ||
  285. c->frame.width != *width ||
  286. c->frame.height != *height) {
  287. obs_source_frame_free(&c->frame);
  288. obs_source_frame_init(&c->frame, new_format, *width, *height);
  289. c->frame.format = new_format;
  290. c->frame.full_range = new_range;
  291. range = c->frame.full_range ?
  292. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  293. video_format_get_parameters(VIDEO_CS_DEFAULT, range,
  294. c->frame.color_matrix,
  295. c->frame.color_range_min,
  296. c->frame.color_range_max);
  297. }
  298. while (c->frame.data[i]) {
  299. pitches[i] = (unsigned)c->frame.linesize[i];
  300. lines[i] = get_format_lines(c->frame.format, *height, i);
  301. i++;
  302. }
  303. return 1;
  304. }
  305. static void vlcs_audio_play(void *data, const void *samples, unsigned count,
  306. int64_t pts)
  307. {
  308. struct vlc_source *c = data;
  309. size_t size = get_audio_size(c->audio.format, c->audio.speakers, count);
  310. if (c->audio_capacity < count) {
  311. c->audio.data[0] = brealloc((void*)c->audio.data[0], size);
  312. c->audio_capacity = count;
  313. }
  314. memcpy((void*)c->audio.data[0], samples, size);
  315. c->audio.timestamp = (uint64_t)pts * 1000ULL - time_start;
  316. c->audio.frames = count;
  317. obs_source_output_audio(c->source, &c->audio);
  318. }
  319. static int vlcs_audio_setup(void **p_data, char *format, unsigned *rate,
  320. unsigned *channels)
  321. {
  322. struct vlc_source *c = *p_data;
  323. enum audio_format new_audio_format;
  324. new_audio_format = convert_vlc_audio_format(format);
  325. if (*channels > 2)
  326. *channels = 2;
  327. /* don't free audio data if the data is the same format */
  328. if (c->audio.format == new_audio_format &&
  329. c->audio.samples_per_sec == *rate &&
  330. c->audio.speakers == (enum speaker_layout)*channels)
  331. return 0;
  332. c->audio_capacity = 0;
  333. bfree((void*)c->audio.data[0]);
  334. memset(&c->audio, 0, sizeof(c->audio));
  335. c->audio.speakers = (enum speaker_layout)*channels;
  336. c->audio.samples_per_sec = *rate;
  337. c->audio.format = new_audio_format;
  338. return 0;
  339. }
  340. static void add_file(struct vlc_source *c, struct darray *array,
  341. const char *path, int network_caching, int track_index,
  342. int subtitle_index, bool subtitle_enable)
  343. {
  344. DARRAY(struct media_file_data) new_files;
  345. struct media_file_data data;
  346. struct dstr new_path = {0};
  347. libvlc_media_t *new_media;
  348. bool is_url = path && strstr(path, "://") != NULL;
  349. new_files.da = *array;
  350. dstr_copy(&new_path, path);
  351. #ifdef _WIN32
  352. if (!is_url)
  353. dstr_replace(&new_path, "/", "\\");
  354. #endif
  355. path = new_path.array;
  356. new_media = get_media(&c->files.da, path);
  357. if (!new_media)
  358. new_media = get_media(&new_files.da, path);
  359. if (!new_media)
  360. new_media = create_media_from_file(path);
  361. if (new_media) {
  362. if (is_url) {
  363. struct dstr network_caching_option = {0};
  364. dstr_catf(&network_caching_option,
  365. ":network-caching=%d", network_caching);
  366. libvlc_media_add_option_(new_media,
  367. network_caching_option.array);
  368. dstr_free(&network_caching_option);
  369. }
  370. struct dstr track_option = { 0 };
  371. dstr_catf(&track_option,
  372. ":audio-track=%d", track_index - 1);
  373. libvlc_media_add_option_(new_media, track_option.array);
  374. dstr_free(&track_option);
  375. struct dstr sub_option = {0};
  376. if (subtitle_enable) {
  377. dstr_catf(&sub_option,
  378. ":sub-track=%d", subtitle_index - 1);
  379. }
  380. libvlc_media_add_option_(new_media, sub_option.array);
  381. dstr_free(&sub_option);
  382. data.path = new_path.array;
  383. data.media = new_media;
  384. da_push_back(new_files, &data);
  385. } else {
  386. dstr_free(&new_path);
  387. }
  388. *array = new_files.da;
  389. }
  390. static bool valid_extension(const char *ext)
  391. {
  392. struct dstr test = {0};
  393. bool valid = false;
  394. const char *b;
  395. const char *e;
  396. if (!ext || !*ext)
  397. return false;
  398. b = EXTENSIONS_MEDIA + 1;
  399. e = strchr(b, ';');
  400. for (;;) {
  401. if (e) dstr_ncopy(&test, b, e - b);
  402. else dstr_copy(&test, b);
  403. if (dstr_cmp(&test, ext) == 0) {
  404. valid = true;
  405. break;
  406. }
  407. if (!e)
  408. break;
  409. b = e + 2;
  410. e = strchr(b, ';');
  411. }
  412. dstr_free(&test);
  413. return valid;
  414. }
  415. static void vlcs_update(void *data, obs_data_t *settings)
  416. {
  417. DARRAY(struct media_file_data) new_files;
  418. DARRAY(struct media_file_data) old_files;
  419. libvlc_media_list_t *media_list;
  420. struct vlc_source *c = data;
  421. obs_data_array_t *array;
  422. const char *behavior;
  423. size_t count;
  424. int network_caching;
  425. int track_index;
  426. int subtitle_index;
  427. bool subtitle_enable;
  428. da_init(new_files);
  429. da_init(old_files);
  430. array = obs_data_get_array(settings, S_PLAYLIST);
  431. count = obs_data_array_count(array);
  432. c->loop = obs_data_get_bool(settings, S_LOOP);
  433. behavior = obs_data_get_string(settings, S_BEHAVIOR);
  434. network_caching = (int)obs_data_get_int(settings, S_NETWORK_CACHING);
  435. track_index = (int)obs_data_get_int(settings, S_TRACK);
  436. subtitle_index = (int)obs_data_get_int(settings, S_SUBTITLE_TRACK);
  437. subtitle_enable = obs_data_get_bool(settings, S_SUBTITLE_ENABLE);
  438. if (astrcmpi(behavior, S_BEHAVIOR_PAUSE_UNPAUSE) == 0) {
  439. c->behavior = BEHAVIOR_PAUSE_UNPAUSE;
  440. } else if (astrcmpi(behavior, S_BEHAVIOR_ALWAYS_PLAY) == 0) {
  441. c->behavior = BEHAVIOR_ALWAYS_PLAY;
  442. } else { /* S_BEHAVIOR_STOP_RESTART */
  443. c->behavior = BEHAVIOR_STOP_RESTART;
  444. }
  445. /* ------------------------------------- */
  446. /* create new list of sources */
  447. for (size_t i = 0; i < count; i++) {
  448. obs_data_t *item = obs_data_array_item(array, i);
  449. const char *path = obs_data_get_string(item, "value");
  450. os_dir_t *dir = os_opendir(path);
  451. if (dir) {
  452. struct dstr dir_path = {0};
  453. struct os_dirent *ent;
  454. for (;;) {
  455. const char *ext;
  456. ent = os_readdir(dir);
  457. if (!ent)
  458. break;
  459. if (ent->directory)
  460. continue;
  461. ext = os_get_path_extension(ent->d_name);
  462. if (!valid_extension(ext))
  463. continue;
  464. dstr_copy(&dir_path, path);
  465. dstr_cat_ch(&dir_path, '/');
  466. dstr_cat(&dir_path, ent->d_name);
  467. add_file(c, &new_files.da, dir_path.array,
  468. network_caching, track_index,
  469. subtitle_index,
  470. subtitle_enable);
  471. }
  472. dstr_free(&dir_path);
  473. os_closedir(dir);
  474. } else {
  475. add_file(c, &new_files.da, path, network_caching,
  476. track_index, subtitle_index,
  477. subtitle_enable);
  478. }
  479. obs_data_release(item);
  480. }
  481. /* ------------------------------------- */
  482. /* update settings data */
  483. libvlc_media_list_player_stop_(c->media_list_player);
  484. pthread_mutex_lock(&c->mutex);
  485. old_files.da = c->files.da;
  486. c->files.da = new_files.da;
  487. pthread_mutex_unlock(&c->mutex);
  488. /* ------------------------------------- */
  489. /* shuffle playlist */
  490. c->shuffle = obs_data_get_bool(settings, S_SHUFFLE);
  491. if (c->files.num > 1 && c->shuffle) {
  492. DARRAY(struct media_file_data) new_files;
  493. DARRAY(size_t) idxs;
  494. da_init(new_files);
  495. da_init(idxs);
  496. da_resize(idxs, c->files.num);
  497. da_reserve(new_files, c->files.num);
  498. for (size_t i = 0; i < c->files.num; i++) {
  499. idxs.array[i] = i;
  500. }
  501. for (size_t i = idxs.num; i > 0; i--) {
  502. size_t val = rand() % i;
  503. size_t idx = idxs.array[val];
  504. da_push_back(new_files, &c->files.array[idx]);
  505. da_erase(idxs, val);
  506. }
  507. da_free(c->files);
  508. da_free(idxs);
  509. c->files.da = new_files.da;
  510. }
  511. /* ------------------------------------- */
  512. /* clean up and restart playback */
  513. free_files(&old_files.da);
  514. media_list = libvlc_media_list_new_(libvlc);
  515. libvlc_media_list_lock_(media_list);
  516. for (size_t i = 0; i < c->files.num; i++)
  517. libvlc_media_list_add_media_(media_list,
  518. c->files.array[i].media);
  519. libvlc_media_list_unlock_(media_list);
  520. libvlc_media_list_player_set_media_list_(c->media_list_player,
  521. media_list);
  522. libvlc_media_list_release_(media_list);
  523. libvlc_media_list_player_set_playback_mode_(c->media_list_player,
  524. c->loop ? libvlc_playback_mode_loop :
  525. libvlc_playback_mode_default);
  526. if (c->files.num &&
  527. (c->behavior == BEHAVIOR_ALWAYS_PLAY || obs_source_active(c->source)))
  528. libvlc_media_list_player_play_(c->media_list_player);
  529. else
  530. obs_source_output_video(c->source, NULL);
  531. obs_data_array_release(array);
  532. }
  533. static void vlcs_stopped(const struct libvlc_event_t *event, void *data)
  534. {
  535. struct vlc_source *c = data;
  536. if (!c->loop)
  537. obs_source_output_video(c->source, NULL);
  538. UNUSED_PARAMETER(event);
  539. }
  540. static void vlcs_play_pause(void *data)
  541. {
  542. struct vlc_source *c = data;
  543. libvlc_media_list_player_pause_(c->media_list_player);
  544. }
  545. static void vlcs_restart(void *data)
  546. {
  547. struct vlc_source *c = data;
  548. libvlc_media_list_player_stop_(c->media_list_player);
  549. libvlc_media_list_player_play_(c->media_list_player);
  550. }
  551. static void vlcs_stop(void *data)
  552. {
  553. struct vlc_source *c = data;
  554. libvlc_media_list_player_stop_(c->media_list_player);
  555. obs_source_output_video(c->source, NULL);
  556. }
  557. static void vlcs_playlist_next(void *data)
  558. {
  559. struct vlc_source *c = data;
  560. libvlc_media_list_player_next_(c->media_list_player);
  561. }
  562. static void vlcs_playlist_prev(void *data)
  563. {
  564. struct vlc_source *c = data;
  565. libvlc_media_list_player_previous_(c->media_list_player);
  566. }
  567. static void vlcs_play_pause_hotkey(void *data, obs_hotkey_id id,
  568. obs_hotkey_t *hotkey, bool pressed)
  569. {
  570. UNUSED_PARAMETER(id);
  571. UNUSED_PARAMETER(hotkey);
  572. struct vlc_source *c = data;
  573. if (pressed && obs_source_active(c->source))
  574. vlcs_play_pause(c);
  575. }
  576. static void vlcs_restart_hotkey(void *data, obs_hotkey_id id,
  577. obs_hotkey_t *hotkey, bool pressed)
  578. {
  579. UNUSED_PARAMETER(id);
  580. UNUSED_PARAMETER(hotkey);
  581. struct vlc_source *c = data;
  582. if (pressed && obs_source_active(c->source))
  583. vlcs_restart(c);
  584. }
  585. static void vlcs_stop_hotkey(void *data, obs_hotkey_id id,
  586. obs_hotkey_t *hotkey, bool pressed)
  587. {
  588. UNUSED_PARAMETER(id);
  589. UNUSED_PARAMETER(hotkey);
  590. struct vlc_source *c = data;
  591. if (pressed && obs_source_active(c->source))
  592. vlcs_stop(c);
  593. }
  594. static void vlcs_playlist_next_hotkey(void *data, obs_hotkey_id id,
  595. obs_hotkey_t *hotkey, bool pressed)
  596. {
  597. UNUSED_PARAMETER(id);
  598. UNUSED_PARAMETER(hotkey);
  599. struct vlc_source *c = data;
  600. if (pressed && obs_source_active(c->source))
  601. vlcs_playlist_next(c);
  602. }
  603. static void vlcs_playlist_prev_hotkey(void *data, obs_hotkey_id id,
  604. obs_hotkey_t *hotkey, bool pressed)
  605. {
  606. UNUSED_PARAMETER(id);
  607. UNUSED_PARAMETER(hotkey);
  608. struct vlc_source *c = data;
  609. if (pressed && obs_source_active(c->source))
  610. vlcs_playlist_prev(c);
  611. }
  612. static void *vlcs_create(obs_data_t *settings, obs_source_t *source)
  613. {
  614. struct vlc_source *c = bzalloc(sizeof(*c));
  615. c->source = source;
  616. c->play_pause_hotkey = obs_hotkey_register_source(source,
  617. "VLCSource.PlayPause",
  618. obs_module_text("PlayPause"),
  619. vlcs_play_pause_hotkey, c);
  620. c->restart_hotkey = obs_hotkey_register_source(source,
  621. "VLCSource.Restart",
  622. obs_module_text("Restart"),
  623. vlcs_restart_hotkey, c);
  624. c->stop_hotkey = obs_hotkey_register_source(source,
  625. "VLCSource.Stop",
  626. obs_module_text("Stop"),
  627. vlcs_stop_hotkey, c);
  628. c->playlist_next_hotkey = obs_hotkey_register_source(source,
  629. "VLCSource.PlaylistNext",
  630. obs_module_text("PlaylistNext"),
  631. vlcs_playlist_next_hotkey, c);
  632. c->playlist_prev_hotkey = obs_hotkey_register_source(source,
  633. "VLCSource.PlaylistPrev",
  634. obs_module_text("PlaylistPrev"),
  635. vlcs_playlist_prev_hotkey, c);
  636. pthread_mutex_init_value(&c->mutex);
  637. if (pthread_mutex_init(&c->mutex, NULL) != 0)
  638. goto error;
  639. if (!load_libvlc())
  640. goto error;
  641. c->media_list_player = libvlc_media_list_player_new_(libvlc);
  642. if (!c->media_list_player)
  643. goto error;
  644. c->media_player = libvlc_media_player_new_(libvlc);
  645. if (!c->media_player)
  646. goto error;
  647. libvlc_media_list_player_set_media_player_(c->media_list_player,
  648. c->media_player);
  649. libvlc_video_set_callbacks_(c->media_player,
  650. vlcs_video_lock, NULL, vlcs_video_display,
  651. c);
  652. libvlc_video_set_format_callbacks_(c->media_player,
  653. vlcs_video_format, NULL);
  654. libvlc_audio_set_callbacks_(c->media_player,
  655. vlcs_audio_play, NULL, NULL, NULL, NULL, c);
  656. libvlc_audio_set_format_callbacks_(c->media_player,
  657. vlcs_audio_setup, NULL);
  658. libvlc_event_manager_t *event_manager;
  659. event_manager = libvlc_media_player_event_manager_(c->media_player);
  660. libvlc_event_attach_(event_manager, libvlc_MediaPlayerEndReached,
  661. vlcs_stopped, c);
  662. obs_source_update(source, NULL);
  663. UNUSED_PARAMETER(settings);
  664. return c;
  665. error:
  666. vlcs_destroy(c);
  667. return NULL;
  668. }
  669. static void vlcs_activate(void *data)
  670. {
  671. struct vlc_source *c = data;
  672. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  673. libvlc_media_list_player_play_(c->media_list_player);
  674. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  675. libvlc_media_list_player_play_(c->media_list_player);
  676. }
  677. }
  678. static void vlcs_deactivate(void *data)
  679. {
  680. struct vlc_source *c = data;
  681. if (c->behavior == BEHAVIOR_STOP_RESTART) {
  682. libvlc_media_list_player_stop_(c->media_list_player);
  683. obs_source_output_video(c->source, NULL);
  684. } else if (c->behavior == BEHAVIOR_PAUSE_UNPAUSE) {
  685. libvlc_media_list_player_pause_(c->media_list_player);
  686. }
  687. }
  688. static void vlcs_defaults(obs_data_t *settings)
  689. {
  690. obs_data_set_default_bool(settings, S_LOOP, true);
  691. obs_data_set_default_bool(settings, S_SHUFFLE, false);
  692. obs_data_set_default_string(settings, S_BEHAVIOR,
  693. S_BEHAVIOR_STOP_RESTART);
  694. obs_data_set_default_int(settings, S_NETWORK_CACHING, 400);
  695. obs_data_set_default_int(settings, S_TRACK, 1);
  696. obs_data_set_default_bool(settings, S_SUBTITLE_ENABLE, false);
  697. obs_data_set_default_int(settings, S_SUBTITLE_TRACK, 1);
  698. }
  699. static obs_properties_t *vlcs_properties(void *data)
  700. {
  701. obs_properties_t *ppts = obs_properties_create();
  702. struct vlc_source *c = data;
  703. struct dstr filter = {0};
  704. struct dstr exts = {0};
  705. struct dstr path = {0};
  706. obs_property_t *p;
  707. obs_properties_set_flags(ppts, OBS_PROPERTIES_DEFER_UPDATE);
  708. obs_properties_add_bool(ppts, S_LOOP, T_LOOP);
  709. obs_properties_add_bool(ppts, S_SHUFFLE, T_SHUFFLE);
  710. if (c) {
  711. pthread_mutex_lock(&c->mutex);
  712. if (c->files.num) {
  713. struct media_file_data *last = da_end(c->files);
  714. const char *slash;
  715. dstr_copy(&path, last->path);
  716. dstr_replace(&path, "\\", "/");
  717. slash = strrchr(path.array, '/');
  718. if (slash)
  719. dstr_resize(&path, slash - path.array + 1);
  720. }
  721. pthread_mutex_unlock(&c->mutex);
  722. }
  723. p = obs_properties_add_list(ppts, S_BEHAVIOR, T_BEHAVIOR,
  724. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  725. obs_property_list_add_string(p, T_BEHAVIOR_STOP_RESTART,
  726. S_BEHAVIOR_STOP_RESTART);
  727. obs_property_list_add_string(p, T_BEHAVIOR_PAUSE_UNPAUSE,
  728. S_BEHAVIOR_PAUSE_UNPAUSE);
  729. obs_property_list_add_string(p, T_BEHAVIOR_ALWAYS_PLAY,
  730. S_BEHAVIOR_ALWAYS_PLAY);
  731. dstr_cat(&filter, "Media Files (");
  732. dstr_copy(&exts, EXTENSIONS_MEDIA);
  733. dstr_replace(&exts, ";", " ");
  734. dstr_cat_dstr(&filter, &exts);
  735. dstr_cat(&filter, ");;Video Files (");
  736. dstr_copy(&exts, EXTENSIONS_VIDEO);
  737. dstr_replace(&exts, ";", " ");
  738. dstr_cat_dstr(&filter, &exts);
  739. dstr_cat(&filter, ");;Audio Files (");
  740. dstr_copy(&exts, EXTENSIONS_AUDIO);
  741. dstr_replace(&exts, ";", " ");
  742. dstr_cat_dstr(&filter, &exts);
  743. dstr_cat(&filter, ");;Playlist Files (");
  744. dstr_copy(&exts, EXTENSIONS_PLAYLIST);
  745. dstr_replace(&exts, ";", " ");
  746. dstr_cat_dstr(&filter, &exts);
  747. dstr_cat(&filter, ")");
  748. obs_properties_add_editable_list(ppts, S_PLAYLIST, T_PLAYLIST,
  749. OBS_EDITABLE_LIST_TYPE_FILES_AND_URLS,
  750. filter.array, path.array);
  751. dstr_free(&path);
  752. dstr_free(&filter);
  753. dstr_free(&exts);
  754. obs_properties_add_int(ppts, S_NETWORK_CACHING, T_NETWORK_CACHING,
  755. 100, 60000, 10);
  756. obs_properties_add_int(ppts, S_TRACK, T_TRACK, 1, 10, 1);
  757. obs_properties_add_bool(ppts, S_SUBTITLE_ENABLE, T_SUBTITLE_ENABLE);
  758. obs_properties_add_int(ppts, S_SUBTITLE_TRACK, T_SUBTITLE_TRACK,
  759. 1, 10, 1);
  760. return ppts;
  761. }
  762. struct obs_source_info vlc_source_info = {
  763. .id = "vlc_source",
  764. .type = OBS_SOURCE_TYPE_INPUT,
  765. .output_flags = OBS_SOURCE_ASYNC_VIDEO |
  766. OBS_SOURCE_AUDIO |
  767. OBS_SOURCE_DO_NOT_DUPLICATE,
  768. .get_name = vlcs_get_name,
  769. .create = vlcs_create,
  770. .destroy = vlcs_destroy,
  771. .update = vlcs_update,
  772. .get_defaults = vlcs_defaults,
  773. .get_properties = vlcs_properties,
  774. .activate = vlcs_activate,
  775. .deactivate = vlcs_deactivate
  776. };