obs-ffmpeg-source.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. bool is_linear_alpha;
  41. obs_source_t *source;
  42. obs_hotkey_id hotkey;
  43. char *input;
  44. char *input_format;
  45. char *ffmpeg_options;
  46. int buffering_mb;
  47. int speed_percent;
  48. bool is_looping;
  49. bool is_local_file;
  50. bool is_hw_decoding;
  51. bool is_clear_on_media_end;
  52. bool restart_on_activate;
  53. bool close_when_inactive;
  54. bool seekable;
  55. pthread_t reconnect_thread;
  56. bool stop_reconnect;
  57. bool reconnect_thread_valid;
  58. volatile bool reconnecting;
  59. int reconnect_delay_sec;
  60. enum obs_media_state state;
  61. obs_hotkey_pair_id play_pause_hotkey;
  62. obs_hotkey_id stop_hotkey;
  63. };
  64. static void set_media_state(void *data, enum obs_media_state state)
  65. {
  66. struct ffmpeg_source *s = data;
  67. s->state = state;
  68. }
  69. static bool is_local_file_modified(obs_properties_t *props,
  70. obs_property_t *prop, obs_data_t *settings)
  71. {
  72. UNUSED_PARAMETER(prop);
  73. bool enabled = obs_data_get_bool(settings, "is_local_file");
  74. obs_property_t *input = obs_properties_get(props, "input");
  75. obs_property_t *input_format =
  76. obs_properties_get(props, "input_format");
  77. obs_property_t *local_file = obs_properties_get(props, "local_file");
  78. obs_property_t *looping = obs_properties_get(props, "looping");
  79. obs_property_t *buffering = obs_properties_get(props, "buffering_mb");
  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(local_file, enabled);
  88. obs_property_set_visible(looping, enabled);
  89. obs_property_set_visible(speed, enabled);
  90. obs_property_set_visible(seekable, !enabled);
  91. obs_property_set_visible(reconnect_delay_sec, !enabled);
  92. return true;
  93. }
  94. static void ffmpeg_source_defaults(obs_data_t *settings)
  95. {
  96. obs_data_set_default_bool(settings, "is_local_file", true);
  97. obs_data_set_default_bool(settings, "looping", false);
  98. obs_data_set_default_bool(settings, "clear_on_media_end", true);
  99. obs_data_set_default_bool(settings, "restart_on_activate", true);
  100. obs_data_set_default_bool(settings, "linear_alpha", false);
  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 *.mxf *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
  107. static const char *video_filter =
  108. " (*.mp4 *.ts *.mov *.mxf *.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. obs_properties_add_bool(props, "looping", obs_module_text("Looping"));
  145. obs_properties_add_bool(props, "restart_on_activate",
  146. obs_module_text("RestartWhenActivated"));
  147. prop = obs_properties_add_int_slider(props, "buffering_mb",
  148. obs_module_text("BufferingMB"), 0,
  149. 16, 1);
  150. obs_property_int_set_suffix(prop, " MB");
  151. obs_properties_add_text(props, "input", obs_module_text("Input"),
  152. OBS_TEXT_DEFAULT);
  153. obs_properties_add_text(props, "input_format",
  154. obs_module_text("InputFormat"),
  155. OBS_TEXT_DEFAULT);
  156. prop = obs_properties_add_int_slider(
  157. props, "reconnect_delay_sec",
  158. obs_module_text("ReconnectDelayTime"), 1, 60, 1);
  159. obs_property_int_set_suffix(prop, " S");
  160. obs_properties_add_bool(props, "hw_decode",
  161. obs_module_text("HardwareDecode"));
  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, "linear_alpha",
  184. obs_module_text("LinearAlpha"));
  185. obs_properties_add_bool(props, "seekable", obs_module_text("Seekable"));
  186. prop = obs_properties_add_text(props, "ffmpeg_options",
  187. obs_module_text("FFmpeg.Options"),
  188. OBS_TEXT_DEFAULT);
  189. obs_property_set_long_description(
  190. prop, obs_module_text("FFmpeg.Options.Tooltip"));
  191. return props;
  192. }
  193. static void dump_source_info(struct ffmpeg_source *s, const char *input,
  194. const char *input_format)
  195. {
  196. FF_BLOG(LOG_INFO,
  197. "settings:\n"
  198. "\tinput: %s\n"
  199. "\tinput_format: %s\n"
  200. "\tspeed: %d\n"
  201. "\tis_looping: %s\n"
  202. "\tis_linear_alpha: %s\n"
  203. "\tis_hw_decoding: %s\n"
  204. "\tis_clear_on_media_end: %s\n"
  205. "\trestart_on_activate: %s\n"
  206. "\tclose_when_inactive: %s\n"
  207. "\tffmpeg_options: %s",
  208. input ? input : "(null)",
  209. input_format ? input_format : "(null)", s->speed_percent,
  210. s->is_looping ? "yes" : "no", s->is_linear_alpha ? "yes" : "no",
  211. s->is_hw_decoding ? "yes" : "no",
  212. s->is_clear_on_media_end ? "yes" : "no",
  213. s->restart_on_activate ? "yes" : "no",
  214. s->close_when_inactive ? "yes" : "no", s->ffmpeg_options);
  215. }
  216. static void get_frame(void *opaque, struct obs_source_frame *f)
  217. {
  218. struct ffmpeg_source *s = opaque;
  219. obs_source_output_video(s->source, f);
  220. }
  221. static void preload_frame(void *opaque, struct obs_source_frame *f)
  222. {
  223. struct ffmpeg_source *s = opaque;
  224. if (s->close_when_inactive)
  225. return;
  226. if (s->is_clear_on_media_end || s->is_looping)
  227. obs_source_preload_video(s->source, f);
  228. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  229. FF_BLOG(LOG_INFO, "Reconnected.");
  230. }
  231. static void seek_frame(void *opaque, struct obs_source_frame *f)
  232. {
  233. struct ffmpeg_source *s = opaque;
  234. obs_source_set_video_frame(s->source, f);
  235. }
  236. static void get_audio(void *opaque, struct obs_source_audio *a)
  237. {
  238. struct ffmpeg_source *s = opaque;
  239. obs_source_output_audio(s->source, a);
  240. if (!s->is_local_file && os_atomic_set_bool(&s->reconnecting, false))
  241. FF_BLOG(LOG_INFO, "Reconnected.");
  242. }
  243. static void media_stopped(void *opaque)
  244. {
  245. struct ffmpeg_source *s = opaque;
  246. if (s->is_clear_on_media_end) {
  247. obs_source_output_video(s->source, NULL);
  248. }
  249. if ((s->close_when_inactive || !s->is_local_file) && s->media_valid)
  250. s->destroy_media = true;
  251. set_media_state(s, OBS_MEDIA_STATE_ENDED);
  252. obs_source_media_ended(s->source);
  253. }
  254. static void ffmpeg_source_open(struct ffmpeg_source *s)
  255. {
  256. if (s->input && *s->input) {
  257. struct mp_media_info info = {
  258. .opaque = s,
  259. .v_cb = get_frame,
  260. .v_preload_cb = preload_frame,
  261. .v_seek_cb = seek_frame,
  262. .a_cb = get_audio,
  263. .stop_cb = media_stopped,
  264. .path = s->input,
  265. .format = s->input_format,
  266. .buffering = s->buffering_mb * 1024 * 1024,
  267. .speed = s->speed_percent,
  268. .force_range = s->range,
  269. .is_linear_alpha = s->is_linear_alpha,
  270. .hardware_decoding = s->is_hw_decoding,
  271. .ffmpeg_options = s->ffmpeg_options,
  272. .is_local_file = s->is_local_file || s->seekable,
  273. .reconnecting = s->reconnecting,
  274. };
  275. s->media_valid = mp_media_init(&s->media, &info);
  276. }
  277. }
  278. static void ffmpeg_source_start(struct ffmpeg_source *s)
  279. {
  280. if (!s->media_valid)
  281. ffmpeg_source_open(s);
  282. if (!s->media_valid)
  283. return;
  284. mp_media_play(&s->media, s->is_looping, s->reconnecting);
  285. if (s->is_local_file && (s->is_clear_on_media_end || s->is_looping))
  286. obs_source_show_preloaded_video(s->source);
  287. else
  288. obs_source_output_video(s->source, NULL);
  289. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  290. obs_source_media_started(s->source);
  291. }
  292. static void *ffmpeg_source_reconnect(void *data)
  293. {
  294. struct ffmpeg_source *s = data;
  295. os_sleep_ms(s->reconnect_delay_sec * 1000);
  296. if (s->stop_reconnect || s->media_valid)
  297. goto finish;
  298. bool active = obs_source_active(s->source);
  299. if (!s->close_when_inactive || active)
  300. ffmpeg_source_open(s);
  301. if (!s->restart_on_activate || active)
  302. ffmpeg_source_start(s);
  303. finish:
  304. s->reconnect_thread_valid = false;
  305. return NULL;
  306. }
  307. static void ffmpeg_source_tick(void *data, float seconds)
  308. {
  309. UNUSED_PARAMETER(seconds);
  310. struct ffmpeg_source *s = data;
  311. if (s->destroy_media) {
  312. if (s->media_valid) {
  313. mp_media_free(&s->media);
  314. s->media_valid = false;
  315. }
  316. s->destroy_media = false;
  317. if (!s->is_local_file) {
  318. if (!os_atomic_set_bool(&s->reconnecting, true)) {
  319. FF_BLOG(LOG_WARNING, "Disconnected. "
  320. "Reconnecting...");
  321. }
  322. if (pthread_create(&s->reconnect_thread, NULL,
  323. ffmpeg_source_reconnect, s) != 0) {
  324. FF_BLOG(LOG_WARNING, "Could not create "
  325. "reconnect thread");
  326. return;
  327. }
  328. s->reconnect_thread_valid = true;
  329. }
  330. }
  331. }
  332. #define SRT_PROTO "srt"
  333. #define RIST_PROTO "rist"
  334. static bool requires_mpegts(const char *path)
  335. {
  336. return !astrcmpi_n(path, SRT_PROTO, sizeof(SRT_PROTO) - 1) ||
  337. !astrcmpi_n(path, RIST_PROTO, sizeof(RIST_PROTO) - 1);
  338. }
  339. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  340. {
  341. struct ffmpeg_source *s = data;
  342. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  343. const char *input;
  344. const char *input_format;
  345. const char *ffmpeg_options;
  346. bfree(s->input);
  347. bfree(s->input_format);
  348. bfree(s->ffmpeg_options);
  349. if (is_local_file) {
  350. input = obs_data_get_string(settings, "local_file");
  351. input_format = NULL;
  352. s->is_looping = obs_data_get_bool(settings, "looping");
  353. } else {
  354. input = obs_data_get_string(settings, "input");
  355. input_format = obs_data_get_string(settings, "input_format");
  356. if (requires_mpegts(input)) {
  357. input_format = "mpegts";
  358. obs_data_set_string(settings, "input_format", "mpegts");
  359. }
  360. s->reconnect_delay_sec =
  361. (int)obs_data_get_int(settings, "reconnect_delay_sec");
  362. s->reconnect_delay_sec = s->reconnect_delay_sec == 0
  363. ? 10
  364. : s->reconnect_delay_sec;
  365. s->is_looping = false;
  366. if (s->reconnect_thread_valid) {
  367. s->stop_reconnect = true;
  368. pthread_join(s->reconnect_thread, NULL);
  369. s->stop_reconnect = false;
  370. }
  371. }
  372. ffmpeg_options = obs_data_get_string(settings, "ffmpeg_options");
  373. s->close_when_inactive =
  374. obs_data_get_bool(settings, "close_when_inactive");
  375. s->input = input ? bstrdup(input) : NULL;
  376. s->input_format = input_format ? bstrdup(input_format) : NULL;
  377. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  378. s->is_clear_on_media_end =
  379. obs_data_get_bool(settings, "clear_on_media_end");
  380. s->restart_on_activate =
  381. !astrcmpi_n(input, RIST_PROTO, sizeof(RIST_PROTO) - 1)
  382. ? false
  383. : obs_data_get_bool(settings, "restart_on_activate");
  384. s->range = (enum video_range_type)obs_data_get_int(settings,
  385. "color_range");
  386. s->is_linear_alpha = obs_data_get_bool(settings, "linear_alpha");
  387. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  388. s->speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  389. s->is_local_file = is_local_file;
  390. s->seekable = obs_data_get_bool(settings, "seekable");
  391. s->ffmpeg_options = ffmpeg_options ? bstrdup(ffmpeg_options) : NULL;
  392. if (s->speed_percent < 1 || s->speed_percent > 200)
  393. s->speed_percent = 100;
  394. if (s->media_valid) {
  395. mp_media_free(&s->media);
  396. s->media_valid = false;
  397. }
  398. bool active = obs_source_active(s->source);
  399. if (!s->close_when_inactive || active)
  400. ffmpeg_source_open(s);
  401. dump_source_info(s, input, input_format);
  402. if (!s->restart_on_activate || active)
  403. ffmpeg_source_start(s);
  404. }
  405. static const char *ffmpeg_source_getname(void *unused)
  406. {
  407. UNUSED_PARAMETER(unused);
  408. return obs_module_text("FFMpegSource");
  409. }
  410. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  411. bool pressed)
  412. {
  413. UNUSED_PARAMETER(id);
  414. UNUSED_PARAMETER(hotkey);
  415. if (!pressed)
  416. return;
  417. struct ffmpeg_source *s = data;
  418. if (obs_source_showing(s->source))
  419. obs_source_media_restart(s->source);
  420. }
  421. static void restart_proc(void *data, calldata_t *cd)
  422. {
  423. restart_hotkey(data, 0, NULL, true);
  424. UNUSED_PARAMETER(cd);
  425. }
  426. static void get_duration(void *data, calldata_t *cd)
  427. {
  428. struct ffmpeg_source *s = data;
  429. int64_t dur = 0;
  430. if (s->media.fmt)
  431. dur = s->media.fmt->duration;
  432. calldata_set_int(cd, "duration", dur * 1000);
  433. }
  434. static void get_nb_frames(void *data, calldata_t *cd)
  435. {
  436. struct ffmpeg_source *s = data;
  437. int64_t frames = 0;
  438. if (!s->media.fmt) {
  439. calldata_set_int(cd, "num_frames", frames);
  440. return;
  441. }
  442. int video_stream_index = av_find_best_stream(
  443. s->media.fmt, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  444. if (video_stream_index < 0) {
  445. FF_BLOG(LOG_WARNING, "Getting number of frames failed: No "
  446. "video stream in media file!");
  447. calldata_set_int(cd, "num_frames", frames);
  448. return;
  449. }
  450. AVStream *stream = s->media.fmt->streams[video_stream_index];
  451. if (stream->nb_frames > 0) {
  452. frames = stream->nb_frames;
  453. } else {
  454. FF_BLOG(LOG_DEBUG, "nb_frames not set, estimating using frame "
  455. "rate and duration");
  456. AVRational avg_frame_rate = stream->avg_frame_rate;
  457. frames = (int64_t)ceil((double)s->media.fmt->duration /
  458. (double)AV_TIME_BASE *
  459. (double)avg_frame_rate.num /
  460. (double)avg_frame_rate.den);
  461. }
  462. calldata_set_int(cd, "num_frames", frames);
  463. }
  464. static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
  465. obs_hotkey_t *hotkey, bool pressed)
  466. {
  467. UNUSED_PARAMETER(id);
  468. UNUSED_PARAMETER(hotkey);
  469. if (!pressed)
  470. return false;
  471. struct ffmpeg_source *s = data;
  472. if (s->state == OBS_MEDIA_STATE_PLAYING ||
  473. !obs_source_showing(s->source))
  474. return false;
  475. obs_source_media_play_pause(s->source, false);
  476. return true;
  477. }
  478. static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
  479. obs_hotkey_t *hotkey, bool pressed)
  480. {
  481. UNUSED_PARAMETER(id);
  482. UNUSED_PARAMETER(hotkey);
  483. if (!pressed)
  484. return false;
  485. struct ffmpeg_source *s = data;
  486. if (s->state != OBS_MEDIA_STATE_PLAYING ||
  487. !obs_source_showing(s->source))
  488. return false;
  489. obs_source_media_play_pause(s->source, true);
  490. return true;
  491. }
  492. static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
  493. obs_hotkey_t *hotkey, bool pressed)
  494. {
  495. UNUSED_PARAMETER(id);
  496. UNUSED_PARAMETER(hotkey);
  497. if (!pressed)
  498. return;
  499. struct ffmpeg_source *s = data;
  500. if (obs_source_showing(s->source))
  501. obs_source_media_stop(s->source);
  502. }
  503. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  504. {
  505. UNUSED_PARAMETER(settings);
  506. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  507. s->source = source;
  508. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  509. obs_module_text("RestartMedia"),
  510. restart_hotkey, s);
  511. s->play_pause_hotkey = obs_hotkey_pair_register_source(
  512. s->source, "MediaSource.Play", obs_module_text("Play"),
  513. "MediaSource.Pause", obs_module_text("Pause"),
  514. ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
  515. s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
  516. obs_module_text("Stop"),
  517. ffmpeg_source_stop_hotkey,
  518. s);
  519. proc_handler_t *ph = obs_source_get_proc_handler(source);
  520. proc_handler_add(ph, "void restart()", restart_proc, s);
  521. proc_handler_add(ph, "void get_duration(out int duration)",
  522. get_duration, s);
  523. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  524. get_nb_frames, s);
  525. ffmpeg_source_update(s, settings);
  526. return s;
  527. }
  528. static void ffmpeg_source_destroy(void *data)
  529. {
  530. struct ffmpeg_source *s = data;
  531. if (s->hotkey)
  532. obs_hotkey_unregister(s->hotkey);
  533. if (!s->is_local_file) {
  534. s->stop_reconnect = true;
  535. if (s->reconnect_thread_valid)
  536. pthread_join(s->reconnect_thread, NULL);
  537. }
  538. if (s->media_valid)
  539. mp_media_free(&s->media);
  540. if (s->sws_ctx != NULL)
  541. sws_freeContext(s->sws_ctx);
  542. bfree(s->sws_data);
  543. bfree(s->input);
  544. bfree(s->input_format);
  545. bfree(s->ffmpeg_options);
  546. bfree(s);
  547. }
  548. static void ffmpeg_source_activate(void *data)
  549. {
  550. struct ffmpeg_source *s = data;
  551. if (s->restart_on_activate)
  552. obs_source_media_restart(s->source);
  553. }
  554. static void ffmpeg_source_deactivate(void *data)
  555. {
  556. struct ffmpeg_source *s = data;
  557. if (s->restart_on_activate) {
  558. if (s->media_valid) {
  559. mp_media_stop(&s->media);
  560. if (s->is_clear_on_media_end)
  561. obs_source_output_video(s->source, NULL);
  562. }
  563. }
  564. }
  565. static void ffmpeg_source_play_pause(void *data, bool pause)
  566. {
  567. struct ffmpeg_source *s = data;
  568. if (!s->media_valid)
  569. ffmpeg_source_open(s);
  570. if (!s->media_valid)
  571. return;
  572. mp_media_play_pause(&s->media, pause);
  573. if (pause) {
  574. set_media_state(s, OBS_MEDIA_STATE_PAUSED);
  575. } else {
  576. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  577. obs_source_media_started(s->source);
  578. }
  579. }
  580. static void ffmpeg_source_stop(void *data)
  581. {
  582. struct ffmpeg_source *s = data;
  583. if (s->media_valid) {
  584. mp_media_stop(&s->media);
  585. obs_source_output_video(s->source, NULL);
  586. set_media_state(s, OBS_MEDIA_STATE_STOPPED);
  587. }
  588. }
  589. static void ffmpeg_source_restart(void *data)
  590. {
  591. struct ffmpeg_source *s = data;
  592. if (obs_source_showing(s->source))
  593. ffmpeg_source_start(s);
  594. set_media_state(s, OBS_MEDIA_STATE_PLAYING);
  595. }
  596. static int64_t ffmpeg_source_get_duration(void *data)
  597. {
  598. struct ffmpeg_source *s = data;
  599. int64_t dur = 0;
  600. if (s->media.fmt)
  601. dur = s->media.fmt->duration / INT64_C(1000);
  602. return dur;
  603. }
  604. static int64_t ffmpeg_source_get_time(void *data)
  605. {
  606. struct ffmpeg_source *s = data;
  607. return mp_get_current_time(&s->media);
  608. }
  609. static void ffmpeg_source_set_time(void *data, int64_t ms)
  610. {
  611. struct ffmpeg_source *s = data;
  612. if (!s->media_valid)
  613. return;
  614. mp_media_seek_to(&s->media, ms);
  615. }
  616. static enum obs_media_state ffmpeg_source_get_state(void *data)
  617. {
  618. struct ffmpeg_source *s = data;
  619. return s->state;
  620. }
  621. static void missing_file_callback(void *src, const char *new_path, void *data)
  622. {
  623. struct ffmpeg_source *s = src;
  624. obs_source_t *source = s->source;
  625. obs_data_t *settings = obs_source_get_settings(source);
  626. obs_data_set_string(settings, "local_file", new_path);
  627. obs_source_update(source, settings);
  628. obs_data_release(settings);
  629. UNUSED_PARAMETER(data);
  630. }
  631. static obs_missing_files_t *ffmpeg_source_missingfiles(void *data)
  632. {
  633. struct ffmpeg_source *s = data;
  634. obs_missing_files_t *files = obs_missing_files_create();
  635. if (s->is_local_file && strcmp(s->input, "") != 0) {
  636. if (!os_file_exists(s->input)) {
  637. obs_missing_file_t *file = obs_missing_file_create(
  638. s->input, missing_file_callback,
  639. OBS_MISSING_FILE_SOURCE, s->source, NULL);
  640. obs_missing_files_add_file(files, file);
  641. }
  642. }
  643. return files;
  644. }
  645. struct obs_source_info ffmpeg_source = {
  646. .id = "ffmpeg_source",
  647. .type = OBS_SOURCE_TYPE_INPUT,
  648. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  649. OBS_SOURCE_DO_NOT_DUPLICATE |
  650. OBS_SOURCE_CONTROLLABLE_MEDIA,
  651. .get_name = ffmpeg_source_getname,
  652. .create = ffmpeg_source_create,
  653. .destroy = ffmpeg_source_destroy,
  654. .get_defaults = ffmpeg_source_defaults,
  655. .get_properties = ffmpeg_source_getproperties,
  656. .activate = ffmpeg_source_activate,
  657. .deactivate = ffmpeg_source_deactivate,
  658. .video_tick = ffmpeg_source_tick,
  659. .missing_files = ffmpeg_source_missingfiles,
  660. .update = ffmpeg_source_update,
  661. .icon_type = OBS_ICON_TYPE_MEDIA,
  662. .media_play_pause = ffmpeg_source_play_pause,
  663. .media_restart = ffmpeg_source_restart,
  664. .media_stop = ffmpeg_source_stop,
  665. .media_get_duration = ffmpeg_source_get_duration,
  666. .media_get_time = ffmpeg_source_get_time,
  667. .media_set_time = ffmpeg_source_set_time,
  668. .media_get_state = ffmpeg_source_get_state,
  669. };