yolobs-studio/UI/window-basic-main-outputs.cpp

1879 lines
53 KiB
C++
Raw Normal View History

2016-02-23 23:16:51 +00:00
#include <string>
2016-08-28 12:07:43 +00:00
#include <algorithm>
2016-02-23 23:16:51 +00:00
#include <QMessageBox>
2017-06-29 19:01:10 +00:00
#include "qt-wrappers.hpp"
2016-02-23 23:16:51 +00:00
#include "audio-encoders.hpp"
#include "window-basic-main.hpp"
#include "window-basic-main-outputs.hpp"
using namespace std;
2019-07-27 12:47:10 +00:00
extern bool EncoderAvailable(const char *encoder);
volatile bool streaming_active = false;
volatile bool recording_active = false;
2019-09-22 21:19:10 +00:00
volatile bool recording_paused = false;
2019-07-27 12:47:10 +00:00
volatile bool replaybuf_active = false;
2020-10-01 20:15:25 +00:00
volatile bool virtualcam_active = false;
2019-07-27 12:47:10 +00:00
2020-03-25 08:07:22 +00:00
#define RTMP_PROTOCOL "rtmp"
2016-02-23 23:16:51 +00:00
static void OBSStreamStarting(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
obs_output_t *obj = (obs_output_t *)calldata_ptr(params, "output");
2016-02-23 23:16:51 +00:00
int sec = (int)obs_output_get_active_delay(obj);
if (sec == 0)
return;
output->delayActive = true;
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(output->main, "StreamDelayStarting",
Q_ARG(int, sec));
2016-02-23 23:16:51 +00:00
}
static void OBSStreamStopping(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
obs_output_t *obj = (obs_output_t *)calldata_ptr(params, "output");
2016-02-23 23:16:51 +00:00
int sec = (int)obs_output_get_active_delay(obj);
if (sec == 0)
2016-08-28 12:07:43 +00:00
QMetaObject::invokeMethod(output->main, "StreamStopping");
else
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(output->main, "StreamDelayStopping",
Q_ARG(int, sec));
2016-02-23 23:16:51 +00:00
}
static void OBSStartStreaming(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2016-02-23 23:16:51 +00:00
output->streamingActive = true;
2019-07-27 12:47:10 +00:00
os_atomic_set_bool(&streaming_active, true);
2016-02-23 23:16:51 +00:00
QMetaObject::invokeMethod(output->main, "StreamingStart");
UNUSED_PARAMETER(params);
}
static void OBSStopStreaming(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2016-02-23 23:16:51 +00:00
int code = (int)calldata_int(params, "code");
2017-06-29 19:01:10 +00:00
const char *last_error = calldata_string(params, "last_error");
QString arg_last_error = QString::fromUtf8(last_error);
2016-02-23 23:16:51 +00:00
output->streamingActive = false;
output->delayActive = false;
2019-07-27 12:47:10 +00:00
os_atomic_set_bool(&streaming_active, false);
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(output->main, "StreamingStop",
Q_ARG(int, code),
Q_ARG(QString, arg_last_error));
2016-02-23 23:16:51 +00:00
}
static void OBSStartRecording(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2016-02-23 23:16:51 +00:00
output->recordingActive = true;
2019-07-27 12:47:10 +00:00
os_atomic_set_bool(&recording_active, true);
2016-02-23 23:16:51 +00:00
QMetaObject::invokeMethod(output->main, "RecordingStart");
UNUSED_PARAMETER(params);
}
static void OBSStopRecording(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2016-02-23 23:16:51 +00:00
int code = (int)calldata_int(params, "code");
2019-07-27 12:47:10 +00:00
const char *last_error = calldata_string(params, "last_error");
QString arg_last_error = QString::fromUtf8(last_error);
2016-02-23 23:16:51 +00:00
output->recordingActive = false;
2019-07-27 12:47:10 +00:00
os_atomic_set_bool(&recording_active, false);
2019-09-22 21:19:10 +00:00
os_atomic_set_bool(&recording_paused, false);
QMetaObject::invokeMethod(output->main, "RecordingStop",
Q_ARG(int, code),
Q_ARG(QString, arg_last_error));
2016-02-23 23:16:51 +00:00
UNUSED_PARAMETER(params);
}
2016-08-28 12:07:43 +00:00
static void OBSRecordStopping(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2016-08-28 12:07:43 +00:00
QMetaObject::invokeMethod(output->main, "RecordStopping");
UNUSED_PARAMETER(params);
}
2017-04-19 19:54:15 +00:00
static void OBSStartReplayBuffer(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2017-04-19 19:54:15 +00:00
output->replayBufferActive = true;
2019-07-27 12:47:10 +00:00
os_atomic_set_bool(&replaybuf_active, true);
2017-04-19 19:54:15 +00:00
QMetaObject::invokeMethod(output->main, "ReplayBufferStart");
UNUSED_PARAMETER(params);
}
static void OBSStopReplayBuffer(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2017-04-19 19:54:15 +00:00
int code = (int)calldata_int(params, "code");
output->replayBufferActive = false;
2019-07-27 12:47:10 +00:00
os_atomic_set_bool(&replaybuf_active, false);
2019-09-22 21:19:10 +00:00
QMetaObject::invokeMethod(output->main, "ReplayBufferStop",
Q_ARG(int, code));
2017-04-19 19:54:15 +00:00
UNUSED_PARAMETER(params);
}
static void OBSReplayBufferStopping(void *data, calldata_t *params)
{
2019-09-22 21:19:10 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2017-04-19 19:54:15 +00:00
QMetaObject::invokeMethod(output->main, "ReplayBufferStopping");
UNUSED_PARAMETER(params);
}
2020-10-01 20:15:25 +00:00
static void OBSStartVirtualCam(void *data, calldata_t *params)
2016-05-24 19:53:01 +00:00
{
2020-10-01 20:15:25 +00:00
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
2016-05-24 19:53:01 +00:00
2020-10-01 20:15:25 +00:00
output->virtualCamActive = true;
os_atomic_set_bool(&virtualcam_active, true);
QMetaObject::invokeMethod(output->main, "OnVirtualCamStart");
2016-05-24 19:53:01 +00:00
2020-10-01 20:15:25 +00:00
UNUSED_PARAMETER(params);
}
2016-05-24 19:53:01 +00:00
2020-10-01 20:15:25 +00:00
static void OBSStopVirtualCam(void *data, calldata_t *params)
{
BasicOutputHandler *output = static_cast<BasicOutputHandler *>(data);
int code = (int)calldata_int(params, "code");
2016-05-24 19:53:01 +00:00
2020-10-01 20:15:25 +00:00
output->virtualCamActive = false;
os_atomic_set_bool(&virtualcam_active, false);
QMetaObject::invokeMethod(output->main, "OnVirtualCamStop",
Q_ARG(int, code));
2016-05-24 19:53:01 +00:00
2020-10-01 20:15:25 +00:00
UNUSED_PARAMETER(params);
2016-05-24 19:53:01 +00:00
}
2016-02-23 23:16:51 +00:00
/* ------------------------------------------------------------------------ */
static bool CreateAACEncoder(OBSEncoder &res, string &id, int bitrate,
2019-09-22 21:19:10 +00:00
const char *name, size_t idx)
2016-02-23 23:16:51 +00:00
{
const char *id_ = GetAACEncoderForBitrate(bitrate);
if (!id_) {
id.clear();
res = nullptr;
return false;
}
if (id == id_)
return true;
id = id_;
res = obs_audio_encoder_create(id_, name, nullptr, idx, nullptr);
if (res) {
obs_encoder_release(res);
return true;
}
return false;
}
/* ------------------------------------------------------------------------ */
2020-10-01 20:15:25 +00:00
inline BasicOutputHandler::BasicOutputHandler(OBSBasic *main_) : main(main_)
{
if (main->vcamEnabled) {
virtualCam = obs_output_create("virtualcam_output",
"virtualcam_output", nullptr,
nullptr);
obs_output_release(virtualCam);
signal_handler_t *signal =
obs_output_get_signal_handler(virtualCam);
startVirtualCam.Connect(signal, "start", OBSStartVirtualCam,
this);
stopVirtualCam.Connect(signal, "stop", OBSStopVirtualCam, this);
}
}
bool BasicOutputHandler::StartVirtualCam()
{
if (main->vcamEnabled) {
obs_output_set_media(virtualCam, obs_get_video(),
obs_get_audio());
if (!Active())
SetupOutputs();
return obs_output_start(virtualCam);
}
return false;
}
void BasicOutputHandler::StopVirtualCam()
{
if (main->vcamEnabled) {
obs_output_stop(virtualCam);
}
}
bool BasicOutputHandler::VirtualCamActive() const
{
if (main->vcamEnabled) {
return obs_output_active(virtualCam);
}
return false;
}
/* ------------------------------------------------------------------------ */
2016-02-23 23:16:51 +00:00
struct SimpleOutput : BasicOutputHandler {
2019-09-22 21:19:10 +00:00
OBSEncoder aacStreaming;
OBSEncoder h264Streaming;
OBSEncoder aacRecording;
OBSEncoder h264Recording;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
string aacRecEncID;
string aacStreamEncID;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
string videoEncoder;
string videoQuality;
bool usingRecordingPreset = false;
bool recordingConfigured = false;
bool ffmpegOutput = false;
bool lowCPUx264 = false;
2016-02-23 23:16:51 +00:00
SimpleOutput(OBSBasic *main_);
int CalcCRF(int crf);
2016-10-10 19:01:40 +00:00
void UpdateStreamingSettings_amd(obs_data_t *settings, int bitrate);
2016-02-23 23:16:51 +00:00
void UpdateRecordingSettings_x264_crf(int crf);
2016-05-24 19:53:01 +00:00
void UpdateRecordingSettings_qsv11(int crf);
void UpdateRecordingSettings_nvenc(int cqp);
2016-10-10 19:01:40 +00:00
void UpdateRecordingSettings_amd_cqp(int cqp);
2016-02-23 23:16:51 +00:00
void UpdateRecordingSettings();
void UpdateRecordingAudioSettings();
virtual void Update() override;
2020-10-01 20:15:25 +00:00
void SetupOutputs() override;
2016-02-23 23:16:51 +00:00
int GetAudioBitrate() const;
2016-05-24 19:53:01 +00:00
void LoadRecordingPreset_h264(const char *encoder);
2016-02-23 23:16:51 +00:00
void LoadRecordingPreset_Lossless();
void LoadRecordingPreset();
2016-05-24 19:53:01 +00:00
void LoadStreamingPreset_h264(const char *encoder);
2017-04-19 19:54:15 +00:00
void UpdateRecording();
bool ConfigureRecording(bool useReplayBuffer);
2020-10-01 20:15:25 +00:00
virtual bool SetupStreaming(obs_service_t *service) override;
2016-02-23 23:16:51 +00:00
virtual bool StartStreaming(obs_service_t *service) override;
virtual bool StartRecording() override;
2017-04-19 19:54:15 +00:00
virtual bool StartReplayBuffer() override;
2016-10-10 19:01:40 +00:00
virtual void StopStreaming(bool force) override;
virtual void StopRecording(bool force) override;
2017-04-19 19:54:15 +00:00
virtual void StopReplayBuffer(bool force) override;
2016-02-23 23:16:51 +00:00
virtual bool StreamingActive() const override;
virtual bool RecordingActive() const override;
2017-04-19 19:54:15 +00:00
virtual bool ReplayBufferActive() const override;
2016-02-23 23:16:51 +00:00
};
void SimpleOutput::LoadRecordingPreset_Lossless()
{
2019-09-22 21:19:10 +00:00
fileOutput = obs_output_create("ffmpeg_output", "simple_ffmpeg_output",
nullptr, nullptr);
2016-02-23 23:16:51 +00:00
if (!fileOutput)
throw "Failed to create recording FFmpeg output "
"(simple output)";
obs_output_release(fileOutput);
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "format_name", "avi");
obs_data_set_string(settings, "video_encoder", "utvideo");
obs_data_set_string(settings, "audio_encoder", "pcm_s16le");
2019-07-27 12:47:10 +00:00
int aMixes = 1;
obs_output_set_mixers(fileOutput, aMixes);
2016-02-23 23:16:51 +00:00
obs_output_update(fileOutput, settings);
obs_data_release(settings);
}
2016-05-24 19:53:01 +00:00
void SimpleOutput::LoadRecordingPreset_h264(const char *encoderId)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
h264Recording = obs_video_encoder_create(
encoderId, "simple_h264_recording", nullptr, nullptr);
2016-02-23 23:16:51 +00:00
if (!h264Recording)
throw "Failed to create h264 recording encoder (simple output)";
obs_encoder_release(h264Recording);
2016-05-24 19:53:01 +00:00
}
2016-02-23 23:16:51 +00:00
2016-05-24 19:53:01 +00:00
void SimpleOutput::LoadStreamingPreset_h264(const char *encoderId)
{
2019-09-22 21:19:10 +00:00
h264Streaming = obs_video_encoder_create(
encoderId, "simple_h264_stream", nullptr, nullptr);
2016-05-24 19:53:01 +00:00
if (!h264Streaming)
throw "Failed to create h264 streaming encoder (simple output)";
obs_encoder_release(h264Streaming);
2016-02-23 23:16:51 +00:00
}
void SimpleOutput::LoadRecordingPreset()
{
2019-09-22 21:19:10 +00:00
const char *quality =
config_get_string(main->Config(), "SimpleOutput", "RecQuality");
const char *encoder =
config_get_string(main->Config(), "SimpleOutput", "RecEncoder");
2016-02-23 23:16:51 +00:00
videoEncoder = encoder;
videoQuality = quality;
ffmpegOutput = false;
if (strcmp(quality, "Stream") == 0) {
h264Recording = h264Streaming;
aacRecording = aacStreaming;
usingRecordingPreset = false;
return;
} else if (strcmp(quality, "Lossless") == 0) {
LoadRecordingPreset_Lossless();
usingRecordingPreset = true;
ffmpegOutput = true;
return;
} else {
2016-05-24 19:53:01 +00:00
lowCPUx264 = false;
if (strcmp(encoder, SIMPLE_ENCODER_X264) == 0) {
LoadRecordingPreset_h264("obs_x264");
} else if (strcmp(encoder, SIMPLE_ENCODER_X264_LOWCPU) == 0) {
LoadRecordingPreset_h264("obs_x264");
lowCPUx264 = true;
} else if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
LoadRecordingPreset_h264("obs_qsv11");
2016-10-10 19:01:40 +00:00
} else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
LoadRecordingPreset_h264("amd_amf_h264");
2016-05-24 19:53:01 +00:00
} else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
2019-07-27 12:47:10 +00:00
const char *id = EncoderAvailable("jim_nvenc")
2019-09-22 21:19:10 +00:00
? "jim_nvenc"
: "ffmpeg_nvenc";
2019-07-27 12:47:10 +00:00
LoadRecordingPreset_h264(id);
2016-05-24 19:53:01 +00:00
}
2016-02-23 23:16:51 +00:00
usingRecordingPreset = true;
2016-05-24 19:53:01 +00:00
if (!CreateAACEncoder(aacRecording, aacRecEncID, 192,
2019-09-22 21:19:10 +00:00
"simple_aac_recording", 0))
2016-05-24 19:53:01 +00:00
throw "Failed to create aac recording encoder "
"(simple output)";
2016-02-23 23:16:51 +00:00
}
}
SimpleOutput::SimpleOutput(OBSBasic *main_) : BasicOutputHandler(main_)
{
2016-05-24 19:53:01 +00:00
const char *encoder = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"StreamEncoder");
2019-07-27 12:47:10 +00:00
if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
2016-05-24 19:53:01 +00:00
LoadStreamingPreset_h264("obs_qsv11");
2019-07-27 12:47:10 +00:00
} else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
2016-10-10 19:01:40 +00:00
LoadStreamingPreset_h264("amd_amf_h264");
2019-07-27 12:47:10 +00:00
} else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
2019-09-22 21:19:10 +00:00
const char *id = EncoderAvailable("jim_nvenc") ? "jim_nvenc"
: "ffmpeg_nvenc";
2019-07-27 12:47:10 +00:00
LoadStreamingPreset_h264(id);
} else {
2016-05-24 19:53:01 +00:00
LoadStreamingPreset_h264("obs_x264");
2019-07-27 12:47:10 +00:00
}
2016-02-23 23:16:51 +00:00
if (!CreateAACEncoder(aacStreaming, aacStreamEncID, GetAudioBitrate(),
2019-09-22 21:19:10 +00:00
"simple_aac", 0))
2016-02-23 23:16:51 +00:00
throw "Failed to create aac streaming encoder (simple output)";
LoadRecordingPreset();
if (!ffmpegOutput) {
2017-04-19 19:54:15 +00:00
bool useReplayBuffer = config_get_bool(main->Config(),
2019-09-22 21:19:10 +00:00
"SimpleOutput", "RecRB");
2017-04-19 19:54:15 +00:00
if (useReplayBuffer) {
2020-10-01 20:15:25 +00:00
obs_data_t *hotkey;
2019-09-22 21:19:10 +00:00
const char *str = config_get_string(
main->Config(), "Hotkeys", "ReplayBuffer");
2020-10-01 20:15:25 +00:00
if (str)
hotkey = obs_data_create_from_json(str);
else
hotkey = nullptr;
2017-04-19 19:54:15 +00:00
replayBuffer = obs_output_create("replay_buffer",
2019-09-22 21:19:10 +00:00
Str("ReplayBuffer"),
nullptr, hotkey);
2017-04-19 19:54:15 +00:00
obs_data_release(hotkey);
if (!replayBuffer)
throw "Failed to create replay buffer output "
"(simple output)";
obs_output_release(replayBuffer);
signal_handler_t *signal =
obs_output_get_signal_handler(replayBuffer);
startReplayBuffer.Connect(signal, "start",
2019-09-22 21:19:10 +00:00
OBSStartReplayBuffer, this);
2017-04-19 19:54:15 +00:00
stopReplayBuffer.Connect(signal, "stop",
2019-09-22 21:19:10 +00:00
OBSStopReplayBuffer, this);
2017-04-19 19:54:15 +00:00
replayBufferStopping.Connect(signal, "stopping",
2019-09-22 21:19:10 +00:00
OBSReplayBufferStopping,
this);
2017-04-19 19:54:15 +00:00
}
2019-09-22 21:19:10 +00:00
fileOutput = obs_output_create(
"ffmpeg_muxer", "simple_file_output", nullptr, nullptr);
2016-02-23 23:16:51 +00:00
if (!fileOutput)
throw "Failed to create recording output "
"(simple output)";
obs_output_release(fileOutput);
}
startRecording.Connect(obs_output_get_signal_handler(fileOutput),
2019-09-22 21:19:10 +00:00
"start", OBSStartRecording, this);
stopRecording.Connect(obs_output_get_signal_handler(fileOutput), "stop",
OBSStopRecording, this);
2016-08-28 12:07:43 +00:00
recordStopping.Connect(obs_output_get_signal_handler(fileOutput),
2019-09-22 21:19:10 +00:00
"stopping", OBSRecordStopping, this);
2016-02-23 23:16:51 +00:00
}
int SimpleOutput::GetAudioBitrate() const
{
int bitrate = (int)config_get_uint(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"ABitrate");
2016-02-23 23:16:51 +00:00
return FindClosestAvailableAACBitrate(bitrate);
}
void SimpleOutput::Update()
{
obs_data_t *h264Settings = obs_data_create();
2019-09-22 21:19:10 +00:00
obs_data_t *aacSettings = obs_data_create();
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
int videoBitrate =
config_get_uint(main->Config(), "SimpleOutput", "VBitrate");
2016-02-23 23:16:51 +00:00
int audioBitrate = GetAudioBitrate();
2019-09-22 21:19:10 +00:00
bool advanced =
config_get_bool(main->Config(), "SimpleOutput", "UseAdvanced");
2016-05-24 19:53:01 +00:00
bool enforceBitrate = config_get_bool(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"EnforceBitrate");
const char *custom = config_get_string(main->Config(), "SimpleOutput",
"x264Settings");
2016-05-24 19:53:01 +00:00
const char *encoder = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"StreamEncoder");
2016-05-24 19:53:01 +00:00
const char *presetType;
const char *preset;
2016-10-10 19:01:40 +00:00
if (strcmp(encoder, SIMPLE_ENCODER_QSV) == 0) {
2016-05-24 19:53:01 +00:00
presetType = "QSVPreset";
2016-10-10 19:01:40 +00:00
} else if (strcmp(encoder, SIMPLE_ENCODER_AMD) == 0) {
presetType = "AMDPreset";
UpdateStreamingSettings_amd(h264Settings, videoBitrate);
} else if (strcmp(encoder, SIMPLE_ENCODER_NVENC) == 0) {
2016-05-24 19:53:01 +00:00
presetType = "NVENCPreset";
2016-10-10 19:01:40 +00:00
} else {
2016-05-24 19:53:01 +00:00
presetType = "Preset";
2016-10-10 19:01:40 +00:00
}
2016-05-24 19:53:01 +00:00
preset = config_get_string(main->Config(), "SimpleOutput", presetType);
2016-02-23 23:16:51 +00:00
2016-08-28 12:07:43 +00:00
obs_data_set_string(h264Settings, "rate_control", "CBR");
2016-02-23 23:16:51 +00:00
obs_data_set_int(h264Settings, "bitrate", videoBitrate);
if (advanced) {
obs_data_set_string(h264Settings, "preset", preset);
obs_data_set_string(h264Settings, "x264opts", custom);
}
2016-05-24 19:53:01 +00:00
obs_data_set_string(aacSettings, "rate_control", "CBR");
2016-02-23 23:16:51 +00:00
obs_data_set_int(aacSettings, "bitrate", audioBitrate);
2019-09-22 21:19:10 +00:00
obs_service_apply_encoder_settings(main->GetService(), h264Settings,
aacSettings);
2016-02-23 23:16:51 +00:00
2016-05-24 19:53:01 +00:00
if (advanced && !enforceBitrate) {
obs_data_set_int(h264Settings, "bitrate", videoBitrate);
obs_data_set_int(aacSettings, "bitrate", audioBitrate);
}
2016-02-23 23:16:51 +00:00
video_t *video = obs_get_video();
enum video_format format = video_output_get_format(video);
if (format != VIDEO_FORMAT_NV12 && format != VIDEO_FORMAT_I420)
obs_encoder_set_preferred_video_format(h264Streaming,
2019-09-22 21:19:10 +00:00
VIDEO_FORMAT_NV12);
2016-02-23 23:16:51 +00:00
obs_encoder_update(h264Streaming, h264Settings);
2019-09-22 21:19:10 +00:00
obs_encoder_update(aacStreaming, aacSettings);
2016-02-23 23:16:51 +00:00
obs_data_release(h264Settings);
obs_data_release(aacSettings);
}
void SimpleOutput::UpdateRecordingAudioSettings()
{
obs_data_t *settings = obs_data_create();
obs_data_set_int(settings, "bitrate", 192);
2016-05-24 19:53:01 +00:00
obs_data_set_string(settings, "rate_control", "CBR");
2016-02-23 23:16:51 +00:00
obs_encoder_update(aacRecording, settings);
obs_data_release(settings);
}
#define CROSS_DIST_CUTOFF 2000.0
int SimpleOutput::CalcCRF(int crf)
{
int cx = config_get_uint(main->Config(), "Video", "OutputCX");
int cy = config_get_uint(main->Config(), "Video", "OutputCY");
double fCX = double(cx);
double fCY = double(cy);
if (lowCPUx264)
crf -= 2;
double crossDist = sqrt(fCX * fCX + fCY * fCY);
double crfResReduction =
fmin(CROSS_DIST_CUTOFF, crossDist) / CROSS_DIST_CUTOFF;
crfResReduction = (1.0 - crfResReduction) * 10.0;
return crf - int(crfResReduction);
}
void SimpleOutput::UpdateRecordingSettings_x264_crf(int crf)
{
obs_data_t *settings = obs_data_create();
obs_data_set_int(settings, "crf", crf);
obs_data_set_bool(settings, "use_bufsize", true);
2016-05-24 19:53:01 +00:00
obs_data_set_string(settings, "rate_control", "CRF");
2016-02-23 23:16:51 +00:00
obs_data_set_string(settings, "profile", "high");
obs_data_set_string(settings, "preset",
2019-09-22 21:19:10 +00:00
lowCPUx264 ? "ultrafast" : "veryfast");
2016-02-23 23:16:51 +00:00
obs_encoder_update(h264Recording, settings);
obs_data_release(settings);
}
2016-05-24 19:53:01 +00:00
static bool icq_available(obs_encoder_t *encoder)
{
obs_properties_t *props = obs_encoder_properties(encoder);
obs_property_t *p = obs_properties_get(props, "rate_control");
bool icq_found = false;
size_t num = obs_property_list_item_count(p);
for (size_t i = 0; i < num; i++) {
const char *val = obs_property_list_item_string(p, i);
if (strcmp(val, "ICQ") == 0) {
icq_found = true;
break;
}
}
obs_properties_destroy(props);
return icq_found;
}
void SimpleOutput::UpdateRecordingSettings_qsv11(int crf)
{
bool icq = icq_available(h264Recording);
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "profile", "high");
if (icq) {
obs_data_set_string(settings, "rate_control", "ICQ");
obs_data_set_int(settings, "icq_quality", crf);
} else {
obs_data_set_string(settings, "rate_control", "CQP");
obs_data_set_int(settings, "qpi", crf);
obs_data_set_int(settings, "qpp", crf);
obs_data_set_int(settings, "qpb", crf);
}
obs_encoder_update(h264Recording, settings);
obs_data_release(settings);
}
void SimpleOutput::UpdateRecordingSettings_nvenc(int cqp)
{
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "rate_control", "CQP");
obs_data_set_string(settings, "profile", "high");
obs_data_set_string(settings, "preset", "hq");
obs_data_set_int(settings, "cqp", cqp);
obs_encoder_update(h264Recording, settings);
obs_data_release(settings);
}
2016-10-10 19:01:40 +00:00
void SimpleOutput::UpdateStreamingSettings_amd(obs_data_t *settings,
2019-09-22 21:19:10 +00:00
int bitrate)
2016-10-10 19:01:40 +00:00
{
2017-04-19 19:54:15 +00:00
// Static Properties
2017-06-29 19:01:10 +00:00
obs_data_set_int(settings, "Usage", 0);
obs_data_set_int(settings, "Profile", 100); // High
2018-02-19 19:54:37 +00:00
2017-04-19 19:54:15 +00:00
// Rate Control Properties
2018-02-19 19:54:37 +00:00
obs_data_set_int(settings, "RateControlMethod", 3);
2017-06-29 19:01:10 +00:00
obs_data_set_int(settings, "Bitrate.Target", bitrate);
obs_data_set_int(settings, "FillerData", 1);
obs_data_set_int(settings, "VBVBuffer", 1);
obs_data_set_int(settings, "VBVBuffer.Size", bitrate);
2018-02-19 19:54:37 +00:00
2017-04-19 19:54:15 +00:00
// Picture Control Properties
2017-06-29 19:01:10 +00:00
obs_data_set_double(settings, "KeyframeInterval", 2.0);
2018-02-19 19:54:37 +00:00
obs_data_set_int(settings, "BFrame.Pattern", 0);
2016-10-10 19:01:40 +00:00
}
void SimpleOutput::UpdateRecordingSettings_amd_cqp(int cqp)
{
obs_data_t *settings = obs_data_create();
2017-04-19 19:54:15 +00:00
// Static Properties
2017-06-29 19:01:10 +00:00
obs_data_set_int(settings, "Usage", 0);
obs_data_set_int(settings, "Profile", 100); // High
2017-04-19 19:54:15 +00:00
// Rate Control Properties
2017-06-29 19:01:10 +00:00
obs_data_set_int(settings, "RateControlMethod", 0);
obs_data_set_int(settings, "QP.IFrame", cqp);
obs_data_set_int(settings, "QP.PFrame", cqp);
obs_data_set_int(settings, "QP.BFrame", cqp);
obs_data_set_int(settings, "VBVBuffer", 1);
obs_data_set_int(settings, "VBVBuffer.Size", 100000);
2016-10-10 19:01:40 +00:00
2017-04-19 19:54:15 +00:00
// Picture Control Properties
2017-06-29 19:01:10 +00:00
obs_data_set_double(settings, "KeyframeInterval", 2.0);
2018-02-19 19:54:37 +00:00
obs_data_set_int(settings, "BFrame.Pattern", 0);
2016-10-10 19:01:40 +00:00
2017-04-19 19:54:15 +00:00
// Update and release
obs_encoder_update(h264Recording, settings);
2016-10-10 19:01:40 +00:00
obs_data_release(settings);
}
2016-02-23 23:16:51 +00:00
void SimpleOutput::UpdateRecordingSettings()
{
2016-05-24 19:53:01 +00:00
bool ultra_hq = (videoQuality == "HQ");
int crf = CalcCRF(ultra_hq ? 16 : 23);
2016-02-23 23:16:51 +00:00
if (astrcmp_n(videoEncoder.c_str(), "x264", 4) == 0) {
2016-05-24 19:53:01 +00:00
UpdateRecordingSettings_x264_crf(crf);
} else if (videoEncoder == SIMPLE_ENCODER_QSV) {
UpdateRecordingSettings_qsv11(crf);
2016-10-10 19:01:40 +00:00
} else if (videoEncoder == SIMPLE_ENCODER_AMD) {
UpdateRecordingSettings_amd_cqp(crf);
2016-05-24 19:53:01 +00:00
} else if (videoEncoder == SIMPLE_ENCODER_NVENC) {
UpdateRecordingSettings_nvenc(crf);
2016-02-23 23:16:51 +00:00
}
2019-12-10 19:31:54 +00:00
UpdateRecordingAudioSettings();
2016-02-23 23:16:51 +00:00
}
inline void SimpleOutput::SetupOutputs()
{
SimpleOutput::Update();
obs_encoder_set_video(h264Streaming, obs_get_video());
2019-09-22 21:19:10 +00:00
obs_encoder_set_audio(aacStreaming, obs_get_audio());
2016-02-23 23:16:51 +00:00
if (usingRecordingPreset) {
if (ffmpegOutput) {
obs_output_set_media(fileOutput, obs_get_video(),
2019-09-22 21:19:10 +00:00
obs_get_audio());
2016-02-23 23:16:51 +00:00
} else {
obs_encoder_set_video(h264Recording, obs_get_video());
2019-09-22 21:19:10 +00:00
obs_encoder_set_audio(aacRecording, obs_get_audio());
2016-02-23 23:16:51 +00:00
}
}
}
2018-02-19 19:54:37 +00:00
const char *FindAudioEncoderFromCodec(const char *type)
{
const char *alt_enc_id = nullptr;
size_t i = 0;
while (obs_enum_encoder_types(i++, &alt_enc_id)) {
const char *codec = obs_get_encoder_codec(alt_enc_id);
if (strcmp(type, codec) == 0) {
return alt_enc_id;
}
}
return nullptr;
}
2020-10-01 20:15:25 +00:00
bool SimpleOutput::SetupStreaming(obs_service_t *service)
2016-02-23 23:16:51 +00:00
{
if (!Active())
SetupOutputs();
2019-07-27 12:47:10 +00:00
Auth *auth = main->GetAuth();
if (auth)
auth->OnStreamConfig();
2018-02-19 19:54:37 +00:00
/* --------------------- */
const char *type = obs_service_get_output_type(service);
2020-03-25 08:07:22 +00:00
if (!type) {
2018-02-19 19:54:37 +00:00
type = "rtmp_output";
2020-03-25 08:07:22 +00:00
const char *url = obs_service_get_url(service);
if (url != NULL &&
strncmp(url, RTMP_PROTOCOL, strlen(RTMP_PROTOCOL)) != 0) {
2020-10-01 20:15:25 +00:00
type = "ffmpeg_mpegts_muxer";
2020-03-25 08:07:22 +00:00
}
}
2018-02-19 19:54:37 +00:00
/* XXX: this is messy and disgusting and should be refactored */
if (outputType != type) {
streamDelayStarting.Disconnect();
streamStopping.Disconnect();
startStreaming.Disconnect();
stopStreaming.Disconnect();
2019-09-22 21:19:10 +00:00
streamOutput = obs_output_create(type, "simple_stream", nullptr,
nullptr);
2018-02-19 19:54:37 +00:00
if (!streamOutput) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"Creation of stream output type '%s' "
"failed!",
type);
2018-02-19 19:54:37 +00:00
return false;
}
obs_output_release(streamOutput);
streamDelayStarting.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "starting",
OBSStreamStarting, this);
2018-02-19 19:54:37 +00:00
streamStopping.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "stopping",
OBSStreamStopping, this);
2018-02-19 19:54:37 +00:00
startStreaming.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "start",
OBSStartStreaming, this);
2018-02-19 19:54:37 +00:00
stopStreaming.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "stop",
OBSStopStreaming, this);
2018-02-19 19:54:37 +00:00
2019-09-22 21:19:10 +00:00
bool isEncoded = obs_output_get_flags(streamOutput) &
OBS_OUTPUT_ENCODED;
2018-02-19 19:54:37 +00:00
2019-07-27 12:47:10 +00:00
if (isEncoded) {
const char *codec =
obs_output_get_supported_audio_codecs(
2019-09-22 21:19:10 +00:00
streamOutput);
2019-07-27 12:47:10 +00:00
if (!codec) {
blog(LOG_WARNING, "Failed to load audio codec");
2018-02-19 19:54:37 +00:00
return false;
2019-07-27 12:47:10 +00:00
}
2018-02-19 19:54:37 +00:00
2019-07-27 12:47:10 +00:00
if (strcmp(codec, "aac") != 0) {
2019-09-22 21:19:10 +00:00
const char *id =
FindAudioEncoderFromCodec(codec);
2019-07-27 12:47:10 +00:00
int audioBitrate = GetAudioBitrate();
obs_data_t *settings = obs_data_create();
obs_data_set_int(settings, "bitrate",
2019-09-22 21:19:10 +00:00
audioBitrate);
2019-07-27 12:47:10 +00:00
2019-09-22 21:19:10 +00:00
aacStreaming = obs_audio_encoder_create(
id, "alt_audio_enc", nullptr, 0,
nullptr);
2019-07-27 12:47:10 +00:00
obs_encoder_release(aacStreaming);
if (!aacStreaming)
return false;
obs_encoder_update(aacStreaming, settings);
obs_encoder_set_audio(aacStreaming,
2019-09-22 21:19:10 +00:00
obs_get_audio());
2019-07-27 12:47:10 +00:00
obs_data_release(settings);
}
2018-02-19 19:54:37 +00:00
}
outputType = type;
}
2016-02-23 23:16:51 +00:00
obs_output_set_video_encoder(streamOutput, h264Streaming);
obs_output_set_audio_encoder(streamOutput, aacStreaming, 0);
obs_output_set_service(streamOutput, service);
2020-10-01 20:15:25 +00:00
return true;
}
2016-02-23 23:16:51 +00:00
2020-10-01 20:15:25 +00:00
bool SimpleOutput::StartStreaming(obs_service_t *service)
{
2019-09-22 21:19:10 +00:00
bool reconnect = config_get_bool(main->Config(), "Output", "Reconnect");
int retryDelay =
config_get_uint(main->Config(), "Output", "RetryDelay");
int maxRetries =
config_get_uint(main->Config(), "Output", "MaxRetries");
bool useDelay =
config_get_bool(main->Config(), "Output", "DelayEnable");
int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
bool preserveDelay =
config_get_bool(main->Config(), "Output", "DelayPreserve");
const char *bindIP =
config_get_string(main->Config(), "Output", "BindIP");
2017-04-19 19:54:15 +00:00
bool enableNewSocketLoop = config_get_bool(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"NewSocketLoopEnable");
bool enableLowLatencyMode =
config_get_bool(main->Config(), "Output", "LowLatencyEnable");
bool enableDynBitrate =
config_get_bool(main->Config(), "Output", "DynamicBitrate");
2016-08-28 12:07:43 +00:00
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "bind_ip", bindIP);
2017-04-19 19:54:15 +00:00
obs_data_set_bool(settings, "new_socket_loop_enabled",
2019-09-22 21:19:10 +00:00
enableNewSocketLoop);
2017-04-19 19:54:15 +00:00
obs_data_set_bool(settings, "low_latency_mode_enabled",
2019-09-22 21:19:10 +00:00
enableLowLatencyMode);
obs_data_set_bool(settings, "dyn_bitrate", enableDynBitrate);
2016-08-28 12:07:43 +00:00
obs_output_update(streamOutput, settings);
obs_data_release(settings);
2016-02-23 23:16:51 +00:00
if (!reconnect)
maxRetries = 0;
obs_output_set_delay(streamOutput, useDelay ? delaySec : 0,
2019-09-22 21:19:10 +00:00
preserveDelay ? OBS_OUTPUT_DELAY_PRESERVE : 0);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
obs_output_set_reconnect_settings(streamOutput, maxRetries, retryDelay);
2016-02-23 23:16:51 +00:00
if (obs_output_start(streamOutput)) {
return true;
}
2018-02-19 19:54:37 +00:00
const char *error = obs_output_get_last_error(streamOutput);
2019-07-27 12:47:10 +00:00
bool hasLastError = error && *error;
if (hasLastError)
lastError = error;
else
lastError = string();
2018-02-19 19:54:37 +00:00
2020-10-01 20:15:25 +00:00
const char *type = obs_service_get_output_type(service);
2019-07-27 12:47:10 +00:00
blog(LOG_WARNING, "Stream output type '%s' failed to start!%s%s", type,
2019-09-22 21:19:10 +00:00
hasLastError ? " Last Error: " : "", hasLastError ? error : "");
2016-02-23 23:16:51 +00:00
return false;
}
2017-04-19 19:54:15 +00:00
void SimpleOutput::UpdateRecording()
2016-02-23 23:16:51 +00:00
{
2017-04-19 19:54:15 +00:00
if (replayBufferActive || recordingActive)
return;
2016-02-23 23:16:51 +00:00
if (usingRecordingPreset) {
if (!ffmpegOutput)
UpdateRecordingSettings();
} else if (!obs_output_active(streamOutput)) {
Update();
}
if (!Active())
SetupOutputs();
2017-04-19 19:54:15 +00:00
if (!ffmpegOutput) {
obs_output_set_video_encoder(fileOutput, h264Recording);
obs_output_set_audio_encoder(fileOutput, aacRecording, 0);
}
if (replayBuffer) {
obs_output_set_video_encoder(replayBuffer, h264Recording);
obs_output_set_audio_encoder(replayBuffer, aacRecording, 0);
}
recordingConfigured = true;
}
bool SimpleOutput::ConfigureRecording(bool updateReplayBuffer)
{
2019-09-22 21:19:10 +00:00
const char *path =
config_get_string(main->Config(), "SimpleOutput", "FilePath");
const char *format =
config_get_string(main->Config(), "SimpleOutput", "RecFormat");
2016-02-23 23:16:51 +00:00
const char *mux = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"MuxerCustom");
2016-02-23 23:16:51 +00:00
bool noSpace = config_get_bool(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"FileNameWithoutSpace");
2016-05-24 19:53:01 +00:00
const char *filenameFormat = config_get_string(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"FilenameFormatting");
bool overwriteIfExists =
config_get_bool(main->Config(), "Output", "OverwriteIfExists");
2017-04-19 19:54:15 +00:00
const char *rbPrefix = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"RecRBPrefix");
2017-04-19 19:54:15 +00:00
const char *rbSuffix = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"RecRBSuffix");
int rbTime =
config_get_int(main->Config(), "SimpleOutput", "RecRBTime");
int rbSize =
config_get_int(main->Config(), "SimpleOutput", "RecRBSize");
2016-02-23 23:16:51 +00:00
2020-10-01 20:15:25 +00:00
string f;
2016-02-23 23:16:51 +00:00
string strPath;
2017-04-19 19:54:15 +00:00
obs_data_t *settings = obs_data_create();
if (updateReplayBuffer) {
2020-10-01 20:15:25 +00:00
f = GetFormatString(filenameFormat, rbPrefix, rbSuffix);
strPath = GetOutputFilename(path, ffmpegOutput ? "avi" : format,
noSpace, overwriteIfExists,
f.c_str());
2017-04-19 19:54:15 +00:00
obs_data_set_string(settings, "directory", path);
obs_data_set_string(settings, "format", f.c_str());
obs_data_set_string(settings, "extension", format);
2018-12-16 16:14:58 +00:00
obs_data_set_bool(settings, "allow_spaces", !noSpace);
2017-04-19 19:54:15 +00:00
obs_data_set_int(settings, "max_time_sec", rbTime);
obs_data_set_int(settings, "max_size_mb",
2019-09-22 21:19:10 +00:00
usingRecordingPreset ? rbSize : 0);
2017-04-19 19:54:15 +00:00
} else {
2020-10-01 20:15:25 +00:00
f = GetFormatString(filenameFormat, nullptr, nullptr);
strPath = GetRecordingFilename(path,
ffmpegOutput ? "avi" : format,
noSpace, overwriteIfExists,
f.c_str(), ffmpegOutput);
2017-04-19 19:54:15 +00:00
obs_data_set_string(settings, ffmpegOutput ? "url" : "path",
2019-09-22 21:19:10 +00:00
strPath.c_str());
2016-02-23 23:16:51 +00:00
}
obs_data_set_string(settings, "muxer_settings", mux);
2017-04-19 19:54:15 +00:00
if (updateReplayBuffer)
obs_output_update(replayBuffer, settings);
else
obs_output_update(fileOutput, settings);
2016-02-23 23:16:51 +00:00
obs_data_release(settings);
2017-04-19 19:54:15 +00:00
return true;
}
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
bool SimpleOutput::StartRecording()
{
UpdateRecording();
if (!ConfigureRecording(false))
return false;
2019-09-22 21:19:10 +00:00
if (!obs_output_start(fileOutput)) {
2018-02-19 19:54:37 +00:00
QString error_reason;
const char *error = obs_output_get_last_error(fileOutput);
if (error)
error_reason = QT_UTF8(error);
else
error_reason = QTStr("Output.StartFailedGeneric");
2017-04-19 19:54:15 +00:00
QMessageBox::critical(main,
2019-09-22 21:19:10 +00:00
QTStr("Output.StartRecordingFailed"),
error_reason);
2017-04-19 19:54:15 +00:00
return false;
2016-02-23 23:16:51 +00:00
}
2017-04-19 19:54:15 +00:00
return true;
}
bool SimpleOutput::StartReplayBuffer()
{
UpdateRecording();
if (!ConfigureRecording(true))
return false;
if (!obs_output_start(replayBuffer)) {
2019-09-22 21:19:10 +00:00
QMessageBox::critical(main, QTStr("Output.StartReplayFailed"),
QTStr("Output.StartFailedGeneric"));
2017-04-19 19:54:15 +00:00
return false;
}
return true;
2016-02-23 23:16:51 +00:00
}
2016-10-10 19:01:40 +00:00
void SimpleOutput::StopStreaming(bool force)
2016-02-23 23:16:51 +00:00
{
2016-10-10 19:01:40 +00:00
if (force)
obs_output_force_stop(streamOutput);
else
obs_output_stop(streamOutput);
2016-02-23 23:16:51 +00:00
}
2016-10-10 19:01:40 +00:00
void SimpleOutput::StopRecording(bool force)
2016-02-23 23:16:51 +00:00
{
2016-10-10 19:01:40 +00:00
if (force)
obs_output_force_stop(fileOutput);
else
obs_output_stop(fileOutput);
2016-02-23 23:16:51 +00:00
}
2017-04-19 19:54:15 +00:00
void SimpleOutput::StopReplayBuffer(bool force)
{
if (force)
obs_output_force_stop(replayBuffer);
else
obs_output_stop(replayBuffer);
}
2016-02-23 23:16:51 +00:00
bool SimpleOutput::StreamingActive() const
{
return obs_output_active(streamOutput);
}
bool SimpleOutput::RecordingActive() const
{
return obs_output_active(fileOutput);
}
2017-04-19 19:54:15 +00:00
bool SimpleOutput::ReplayBufferActive() const
{
return obs_output_active(replayBuffer);
}
2016-02-23 23:16:51 +00:00
/* ------------------------------------------------------------------------ */
struct AdvancedOutput : BasicOutputHandler {
2019-09-22 21:19:10 +00:00
OBSEncoder streamAudioEnc;
OBSEncoder aacTrack[MAX_AUDIO_MIXES];
OBSEncoder h264Streaming;
OBSEncoder h264Recording;
2018-02-19 19:54:37 +00:00
2019-09-22 21:19:10 +00:00
bool ffmpegOutput;
bool ffmpegRecording;
bool useStreamEncoder;
bool usesBitrate = false;
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
string aacEncoderID[MAX_AUDIO_MIXES];
2016-02-23 23:16:51 +00:00
AdvancedOutput(OBSBasic *main_);
inline void UpdateStreamSettings();
inline void UpdateRecordingSettings();
inline void UpdateAudioSettings();
virtual void Update() override;
inline void SetupStreaming();
inline void SetupRecording();
inline void SetupFFmpeg();
2020-10-01 20:15:25 +00:00
void SetupOutputs() override;
2016-02-23 23:16:51 +00:00
int GetAudioBitrate(size_t i) const;
2020-10-01 20:15:25 +00:00
virtual bool SetupStreaming(obs_service_t *service) override;
2016-02-23 23:16:51 +00:00
virtual bool StartStreaming(obs_service_t *service) override;
virtual bool StartRecording() override;
2018-02-19 19:54:37 +00:00
virtual bool StartReplayBuffer() override;
2016-10-10 19:01:40 +00:00
virtual void StopStreaming(bool force) override;
virtual void StopRecording(bool force) override;
2018-02-19 19:54:37 +00:00
virtual void StopReplayBuffer(bool force) override;
2016-02-23 23:16:51 +00:00
virtual bool StreamingActive() const override;
virtual bool RecordingActive() const override;
2018-02-19 19:54:37 +00:00
virtual bool ReplayBufferActive() const override;
2016-02-23 23:16:51 +00:00
};
static OBSData GetDataFromJsonFile(const char *jsonFile)
{
char fullPath[512];
2018-12-16 16:14:58 +00:00
obs_data_t *data = nullptr;
2016-02-23 23:16:51 +00:00
int ret = GetProfilePath(fullPath, sizeof(fullPath), jsonFile);
if (ret > 0) {
BPtr<char> jsonData = os_quick_read_utf8_file(fullPath);
if (!!jsonData) {
2018-12-16 16:14:58 +00:00
data = obs_data_create_from_json(jsonData);
2016-02-23 23:16:51 +00:00
}
}
2018-12-16 16:14:58 +00:00
if (!data)
data = obs_data_create();
OBSData dataRet(data);
obs_data_release(data);
return dataRet;
}
static void ApplyEncoderDefaults(OBSData &settings,
2019-09-22 21:19:10 +00:00
const obs_encoder_t *encoder)
2018-12-16 16:14:58 +00:00
{
OBSData dataRet = obs_encoder_get_defaults(encoder);
obs_data_release(dataRet);
if (!!settings)
obs_data_apply(dataRet, settings);
settings = std::move(dataRet);
2016-02-23 23:16:51 +00:00
}
AdvancedOutput::AdvancedOutput(OBSBasic *main_) : BasicOutputHandler(main_)
{
2019-09-22 21:19:10 +00:00
const char *recType =
config_get_string(main->Config(), "AdvOut", "RecType");
const char *streamEncoder =
config_get_string(main->Config(), "AdvOut", "Encoder");
const char *recordEncoder =
config_get_string(main->Config(), "AdvOut", "RecEncoder");
2016-02-23 23:16:51 +00:00
ffmpegOutput = astrcmpi(recType, "FFmpeg") == 0;
2019-09-22 21:19:10 +00:00
ffmpegRecording =
ffmpegOutput &&
2016-02-23 23:16:51 +00:00
config_get_bool(main->Config(), "AdvOut", "FFOutputToFile");
useStreamEncoder = astrcmpi(recordEncoder, "none") == 0;
OBSData streamEncSettings = GetDataFromJsonFile("streamEncoder.json");
OBSData recordEncSettings = GetDataFromJsonFile("recordEncoder.json");
2018-02-19 19:54:37 +00:00
const char *rate_control = obs_data_get_string(
2019-09-22 21:19:10 +00:00
useStreamEncoder ? streamEncSettings : recordEncSettings,
"rate_control");
2018-02-19 19:54:37 +00:00
if (!rate_control)
rate_control = "";
usesBitrate = astrcmpi(rate_control, "CBR") == 0 ||
2019-09-22 21:19:10 +00:00
astrcmpi(rate_control, "VBR") == 0 ||
astrcmpi(rate_control, "ABR") == 0;
2016-02-23 23:16:51 +00:00
if (ffmpegOutput) {
2019-09-22 21:19:10 +00:00
fileOutput = obs_output_create(
"ffmpeg_output", "adv_ffmpeg_output", nullptr, nullptr);
2016-02-23 23:16:51 +00:00
if (!fileOutput)
throw "Failed to create recording FFmpeg output "
"(advanced output)";
obs_output_release(fileOutput);
} else {
2019-09-22 21:19:10 +00:00
bool useReplayBuffer =
config_get_bool(main->Config(), "AdvOut", "RecRB");
2018-02-19 19:54:37 +00:00
if (useReplayBuffer) {
2019-09-22 21:19:10 +00:00
const char *str = config_get_string(
main->Config(), "Hotkeys", "ReplayBuffer");
2018-02-19 19:54:37 +00:00
obs_data_t *hotkey = obs_data_create_from_json(str);
replayBuffer = obs_output_create("replay_buffer",
2019-09-22 21:19:10 +00:00
Str("ReplayBuffer"),
nullptr, hotkey);
2018-02-19 19:54:37 +00:00
obs_data_release(hotkey);
if (!replayBuffer)
throw "Failed to create replay buffer output "
2019-09-22 21:19:10 +00:00
"(simple output)";
2018-02-19 19:54:37 +00:00
obs_output_release(replayBuffer);
signal_handler_t *signal =
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(replayBuffer);
2018-02-19 19:54:37 +00:00
startReplayBuffer.Connect(signal, "start",
2019-09-22 21:19:10 +00:00
OBSStartReplayBuffer, this);
2018-02-19 19:54:37 +00:00
stopReplayBuffer.Connect(signal, "stop",
2019-09-22 21:19:10 +00:00
OBSStopReplayBuffer, this);
2018-02-19 19:54:37 +00:00
replayBufferStopping.Connect(signal, "stopping",
2019-09-22 21:19:10 +00:00
OBSReplayBufferStopping,
this);
2018-02-19 19:54:37 +00:00
}
2019-09-22 21:19:10 +00:00
fileOutput = obs_output_create(
"ffmpeg_muxer", "adv_file_output", nullptr, nullptr);
2016-02-23 23:16:51 +00:00
if (!fileOutput)
throw "Failed to create recording output "
"(advanced output)";
obs_output_release(fileOutput);
if (!useStreamEncoder) {
2019-09-22 21:19:10 +00:00
h264Recording = obs_video_encoder_create(
recordEncoder, "recording_h264",
recordEncSettings, nullptr);
2016-02-23 23:16:51 +00:00
if (!h264Recording)
throw "Failed to create recording h264 "
"encoder (advanced output)";
obs_encoder_release(h264Recording);
}
}
2019-09-22 21:19:10 +00:00
h264Streaming = obs_video_encoder_create(
streamEncoder, "streaming_h264", streamEncSettings, nullptr);
2016-02-23 23:16:51 +00:00
if (!h264Streaming)
throw "Failed to create streaming h264 encoder "
"(advanced output)";
obs_encoder_release(h264Streaming);
2017-04-19 19:54:15 +00:00
for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
2016-02-23 23:16:51 +00:00
char name[9];
sprintf(name, "adv_aac%d", i);
if (!CreateAACEncoder(aacTrack[i], aacEncoderID[i],
2019-09-22 21:19:10 +00:00
GetAudioBitrate(i), name, i))
2016-02-23 23:16:51 +00:00
throw "Failed to create audio encoder "
"(advanced output)";
}
2019-09-22 21:19:10 +00:00
std::string id;
int streamTrack =
config_get_int(main->Config(), "AdvOut", "TrackIndex") - 1;
if (!CreateAACEncoder(streamAudioEnc, id, GetAudioBitrate(streamTrack),
"avc_aac_stream", streamTrack))
throw "Failed to create streaming audio encoder "
"(advanced output)";
2016-02-23 23:16:51 +00:00
startRecording.Connect(obs_output_get_signal_handler(fileOutput),
2019-09-22 21:19:10 +00:00
"start", OBSStartRecording, this);
stopRecording.Connect(obs_output_get_signal_handler(fileOutput), "stop",
OBSStopRecording, this);
2016-08-28 12:07:43 +00:00
recordStopping.Connect(obs_output_get_signal_handler(fileOutput),
2019-09-22 21:19:10 +00:00
"stopping", OBSRecordStopping, this);
2016-02-23 23:16:51 +00:00
}
void AdvancedOutput::UpdateStreamSettings()
{
bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
"ApplyServiceSettings");
bool dynBitrate =
config_get_bool(main->Config(), "Output", "DynamicBitrate");
const char *streamEncoder =
config_get_string(main->Config(), "AdvOut", "Encoder");
2016-02-23 23:16:51 +00:00
OBSData settings = GetDataFromJsonFile("streamEncoder.json");
2018-12-16 16:14:58 +00:00
ApplyEncoderDefaults(settings, h264Streaming);
2016-02-23 23:16:51 +00:00
if (applyServiceSettings)
2019-09-22 21:19:10 +00:00
obs_service_apply_encoder_settings(main->GetService(), settings,
nullptr);
if (dynBitrate && astrcmpi(streamEncoder, "jim_nvenc") == 0)
obs_data_set_bool(settings, "lookahead", false);
2016-02-23 23:16:51 +00:00
video_t *video = obs_get_video();
enum video_format format = video_output_get_format(video);
if (format != VIDEO_FORMAT_NV12 && format != VIDEO_FORMAT_I420)
obs_encoder_set_preferred_video_format(h264Streaming,
2019-09-22 21:19:10 +00:00
VIDEO_FORMAT_NV12);
2016-02-23 23:16:51 +00:00
obs_encoder_update(h264Streaming, settings);
}
inline void AdvancedOutput::UpdateRecordingSettings()
{
OBSData settings = GetDataFromJsonFile("recordEncoder.json");
obs_encoder_update(h264Recording, settings);
}
void AdvancedOutput::Update()
{
UpdateStreamSettings();
if (!useStreamEncoder && !ffmpegOutput)
UpdateRecordingSettings();
UpdateAudioSettings();
}
inline void AdvancedOutput::SetupStreaming()
{
2019-09-22 21:19:10 +00:00
bool rescale = config_get_bool(main->Config(), "AdvOut", "Rescale");
const char *rescaleRes =
config_get_string(main->Config(), "AdvOut", "RescaleRes");
2016-02-23 23:16:51 +00:00
unsigned int cx = 0;
unsigned int cy = 0;
if (rescale && rescaleRes && *rescaleRes) {
if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
cx = 0;
cy = 0;
}
}
2020-03-25 08:07:22 +00:00
obs_output_set_audio_encoder(streamOutput, streamAudioEnc, 0);
2016-02-23 23:16:51 +00:00
obs_encoder_set_scaled_size(h264Streaming, cx, cy);
obs_encoder_set_video(h264Streaming, obs_get_video());
}
inline void AdvancedOutput::SetupRecording()
{
2019-09-22 21:19:10 +00:00
const char *path =
config_get_string(main->Config(), "AdvOut", "RecFilePath");
const char *mux =
config_get_string(main->Config(), "AdvOut", "RecMuxerCustom");
bool rescale = config_get_bool(main->Config(), "AdvOut", "RecRescale");
const char *rescaleRes =
config_get_string(main->Config(), "AdvOut", "RecRescaleRes");
2020-03-25 08:07:22 +00:00
int tracks;
const char *recFormat =
config_get_string(main->Config(), "AdvOut", "RecFormat");
bool flv = strcmp(recFormat, "flv") == 0;
if (flv)
tracks = config_get_int(main->Config(), "AdvOut", "FLVTrack");
else
tracks = config_get_int(main->Config(), "AdvOut", "RecTracks");
2016-02-23 23:16:51 +00:00
obs_data_t *settings = obs_data_create();
unsigned int cx = 0;
unsigned int cy = 0;
int idx = 0;
2019-09-22 21:19:10 +00:00
if (tracks == 0)
tracks = config_get_int(main->Config(), "AdvOut", "TrackIndex");
2016-02-23 23:16:51 +00:00
if (useStreamEncoder) {
obs_output_set_video_encoder(fileOutput, h264Streaming);
2018-02-19 19:54:37 +00:00
if (replayBuffer)
obs_output_set_video_encoder(replayBuffer,
2019-09-22 21:19:10 +00:00
h264Streaming);
2016-02-23 23:16:51 +00:00
} else {
if (rescale && rescaleRes && *rescaleRes) {
if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
cx = 0;
cy = 0;
}
}
obs_encoder_set_scaled_size(h264Recording, cx, cy);
obs_encoder_set_video(h264Recording, obs_get_video());
obs_output_set_video_encoder(fileOutput, h264Recording);
2018-02-19 19:54:37 +00:00
if (replayBuffer)
obs_output_set_video_encoder(replayBuffer,
2019-09-22 21:19:10 +00:00
h264Recording);
2016-02-23 23:16:51 +00:00
}
2020-03-25 08:07:22 +00:00
if (!flv) {
for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
if ((tracks & (1 << i)) != 0) {
obs_output_set_audio_encoder(fileOutput,
2019-09-22 21:19:10 +00:00
aacTrack[i], idx);
2020-03-25 08:07:22 +00:00
if (replayBuffer)
obs_output_set_audio_encoder(
replayBuffer, aacTrack[i], idx);
idx++;
}
2016-02-23 23:16:51 +00:00
}
2020-03-25 08:07:22 +00:00
} else if (flv && tracks != 0) {
obs_output_set_audio_encoder(fileOutput, aacTrack[tracks - 1],
idx);
if (replayBuffer)
obs_output_set_audio_encoder(replayBuffer,
aacTrack[tracks - 1], idx);
2016-02-23 23:16:51 +00:00
}
obs_data_set_string(settings, "path", path);
obs_data_set_string(settings, "muxer_settings", mux);
obs_output_update(fileOutput, settings);
2018-02-19 19:54:37 +00:00
if (replayBuffer)
obs_output_update(replayBuffer, settings);
2016-02-23 23:16:51 +00:00
obs_data_release(settings);
}
inline void AdvancedOutput::SetupFFmpeg()
{
const char *url = config_get_string(main->Config(), "AdvOut", "FFURL");
2019-09-22 21:19:10 +00:00
int vBitrate = config_get_int(main->Config(), "AdvOut", "FFVBitrate");
int gopSize = config_get_int(main->Config(), "AdvOut", "FFVGOPSize");
bool rescale = config_get_bool(main->Config(), "AdvOut", "FFRescale");
const char *rescaleRes =
config_get_string(main->Config(), "AdvOut", "FFRescaleRes");
const char *formatName =
config_get_string(main->Config(), "AdvOut", "FFFormat");
const char *mimeType =
config_get_string(main->Config(), "AdvOut", "FFFormatMimeType");
const char *muxCustom =
config_get_string(main->Config(), "AdvOut", "FFMCustom");
const char *vEncoder =
config_get_string(main->Config(), "AdvOut", "FFVEncoder");
int vEncoderId =
config_get_int(main->Config(), "AdvOut", "FFVEncoderId");
const char *vEncCustom =
config_get_string(main->Config(), "AdvOut", "FFVCustom");
int aBitrate = config_get_int(main->Config(), "AdvOut", "FFABitrate");
int aMixes = config_get_int(main->Config(), "AdvOut", "FFAudioMixes");
const char *aEncoder =
config_get_string(main->Config(), "AdvOut", "FFAEncoder");
int aEncoderId =
config_get_int(main->Config(), "AdvOut", "FFAEncoderId");
const char *aEncCustom =
config_get_string(main->Config(), "AdvOut", "FFACustom");
2016-02-23 23:16:51 +00:00
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "url", url);
obs_data_set_string(settings, "format_name", formatName);
obs_data_set_string(settings, "format_mime_type", mimeType);
obs_data_set_string(settings, "muxer_settings", muxCustom);
2017-04-19 19:54:15 +00:00
obs_data_set_int(settings, "gop_size", gopSize);
2016-02-23 23:16:51 +00:00
obs_data_set_int(settings, "video_bitrate", vBitrate);
obs_data_set_string(settings, "video_encoder", vEncoder);
obs_data_set_int(settings, "video_encoder_id", vEncoderId);
obs_data_set_string(settings, "video_settings", vEncCustom);
obs_data_set_int(settings, "audio_bitrate", aBitrate);
obs_data_set_string(settings, "audio_encoder", aEncoder);
obs_data_set_int(settings, "audio_encoder_id", aEncoderId);
obs_data_set_string(settings, "audio_settings", aEncCustom);
if (rescale && rescaleRes && *rescaleRes) {
int width;
int height;
int val = sscanf(rescaleRes, "%dx%d", &width, &height);
if (val == 2 && width && height) {
obs_data_set_int(settings, "scale_width", width);
obs_data_set_int(settings, "scale_height", height);
}
}
2019-07-27 12:47:10 +00:00
obs_output_set_mixers(fileOutput, aMixes);
2016-02-23 23:16:51 +00:00
obs_output_set_media(fileOutput, obs_get_video(), obs_get_audio());
obs_output_update(fileOutput, settings);
obs_data_release(settings);
}
static inline void SetEncoderName(obs_encoder_t *encoder, const char *name,
2019-09-22 21:19:10 +00:00
const char *defaultName)
2016-02-23 23:16:51 +00:00
{
obs_encoder_set_name(encoder, (name && *name) ? name : defaultName);
}
inline void AdvancedOutput::UpdateAudioSettings()
{
bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
"ApplyServiceSettings");
int streamTrackIndex =
config_get_int(main->Config(), "AdvOut", "TrackIndex");
2017-04-19 19:54:15 +00:00
obs_data_t *settings[MAX_AUDIO_MIXES];
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
for (size_t i = 0; i < MAX_AUDIO_MIXES; i++) {
2016-02-23 23:16:51 +00:00
settings[i] = obs_data_create();
obs_data_set_int(settings[i], "bitrate", GetAudioBitrate(i));
}
2017-04-19 19:54:15 +00:00
for (size_t i = 0; i < MAX_AUDIO_MIXES; i++) {
string cfg_name = "Track";
cfg_name += to_string((int)i + 1);
cfg_name += "Name";
const char *name = config_get_string(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
cfg_name.c_str());
2016-02-23 23:16:51 +00:00
2017-04-19 19:54:15 +00:00
string def_name = "Track";
def_name += to_string((int)i + 1);
SetEncoderName(aacTrack[i], name, def_name.c_str());
}
for (size_t i = 0; i < MAX_AUDIO_MIXES; i++) {
2016-02-23 23:16:51 +00:00
obs_encoder_update(aacTrack[i], settings[i]);
2019-09-22 21:19:10 +00:00
if ((int)(i + 1) == streamTrackIndex) {
if (applyServiceSettings) {
obs_service_apply_encoder_settings(
main->GetService(), nullptr,
settings[i]);
}
obs_encoder_update(streamAudioEnc, settings[i]);
}
2016-02-23 23:16:51 +00:00
obs_data_release(settings[i]);
}
}
void AdvancedOutput::SetupOutputs()
{
obs_encoder_set_video(h264Streaming, obs_get_video());
if (h264Recording)
obs_encoder_set_video(h264Recording, obs_get_video());
2017-04-19 19:54:15 +00:00
for (size_t i = 0; i < MAX_AUDIO_MIXES; i++)
obs_encoder_set_audio(aacTrack[i], obs_get_audio());
2019-09-22 21:19:10 +00:00
obs_encoder_set_audio(streamAudioEnc, obs_get_audio());
2016-02-23 23:16:51 +00:00
SetupStreaming();
if (ffmpegOutput)
SetupFFmpeg();
else
SetupRecording();
}
int AdvancedOutput::GetAudioBitrate(size_t i) const
{
2017-04-19 19:54:15 +00:00
static const char *names[] = {
2019-09-22 21:19:10 +00:00
"Track1Bitrate", "Track2Bitrate", "Track3Bitrate",
"Track4Bitrate", "Track5Bitrate", "Track6Bitrate",
2016-02-23 23:16:51 +00:00
};
int bitrate = (int)config_get_uint(main->Config(), "AdvOut", names[i]);
return FindClosestAvailableAACBitrate(bitrate);
}
2020-10-01 20:15:25 +00:00
bool AdvancedOutput::SetupStreaming(obs_service_t *service)
2016-02-23 23:16:51 +00:00
{
2019-09-22 21:19:10 +00:00
int streamTrack =
config_get_int(main->Config(), "AdvOut", "TrackIndex") - 1;
2016-02-23 23:16:51 +00:00
if (!useStreamEncoder ||
(!ffmpegOutput && !obs_output_active(fileOutput))) {
UpdateStreamSettings();
}
UpdateAudioSettings();
if (!Active())
SetupOutputs();
2019-07-27 12:47:10 +00:00
Auth *auth = main->GetAuth();
if (auth)
auth->OnStreamConfig();
2018-02-19 19:54:37 +00:00
/* --------------------- */
const char *type = obs_service_get_output_type(service);
2020-03-25 08:07:22 +00:00
if (!type) {
2018-02-19 19:54:37 +00:00
type = "rtmp_output";
2020-03-25 08:07:22 +00:00
const char *url = obs_service_get_url(service);
if (url != NULL &&
strncmp(url, RTMP_PROTOCOL, strlen(RTMP_PROTOCOL)) != 0) {
2020-10-01 20:15:25 +00:00
type = "ffmpeg_mpegts_muxer";
2020-03-25 08:07:22 +00:00
}
}
2018-02-19 19:54:37 +00:00
/* XXX: this is messy and disgusting and should be refactored */
if (outputType != type) {
streamDelayStarting.Disconnect();
streamStopping.Disconnect();
startStreaming.Disconnect();
stopStreaming.Disconnect();
2019-09-22 21:19:10 +00:00
streamOutput =
obs_output_create(type, "adv_stream", nullptr, nullptr);
2018-02-19 19:54:37 +00:00
if (!streamOutput) {
2019-09-22 21:19:10 +00:00
blog(LOG_WARNING,
"Creation of stream output type '%s' "
"failed!",
type);
2018-02-19 19:54:37 +00:00
return false;
}
obs_output_release(streamOutput);
streamDelayStarting.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "starting",
OBSStreamStarting, this);
2018-02-19 19:54:37 +00:00
streamStopping.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "stopping",
OBSStreamStopping, this);
2018-02-19 19:54:37 +00:00
startStreaming.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "start",
OBSStartStreaming, this);
2018-02-19 19:54:37 +00:00
stopStreaming.Connect(
2019-09-22 21:19:10 +00:00
obs_output_get_signal_handler(streamOutput), "stop",
OBSStopStreaming, this);
2018-02-19 19:54:37 +00:00
2019-09-22 21:19:10 +00:00
bool isEncoded = obs_output_get_flags(streamOutput) &
OBS_OUTPUT_ENCODED;
2018-02-19 19:54:37 +00:00
2019-07-27 12:47:10 +00:00
if (isEncoded) {
const char *codec =
obs_output_get_supported_audio_codecs(
2019-09-22 21:19:10 +00:00
streamOutput);
2019-07-27 12:47:10 +00:00
if (!codec) {
blog(LOG_WARNING, "Failed to load audio codec");
2018-02-19 19:54:37 +00:00
return false;
2019-07-27 12:47:10 +00:00
}
2018-02-19 19:54:37 +00:00
2019-09-22 21:19:10 +00:00
if (strcmp(codec, "aac") != 0) {
OBSData settings = obs_encoder_get_settings(
streamAudioEnc);
obs_data_release(settings);
2019-07-27 12:47:10 +00:00
const char *id =
FindAudioEncoderFromCodec(codec);
2019-09-22 21:19:10 +00:00
streamAudioEnc = obs_audio_encoder_create(
id, "alt_audio_enc", nullptr,
streamTrack, nullptr);
2019-07-27 12:47:10 +00:00
if (!streamAudioEnc)
return false;
2019-09-22 21:19:10 +00:00
obs_encoder_release(streamAudioEnc);
2019-07-27 12:47:10 +00:00
obs_encoder_update(streamAudioEnc, settings);
obs_encoder_set_audio(streamAudioEnc,
2019-09-22 21:19:10 +00:00
obs_get_audio());
2019-07-27 12:47:10 +00:00
}
2018-02-19 19:54:37 +00:00
}
outputType = type;
}
obs_output_set_video_encoder(streamOutput, h264Streaming);
obs_output_set_audio_encoder(streamOutput, streamAudioEnc, 0);
2020-10-01 20:15:25 +00:00
return true;
}
2018-02-19 19:54:37 +00:00
2020-10-01 20:15:25 +00:00
bool AdvancedOutput::StartStreaming(obs_service_t *service)
{
2016-02-23 23:16:51 +00:00
obs_output_set_service(streamOutput, service);
bool reconnect = config_get_bool(main->Config(), "Output", "Reconnect");
int retryDelay = config_get_int(main->Config(), "Output", "RetryDelay");
int maxRetries = config_get_int(main->Config(), "Output", "MaxRetries");
2019-09-22 21:19:10 +00:00
bool useDelay =
config_get_bool(main->Config(), "Output", "DelayEnable");
int delaySec = config_get_int(main->Config(), "Output", "DelaySec");
bool preserveDelay =
config_get_bool(main->Config(), "Output", "DelayPreserve");
const char *bindIP =
config_get_string(main->Config(), "Output", "BindIP");
2017-04-19 19:54:15 +00:00
bool enableNewSocketLoop = config_get_bool(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"NewSocketLoopEnable");
bool enableLowLatencyMode =
config_get_bool(main->Config(), "Output", "LowLatencyEnable");
bool enableDynBitrate =
config_get_bool(main->Config(), "Output", "DynamicBitrate");
2016-08-28 12:07:43 +00:00
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "bind_ip", bindIP);
2017-04-19 19:54:15 +00:00
obs_data_set_bool(settings, "new_socket_loop_enabled",
2019-09-22 21:19:10 +00:00
enableNewSocketLoop);
2017-04-19 19:54:15 +00:00
obs_data_set_bool(settings, "low_latency_mode_enabled",
2019-09-22 21:19:10 +00:00
enableLowLatencyMode);
obs_data_set_bool(settings, "dyn_bitrate", enableDynBitrate);
2016-08-28 12:07:43 +00:00
obs_output_update(streamOutput, settings);
obs_data_release(settings);
2016-02-23 23:16:51 +00:00
if (!reconnect)
maxRetries = 0;
obs_output_set_delay(streamOutput, useDelay ? delaySec : 0,
2019-09-22 21:19:10 +00:00
preserveDelay ? OBS_OUTPUT_DELAY_PRESERVE : 0);
2016-02-23 23:16:51 +00:00
2019-09-22 21:19:10 +00:00
obs_output_set_reconnect_settings(streamOutput, maxRetries, retryDelay);
2016-02-23 23:16:51 +00:00
if (obs_output_start(streamOutput)) {
return true;
}
2018-02-19 19:54:37 +00:00
const char *error = obs_output_get_last_error(streamOutput);
2019-07-27 12:47:10 +00:00
bool hasLastError = error && *error;
if (hasLastError)
lastError = error;
else
lastError = string();
2018-02-19 19:54:37 +00:00
2020-10-01 20:15:25 +00:00
const char *type = obs_service_get_output_type(service);
2019-07-27 12:47:10 +00:00
blog(LOG_WARNING, "Stream output type '%s' failed to start!%s%s", type,
2019-09-22 21:19:10 +00:00
hasLastError ? " Last Error: " : "", hasLastError ? error : "");
2016-02-23 23:16:51 +00:00
return false;
}
bool AdvancedOutput::StartRecording()
{
const char *path;
2016-05-24 19:53:01 +00:00
const char *recFormat;
const char *filenameFormat;
2016-02-23 23:16:51 +00:00
bool noSpace = false;
2016-05-24 19:53:01 +00:00
bool overwriteIfExists = false;
2016-02-23 23:16:51 +00:00
if (!useStreamEncoder) {
if (!ffmpegOutput) {
UpdateRecordingSettings();
}
} else if (!obs_output_active(streamOutput)) {
UpdateStreamSettings();
}
UpdateAudioSettings();
if (!Active())
SetupOutputs();
if (!ffmpegOutput || ffmpegRecording) {
path = config_get_string(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
ffmpegRecording ? "FFFilePath"
: "RecFilePath");
2016-05-24 19:53:01 +00:00
recFormat = config_get_string(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
ffmpegRecording ? "FFExtension"
: "RecFormat");
2016-05-24 19:53:01 +00:00
filenameFormat = config_get_string(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"FilenameFormatting");
2016-05-24 19:53:01 +00:00
overwriteIfExists = config_get_bool(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"OverwriteIfExists");
2016-02-23 23:16:51 +00:00
noSpace = config_get_bool(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
ffmpegRecording
? "FFFileNameWithoutSpace"
: "RecFileNameWithoutSpace");
2016-02-23 23:16:51 +00:00
2020-10-01 20:15:25 +00:00
string strPath = GetRecordingFilename(path, recFormat, noSpace,
overwriteIfExists,
filenameFormat,
ffmpegRecording);
2016-02-23 23:16:51 +00:00
obs_data_t *settings = obs_data_create();
2019-09-22 21:19:10 +00:00
obs_data_set_string(settings, ffmpegRecording ? "url" : "path",
strPath.c_str());
2016-02-23 23:16:51 +00:00
obs_output_update(fileOutput, settings);
obs_data_release(settings);
}
2017-04-19 19:54:15 +00:00
if (!obs_output_start(fileOutput)) {
2018-02-19 19:54:37 +00:00
QString error_reason;
const char *error = obs_output_get_last_error(fileOutput);
if (error)
error_reason = QT_UTF8(error);
else
error_reason = QTStr("Output.StartFailedGeneric");
QMessageBox::critical(main,
2019-09-22 21:19:10 +00:00
QTStr("Output.StartRecordingFailed"),
error_reason);
2018-02-19 19:54:37 +00:00
return false;
}
return true;
}
bool AdvancedOutput::StartReplayBuffer()
{
const char *path;
const char *recFormat;
const char *filenameFormat;
bool noSpace = false;
bool overwriteIfExists = false;
const char *rbPrefix;
const char *rbSuffix;
int rbTime;
int rbSize;
if (!useStreamEncoder) {
if (!ffmpegOutput)
UpdateRecordingSettings();
} else if (!obs_output_active(streamOutput)) {
UpdateStreamSettings();
}
UpdateAudioSettings();
if (!Active())
SetupOutputs();
if (!ffmpegOutput || ffmpegRecording) {
path = config_get_string(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
ffmpegRecording ? "FFFilePath"
: "RecFilePath");
2018-02-19 19:54:37 +00:00
recFormat = config_get_string(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
ffmpegRecording ? "FFExtension"
: "RecFormat");
2018-02-19 19:54:37 +00:00
filenameFormat = config_get_string(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"FilenameFormatting");
2018-02-19 19:54:37 +00:00
overwriteIfExists = config_get_bool(main->Config(), "Output",
2019-09-22 21:19:10 +00:00
"OverwriteIfExists");
2018-02-19 19:54:37 +00:00
noSpace = config_get_bool(main->Config(), "AdvOut",
2019-09-22 21:19:10 +00:00
ffmpegRecording
? "FFFileNameWithoutSpace"
: "RecFileNameWithoutSpace");
2018-02-19 19:54:37 +00:00
rbPrefix = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"RecRBPrefix");
2018-02-19 19:54:37 +00:00
rbSuffix = config_get_string(main->Config(), "SimpleOutput",
2019-09-22 21:19:10 +00:00
"RecRBSuffix");
rbTime = config_get_int(main->Config(), "AdvOut", "RecRBTime");
rbSize = config_get_int(main->Config(), "AdvOut", "RecRBSize");
2018-02-19 19:54:37 +00:00
2020-10-01 20:15:25 +00:00
string f = GetFormatString(filenameFormat, rbPrefix, rbSuffix);
string strPath = GetOutputFilename(
path, recFormat, noSpace, overwriteIfExists, f.c_str());
2018-02-19 19:54:37 +00:00
obs_data_t *settings = obs_data_create();
obs_data_set_string(settings, "directory", path);
obs_data_set_string(settings, "format", f.c_str());
obs_data_set_string(settings, "extension", recFormat);
2018-12-16 16:14:58 +00:00
obs_data_set_bool(settings, "allow_spaces", !noSpace);
2018-02-19 19:54:37 +00:00
obs_data_set_int(settings, "max_time_sec", rbTime);
obs_data_set_int(settings, "max_size_mb",
2019-09-22 21:19:10 +00:00
usesBitrate ? 0 : rbSize);
2018-02-19 19:54:37 +00:00
obs_output_update(replayBuffer, settings);
obs_data_release(settings);
}
if (!obs_output_start(replayBuffer)) {
2017-04-19 19:54:15 +00:00
QMessageBox::critical(main,
2019-09-22 21:19:10 +00:00
QTStr("Output.StartRecordingFailed"),
QTStr("Output.StartFailedGeneric"));
2017-04-19 19:54:15 +00:00
return false;
2016-02-23 23:16:51 +00:00
}
2017-04-19 19:54:15 +00:00
return true;
2016-02-23 23:16:51 +00:00
}
2016-10-10 19:01:40 +00:00
void AdvancedOutput::StopStreaming(bool force)
2016-02-23 23:16:51 +00:00
{
2016-10-10 19:01:40 +00:00
if (force)
obs_output_force_stop(streamOutput);
else
obs_output_stop(streamOutput);
2016-02-23 23:16:51 +00:00
}
2016-10-10 19:01:40 +00:00
void AdvancedOutput::StopRecording(bool force)
2016-02-23 23:16:51 +00:00
{
2016-10-10 19:01:40 +00:00
if (force)
obs_output_force_stop(fileOutput);
else
obs_output_stop(fileOutput);
2016-02-23 23:16:51 +00:00
}
2018-02-19 19:54:37 +00:00
void AdvancedOutput::StopReplayBuffer(bool force)
{
if (force)
obs_output_force_stop(replayBuffer);
else
obs_output_stop(replayBuffer);
}
2016-02-23 23:16:51 +00:00
bool AdvancedOutput::StreamingActive() const
{
return obs_output_active(streamOutput);
}
bool AdvancedOutput::RecordingActive() const
{
return obs_output_active(fileOutput);
}
2018-02-19 19:54:37 +00:00
bool AdvancedOutput::ReplayBufferActive() const
{
return obs_output_active(replayBuffer);
}
2016-02-23 23:16:51 +00:00
/* ------------------------------------------------------------------------ */
2020-10-01 20:15:25 +00:00
bool BasicOutputHandler::SetupAutoRemux(const char *&ext)
{
bool autoRemux = config_get_bool(main->Config(), "Video", "AutoRemux");
if (autoRemux && strcmp(ext, "mp4") == 0)
ext = "mkv";
return autoRemux;
}
std::string
BasicOutputHandler::GetRecordingFilename(const char *path, const char *ext,
bool noSpace, bool overwrite,
const char *format, bool ffmpeg)
{
bool remux = !ffmpeg && SetupAutoRemux(ext);
string dst = GetOutputFilename(path, ext, noSpace, overwrite, format);
lastRecordingPath = remux ? dst : "";
return dst;
}
2016-02-23 23:16:51 +00:00
BasicOutputHandler *CreateSimpleOutputHandler(OBSBasic *main)
{
return new SimpleOutput(main);
}
BasicOutputHandler *CreateAdvancedOutputHandler(OBSBasic *main)
{
return new AdvancedOutput(main);
}