obs-ffmpeg-source.c 11 KB

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