2015-03-04 10:45:50 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 John R. Bradley <jrb@turrettech.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <obs-module.h>
|
|
|
|
#include <util/platform.h>
|
2016-01-23 02:28:36 -08:00
|
|
|
#include <util/dstr.h>
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
#include "obs-ffmpeg-compat.h"
|
|
|
|
#include "obs-ffmpeg-formats.h"
|
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
#include <media-playback/media.h>
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2015-08-03 13:40:40 -07:00
|
|
|
#define FF_LOG(level, format, ...) \
|
|
|
|
blog(level, "[Media Source]: " format, ##__VA_ARGS__)
|
2019-06-22 22:13:45 -07:00
|
|
|
#define FF_LOG_S(source, level, format, ...) \
|
2015-08-03 13:40:40 -07:00
|
|
|
blog(level, "[Media Source '%s']: " format, \
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_source_get_name(source), ##__VA_ARGS__)
|
2015-08-03 13:40:40 -07:00
|
|
|
#define FF_BLOG(level, format, ...) \
|
|
|
|
FF_LOG_S(s->source, level, format, ##__VA_ARGS__)
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
struct ffmpeg_source {
|
2017-03-26 06:21:59 -07:00
|
|
|
mp_media_t media;
|
|
|
|
bool media_valid;
|
|
|
|
bool destroy_media;
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
struct SwsContext *sws_ctx;
|
2015-03-22 21:03:28 -07:00
|
|
|
int sws_width;
|
|
|
|
int sws_height;
|
|
|
|
enum AVPixelFormat sws_format;
|
2015-03-10 12:37:28 -07:00
|
|
|
uint8_t *sws_data;
|
|
|
|
int sws_linesize;
|
2017-03-31 12:44:08 -07:00
|
|
|
enum video_range_type range;
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_source_t *source;
|
2017-05-10 19:53:03 -07:00
|
|
|
obs_hotkey_id hotkey;
|
2016-01-23 02:31:45 -08:00
|
|
|
|
|
|
|
char *input;
|
|
|
|
char *input_format;
|
2017-05-29 19:13:18 -07:00
|
|
|
int buffering_mb;
|
2018-02-15 15:13:35 -08:00
|
|
|
int speed_percent;
|
2016-01-23 02:31:45 -08:00
|
|
|
bool is_looping;
|
2017-05-16 01:57:25 -07:00
|
|
|
bool is_local_file;
|
2015-03-04 10:45:50 -08:00
|
|
|
bool is_hw_decoding;
|
2015-03-23 22:21:24 -07:00
|
|
|
bool is_clear_on_media_end;
|
2016-01-23 02:36:57 -08:00
|
|
|
bool restart_on_activate;
|
2017-03-26 06:21:59 -07:00
|
|
|
bool close_when_inactive;
|
2017-09-17 05:50:30 -07:00
|
|
|
bool seekable;
|
2020-01-31 11:03:01 -08:00
|
|
|
|
|
|
|
enum obs_media_state state;
|
2020-01-31 11:12:09 -08:00
|
|
|
obs_hotkey_pair_id play_pause_hotkey;
|
|
|
|
obs_hotkey_id stop_hotkey;
|
2015-03-04 10:45:50 -08:00
|
|
|
};
|
|
|
|
|
2020-01-31 11:12:09 -08:00
|
|
|
static void set_media_state(void *data, enum obs_media_state state)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
s->state = state;
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static bool is_local_file_modified(obs_properties_t *props,
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_property_t *prop, obs_data_t *settings)
|
2015-03-04 10:45:50 -08:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(prop);
|
|
|
|
|
|
|
|
bool enabled = obs_data_get_bool(settings, "is_local_file");
|
|
|
|
obs_property_t *input = obs_properties_get(props, "input");
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_property_t *input_format =
|
|
|
|
obs_properties_get(props, "input_format");
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_property_t *local_file = obs_properties_get(props, "local_file");
|
|
|
|
obs_property_t *looping = obs_properties_get(props, "looping");
|
2017-05-29 19:13:18 -07:00
|
|
|
obs_property_t *buffering = obs_properties_get(props, "buffering_mb");
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_property_t *close =
|
|
|
|
obs_properties_get(props, "close_when_inactive");
|
2017-09-17 05:50:30 -07:00
|
|
|
obs_property_t *seekable = obs_properties_get(props, "seekable");
|
2018-02-15 15:13:35 -08:00
|
|
|
obs_property_t *speed = obs_properties_get(props, "speed_percent");
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_property_set_visible(input, !enabled);
|
|
|
|
obs_property_set_visible(input_format, !enabled);
|
2017-05-29 19:13:18 -07:00
|
|
|
obs_property_set_visible(buffering, !enabled);
|
2017-05-16 01:58:18 -07:00
|
|
|
obs_property_set_visible(close, enabled);
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_property_set_visible(local_file, enabled);
|
|
|
|
obs_property_set_visible(looping, enabled);
|
2018-02-15 15:13:35 -08:00
|
|
|
obs_property_set_visible(speed, enabled);
|
2017-09-17 05:50:30 -07:00
|
|
|
obs_property_set_visible(seekable, !enabled);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:31:45 -08:00
|
|
|
static void ffmpeg_source_defaults(obs_data_t *settings)
|
|
|
|
{
|
|
|
|
obs_data_set_default_bool(settings, "is_local_file", true);
|
|
|
|
obs_data_set_default_bool(settings, "looping", false);
|
|
|
|
obs_data_set_default_bool(settings, "clear_on_media_end", true);
|
2016-01-23 02:36:57 -08:00
|
|
|
obs_data_set_default_bool(settings, "restart_on_activate", true);
|
2017-05-29 19:13:18 -07:00
|
|
|
obs_data_set_default_int(settings, "buffering_mb", 2);
|
2018-02-15 15:13:35 -08:00
|
|
|
obs_data_set_default_int(settings, "speed_percent", 100);
|
2016-01-23 02:31:45 -08:00
|
|
|
}
|
|
|
|
|
2016-01-23 02:28:36 -08:00
|
|
|
static const char *media_filter =
|
|
|
|
" (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.mp3 *.ogg *.aac *.wav *.gif *.webm);;";
|
|
|
|
static const char *video_filter =
|
|
|
|
" (*.mp4 *.ts *.mov *.flv *.mkv *.avi *.gif *.webm);;";
|
2019-06-22 22:13:45 -07:00
|
|
|
static const char *audio_filter = " (*.mp3 *.aac *.ogg *.wav);;";
|
2016-01-23 02:28:36 -08:00
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static obs_properties_t *ffmpeg_source_getproperties(void *data)
|
|
|
|
{
|
2016-03-15 20:56:30 -07:00
|
|
|
struct ffmpeg_source *s = data;
|
2016-01-23 02:28:36 -08:00
|
|
|
struct dstr filter = {0};
|
2016-03-15 20:56:30 -07:00
|
|
|
struct dstr path = {0};
|
2015-03-04 10:45:50 -08:00
|
|
|
UNUSED_PARAMETER(data);
|
|
|
|
|
|
|
|
obs_properties_t *props = obs_properties_create();
|
2015-03-19 14:01:10 -07:00
|
|
|
|
|
|
|
obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_property_t *prop;
|
|
|
|
// use this when obs allows non-readonly paths
|
|
|
|
prop = obs_properties_add_bool(props, "is_local_file",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("LocalFile"));
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
obs_property_set_modified_callback(prop, is_local_file_modified);
|
|
|
|
|
2016-01-23 02:28:36 -08:00
|
|
|
dstr_copy(&filter, obs_module_text("MediaFileFilter.AllMediaFiles"));
|
|
|
|
dstr_cat(&filter, media_filter);
|
|
|
|
dstr_cat(&filter, obs_module_text("MediaFileFilter.VideoFiles"));
|
|
|
|
dstr_cat(&filter, video_filter);
|
|
|
|
dstr_cat(&filter, obs_module_text("MediaFileFilter.AudioFiles"));
|
|
|
|
dstr_cat(&filter, audio_filter);
|
|
|
|
dstr_cat(&filter, obs_module_text("MediaFileFilter.AllFiles"));
|
|
|
|
dstr_cat(&filter, " (*.*)");
|
|
|
|
|
2016-03-15 20:56:30 -07:00
|
|
|
if (s && s->input && *s->input) {
|
|
|
|
const char *slash;
|
|
|
|
|
|
|
|
dstr_copy(&path, s->input);
|
|
|
|
dstr_replace(&path, "\\", "/");
|
|
|
|
slash = strrchr(path.array, '/');
|
|
|
|
if (slash)
|
|
|
|
dstr_resize(&path, slash - path.array + 1);
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_properties_add_path(props, "local_file",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("LocalFile"), OBS_PATH_FILE,
|
|
|
|
filter.array, path.array);
|
2016-01-23 02:28:36 -08:00
|
|
|
dstr_free(&filter);
|
2016-03-15 20:56:30 -07:00
|
|
|
dstr_free(&path);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
prop = obs_properties_add_bool(props, "looping",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("Looping"));
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2016-01-23 02:36:57 -08:00
|
|
|
obs_properties_add_bool(props, "restart_on_activate",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("RestartWhenActivated"));
|
2016-01-23 02:36:57 -08:00
|
|
|
|
2019-07-18 00:38:50 -07:00
|
|
|
prop = obs_properties_add_int_slider(props, "buffering_mb",
|
|
|
|
obs_module_text("BufferingMB"), 1,
|
|
|
|
16, 1);
|
|
|
|
obs_property_int_set_suffix(prop, " MB");
|
2018-12-09 16:50:25 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_properties_add_text(props, "input", obs_module_text("Input"),
|
|
|
|
OBS_TEXT_DEFAULT);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
|
|
|
obs_properties_add_text(props, "input_format",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("InputFormat"),
|
|
|
|
OBS_TEXT_DEFAULT);
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2017-05-18 23:42:01 -07:00
|
|
|
#ifndef __APPLE__
|
2015-03-04 10:45:50 -08:00
|
|
|
obs_properties_add_bool(props, "hw_decode",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("HardwareDecode"));
|
2017-05-18 23:42:01 -07:00
|
|
|
#endif
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2015-03-23 22:21:24 -07:00
|
|
|
obs_properties_add_bool(props, "clear_on_media_end",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("ClearOnMediaEnd"));
|
2015-03-23 22:21:24 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
prop = obs_properties_add_bool(
|
|
|
|
props, "close_when_inactive",
|
|
|
|
obs_module_text("CloseFileWhenInactive"));
|
2015-03-04 10:45:50 -08:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_property_set_long_description(
|
|
|
|
prop, obs_module_text("CloseFileWhenInactive.ToolTip"));
|
2015-03-12 14:17:13 -07:00
|
|
|
|
2019-07-18 00:38:50 -07:00
|
|
|
prop = obs_properties_add_int_slider(props, "speed_percent",
|
|
|
|
obs_module_text("SpeedPercentage"),
|
|
|
|
1, 200, 1);
|
|
|
|
obs_property_int_set_suffix(prop, "%");
|
2018-02-15 15:13:35 -08:00
|
|
|
|
2016-04-03 20:59:10 -07:00
|
|
|
prop = obs_properties_add_list(props, "color_range",
|
2019-06-22 22:13:45 -07:00
|
|
|
obs_module_text("ColorRange"),
|
|
|
|
OBS_COMBO_TYPE_LIST,
|
|
|
|
OBS_COMBO_FORMAT_INT);
|
2016-04-03 20:59:10 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("ColorRange.Auto"),
|
2019-06-22 22:13:45 -07:00
|
|
|
VIDEO_RANGE_DEFAULT);
|
2016-04-03 20:59:10 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("ColorRange.Partial"),
|
2019-06-22 22:13:45 -07:00
|
|
|
VIDEO_RANGE_PARTIAL);
|
2016-04-03 20:59:10 -07:00
|
|
|
obs_property_list_add_int(prop, obs_module_text("ColorRange.Full"),
|
2019-06-22 22:13:45 -07:00
|
|
|
VIDEO_RANGE_FULL);
|
2016-04-03 20:59:10 -07:00
|
|
|
|
2017-09-17 05:50:30 -07:00
|
|
|
obs_properties_add_bool(props, "seekable", obs_module_text("Seekable"));
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
return props;
|
|
|
|
}
|
|
|
|
|
2015-08-03 14:27:34 -07:00
|
|
|
static void dump_source_info(struct ffmpeg_source *s, const char *input,
|
2019-06-22 22:13:45 -07:00
|
|
|
const char *input_format)
|
2015-08-03 14:27:34 -07:00
|
|
|
{
|
|
|
|
FF_BLOG(LOG_INFO,
|
2019-06-22 22:13:45 -07:00
|
|
|
"settings:\n"
|
|
|
|
"\tinput: %s\n"
|
|
|
|
"\tinput_format: %s\n"
|
|
|
|
"\tspeed: %d\n"
|
|
|
|
"\tis_looping: %s\n"
|
|
|
|
"\tis_hw_decoding: %s\n"
|
|
|
|
"\tis_clear_on_media_end: %s\n"
|
|
|
|
"\trestart_on_activate: %s\n"
|
|
|
|
"\tclose_when_inactive: %s",
|
|
|
|
input ? input : "(null)",
|
|
|
|
input_format ? input_format : "(null)", s->speed_percent,
|
|
|
|
s->is_looping ? "yes" : "no", s->is_hw_decoding ? "yes" : "no",
|
|
|
|
s->is_clear_on_media_end ? "yes" : "no",
|
|
|
|
s->restart_on_activate ? "yes" : "no",
|
|
|
|
s->close_when_inactive ? "yes" : "no");
|
2017-03-26 06:21:59 -07:00
|
|
|
}
|
2015-08-03 14:27:34 -07:00
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
static void get_frame(void *opaque, struct obs_source_frame *f)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = opaque;
|
|
|
|
obs_source_output_video(s->source, f);
|
|
|
|
}
|
2015-08-03 14:27:34 -07:00
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
static void preload_frame(void *opaque, struct obs_source_frame *f)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = opaque;
|
2017-06-19 22:58:01 -07:00
|
|
|
if (s->close_when_inactive)
|
|
|
|
return;
|
|
|
|
|
2017-06-14 08:30:44 -07:00
|
|
|
if (s->is_clear_on_media_end || s->is_looping)
|
|
|
|
obs_source_preload_video(s->source, f);
|
2016-01-23 02:31:45 -08:00
|
|
|
}
|
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
static void get_audio(void *opaque, struct obs_source_audio *a)
|
2016-01-23 02:31:45 -08:00
|
|
|
{
|
2017-03-26 06:21:59 -07:00
|
|
|
struct ffmpeg_source *s = opaque;
|
|
|
|
obs_source_output_audio(s->source, a);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void media_stopped(void *opaque)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = opaque;
|
|
|
|
if (s->is_clear_on_media_end) {
|
|
|
|
obs_source_output_video(s->source, NULL);
|
2017-09-06 20:34:06 -07:00
|
|
|
if (s->close_when_inactive && s->media_valid)
|
2017-03-26 06:21:59 -07:00
|
|
|
s->destroy_media = true;
|
2016-01-23 02:31:45 -08:00
|
|
|
}
|
2020-02-06 03:10:57 -08:00
|
|
|
|
|
|
|
set_media_state(s, OBS_MEDIA_STATE_ENDED);
|
|
|
|
obs_source_media_ended(s->source);
|
2017-03-26 06:21:59 -07:00
|
|
|
}
|
2016-01-23 02:31:45 -08:00
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
static void ffmpeg_source_open(struct ffmpeg_source *s)
|
|
|
|
{
|
2018-02-15 13:32:35 -08:00
|
|
|
if (s->input && *s->input) {
|
|
|
|
struct mp_media_info info = {
|
|
|
|
.opaque = s,
|
|
|
|
.v_cb = get_frame,
|
|
|
|
.v_preload_cb = preload_frame,
|
|
|
|
.a_cb = get_audio,
|
|
|
|
.stop_cb = media_stopped,
|
|
|
|
.path = s->input,
|
|
|
|
.format = s->input_format,
|
|
|
|
.buffering = s->buffering_mb * 1024 * 1024,
|
2018-02-15 15:13:35 -08:00
|
|
|
.speed = s->speed_percent,
|
2018-02-15 13:32:35 -08:00
|
|
|
.force_range = s->range,
|
|
|
|
.hardware_decoding = s->is_hw_decoding,
|
2019-06-22 22:13:45 -07:00
|
|
|
.is_local_file = s->is_local_file || s->seekable};
|
2018-02-15 13:32:35 -08:00
|
|
|
|
|
|
|
s->media_valid = mp_media_init(&s->media, &info);
|
|
|
|
}
|
2017-03-26 06:21:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_tick(void *data, float seconds)
|
|
|
|
{
|
2017-05-31 23:58:42 -07:00
|
|
|
UNUSED_PARAMETER(seconds);
|
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
if (s->destroy_media) {
|
|
|
|
if (s->media_valid) {
|
|
|
|
mp_media_free(&s->media);
|
|
|
|
s->media_valid = false;
|
|
|
|
}
|
|
|
|
s->destroy_media = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_start(struct ffmpeg_source *s)
|
|
|
|
{
|
|
|
|
if (!s->media_valid)
|
|
|
|
ffmpeg_source_open(s);
|
|
|
|
|
|
|
|
if (s->media_valid) {
|
|
|
|
mp_media_play(&s->media, s->is_looping);
|
2017-05-16 01:57:25 -07:00
|
|
|
if (s->is_local_file)
|
|
|
|
obs_source_show_preloaded_video(s->source);
|
2020-01-31 11:12:09 -08:00
|
|
|
set_media_state(s, OBS_MEDIA_STATE_PLAYING);
|
2020-02-06 03:10:57 -08:00
|
|
|
obs_source_media_started(s->source);
|
2017-03-26 06:21:59 -07:00
|
|
|
}
|
2015-08-03 14:27:34 -07:00
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static void ffmpeg_source_update(void *data, obs_data_t *settings)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
bool is_local_file = obs_data_get_bool(settings, "is_local_file");
|
|
|
|
|
|
|
|
char *input;
|
|
|
|
char *input_format;
|
|
|
|
|
2016-01-23 02:31:45 -08:00
|
|
|
bfree(s->input);
|
|
|
|
bfree(s->input_format);
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
if (is_local_file) {
|
2015-03-10 10:18:19 -07:00
|
|
|
input = (char *)obs_data_get_string(settings, "local_file");
|
2015-03-04 10:45:50 -08:00
|
|
|
input_format = NULL;
|
2016-01-23 02:31:45 -08:00
|
|
|
s->is_looping = obs_data_get_bool(settings, "looping");
|
2019-06-22 22:13:45 -07:00
|
|
|
s->close_when_inactive =
|
|
|
|
obs_data_get_bool(settings, "close_when_inactive");
|
2015-03-04 10:45:50 -08:00
|
|
|
} else {
|
2015-03-10 10:18:19 -07:00
|
|
|
input = (char *)obs_data_get_string(settings, "input");
|
2019-06-22 22:13:45 -07:00
|
|
|
input_format =
|
|
|
|
(char *)obs_data_get_string(settings, "input_format");
|
2016-01-23 02:31:45 -08:00
|
|
|
s->is_looping = false;
|
2017-05-16 01:58:18 -07:00
|
|
|
s->close_when_inactive = true;
|
2015-03-04 10:45:50 -08:00
|
|
|
}
|
|
|
|
|
2016-01-23 02:31:45 -08:00
|
|
|
s->input = input ? bstrdup(input) : NULL;
|
|
|
|
s->input_format = input_format ? bstrdup(input_format) : NULL;
|
2017-05-18 23:42:01 -07:00
|
|
|
#ifndef __APPLE__
|
2015-03-10 10:18:19 -07:00
|
|
|
s->is_hw_decoding = obs_data_get_bool(settings, "hw_decode");
|
2017-05-18 23:42:01 -07:00
|
|
|
#endif
|
2019-06-22 22:13:45 -07:00
|
|
|
s->is_clear_on_media_end =
|
|
|
|
obs_data_get_bool(settings, "clear_on_media_end");
|
|
|
|
s->restart_on_activate =
|
|
|
|
obs_data_get_bool(settings, "restart_on_activate");
|
2017-03-26 06:21:59 -07:00
|
|
|
s->range = (enum video_range_type)obs_data_get_int(settings,
|
2019-06-22 22:13:45 -07:00
|
|
|
"color_range");
|
2017-05-29 19:13:18 -07:00
|
|
|
s->buffering_mb = (int)obs_data_get_int(settings, "buffering_mb");
|
2018-02-15 15:13:35 -08:00
|
|
|
s->speed_percent = (int)obs_data_get_int(settings, "speed_percent");
|
2017-05-16 01:57:25 -07:00
|
|
|
s->is_local_file = is_local_file;
|
2017-09-17 05:50:30 -07:00
|
|
|
s->seekable = obs_data_get_bool(settings, "seekable");
|
2017-03-26 06:21:59 -07:00
|
|
|
|
2018-02-15 15:13:35 -08:00
|
|
|
if (s->speed_percent < 1 || s->speed_percent > 200)
|
|
|
|
s->speed_percent = 100;
|
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
if (s->media_valid) {
|
|
|
|
mp_media_free(&s->media);
|
|
|
|
s->media_valid = false;
|
2015-03-04 10:45:50 -08:00
|
|
|
}
|
|
|
|
|
2017-03-31 12:30:10 -07:00
|
|
|
bool active = obs_source_active(s->source);
|
|
|
|
if (!s->close_when_inactive || active)
|
|
|
|
ffmpeg_source_open(s);
|
|
|
|
|
2017-03-26 06:21:59 -07:00
|
|
|
dump_source_info(s, input, input_format);
|
2017-03-31 12:30:10 -07:00
|
|
|
if (!s->restart_on_activate || active)
|
2016-02-04 10:58:26 -08:00
|
|
|
ffmpeg_source_start(s);
|
2015-03-04 10:45:50 -08:00
|
|
|
}
|
|
|
|
|
2015-09-16 01:30:51 -07:00
|
|
|
static const char *ffmpeg_source_getname(void *unused)
|
2015-03-04 10:45:50 -08:00
|
|
|
{
|
2015-09-16 01:30:51 -07:00
|
|
|
UNUSED_PARAMETER(unused);
|
2015-03-04 10:45:50 -08:00
|
|
|
return obs_module_text("FFMpegSource");
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
static void restart_hotkey(void *data, obs_hotkey_id id, obs_hotkey_t *hotkey,
|
|
|
|
bool pressed)
|
2017-05-10 19:53:03 -07:00
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(id);
|
|
|
|
UNUSED_PARAMETER(hotkey);
|
|
|
|
UNUSED_PARAMETER(pressed);
|
|
|
|
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
if (obs_source_active(s->source))
|
2020-01-31 11:12:09 -08:00
|
|
|
obs_source_media_restart(s->source);
|
2017-05-10 19:53:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void restart_proc(void *data, calldata_t *cd)
|
|
|
|
{
|
|
|
|
restart_hotkey(data, 0, NULL, true);
|
|
|
|
UNUSED_PARAMETER(cd);
|
|
|
|
}
|
|
|
|
|
2017-07-19 09:32:01 -07:00
|
|
|
static void get_duration(void *data, calldata_t *cd)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
int64_t dur = 0;
|
|
|
|
if (s->media.fmt)
|
|
|
|
dur = s->media.fmt->duration;
|
|
|
|
|
|
|
|
calldata_set_int(cd, "duration", dur * 1000);
|
|
|
|
}
|
|
|
|
|
2017-08-08 01:06:56 -07:00
|
|
|
static void get_nb_frames(void *data, calldata_t *cd)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
int64_t frames = 0;
|
|
|
|
|
|
|
|
if (!s->media.fmt) {
|
|
|
|
calldata_set_int(cd, "num_frames", frames);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
int video_stream_index = av_find_best_stream(
|
|
|
|
s->media.fmt, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
|
2017-08-08 01:06:56 -07:00
|
|
|
|
|
|
|
if (video_stream_index < 0) {
|
|
|
|
FF_BLOG(LOG_WARNING, "Getting number of frames failed: No "
|
2019-06-22 22:13:45 -07:00
|
|
|
"video stream in media file!");
|
2017-08-08 01:06:56 -07:00
|
|
|
calldata_set_int(cd, "num_frames", frames);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVStream *stream = s->media.fmt->streams[video_stream_index];
|
|
|
|
|
|
|
|
if (stream->nb_frames > 0) {
|
|
|
|
frames = stream->nb_frames;
|
|
|
|
} else {
|
|
|
|
FF_BLOG(LOG_DEBUG, "nb_frames not set, estimating using frame "
|
2019-06-22 22:13:45 -07:00
|
|
|
"rate and duration");
|
2017-08-08 01:06:56 -07:00
|
|
|
AVRational avg_frame_rate = stream->avg_frame_rate;
|
|
|
|
frames = (int64_t)ceil((double)s->media.fmt->duration /
|
2019-06-22 22:13:45 -07:00
|
|
|
(double)AV_TIME_BASE *
|
|
|
|
(double)avg_frame_rate.num /
|
|
|
|
(double)avg_frame_rate.den);
|
2017-08-08 01:06:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
calldata_set_int(cd, "num_frames", frames);
|
|
|
|
}
|
|
|
|
|
2020-01-31 11:12:09 -08:00
|
|
|
static bool ffmpeg_source_play_hotkey(void *data, obs_hotkey_pair_id id,
|
|
|
|
obs_hotkey_t *hotkey, bool pressed)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(id);
|
|
|
|
UNUSED_PARAMETER(hotkey);
|
|
|
|
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (s->state == OBS_MEDIA_STATE_PLAYING || !pressed ||
|
|
|
|
!obs_source_active(s->source))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
obs_source_media_play_pause(s->source, false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ffmpeg_source_pause_hotkey(void *data, obs_hotkey_pair_id id,
|
|
|
|
obs_hotkey_t *hotkey, bool pressed)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(id);
|
|
|
|
UNUSED_PARAMETER(hotkey);
|
|
|
|
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (s->state != OBS_MEDIA_STATE_PLAYING || !pressed ||
|
|
|
|
!obs_source_active(s->source))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
obs_source_media_play_pause(s->source, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_stop_hotkey(void *data, obs_hotkey_id id,
|
|
|
|
obs_hotkey_t *hotkey, bool pressed)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(id);
|
|
|
|
UNUSED_PARAMETER(hotkey);
|
|
|
|
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (pressed && obs_source_active(s->source))
|
|
|
|
obs_source_media_stop(s->source);
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
static void *ffmpeg_source_create(obs_data_t *settings, obs_source_t *source)
|
|
|
|
{
|
|
|
|
UNUSED_PARAMETER(settings);
|
|
|
|
|
|
|
|
struct ffmpeg_source *s = bzalloc(sizeof(struct ffmpeg_source));
|
|
|
|
s->source = source;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
s->hotkey = obs_hotkey_register_source(source, "MediaSource.Restart",
|
|
|
|
obs_module_text("RestartMedia"),
|
|
|
|
restart_hotkey, s);
|
2017-05-10 19:53:03 -07:00
|
|
|
|
2020-01-31 11:12:09 -08:00
|
|
|
s->play_pause_hotkey = obs_hotkey_pair_register_source(
|
|
|
|
s->source, "MediaSource.Play", obs_module_text("Play"),
|
|
|
|
"MediaSource.Pause", obs_module_text("Pause"),
|
|
|
|
ffmpeg_source_play_hotkey, ffmpeg_source_pause_hotkey, s, s);
|
|
|
|
|
|
|
|
s->stop_hotkey = obs_hotkey_register_source(source, "MediaSource.Stop",
|
|
|
|
obs_module_text("Stop"),
|
|
|
|
ffmpeg_source_stop_hotkey,
|
|
|
|
s);
|
|
|
|
|
2017-05-10 19:53:03 -07:00
|
|
|
proc_handler_t *ph = obs_source_get_proc_handler(source);
|
|
|
|
proc_handler_add(ph, "void restart()", restart_proc, s);
|
2017-07-19 09:32:01 -07:00
|
|
|
proc_handler_add(ph, "void get_duration(out int duration)",
|
2019-06-22 22:13:45 -07:00
|
|
|
get_duration, s);
|
2017-08-08 01:06:56 -07:00
|
|
|
proc_handler_add(ph, "void get_nb_frames(out int num_frames)",
|
2019-06-22 22:13:45 -07:00
|
|
|
get_nb_frames, s);
|
2017-05-10 19:53:03 -07:00
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
ffmpeg_source_update(s, settings);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_destroy(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
2017-05-10 19:53:03 -07:00
|
|
|
if (s->hotkey)
|
|
|
|
obs_hotkey_unregister(s->hotkey);
|
2017-03-26 06:21:59 -07:00
|
|
|
if (s->media_valid)
|
|
|
|
mp_media_free(&s->media);
|
2015-03-10 10:53:17 -07:00
|
|
|
|
2015-03-10 12:34:37 -07:00
|
|
|
if (s->sws_ctx != NULL)
|
2015-03-10 10:53:17 -07:00
|
|
|
sws_freeContext(s->sws_ctx);
|
2016-01-23 02:31:45 -08:00
|
|
|
bfree(s->sws_data);
|
|
|
|
bfree(s->input);
|
|
|
|
bfree(s->input_format);
|
2015-03-04 10:45:50 -08:00
|
|
|
bfree(s);
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:36:57 -08:00
|
|
|
static void ffmpeg_source_activate(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (s->restart_on_activate)
|
2020-01-31 11:12:09 -08:00
|
|
|
obs_source_media_restart(s->source);
|
2016-01-23 02:36:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_deactivate(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (s->restart_on_activate) {
|
2017-03-26 06:21:59 -07:00
|
|
|
if (s->media_valid) {
|
|
|
|
mp_media_stop(&s->media);
|
2016-01-23 02:36:57 -08:00
|
|
|
|
|
|
|
if (s->is_clear_on_media_end)
|
|
|
|
obs_source_output_video(s->source, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 11:03:01 -08:00
|
|
|
static void ffmpeg_source_play_pause(void *data, bool pause)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
mp_media_play_pause(&s->media, pause);
|
|
|
|
|
|
|
|
if (pause)
|
|
|
|
set_media_state(s, OBS_MEDIA_STATE_PAUSED);
|
|
|
|
else
|
|
|
|
set_media_state(s, OBS_MEDIA_STATE_PLAYING);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_stop(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (s->media_valid) {
|
|
|
|
mp_media_stop(&s->media);
|
|
|
|
obs_source_output_video(s->source, NULL);
|
|
|
|
set_media_state(s, OBS_MEDIA_STATE_STOPPED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_restart(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
if (obs_source_active(s->source))
|
|
|
|
ffmpeg_source_start(s);
|
|
|
|
|
|
|
|
set_media_state(s, OBS_MEDIA_STATE_PLAYING);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t ffmpeg_source_get_duration(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
int64_t dur = 0;
|
|
|
|
|
|
|
|
if (s->media.fmt)
|
|
|
|
dur = (float)s->media.fmt->duration / 1000.0f;
|
|
|
|
|
|
|
|
return dur;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t ffmpeg_source_get_time(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
return mp_get_current_time(&s->media);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ffmpeg_source_set_time(void *data, int64_t ms)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
mp_media_seek_to(&s->media, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
static enum obs_media_state ffmpeg_source_get_state(void *data)
|
|
|
|
{
|
|
|
|
struct ffmpeg_source *s = data;
|
|
|
|
|
|
|
|
return s->state;
|
|
|
|
}
|
|
|
|
|
2015-03-04 10:45:50 -08:00
|
|
|
struct obs_source_info ffmpeg_source = {
|
2019-06-22 22:13:45 -07:00
|
|
|
.id = "ffmpeg_source",
|
|
|
|
.type = OBS_SOURCE_TYPE_INPUT,
|
|
|
|
.output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
|
2020-01-31 11:03:01 -08:00
|
|
|
OBS_SOURCE_DO_NOT_DUPLICATE |
|
|
|
|
OBS_SOURCE_CONTROLLABLE_MEDIA,
|
2019-06-22 22:13:45 -07:00
|
|
|
.get_name = ffmpeg_source_getname,
|
|
|
|
.create = ffmpeg_source_create,
|
|
|
|
.destroy = ffmpeg_source_destroy,
|
|
|
|
.get_defaults = ffmpeg_source_defaults,
|
2015-03-04 10:45:50 -08:00
|
|
|
.get_properties = ffmpeg_source_getproperties,
|
2019-06-22 22:13:45 -07:00
|
|
|
.activate = ffmpeg_source_activate,
|
|
|
|
.deactivate = ffmpeg_source_deactivate,
|
|
|
|
.video_tick = ffmpeg_source_tick,
|
|
|
|
.update = ffmpeg_source_update,
|
2019-07-27 21:59:16 -07:00
|
|
|
.icon_type = OBS_ICON_TYPE_MEDIA,
|
2020-01-31 11:03:01 -08:00
|
|
|
.media_play_pause = ffmpeg_source_play_pause,
|
|
|
|
.media_restart = ffmpeg_source_restart,
|
|
|
|
.media_stop = ffmpeg_source_stop,
|
|
|
|
.media_get_duration = ffmpeg_source_get_duration,
|
|
|
|
.media_get_time = ffmpeg_source_get_time,
|
|
|
|
.media_set_time = ffmpeg_source_set_time,
|
|
|
|
.media_get_state = ffmpeg_source_get_state,
|
2015-03-04 10:45:50 -08:00
|
|
|
};
|