obs-ffmpeg-source.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. /*
  2. * Copyright (c) 2015 John R. Bradley <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <obs-module.h>
  17. #include <util/platform.h>
  18. #include <util/dstr.h>
  19. #include "obs-ffmpeg-compat.h"
  20. #include "obs-ffmpeg-formats.h"
  21. #include <media-playback/media.h>
  22. #define FF_LOG(level, format, ...) \
  23. blog(level, "[Media Source]: " format, ##__VA_ARGS__)
  24. #define FF_LOG_S(source, level, format, ...) \
  25. blog(level, "[Media Source '%s']: " format, \
  26. obs_source_get_name(source), ##__VA_ARGS__)
  27. #define FF_BLOG(level, format, ...) \
  28. FF_LOG_S(s->source, level, format, ##__VA_ARGS__)
  29. struct ffmpeg_source {
  30. mp_media_t media;
  31. bool media_valid;
  32. bool destroy_media;
  33. struct SwsContext *sws_ctx;
  34. int sws_width;
  35. int sws_height;
  36. enum AVPixelFormat sws_format;
  37. uint8_t *sws_data;
  38. int sws_linesize;
  39. enum video_range_type range;
  40. obs_source_t *source;
  41. obs_hotkey_id hotkey;
  42. char *input;
  43. char *input_format;
  44. int buffering_mb;
  45. int speed_percent;
  46. bool is_looping;
  47. bool is_local_file;
  48. bool is_hw_decoding;
  49. bool is_clear_on_media_end;
  50. bool restart_on_activate;
  51. bool close_when_inactive;
  52. bool seekable;
  53. pthread_t reconnect_thread;
  54. bool stop_reconnect;
  55. bool reconnect_thread_valid;
  56. volatile bool reconnecting;
  57. int reconnect_delay_sec;
  58. enum obs_media_state state;
  59. obs_hotkey_pair_id play_pause_hotkey;
  60. obs_hotkey_id stop_hotkey;
  61. };
  62. static void set_media_state(void *data, enum obs_media_state state)
  63. {
  64. struct ffmpeg_source *s = data;
  65. s->state = state;
  66. }
  67. static bool is_local_file_modified(obs_properties_t *props,
  68. obs_property_t *prop, obs_data_t *settings)
  69. {
  70. UNUSED_PARAMETER(prop);
  71. bool enabled = obs_data_get_bool(settings, "is_local_file");
  72. obs_property_t *input = obs_properties_get(props, "input");
  73. obs_property_t *input_format =
  74. obs_properties_get(props, "input_format");
  75. obs_property_t *local_file = obs_properties_get(props, "local_file");
  76. obs_property_t *looping = obs_properties_get(props, "looping");
  77. obs_property_t *buffering = obs_properties_get(props, "buffering_mb");
  78. obs_property_t *close =
  79. obs_properties_get(props, "close_when_inactive");
  80. obs_property_t *seekable = obs_properties_get(props, "seekable");
  81. obs_property_t *speed = obs_properties_get(props, "speed_percent");
  82. obs_property_t *reconnect_delay_sec =
  83. obs_properties_get(props, "reconnect_delay_sec");
  84. obs_property_set_visible(input, !enabled);
  85. obs_property_set_visible(input_format, !enabled);
  86. obs_property_set_visible(buffering, !enabled);
  87. obs_property_set_visible(close, enabled);
  88. obs_property_set_visible(local_file, enabled);
  89. obs_property_set_visible(looping, enabled);
  90. obs_property_set_visible(speed, enabled);
  91. obs_property_set_visible(seekable, !enabled);
  92. obs_property_set_visible(reconnect_delay_sec, !enabled);
  93. return true;
  94. }
  95. static void ffmpeg_source_defaults(obs_data_t *settings)
  96. {
  97. obs_data_set_default_bool(settings, "is_local_file", true);
  98. obs_data_set_default_bool(settings, "looping", false);
  99. obs_data_set_default_bool(settings, "clear_on_media_end", true);
  100. obs_data_set_default_bool(settings, "restart_on_activate", true);
  101. obs_data_set_default_int(settings, "reconnect_delay_sec", 10);
  102. obs_data_set_default_int(settings, "buffering_mb", 2);
  103. obs_data_set_default_int(settings, "speed_percent", 100);
  104. }
  105. static const char *media_filter =
  106. " (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
  107. static const char *video_filter =
  108. " (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.gif *.webm);;";
  109. static const char *audio_filter = " (*.mp3 *.aac *.ogg *.wav);;";
  110. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  111. {
  112. struct ffmpeg_source *s = data;
  113. struct dstr filter = {0};
  114. struct dstr path = {0};
  115. UNUSED_PARAMETER(data);
  116. obs_properties_t *props = obs_properties_create();
  117. obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
  118. obs_property_t *prop;
  119. // use this when obs allows non-readonly paths
  120. prop = obs_properties_add_bool(props, "is_local_file",
  121. obs_module_text("LocalFile"));
  122. obs_property_set_modified_callback(prop, is_local_file_modified);
  123. dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
  124. dstr_cat(&filter, media_filter);
  125. dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
  126. dstr_cat(&filter, video_filter);
  127. dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
  128. dstr_cat(&filter, audio_filter);
  129. dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
  130. dstr_cat(&filter, " (*.*)");
  131. if (s && s->input && *s->input) {
  132. const char *slash;
  133. dstr_copy(&path, s->input);
  134. dstr_replace(&path, "\\", "/");
  135. slash = strrchr(path.array, '/');
  136. if (slash)
  137. dstr_resize(&path, slash - path.array + 1);
  138. }
  139. obs_properties_add_path(props, "local_file",
  140. obs_module_text("LocalFile"), OBS_PATH_FILE,
  141. filter.array, path.array);
  142. dstr_free(&filter);
  143. dstr_free(&path);
  144. prop = obs_properties_add_bool(props, "looping",
  145. obs_module_text("Looping"));
  146. obs_properties_add_bool(props, "restart_on_activate",
  147. obs_module_text("RestartWhenActivated"));
  148. prop = obs_properties_add_int_slider(props, "buffering_mb",
  149. obs_module_text("BufferingMB"), 0,
  150. 16, 1);
  151. obs_property_int_set_suffix(prop, " MB");
  152. obs_properties_add_text(props, "input", obs_module_text("Input"),
  153. OBS_TEXT_DEFAULT);
  154. obs_properties_add_text(props, "input_format",
  155. obs_module_text("InputFormat"),
  156. OBS_TEXT_DEFAULT);
  157. prop = obs_properties_add_int_slider(
  158. props, "reconnect_delay_sec",
  159. obs_module_text("ReconnectDelayTime"), 1, 60, 1);
  160. obs_property_int_set_suffix(prop, " S");
  161. #ifndef __APPLE__
  162. obs_properties_add_bool(props, "hw_decode",
  163. obs_module_text("HardwareDecode"));
  164. #endif
  165. obs_properties_add_bool(props, "clear_on_media_end",
  166. obs_module_text("ClearOnMediaEnd"));
  167. prop = obs_properties_add_bool(
  168. props, "close_when_inactive",
  169. obs_module_text("CloseFileWhenInactive"));
  170. obs_property_set_long_description(
  171. prop, obs_module_text("CloseFileWhenInactive.ToolTip"));
  172. prop = obs_properties_add_int_slider(props, "speed_percent",
  173. obs_module_text("SpeedPercentage"),
  174. 1, 200, 1);
  175. obs_property_int_set_suffix(prop, "%");
  176. prop = obs_properties_add_list(props, "color_range",
  177. obs_module_text("ColorRange"),
  178. OBS_COMBO_TYPE_LIST,
  179. OBS_COMBO_FORMAT_INT);
  180. obs_property_list_add_int(prop, obs_module_text("ColorRange.Auto"),
  181. VIDEO_RANGE_DEFAULT);
  182. obs_property_list_add_int(prop, obs_module_text("ColorRange.Partial"),
  183. VIDEO_RANGE_PARTIAL);
  184. obs_property_list_add_int(prop, obs_module_text("ColorRange.Full"),
  185. VIDEO_RANGE_FULL);
  186. obs_properties_add_bool(props, "seekable", obs_module_text("Seekable"));
  187. return props;
  188. }
  189. static void dump_source_info(struct ffmpeg_source *s, const char *input,
  190. const char *input_format)
  191. {
  192. FF_BLOG(LOG_INFO,
  193. "settings:\n"
  194. "\tinput: %s\n"
  195. "\tinput_format: %s\n"
  196. "\tspeed: %d\n"
  197. "\tis_looping: %s\n"
  198. "\tis_hw_decoding: %s\n"
  199. "\tis_clear_on_media_end: %s\n"
  200. "\trestart_on_activate: %s\n"
  201. "\tclose_when_inactive: %s",
  202. input ? input : "(null)",
  203. input_format ? input_format : "(null)", s->speed_percent,
  204. s->is_looping ? "yes" : "no", s->is_hw_decoding ? "yes" : "no",
  205. s->is_clear_on_media_end ? "yes" : "no",
  206. s->restart_on_activate ? "yes" : "no",
  207. s->close_when_inactive ? "yes" : "no");
  208. }
  209. static void get_frame(void *opaque, struct obs_source_frame *f)
  210. {
  211. struct ffmpeg_source *s = opaque;
  212. obs_source_output_video(s->source, f);
  213. }
  214. static void preload_frame(void *opaque, struct obs_source_frame *f)
  215. {
  216. struct ffmpeg_source *s = opaque;
  217. if (s->close_when_inactive)
  218. return;
  219. if (s->is_clear_on_media_end || s->is_looping)
  220. obs_source_preload_video(s->source, f);
  221. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  222. FF_BLOG(LOG_INFO, "Reconnected.");
  223. }
  224. static void get_audio(void *opaque, struct obs_source_audio *a)
  225. {
  226. struct ffmpeg_source *s = opaque;
  227. obs_source_output_audio(s->source, a);
  228. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  229. FF_BLOG(LOG_INFO, "Reconnected.");
  230. }
  231. static void media_stopped(void *opaque)
  232. {
  233. struct ffmpeg_source *s = opaque;
  234. if (s->is_clear_on_media_end) {
  235. obs_source_output_video(s->source, NULL);
  236. }
  237. if (s->close_when_inactive && s->media_valid)
  238. s->destroy_media = true;
  239. set_media_state(s, OBS_MEDIA_STATE_ENDED);
  240. obs_source_media_ended(s->source);
  241. }
  242. static void ffmpeg_source_open(struct ffmpeg_source *s)
  243. {
  244. if (s->input && *s->input) {
  245. struct mp_media_info info = {
  246. .opaque = s,
  247. .v_cb = get_frame,
  248. .v_preload_cb = preload_frame,
  249. .a_cb = get_audio,
  250. .stop_cb = media_stopped,
  251. .path = s->input,
  252. .format = s->input_format,
  253. .buffering = s->buffering_mb * 1024 * 1024,
  254. .speed = s->speed_percent,
  255. .force_range = s->range,
  256. .hardware_decoding = s->is_hw_decoding,
  257. .is_local_file = s->is_local_file || s->seekable,
  258. .reconnecting = s->reconnecting,
  259. };
  260. s->media_valid = mp_media_init(&s->media, &info);
  261. }
  262. }
  263. static void ffmpeg_source_start(struct ffmpeg_source *s)
  264. {
  265. if (!s->media_valid)
  266. ffmpeg_source_open(s);
  267. if (!s->media_valid)
  268. return;
  269. mp_media_play(&s->media, s->is_looping, s->reconnecting);
  270. if (s->is_local_file)
  271. obs_source_show_preloaded_video(s->source);
  272. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  273. obs_source_media_started(s->source);
  274. }
  275. static void *ffmpeg_source_reconnect(void *data)
  276. {
  277. struct ffmpeg_source *s = data;
  278. os_sleep_ms(s->reconnect_delay_sec * 1000);
  279. if (s->stop_reconnect || s->media_valid)
  280. goto finish;
  281. bool active = obs_source_active(s->source);
  282. if (!s->close_when_inactive || active)
  283. ffmpeg_source_open(s);
  284. if (!s->restart_on_activate || active)
  285. ffmpeg_source_start(s);
  286. finish:
  287. s->reconnect_thread_valid = false;
  288. return NULL;
  289. }
  290. static void ffmpeg_source_tick(void *data, float seconds)
  291. {
  292. UNUSED_PARAMETER(seconds);
  293. struct ffmpeg_source *s = data;
  294. if (s->destroy_media) {
  295. if (s->media_valid) {
  296. mp_media_free(&s->media);
  297. s->media_valid = false;
  298. }
  299. s->destroy_media = false;
  300. if (!s->is_local_file) {
  301. if (!os_atomic_set_bool(&s->reconnecting, true)) {
  302. FF_BLOG(LOG_WARNING, "Disconnected. "
  303. "Reconnecting...");
  304. }
  305. if (pthread_create(&s->reconnect_thread, NULL,
  306. ffmpeg_source_reconnect, s) != 0) {
  307. FF_BLOG(LOG_WARNING, "Could not create "
  308. "reconnect thread");
  309. return;
  310. }
  311. s->reconnect_thread_valid = true;
  312. }
  313. }
  314. }
  315. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  316. {
  317. struct ffmpeg_source *s = data;
  318. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  319. char *input;
  320. char *input_format;
  321. bfree(s->input);
  322. bfree(s->input_format);
  323. if (is_local_file) {
  324. input = (char *)obs_data_get_string(settings, "local_file");
  325. input_format = NULL;
  326. s->is_looping = obs_data_get_bool(settings, "looping");
  327. s->close_when_inactive =
  328. obs_data_get_bool(settings, "close_when_inactive");
  329. } else {
  330. input = (char *)obs_data_get_string(settings, "input");
  331. input_format =
  332. (char *)obs_data_get_string(settings, "input_format");
  333. s->reconnect_delay_sec =
  334. (int)obs_data_get_int(settings, "reconnect_delay_sec");
  335. s->reconnect_delay_sec = s->reconnect_delay_sec == 0
  336. ? 10
  337. : s->reconnect_delay_sec;
  338. s->is_looping = false;
  339. s->close_when_inactive = true;
  340. if (s->reconnect_thread_valid) {
  341. s->stop_reconnect = true;
  342. pthread_join(s->reconnect_thread, NULL);
  343. s->stop_reconnect = false;
  344. }
  345. }
  346. s->input = input ? bstrdup(input) : NULL;
  347. s->input_format = input_format ? bstrdup(input_format) : NULL;
  348. #ifndef __APPLE__
  349. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  350. #endif
  351. s->is_clear_on_media_end =
  352. obs_data_get_bool(settings, "clear_on_media_end");
  353. s->restart_on_activate =
  354. obs_data_get_bool(settings, "restart_on_activate");
  355. s->range = (enum video_range_type)obs_data_get_int(settings,
  356. "color_range");
  357. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  358. s->speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  359. s->is_local_file = is_local_file;
  360. s->seekable = obs_data_get_bool(settings, "seekable");
  361. if (s->speed_percent < 1 || s->speed_percent > 200)
  362. s->speed_percent = 100;
  363. if (s->media_valid) {
  364. mp_media_free(&s->media);
  365. s->media_valid = false;
  366. }
  367. bool active = obs_source_active(s->source);
  368. if (!s->close_when_inactive || active)
  369. ffmpeg_source_open(s);
  370. dump_source_info(s, input, input_format);
  371. if (!s->restart_on_activate || active)
  372. ffmpeg_source_start(s);
  373. }
  374. static const char *ffmpeg_source_getname(void *unused)
  375. {
  376. UNUSED_PARAMETER(unused);
  377. return obs_module_text("FFMpegSource");
  378. }
  379. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  380. bool pressed)
  381. {
  382. UNUSED_PARAMETER(id);
  383. UNUSED_PARAMETER(hotkey);
  384. if (!pressed)
  385. return;
  386. struct ffmpeg_source *s = data;
  387. if (obs_source_showing(s->source))
  388. obs_source_media_restart(s->source);
  389. }
  390. static void restart_proc(void *data, calldata_t *cd)
  391. {
  392. restart_hotkey(data, 0, NULL, true);
  393. UNUSED_PARAMETER(cd);
  394. }
  395. static void get_duration(void *data, calldata_t *cd)
  396. {
  397. struct ffmpeg_source *s = data;
  398. int64_t dur = 0;
  399. if (s->media.fmt)
  400. dur = s->media.fmt->duration;
  401. calldata_set_int(cd, "duration", dur * 1000);
  402. }
  403. static void get_nb_frames(void *data, calldata_t *cd)
  404. {
  405. struct ffmpeg_source *s = data;
  406. int64_t frames = 0;
  407. if (!s->media.fmt) {
  408. calldata_set_int(cd, "num_frames", frames);
  409. return;
  410. }
  411. int video_stream_index = av_find_best_stream(
  412. s->media.fmt, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  413. if (video_stream_index < 0) {
  414. FF_BLOG(LOG_WARNING, "Getting number of frames failed: No "
  415. "video stream in media file!");
  416. calldata_set_int(cd, "num_frames", frames);
  417. return;
  418. }
  419. AVStream *stream = s->media.fmt->streams[video_stream_index];
  420. if (stream->nb_frames > 0) {
  421. frames = stream->nb_frames;
  422. } else {
  423. FF_BLOG(LOG_DEBUG, "nb_frames not set, estimating using frame "
  424. "rate and duration");
  425. AVRational avg_frame_rate = stream->avg_frame_rate;
  426. frames = (int64_t)ceil((double)s->media.fmt->duration /
  427. (double)AV_TIME_BASE *
  428. (double)avg_frame_rate.num /
  429. (double)avg_frame_rate.den);
  430. }
  431. calldata_set_int(cd, "num_frames", frames);
  432. }
  433. static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
  434. obs_hotkey_t *hotkey, bool pressed)
  435. {
  436. UNUSED_PARAMETER(id);
  437. UNUSED_PARAMETER(hotkey);
  438. if (!pressed)
  439. return false;
  440. struct ffmpeg_source *s = data;
  441. if (s->state == OBS_MEDIA_STATE_PLAYING ||
  442. !obs_source_showing(s->source))
  443. return false;
  444. obs_source_media_play_pause(s->source, false);
  445. return true;
  446. }
  447. static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
  448. obs_hotkey_t *hotkey, bool pressed)
  449. {
  450. UNUSED_PARAMETER(id);
  451. UNUSED_PARAMETER(hotkey);
  452. if (!pressed)
  453. return false;
  454. struct ffmpeg_source *s = data;
  455. if (s->state != OBS_MEDIA_STATE_PLAYING ||
  456. !obs_source_showing(s->source))
  457. return false;
  458. obs_source_media_play_pause(s->source, true);
  459. return true;
  460. }
  461. static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
  462. obs_hotkey_t *hotkey, bool pressed)
  463. {
  464. UNUSED_PARAMETER(id);
  465. UNUSED_PARAMETER(hotkey);
  466. if (!pressed)
  467. return;
  468. struct ffmpeg_source *s = data;
  469. if (obs_source_showing(s->source))
  470. obs_source_media_stop(s->source);
  471. }
  472. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  473. {
  474. UNUSED_PARAMETER(settings);
  475. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  476. s->source = source;
  477. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  478. obs_module_text("RestartMedia"),
  479. restart_hotkey, s);
  480. s->play_pause_hotkey = obs_hotkey_pair_register_source(
  481. s->source, "MediaSource.Play", obs_module_text("Play"),
  482. "MediaSource.Pause", obs_module_text("Pause"),
  483. ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
  484. s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
  485. obs_module_text("Stop"),
  486. ffmpeg_source_stop_hotkey,
  487. s);
  488. proc_handler_t *ph = obs_source_get_proc_handler(source);
  489. proc_handler_add(ph, "void restart()", restart_proc, s);
  490. proc_handler_add(ph, "void get_duration(out int duration)",
  491. get_duration, s);
  492. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  493. get_nb_frames, s);
  494. ffmpeg_source_update(s, settings);
  495. return s;
  496. }
  497. static void ffmpeg_source_destroy(void *data)
  498. {
  499. struct ffmpeg_source *s = data;
  500. if (s->hotkey)
  501. obs_hotkey_unregister(s->hotkey);
  502. if (!s->is_local_file) {
  503. s->stop_reconnect = true;
  504. if (s->reconnect_thread_valid)
  505. pthread_join(s->reconnect_thread, NULL);
  506. }
  507. if (s->media_valid)
  508. mp_media_free(&s->media);
  509. if (s->sws_ctx != NULL)
  510. sws_freeContext(s->sws_ctx);
  511. bfree(s->sws_data);
  512. bfree(s->input);
  513. bfree(s->input_format);
  514. bfree(s);
  515. }
  516. static void ffmpeg_source_activate(void *data)
  517. {
  518. struct ffmpeg_source *s = data;
  519. if (s->restart_on_activate)
  520. obs_source_media_restart(s->source);
  521. }
  522. static void ffmpeg_source_deactivate(void *data)
  523. {
  524. struct ffmpeg_source *s = data;
  525. if (s->restart_on_activate) {
  526. if (s->media_valid) {
  527. mp_media_stop(&s->media);
  528. if (s->is_clear_on_media_end)
  529. obs_source_output_video(s->source, NULL);
  530. }
  531. }
  532. }
  533. static void ffmpeg_source_play_pause(void *data, bool pause)
  534. {
  535. struct ffmpeg_source *s = data;
  536. mp_media_play_pause(&s->media, pause);
  537. if (pause)
  538. set_media_state(s, OBS_MEDIA_STATE_PAUSED);
  539. else
  540. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  541. }
  542. static void ffmpeg_source_stop(void *data)
  543. {
  544. struct ffmpeg_source *s = data;
  545. if (s->media_valid) {
  546. mp_media_stop(&s->media);
  547. obs_source_output_video(s->source, NULL);
  548. set_media_state(s, OBS_MEDIA_STATE_STOPPED);
  549. }
  550. }
  551. static void ffmpeg_source_restart(void *data)
  552. {
  553. struct ffmpeg_source *s = data;
  554. if (obs_source_showing(s->source))
  555. ffmpeg_source_start(s);
  556. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  557. }
  558. static int64_t ffmpeg_source_get_duration(void *data)
  559. {
  560. struct ffmpeg_source *s = data;
  561. int64_t dur = 0;
  562. if (s->media.fmt)
  563. dur = s->media.fmt->duration / INT64_C(1000);
  564. return dur;
  565. }
  566. static int64_t ffmpeg_source_get_time(void *data)
  567. {
  568. struct ffmpeg_source *s = data;
  569. return mp_get_current_time(&s->media);
  570. }
  571. static void ffmpeg_source_set_time(void *data, int64_t ms)
  572. {
  573. struct ffmpeg_source *s = data;
  574. mp_media_seek_to(&s->media, ms);
  575. }
  576. static enum obs_media_state ffmpeg_source_get_state(void *data)
  577. {
  578. struct ffmpeg_source *s = data;
  579. return s->state;
  580. }
  581. struct obs_source_info ffmpeg_source = {
  582. .id = "ffmpeg_source",
  583. .type = OBS_SOURCE_TYPE_INPUT,
  584. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  585. OBS_SOURCE_DO_NOT_DUPLICATE |
  586. OBS_SOURCE_CONTROLLABLE_MEDIA,
  587. .get_name = ffmpeg_source_getname,
  588. .create = ffmpeg_source_create,
  589. .destroy = ffmpeg_source_destroy,
  590. .get_defaults = ffmpeg_source_defaults,
  591. .get_properties = ffmpeg_source_getproperties,
  592. .activate = ffmpeg_source_activate,
  593. .deactivate = ffmpeg_source_deactivate,
  594. .video_tick = ffmpeg_source_tick,
  595. .update = ffmpeg_source_update,
  596. .icon_type = OBS_ICON_TYPE_MEDIA,
  597. .media_play_pause = ffmpeg_source_play_pause,
  598. .media_restart = ffmpeg_source_restart,
  599. .media_stop = ffmpeg_source_stop,
  600. .media_get_duration = ffmpeg_source_get_duration,
  601. .media_get_time = ffmpeg_source_get_time,
  602. .media_set_time = ffmpeg_source_set_time,
  603. .media_get_state = ffmpeg_source_get_state,
  604. };