obs-ffmpeg-source.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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. };
  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 =obs_properties_get(props,
  61. "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 = obs_properties_get(props, "close_when_inactive");
  66. obs_property_set_visible(input, !enabled);
  67. obs_property_set_visible(input_format, !enabled);
  68. obs_property_set_visible(buffering, !enabled);
  69. obs_property_set_visible(close, enabled);
  70. obs_property_set_visible(local_file, enabled);
  71. obs_property_set_visible(looping, enabled);
  72. return true;
  73. }
  74. static void ffmpeg_source_defaults(obs_data_t *settings)
  75. {
  76. obs_data_set_default_bool(settings, "is_local_file", true);
  77. obs_data_set_default_bool(settings, "looping", false);
  78. obs_data_set_default_bool(settings, "clear_on_media_end", true);
  79. obs_data_set_default_bool(settings, "restart_on_activate", true);
  80. #if defined(_WIN32)
  81. obs_data_set_default_bool(settings, "hw_decode", true);
  82. #endif
  83. obs_data_set_default_int(settings, "buffering_mb", 2);
  84. }
  85. static const char *media_filter =
  86. " (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
  87. static const char *video_filter =
  88. " (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.gif *.webm);;";
  89. static const char *audio_filter =
  90. " (*.mp3 *.aac *.ogg *.wav);;";
  91. static obs_properties_t *ffmpeg_source_getproperties(void *data)
  92. {
  93. struct ffmpeg_source *s = data;
  94. struct dstr filter = {0};
  95. struct dstr path = {0};
  96. UNUSED_PARAMETER(data);
  97. obs_properties_t *props = obs_properties_create();
  98. obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
  99. obs_property_t *prop;
  100. // use this when obs allows non-readonly paths
  101. prop = obs_properties_add_bool(props, "is_local_file",
  102. obs_module_text("LocalFile"));
  103. obs_property_set_modified_callback(prop, is_local_file_modified);
  104. dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
  105. dstr_cat(&filter, media_filter);
  106. dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
  107. dstr_cat(&filter, video_filter);
  108. dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
  109. dstr_cat(&filter, audio_filter);
  110. dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
  111. dstr_cat(&filter, " (*.*)");
  112. if (s && s->input && *s->input) {
  113. const char *slash;
  114. dstr_copy(&path, s->input);
  115. dstr_replace(&path, "\\", "/");
  116. slash = strrchr(path.array, '/');
  117. if (slash)
  118. dstr_resize(&path, slash - path.array + 1);
  119. }
  120. obs_properties_add_path(props, "local_file",
  121. obs_module_text("LocalFile"), OBS_PATH_FILE,
  122. filter.array, path.array);
  123. dstr_free(&filter);
  124. dstr_free(&path);
  125. prop = obs_properties_add_bool(props, "looping",
  126. obs_module_text("Looping"));
  127. obs_properties_add_bool(props, "restart_on_activate",
  128. obs_module_text("RestartWhenActivated"));
  129. obs_properties_add_text(props, "input",
  130. obs_module_text("Input"), OBS_TEXT_DEFAULT);
  131. obs_properties_add_text(props, "input_format",
  132. obs_module_text("InputFormat"), OBS_TEXT_DEFAULT);
  133. #ifndef __APPLE__
  134. obs_properties_add_bool(props, "hw_decode",
  135. obs_module_text("HardwareDecode"));
  136. #endif
  137. obs_properties_add_bool(props, "clear_on_media_end",
  138. obs_module_text("ClearOnMediaEnd"));
  139. prop = obs_properties_add_bool(props, "close_when_inactive",
  140. obs_module_text("CloseFileWhenInactive"));
  141. obs_property_set_long_description(prop,
  142. obs_module_text("CloseFileWhenInactive.ToolTip"));
  143. prop = obs_properties_add_list(props, "color_range",
  144. obs_module_text("ColorRange"), OBS_COMBO_TYPE_LIST,
  145. OBS_COMBO_FORMAT_INT);
  146. obs_property_list_add_int(prop, obs_module_text("ColorRange.Auto"),
  147. VIDEO_RANGE_DEFAULT);
  148. obs_property_list_add_int(prop, obs_module_text("ColorRange.Partial"),
  149. VIDEO_RANGE_PARTIAL);
  150. obs_property_list_add_int(prop, obs_module_text("ColorRange.Full"),
  151. VIDEO_RANGE_FULL);
  152. return props;
  153. }
  154. static void dump_source_info(struct ffmpeg_source *s, const char *input,
  155. const char *input_format)
  156. {
  157. FF_BLOG(LOG_INFO,
  158. "settings:\n"
  159. "\tinput: %s\n"
  160. "\tinput_format: %s\n"
  161. "\tis_looping: %s\n"
  162. "\tis_hw_decoding: %s\n"
  163. "\tis_clear_on_media_end: %s\n"
  164. "\trestart_on_activate: %s\n"
  165. "\tclose_when_inactive: %s",
  166. input ? input : "(null)",
  167. input_format ? input_format : "(null)",
  168. s->is_looping ? "yes" : "no",
  169. s->is_hw_decoding ? "yes" : "no",
  170. s->is_clear_on_media_end ? "yes" : "no",
  171. s->restart_on_activate ? "yes" : "no",
  172. s->close_when_inactive ? "yes" : "no");
  173. }
  174. static void get_frame(void *opaque, struct obs_source_frame *f)
  175. {
  176. struct ffmpeg_source *s = opaque;
  177. obs_source_output_video(s->source, f);
  178. }
  179. static void preload_frame(void *opaque, struct obs_source_frame *f)
  180. {
  181. struct ffmpeg_source *s = opaque;
  182. if (s->close_when_inactive)
  183. return;
  184. if (s->is_clear_on_media_end || s->is_looping)
  185. obs_source_preload_video(s->source, f);
  186. }
  187. static void get_audio(void *opaque, struct obs_source_audio *a)
  188. {
  189. struct ffmpeg_source *s = opaque;
  190. obs_source_output_audio(s->source, a);
  191. }
  192. static void media_stopped(void *opaque)
  193. {
  194. struct ffmpeg_source *s = opaque;
  195. if (s->is_clear_on_media_end) {
  196. obs_source_output_video(s->source, NULL);
  197. if (s->close_when_inactive)
  198. s->destroy_media = true;
  199. }
  200. }
  201. static void ffmpeg_source_open(struct ffmpeg_source *s)
  202. {
  203. if (s->input && *s->input)
  204. s->media_valid = mp_media_init(&s->media,
  205. s->input, s->input_format,
  206. s->buffering_mb * 1024 * 1024,
  207. s, get_frame, get_audio, media_stopped,
  208. preload_frame, s->is_hw_decoding, s->range);
  209. }
  210. static void ffmpeg_source_tick(void *data, float seconds)
  211. {
  212. UNUSED_PARAMETER(seconds);
  213. struct ffmpeg_source *s = data;
  214. if (s->destroy_media) {
  215. if (s->media_valid) {
  216. mp_media_free(&s->media);
  217. s->media_valid = false;
  218. }
  219. s->destroy_media = false;
  220. }
  221. }
  222. static void ffmpeg_source_start(struct ffmpeg_source *s)
  223. {
  224. if (!s->media_valid)
  225. ffmpeg_source_open(s);
  226. if (s->media_valid) {
  227. mp_media_play(&s->media, s->is_looping);
  228. if (s->is_local_file)
  229. obs_source_show_preloaded_video(s->source);
  230. }
  231. }
  232. static void ffmpeg_source_update(void *data, obs_data_t *settings)
  233. {
  234. struct ffmpeg_source *s = data;
  235. bool is_local_file = obs_data_get_bool(settings, "is_local_file");
  236. char *input;
  237. char *input_format;
  238. bfree(s->input);
  239. bfree(s->input_format);
  240. if (is_local_file) {
  241. input = (char *)obs_data_get_string(settings, "local_file");
  242. input_format = NULL;
  243. s->is_looping = obs_data_get_bool(settings, "looping");
  244. s->close_when_inactive = obs_data_get_bool(settings,
  245. "close_when_inactive");
  246. obs_source_set_async_unbuffered(s->source, true);
  247. } else {
  248. input = (char *)obs_data_get_string(settings, "input");
  249. input_format = (char *)obs_data_get_string(settings,
  250. "input_format");
  251. s->is_looping = false;
  252. s->close_when_inactive = true;
  253. obs_source_set_async_unbuffered(s->source, false);
  254. }
  255. s->input = input ? bstrdup(input) : NULL;
  256. s->input_format = input_format ? bstrdup(input_format) : NULL;
  257. #ifndef __APPLE__
  258. s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
  259. #endif
  260. s->is_clear_on_media_end = obs_data_get_bool(settings,
  261. "clear_on_media_end");
  262. s->restart_on_activate = obs_data_get_bool(settings,
  263. "restart_on_activate");
  264. s->range = (enum video_range_type)obs_data_get_int(settings,
  265. "color_range");
  266. s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
  267. s->is_local_file = is_local_file;
  268. if (s->media_valid) {
  269. mp_media_free(&s->media);
  270. s->media_valid = false;
  271. }
  272. bool active = obs_source_active(s->source);
  273. if (!s->close_when_inactive || active)
  274. ffmpeg_source_open(s);
  275. dump_source_info(s, input, input_format);
  276. if (!s->restart_on_activate || active)
  277. ffmpeg_source_start(s);
  278. }
  279. static const char *ffmpeg_source_getname(void *unused)
  280. {
  281. UNUSED_PARAMETER(unused);
  282. return obs_module_text("FFMpegSource");
  283. }
  284. static void restart_hotkey(void *data, obs_hotkey_id id,
  285. obs_hotkey_t *hotkey, bool pressed)
  286. {
  287. UNUSED_PARAMETER(id);
  288. UNUSED_PARAMETER(hotkey);
  289. UNUSED_PARAMETER(pressed);
  290. struct ffmpeg_source *s = data;
  291. if (obs_source_active(s->source))
  292. ffmpeg_source_start(s);
  293. }
  294. static void restart_proc(void *data, calldata_t *cd)
  295. {
  296. restart_hotkey(data, 0, NULL, true);
  297. UNUSED_PARAMETER(cd);
  298. }
  299. static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
  300. {
  301. UNUSED_PARAMETER(settings);
  302. struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
  303. s->source = source;
  304. s->hotkey = obs_hotkey_register_source(source,
  305. "MediaSource.Restart",
  306. obs_module_text("RestartMedia"),
  307. restart_hotkey, s);
  308. proc_handler_t *ph = obs_source_get_proc_handler(source);
  309. proc_handler_add(ph, "void restart()", restart_proc, s);
  310. ffmpeg_source_update(s, settings);
  311. return s;
  312. }
  313. static void ffmpeg_source_destroy(void *data)
  314. {
  315. struct ffmpeg_source *s = data;
  316. if (s->hotkey)
  317. obs_hotkey_unregister(s->hotkey);
  318. if (s->media_valid)
  319. mp_media_free(&s->media);
  320. if (s->sws_ctx != NULL)
  321. sws_freeContext(s->sws_ctx);
  322. bfree(s->sws_data);
  323. bfree(s->input);
  324. bfree(s->input_format);
  325. bfree(s);
  326. }
  327. static void ffmpeg_source_activate(void *data)
  328. {
  329. struct ffmpeg_source *s = data;
  330. if (s->restart_on_activate)
  331. ffmpeg_source_start(s);
  332. }
  333. static void ffmpeg_source_deactivate(void *data)
  334. {
  335. struct ffmpeg_source *s = data;
  336. if (s->restart_on_activate) {
  337. if (s->media_valid) {
  338. mp_media_stop(&s->media);
  339. if (s->is_clear_on_media_end)
  340. obs_source_output_video(s->source, NULL);
  341. }
  342. }
  343. }
  344. struct obs_source_info ffmpeg_source = {
  345. .id = "ffmpeg_source",
  346. .type = OBS_SOURCE_TYPE_INPUT,
  347. .output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  348. OBS_SOURCE_DO_NOT_DUPLICATE,
  349. .get_name = ffmpeg_source_getname,
  350. .create = ffmpeg_source_create,
  351. .destroy = ffmpeg_source_destroy,
  352. .get_defaults = ffmpeg_source_defaults,
  353. .get_properties = ffmpeg_source_getproperties,
  354. .activate = ffmpeg_source_activate,
  355. .deactivate = ffmpeg_source_deactivate,
  356. .video_tick = ffmpeg_source_tick,
  357. .update = ffmpeg_source_update
  358. };