obs-ffmpeg-source.c 14 KB

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