1
0

obs-ffmpeg-source.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. };
  54. static bool is_local_file_modified(obs_properties_t *props,
  55. obs_property_t *prop, obs_data_t *settings)
  56. {
  57. UNUSED_PARAMETER(prop);
  58. bool enabled = obs_data_get_bool(settings, "is_local_file");
  59. obs_property_t *input = obs_properties_get(props, "input");
  60. obs_property_t *input_format =
  61. obs_properties_get(props, "input_format");
  62. obs_property_t *local_file = obs_properties_get(props, "local_file");
  63. obs_property_t *looping = obs_properties_get(props, "looping");
  64. obs_property_t *buffering = obs_properties_get(props, "buffering_mb");
  65. obs_property_t *close =
  66. obs_properties_get(props, "close_when_inactive");
  67. obs_property_t *seekable = obs_properties_get(props, "seekable");
  68. obs_property_t *speed = obs_properties_get(props, "speed_percent");
  69. obs_property_set_visible(input, !enabled);
  70. obs_property_set_visible(input_format, !enabled);
  71. obs_property_set_visible(buffering, !enabled);
  72. obs_property_set_visible(close, enabled);
  73. obs_property_set_visible(local_file, enabled);
  74. obs_property_set_visible(looping, enabled);
  75. obs_property_set_visible(speed, enabled);
  76. obs_property_set_visible(seekable, !enabled);
  77. return true;
  78. }
  79. static void ffmpeg_source_defaults(obs_data_t *settings)
  80. {
  81. obs_data_set_default_bool(settings, "is_local_file", true);
  82. obs_data_set_default_bool(settings, "looping", false);
  83. obs_data_set_default_bool(settings, "clear_on_media_end", true);
  84. obs_data_set_default_bool(settings, "restart_on_activate", true);
  85. #if defined(_WIN32)
  86. obs_data_set_default_bool(settings, "hw_decode", true);
  87. #endif
  88. obs_data_set_default_int(settings, "buffering_mb", 2);
  89. obs_data_set_default_int(settings, "speed_percent", 100);
  90. }
  91. static const char *media_filter =
  92. " (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
  93. static const char *video_filter =
  94. " (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.gif *.webm);;";
  95. static const char *audio_filter = " (*.mp3 *.aac *.ogg *.wav);;";
  96. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  97. {
  98. struct ffmpeg_source *s = data;
  99. struct dstr filter = {0};
  100. struct dstr path = {0};
  101. UNUSED_PARAMETER(data);
  102. obs_properties_t *props = obs_properties_create();
  103. obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
  104. obs_property_t *prop;
  105. // use this when obs allows non-readonly paths
  106. prop = obs_properties_add_bool(props, "is_local_file",
  107. obs_module_text("LocalFile"));
  108. obs_property_set_modified_callback(prop, is_local_file_modified);
  109. dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
  110. dstr_cat(&filter, media_filter);
  111. dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
  112. dstr_cat(&filter, video_filter);
  113. dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
  114. dstr_cat(&filter, audio_filter);
  115. dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
  116. dstr_cat(&filter, " (*.*)");
  117. if (s && s->input && *s->input) {
  118. const char *slash;
  119. dstr_copy(&path, s->input);
  120. dstr_replace(&path, "\\", "/");
  121. slash = strrchr(path.array, '/');
  122. if (slash)
  123. dstr_resize(&path, slash - path.array + 1);
  124. }
  125. obs_properties_add_path(props, "local_file",
  126. obs_module_text("LocalFile"), OBS_PATH_FILE,
  127. filter.array, path.array);
  128. dstr_free(&filter);
  129. dstr_free(&path);
  130. prop = obs_properties_add_bool(props, "looping",
  131. obs_module_text("Looping"));
  132. obs_properties_add_bool(props, "restart_on_activate",
  133. obs_module_text("RestartWhenActivated"));
  134. prop = obs_properties_add_int_slider(props, "buffering_mb",
  135. obs_module_text("BufferingMB"), 1,
  136. 16, 1);
  137. obs_property_int_set_suffix(prop, " MB");
  138. obs_properties_add_text(props, "input", obs_module_text("Input"),
  139. OBS_TEXT_DEFAULT);
  140. obs_properties_add_text(props, "input_format",
  141. obs_module_text("InputFormat"),
  142. OBS_TEXT_DEFAULT);
  143. #ifndef __APPLE__
  144. obs_properties_add_bool(props, "hw_decode",
  145. obs_module_text("HardwareDecode"));
  146. #endif
  147. obs_properties_add_bool(props, "clear_on_media_end",
  148. obs_module_text("ClearOnMediaEnd"));
  149. prop = obs_properties_add_bool(
  150. props, "close_when_inactive",
  151. obs_module_text("CloseFileWhenInactive"));
  152. obs_property_set_long_description(
  153. prop, obs_module_text("CloseFileWhenInactive.ToolTip"));
  154. prop = obs_properties_add_int_slider(props, "speed_percent",
  155. obs_module_text("SpeedPercentage"),
  156. 1, 200, 1);
  157. obs_property_int_set_suffix(prop, "%");
  158. prop = obs_properties_add_list(props, "color_range",
  159. obs_module_text("ColorRange"),
  160. OBS_COMBO_TYPE_LIST,
  161. OBS_COMBO_FORMAT_INT);
  162. obs_property_list_add_int(prop, obs_module_text("ColorRange.Auto"),
  163. VIDEO_RANGE_DEFAULT);
  164. obs_property_list_add_int(prop, obs_module_text("ColorRange.Partial"),
  165. VIDEO_RANGE_PARTIAL);
  166. obs_property_list_add_int(prop, obs_module_text("ColorRange.Full"),
  167. VIDEO_RANGE_FULL);
  168. obs_properties_add_bool(props, "seekable", obs_module_text("Seekable"));
  169. return props;
  170. }
  171. static void dump_source_info(struct ffmpeg_source *s, const char *input,
  172. const char *input_format)
  173. {
  174. FF_BLOG(LOG_INFO,
  175. "settings:\n"
  176. "\tinput: %s\n"
  177. "\tinput_format: %s\n"
  178. "\tspeed: %d\n"
  179. "\tis_looping: %s\n"
  180. "\tis_hw_decoding: %s\n"
  181. "\tis_clear_on_media_end: %s\n"
  182. "\trestart_on_activate: %s\n"
  183. "\tclose_when_inactive: %s",
  184. input ? input : "(null)",
  185. input_format ? input_format : "(null)", s->speed_percent,
  186. s->is_looping ? "yes" : "no", s->is_hw_decoding ? "yes" : "no",
  187. s->is_clear_on_media_end ? "yes" : "no",
  188. s->restart_on_activate ? "yes" : "no",
  189. s->close_when_inactive ? "yes" : "no");
  190. }
  191. static void get_frame(void *opaque, struct obs_source_frame *f)
  192. {
  193. struct ffmpeg_source *s = opaque;
  194. obs_source_output_video(s->source, f);
  195. }
  196. static void preload_frame(void *opaque, struct obs_source_frame *f)
  197. {
  198. struct ffmpeg_source *s = opaque;
  199. if (s->close_when_inactive)
  200. return;
  201. if (s->is_clear_on_media_end || s->is_looping)
  202. obs_source_preload_video(s->source, f);
  203. }
  204. static void get_audio(void *opaque, struct obs_source_audio *a)
  205. {
  206. struct ffmpeg_source *s = opaque;
  207. obs_source_output_audio(s->source, a);
  208. }
  209. static void media_stopped(void *opaque)
  210. {
  211. struct ffmpeg_source *s = opaque;
  212. if (s->is_clear_on_media_end) {
  213. obs_source_output_video(s->source, NULL);
  214. if (s->close_when_inactive && s->media_valid)
  215. s->destroy_media = true;
  216. }
  217. }
  218. static void ffmpeg_source_open(struct ffmpeg_source *s)
  219. {
  220. if (s->input && *s->input) {
  221. struct mp_media_info info = {
  222. .opaque = s,
  223. .v_cb = get_frame,
  224. .v_preload_cb = preload_frame,
  225. .a_cb = get_audio,
  226. .stop_cb = media_stopped,
  227. .path = s->input,
  228. .format = s->input_format,
  229. .buffering = s->buffering_mb * 1024 * 1024,
  230. .speed = s->speed_percent,
  231. .force_range = s->range,
  232. .hardware_decoding = s->is_hw_decoding,
  233. .is_local_file = s->is_local_file || s->seekable};
  234. s->media_valid = mp_media_init(&s->media, &info);
  235. }
  236. }
  237. static void ffmpeg_source_tick(void *data, float seconds)
  238. {
  239. UNUSED_PARAMETER(seconds);
  240. struct ffmpeg_source *s = data;
  241. if (s->destroy_media) {
  242. if (s->media_valid) {
  243. mp_media_free(&s->media);
  244. s->media_valid = false;
  245. }
  246. s->destroy_media = false;
  247. }
  248. }
  249. static void ffmpeg_source_start(struct ffmpeg_source *s)
  250. {
  251. if (!s->media_valid)
  252. ffmpeg_source_open(s);
  253. if (s->media_valid) {
  254. mp_media_play(&s->media, s->is_looping);
  255. if (s->is_local_file)
  256. obs_source_show_preloaded_video(s->source);
  257. }
  258. }
  259. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  260. {
  261. struct ffmpeg_source *s = data;
  262. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  263. char *input;
  264. char *input_format;
  265. bfree(s->input);
  266. bfree(s->input_format);
  267. if (is_local_file) {
  268. input = (char *)obs_data_get_string(settings, "local_file");
  269. input_format = NULL;
  270. s->is_looping = obs_data_get_bool(settings, "looping");
  271. s->close_when_inactive =
  272. obs_data_get_bool(settings, "close_when_inactive");
  273. obs_source_set_async_unbuffered(s->source, true);
  274. } else {
  275. input = (char *)obs_data_get_string(settings, "input");
  276. input_format =
  277. (char *)obs_data_get_string(settings, "input_format");
  278. s->is_looping = false;
  279. s->close_when_inactive = true;
  280. obs_source_set_async_unbuffered(s->source, false);
  281. }
  282. s->input = input ? bstrdup(input) : NULL;
  283. s->input_format = input_format ? bstrdup(input_format) : NULL;
  284. #ifndef __APPLE__
  285. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  286. #endif
  287. s->is_clear_on_media_end =
  288. obs_data_get_bool(settings, "clear_on_media_end");
  289. s->restart_on_activate =
  290. obs_data_get_bool(settings, "restart_on_activate");
  291. s->range = (enum video_range_type)obs_data_get_int(settings,
  292. "color_range");
  293. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  294. s->speed_percent = (int)obs_data_get_int(settings, "speed_percent");
  295. s->is_local_file = is_local_file;
  296. s->seekable = obs_data_get_bool(settings, "seekable");
  297. if (s->speed_percent < 1 || s->speed_percent > 200)
  298. s->speed_percent = 100;
  299. if (s->media_valid) {
  300. mp_media_free(&s->media);
  301. s->media_valid = false;
  302. }
  303. bool active = obs_source_active(s->source);
  304. if (!s->close_when_inactive || active)
  305. ffmpeg_source_open(s);
  306. dump_source_info(s, input, input_format);
  307. if (!s->restart_on_activate || active)
  308. ffmpeg_source_start(s);
  309. }
  310. static const char *ffmpeg_source_getname(void *unused)
  311. {
  312. UNUSED_PARAMETER(unused);
  313. return obs_module_text("FFMpegSource");
  314. }
  315. static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
  316. bool pressed)
  317. {
  318. UNUSED_PARAMETER(id);
  319. UNUSED_PARAMETER(hotkey);
  320. UNUSED_PARAMETER(pressed);
  321. struct ffmpeg_source *s = data;
  322. if (obs_source_active(s->source))
  323. ffmpeg_source_start(s);
  324. }
  325. static void restart_proc(void *data, calldata_t *cd)
  326. {
  327. restart_hotkey(data, 0, NULL, true);
  328. UNUSED_PARAMETER(cd);
  329. }
  330. static void get_duration(void *data, calldata_t *cd)
  331. {
  332. struct ffmpeg_source *s = data;
  333. int64_t dur = 0;
  334. if (s->media.fmt)
  335. dur = s->media.fmt->duration;
  336. calldata_set_int(cd, "duration", dur * 1000);
  337. }
  338. static void get_nb_frames(void *data, calldata_t *cd)
  339. {
  340. struct ffmpeg_source *s = data;
  341. int64_t frames = 0;
  342. if (!s->media.fmt) {
  343. calldata_set_int(cd, "num_frames", frames);
  344. return;
  345. }
  346. int video_stream_index = av_find_best_stream(
  347. s->media.fmt, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
  348. if (video_stream_index < 0) {
  349. FF_BLOG(LOG_WARNING, "Getting number of frames failed: No "
  350. "video stream in media file!");
  351. calldata_set_int(cd, "num_frames", frames);
  352. return;
  353. }
  354. AVStream *stream = s->media.fmt->streams[video_stream_index];
  355. if (stream->nb_frames > 0) {
  356. frames = stream->nb_frames;
  357. } else {
  358. FF_BLOG(LOG_DEBUG, "nb_frames not set, estimating using frame "
  359. "rate and duration");
  360. AVRational avg_frame_rate = stream->avg_frame_rate;
  361. frames = (int64_t)ceil((double)s->media.fmt->duration /
  362. (double)AV_TIME_BASE *
  363. (double)avg_frame_rate.num /
  364. (double)avg_frame_rate.den);
  365. }
  366. calldata_set_int(cd, "num_frames", frames);
  367. }
  368. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  369. {
  370. UNUSED_PARAMETER(settings);
  371. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  372. s->source = source;
  373. s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
  374. obs_module_text("RestartMedia"),
  375. restart_hotkey, s);
  376. proc_handler_t *ph = obs_source_get_proc_handler(source);
  377. proc_handler_add(ph, "void restart()", restart_proc, s);
  378. proc_handler_add(ph, "void get_duration(out int duration)",
  379. get_duration, s);
  380. proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
  381. get_nb_frames, s);
  382. ffmpeg_source_update(s, settings);
  383. return s;
  384. }
  385. static void ffmpeg_source_destroy(void *data)
  386. {
  387. struct ffmpeg_source *s = data;
  388. if (s->hotkey)
  389. obs_hotkey_unregister(s->hotkey);
  390. if (s->media_valid)
  391. mp_media_free(&s->media);
  392. if (s->sws_ctx != NULL)
  393. sws_freeContext(s->sws_ctx);
  394. bfree(s->sws_data);
  395. bfree(s->input);
  396. bfree(s->input_format);
  397. bfree(s);
  398. }
  399. static void ffmpeg_source_activate(void *data)
  400. {
  401. struct ffmpeg_source *s = data;
  402. if (s->restart_on_activate)
  403. ffmpeg_source_start(s);
  404. }
  405. static void ffmpeg_source_deactivate(void *data)
  406. {
  407. struct ffmpeg_source *s = data;
  408. if (s->restart_on_activate) {
  409. if (s->media_valid) {
  410. mp_media_stop(&s->media);
  411. if (s->is_clear_on_media_end)
  412. obs_source_output_video(s->source, NULL);
  413. }
  414. }
  415. }
  416. struct obs_source_info ffmpeg_source = {
  417. .id = "ffmpeg_source",
  418. .type = OBS_SOURCE_TYPE_INPUT,
  419. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  420. OBS_SOURCE_DO_NOT_DUPLICATE,
  421. .get_name = ffmpeg_source_getname,
  422. .create = ffmpeg_source_create,
  423. .destroy = ffmpeg_source_destroy,
  424. .get_defaults = ffmpeg_source_defaults,
  425. .get_properties = ffmpeg_source_getproperties,
  426. .activate = ffmpeg_source_activate,
  427. .deactivate = ffmpeg_source_deactivate,
  428. .video_tick = ffmpeg_source_tick,
  429. .update = ffmpeg_source_update,
  430. };