obs-ffmpeg-source.c 20 KB

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