window-basic-main-outputs.cpp 21.0 KB
Newer Older
J
jp9000 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#include <string>
#include <QMessageBox>
#include "window-basic-main.hpp"
#include "window-basic-main-outputs.hpp"

using namespace std;

static void OBSStartStreaming(void *data, calldata_t *params)
{
	BasicOutputHandler *output = static_cast<BasicOutputHandler*>(data);
	QMetaObject::invokeMethod(output->main, "StreamingStart");

	UNUSED_PARAMETER(params);
}

static void OBSStopStreaming(void *data, calldata_t *params)
{
	BasicOutputHandler *output = static_cast<BasicOutputHandler*>(data);
	int code = (int)calldata_int(params, "code");

	QMetaObject::invokeMethod(output->main,
			"StreamingStop", Q_ARG(int, code));
	output->activeRefs--;
}

static void OBSStartRecording(void *data, calldata_t *params)
{
	BasicOutputHandler *output = static_cast<BasicOutputHandler*>(data);

	QMetaObject::invokeMethod(output->main, "RecordingStart");

	UNUSED_PARAMETER(params);
}

static void OBSStopRecording(void *data, calldata_t *params)
{
	BasicOutputHandler *output = static_cast<BasicOutputHandler*>(data);

	QMetaObject::invokeMethod(output->main, "RecordingStop");
	output->activeRefs--;

	UNUSED_PARAMETER(params);
}

/* ------------------------------------------------------------------------ */

struct SimpleOutput : BasicOutputHandler {
	OBSEncoder             aac;
	OBSEncoder             h264;

	SimpleOutput(OBSBasic *main_);

	virtual void Update() override;

	void SetupOutputs();

	virtual bool StartStreaming(obs_service_t *service) override;
	virtual bool StartRecording() override;
	virtual void StopStreaming() override;
	virtual void StopRecording() override;
	virtual bool StreamingActive() const override;
	virtual bool RecordingActive() const override;
};

SimpleOutput::SimpleOutput(OBSBasic *main_) : BasicOutputHandler(main_)
{
	streamOutput = obs_output_create("rtmp_output", "simple_stream",
68
			nullptr, nullptr);
J
jp9000 已提交
69 70
	if (!streamOutput)
		throw "Failed to create stream output (simple output)";
71
	obs_output_release(streamOutput);
J
jp9000 已提交
72 73

	fileOutput = obs_output_create("flv_output", "simple_file_output",
74
			nullptr, nullptr);
J
jp9000 已提交
75 76
	if (!fileOutput)
		throw "Failed to create recording output (simple output)";
77
	obs_output_release(fileOutput);
J
jp9000 已提交
78

79 80
	h264 = obs_video_encoder_create("obs_x264", "simple_h264", nullptr,
			nullptr);
J
jp9000 已提交
81 82
	if (!h264)
		throw "Failed to create h264 encoder (simple output)";
83
	obs_encoder_release(h264);
J
jp9000 已提交
84

85 86
	aac = obs_audio_encoder_create("libfdk_aac", "simple_aac", nullptr, 0,
			nullptr);
J
jp9000 已提交
87 88
	if (!aac)
		aac = obs_audio_encoder_create("ffmpeg_aac", "simple_aac",
89
				nullptr, 0, nullptr);
J
jp9000 已提交
90 91
	if (!aac)
		throw "Failed to create audio encoder (simple output)";
92
	obs_encoder_release(aac);
J
jp9000 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	signal_handler_connect(obs_output_get_signal_handler(streamOutput),
			"start", OBSStartStreaming, this);
	signal_handler_connect(obs_output_get_signal_handler(streamOutput),
			"stop", OBSStopStreaming, this);

	signal_handler_connect(obs_output_get_signal_handler(fileOutput),
			"start", OBSStartRecording, this);
	signal_handler_connect(obs_output_get_signal_handler(fileOutput),
			"stop", OBSStopRecording, this);
}

void SimpleOutput::Update()
{
	obs_data_t *h264Settings = obs_data_create();
	obs_data_t *aacSettings  = obs_data_create();

	int videoBitrate = config_get_uint(main->Config(), "SimpleOutput",
			"VBitrate");
	int videoBufsize = config_get_uint(main->Config(), "SimpleOutput",
113
			"VBufsize");
J
jp9000 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	int audioBitrate = config_get_uint(main->Config(), "SimpleOutput",
			"ABitrate");
	bool advanced = config_get_bool(main->Config(), "SimpleOutput",
			"UseAdvanced");
	bool useCBR = config_get_bool(main->Config(), "SimpleOutput",
			"UseCBR");
	bool useBufsize = config_get_bool(main->Config(), "SimpleOutput",
			"UseBufsize");
	const char *preset = config_get_string(main->Config(),
			"SimpleOutput", "Preset");
	const char *custom = config_get_string(main->Config(),
			"SimpleOutput", "x264Settings");

	obs_data_set_int(h264Settings, "bitrate", videoBitrate);
	obs_data_set_bool(h264Settings, "use_bufsize", useBufsize);
	obs_data_set_int(h264Settings, "buffer_size", videoBufsize);

	if (advanced) {
		obs_data_set_string(h264Settings, "preset", preset);
		obs_data_set_string(h264Settings, "x264opts", custom);
		obs_data_set_bool(h264Settings, "cbr", useCBR);
	} else {
		obs_data_set_bool(h264Settings, "cbr", true);
	}

	obs_data_set_int(aacSettings, "bitrate", audioBitrate);

141 142 143
	obs_service_apply_encoder_settings(main->GetService(),
			h264Settings, aacSettings);

144 145 146 147 148 149
	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(h264, VIDEO_FORMAT_NV12);

J
jp9000 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	obs_encoder_update(h264, h264Settings);
	obs_encoder_update(aac,  aacSettings);

	obs_data_release(h264Settings);
	obs_data_release(aacSettings);
}

inline void SimpleOutput::SetupOutputs()
{
	SimpleOutput::Update();
	obs_encoder_set_video(h264, obs_get_video());
	obs_encoder_set_audio(aac,  obs_get_audio());
}

bool SimpleOutput::StartStreaming(obs_service_t *service)
{
	if (!Active())
		SetupOutputs();

	obs_output_set_video_encoder(streamOutput, h264);
	obs_output_set_audio_encoder(streamOutput, aac, 0);
	obs_output_set_service(streamOutput, service);

	bool reconnect = config_get_bool(main->Config(), "SimpleOutput",
			"Reconnect");
	int retryDelay = config_get_uint(main->Config(), "SimpleOutput",
			"RetryDelay");
	int maxRetries = config_get_uint(main->Config(), "SimpleOutput",
			"MaxRetries");
	if (!reconnect)
		maxRetries = 0;

	obs_output_set_reconnect_settings(streamOutput, maxRetries,
			retryDelay);

	if (obs_output_start(streamOutput)) {
		activeRefs++;
		return true;
	}

	return false;
}

bool SimpleOutput::StartRecording()
{
	if (!Active())
		SetupOutputs();

	const char *path = config_get_string(main->Config(),
			"SimpleOutput", "FilePath");

	os_dir_t *dir = path ? os_opendir(path) : nullptr;

	if (!dir) {
		QMessageBox::information(main,
				QTStr("Output.BadPath.Title"),
				QTStr("Output.BadPath.Text"));
		return false;
	}

	os_closedir(dir);

	string strPath;
	strPath += path;

	char lastChar = strPath.back();
	if (lastChar != '/' && lastChar != '\\')
		strPath += "/";

	strPath += GenerateTimeDateFilename("flv");

	SetupOutputs();

	obs_output_set_video_encoder(fileOutput, h264);
	obs_output_set_audio_encoder(fileOutput, aac, 0);

	obs_data_t *settings = obs_data_create();
	obs_data_set_string(settings, "path", strPath.c_str());

	obs_output_update(fileOutput, settings);

	obs_data_release(settings);

	if (obs_output_start(fileOutput)) {
		activeRefs++;
		return true;
	}

	return false;
}

void SimpleOutput::StopStreaming()
{
	obs_output_stop(streamOutput);
}

void SimpleOutput::StopRecording()
{
	obs_output_stop(fileOutput);
}

bool SimpleOutput::StreamingActive() const
{
	return obs_output_active(streamOutput);
}

bool SimpleOutput::RecordingActive() const
{
	return obs_output_active(fileOutput);
}

/* ------------------------------------------------------------------------ */

J
jp9000 已提交
263 264 265 266 267 268 269 270 271 272 273 274
struct AdvancedOutput : BasicOutputHandler {
	OBSEncoder             aacTrack[4];
	OBSEncoder             h264Streaming;
	OBSEncoder             h264Recording;

	bool                   ffmpegRecording;
	bool                   useStreamEncoder;

	AdvancedOutput(OBSBasic *main_);

	inline void UpdateStreamSettings();
	inline void UpdateRecordingSettings();
275
	inline void UpdateAudioSettings();
J
jp9000 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	virtual void Update() override;

	inline void SetupStreaming();
	inline void SetupRecording();
	inline void SetupFFmpeg();
	void SetupOutputs();

	virtual bool StartStreaming(obs_service_t *service) override;
	virtual bool StartRecording() override;
	virtual void StopStreaming() override;
	virtual void StopRecording() override;
	virtual bool StreamingActive() const override;
	virtual bool RecordingActive() const override;
};

static OBSData GetDataFromJsonFile(const char *jsonFile)
{
	char fullPath[512];

295
	int ret = GetConfigPath(fullPath, sizeof(fullPath), jsonFile);
J
jp9000 已提交
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	if (ret > 0) {
		BPtr<char> jsonData = os_quick_read_utf8_file(fullPath);
		if (!!jsonData) {
			obs_data_t *data = obs_data_create_from_json(jsonData);
			OBSData dataRet(data);
			obs_data_release(data);
			return dataRet;
		}
	}

	return nullptr;
}

AdvancedOutput::AdvancedOutput(OBSBasic *main_) : BasicOutputHandler(main_)
{
	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");

	ffmpegRecording = astrcmpi(recType, "FFmpeg") == 0;
	useStreamEncoder = astrcmpi(recordEncoder, "none") == 0;

	OBSData streamEncSettings = GetDataFromJsonFile(
			"obs-studio/basic/streamEncoder.json");
	OBSData recordEncSettings = GetDataFromJsonFile(
			"obs-studio/basic/recordEncoder.json");

	streamOutput = obs_output_create("rtmp_output", "adv_stream",
327
			nullptr, nullptr);
J
jp9000 已提交
328 329
	if (!streamOutput)
		throw "Failed to create stream output (advanced output)";
330
	obs_output_release(streamOutput);
J
jp9000 已提交
331 332 333

	if (ffmpegRecording) {
		fileOutput = obs_output_create("ffmpeg_output",
334
				"adv_ffmpeg_output", nullptr, nullptr);
J
jp9000 已提交
335 336 337
		if (!fileOutput)
			throw "Failed to create recording FFmpeg output "
			      "(advanced output)";
338
		obs_output_release(fileOutput);
J
jp9000 已提交
339 340
	} else {
		fileOutput = obs_output_create("flv_output", "adv_file_output",
341
				nullptr, nullptr);
J
jp9000 已提交
342 343 344
		if (!fileOutput)
			throw "Failed to create recording output "
			      "(advanced output)";
345
		obs_output_release(fileOutput);
J
jp9000 已提交
346 347 348

		if (!useStreamEncoder) {
			h264Recording = obs_video_encoder_create(recordEncoder,
349 350
					"recording_h264", recordEncSettings,
					nullptr);
J
jp9000 已提交
351 352 353
			if (!h264Recording)
				throw "Failed to create recording h264 "
				      "encoder (advanced output)";
354
			obs_encoder_release(h264Recording);
J
jp9000 已提交
355 356 357 358
		}
	}

	h264Streaming = obs_video_encoder_create(streamEncoder,
359
			"streaming_h264", streamEncSettings, nullptr);
J
jp9000 已提交
360 361 362
	if (!h264Streaming)
		throw "Failed to create streaming h264 encoder "
		      "(advanced output)";
363
	obs_encoder_release(h264Streaming);
J
jp9000 已提交
364 365 366 367 368 369

	for (int i = 0; i < 4; i++) {
		char name[9];
		sprintf(name, "adv_aac%d", i);

		aacTrack[i] = obs_audio_encoder_create("libfdk_aac",
370
				name, nullptr, i, nullptr);
J
jp9000 已提交
371 372
		if (!aacTrack[i])
			aacTrack[i] = obs_audio_encoder_create("ffmpeg_aac",
373
					name, nullptr, i, nullptr);
J
jp9000 已提交
374 375 376
		if (!aacTrack[i])
			throw "Failed to create audio encoder "
			      "(advanced output)";
377
		obs_encoder_release(aacTrack[i]);
J
jp9000 已提交
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
	}

	signal_handler_connect(obs_output_get_signal_handler(streamOutput),
			"start", OBSStartStreaming, this);
	signal_handler_connect(obs_output_get_signal_handler(streamOutput),
			"stop", OBSStopStreaming, this);

	signal_handler_connect(obs_output_get_signal_handler(fileOutput),
			"start", OBSStartRecording, this);
	signal_handler_connect(obs_output_get_signal_handler(fileOutput),
			"stop", OBSStopRecording, this);
}

void AdvancedOutput::UpdateStreamSettings()
{
393 394 395
	bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
			"ApplyServiceSettings");

J
jp9000 已提交
396 397
	OBSData settings = GetDataFromJsonFile(
			"obs-studio/basic/streamEncoder.json");
398 399 400 401 402

	if (applyServiceSettings)
		obs_service_apply_encoder_settings(main->GetService(),
				settings, nullptr);

403 404 405 406 407 408 409
	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,
				VIDEO_FORMAT_NV12);

J
jp9000 已提交
410 411 412 413 414 415 416 417 418 419 420 421 422
	obs_encoder_update(h264Streaming, settings);
}

inline void AdvancedOutput::UpdateRecordingSettings()
{
	OBSData settings = GetDataFromJsonFile(
			"obs-studio/basic/recordEncoder.json");
	obs_encoder_update(h264Recording, settings);
}

void AdvancedOutput::Update()
{
	UpdateStreamSettings();
423
	if (!useStreamEncoder && !ffmpegRecording)
J
jp9000 已提交
424
		UpdateRecordingSettings();
425
	UpdateAudioSettings();
J
jp9000 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
}

inline void AdvancedOutput::SetupStreaming()
{
	bool rescale = config_get_bool(main->Config(), "AdvOut",
			"Rescale");
	const char *rescaleRes = config_get_string(main->Config(), "AdvOut",
			"RescaleRes");
	bool multitrack = config_get_bool(main->Config(), "AdvOut",
			"Multitrack");
	int trackIndex = config_get_int(main->Config(), "AdvOut",
			"TrackIndex");
	int trackCount = config_get_int(main->Config(), "AdvOut",
			"TrackCount");
	unsigned int cx = 0;
	unsigned int cy = 0;

443 444 445 446 447
	if (rescale && rescaleRes && *rescaleRes) {
		if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
			cx = 0;
			cy = 0;
		}
J
jp9000 已提交
448 449 450 451 452 453 454 455 456
	}

	obs_encoder_set_scaled_size(h264Streaming, cx, cy);
	obs_encoder_set_video(h264Streaming, obs_get_video());

	obs_output_set_video_encoder(streamOutput, h264Streaming);

	if (multitrack) {
		int i = 0;
457
		for (; i < trackCount; i++)
J
jp9000 已提交
458 459 460 461
			obs_output_set_audio_encoder(streamOutput, aacTrack[i],
					i);
		for (; i < 4; i++)
			obs_output_set_audio_encoder(streamOutput, nullptr, i);
462

J
jp9000 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	} else {
		obs_output_set_audio_encoder(streamOutput,
				aacTrack[trackIndex - 1], 0);
	}
}

inline void AdvancedOutput::SetupRecording()
{
	const char *path = config_get_string(main->Config(), "AdvOut",
			"RecFilePath");
	bool rescale = config_get_bool(main->Config(), "AdvOut",
			"RecRescale");
	const char *rescaleRes = config_get_string(main->Config(), "AdvOut",
			"RecRescaleRes");
	bool multitrack = config_get_bool(main->Config(), "AdvOut",
			"RecMultitrack");
	int trackIndex = config_get_int(main->Config(), "AdvOut",
			"RecTrackIndex");
	int trackCount = config_get_int(main->Config(), "AdvOut",
			"RecTrackCount");
	obs_data_t *settings = obs_data_create();
	unsigned int cx = 0;
	unsigned int cy = 0;

	if (useStreamEncoder) {
		obs_output_set_video_encoder(fileOutput, h264Streaming);
	} else {
490 491 492 493 494
		if (rescale && rescaleRes && *rescaleRes) {
			if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
				cx = 0;
				cy = 0;
			}
J
jp9000 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
		}

		obs_encoder_set_scaled_size(h264Recording, cx, cy);
		obs_encoder_set_video(h264Recording, obs_get_video());
		obs_output_set_video_encoder(fileOutput, h264Recording);
	}

	if (multitrack) {
		int i = 0;
		for (; i < trackCount; i++)
			obs_output_set_audio_encoder(fileOutput, aacTrack[i],
					i);
		for (; i < 4; i++)
			obs_output_set_audio_encoder(fileOutput, nullptr, i);
	} else {
		obs_output_set_audio_encoder(fileOutput,
				aacTrack[trackIndex - 1], 0);
	}

	obs_data_set_string(settings, "path", path);
	obs_output_update(fileOutput, settings);
	obs_data_release(settings);
}

inline void AdvancedOutput::SetupFFmpeg()
{
	const char *url = config_get_string(main->Config(), "AdvOut", "FFURL");
	int vBitrate = config_get_int(main->Config(), "AdvOut",
			"FFVBitrate");
	bool rescale = config_get_bool(main->Config(), "AdvOut",
			"FFRescale");
	const char *rescaleRes = config_get_string(main->Config(), "AdvOut",
			"FFRescaleRes");
528 529 530 531
	const char *formatName = config_get_string(main->Config(), "AdvOut",
			"FFFormat");
	const char *mimeType = config_get_string(main->Config(), "AdvOut",
			"FFFormatMimeType");
J
jp9000 已提交
532 533
	const char *vEncoder = config_get_string(main->Config(), "AdvOut",
			"FFVEncoder");
534 535
	int vEncoderId = config_get_int(main->Config(), "AdvOut",
			"FFVEncoderId");
J
jp9000 已提交
536 537 538 539 540 541 542 543
	const char *vEncCustom = config_get_string(main->Config(), "AdvOut",
			"FFVCustom");
	int aBitrate = config_get_int(main->Config(), "AdvOut",
			"FFABitrate");
	int aTrack = config_get_int(main->Config(), "AdvOut",
			"FFAudioTrack");
	const char *aEncoder = config_get_string(main->Config(), "AdvOut",
			"FFAEncoder");
544 545
	int aEncoderId = config_get_int(main->Config(), "AdvOut",
			"FFAEncoderId");
J
jp9000 已提交
546 547 548 549 550
	const char *aEncCustom = config_get_string(main->Config(), "AdvOut",
			"FFACustom");
	obs_data_t *settings = obs_data_create();

	obs_data_set_string(settings, "url", url);
551 552
	obs_data_set_string(settings, "format_name", formatName);
	obs_data_set_string(settings, "format_mime_type", mimeType);
J
jp9000 已提交
553 554
	obs_data_set_int(settings, "video_bitrate", vBitrate);
	obs_data_set_string(settings, "video_encoder", vEncoder);
555
	obs_data_set_int(settings, "video_encoder_id", vEncoderId);
J
jp9000 已提交
556 557 558
	obs_data_set_string(settings, "video_settings", vEncCustom);
	obs_data_set_int(settings, "audio_bitrate", aBitrate);
	obs_data_set_string(settings, "audio_encoder", aEncoder);
559
	obs_data_set_int(settings, "audio_encoder_id", aEncoderId);
J
jp9000 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	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);
		}
	}

	obs_output_set_mixer(fileOutput, aTrack - 1);
	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,
		const char *defaultName)
{
	obs_encoder_set_name(encoder, (name && *name) ? name : defaultName);
}

586
inline void AdvancedOutput::UpdateAudioSettings()
J
jp9000 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
{
	int track1Bitrate = config_get_uint(main->Config(), "AdvOut",
			"Track1Bitrate");
	int track2Bitrate = config_get_uint(main->Config(), "AdvOut",
			"Track2Bitrate");
	int track3Bitrate = config_get_uint(main->Config(), "AdvOut",
			"Track3Bitrate");
	int track4Bitrate = config_get_uint(main->Config(), "AdvOut",
			"Track4Bitrate");
	const char *name1 = config_get_string(main->Config(), "AdvOut",
			"Track1Name");
	const char *name2 = config_get_string(main->Config(), "AdvOut",
			"Track2Name");
	const char *name3 = config_get_string(main->Config(), "AdvOut",
			"Track3Name");
	const char *name4 = config_get_string(main->Config(), "AdvOut",
			"Track4Name");
604 605
	bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
			"ApplyServiceSettings");
606 607
	int streamTrackIndex = config_get_int(main->Config(), "AdvOut",
			"TrackIndex");
J
jp9000 已提交
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	obs_data_t *settings[4];

	for (size_t i = 0; i < 4; i++)
		settings[i] = obs_data_create();

	obs_data_set_int(settings[0], "bitrate", track1Bitrate);
	obs_data_set_int(settings[1], "bitrate", track2Bitrate);
	obs_data_set_int(settings[2], "bitrate", track3Bitrate);
	obs_data_set_int(settings[3], "bitrate", track4Bitrate);

	SetEncoderName(aacTrack[0], name1, "Track1");
	SetEncoderName(aacTrack[1], name2, "Track2");
	SetEncoderName(aacTrack[2], name3, "Track3");
	SetEncoderName(aacTrack[3], name4, "Track4");

	for (size_t i = 0; i < 4; i++) {
624
		if (applyServiceSettings && (int)(i + 1) == streamTrackIndex)
625 626 627
			obs_service_apply_encoder_settings(main->GetService(),
					nullptr, settings[i]);

J
jp9000 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		obs_encoder_update(aacTrack[i], settings[i]);
		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());
	obs_encoder_set_audio(aacTrack[0], obs_get_audio());
	obs_encoder_set_audio(aacTrack[1], obs_get_audio());
	obs_encoder_set_audio(aacTrack[2], obs_get_audio());
	obs_encoder_set_audio(aacTrack[3], obs_get_audio());

	SetupStreaming();

	if (ffmpegRecording)
		SetupFFmpeg();
	else
		SetupRecording();
}

bool AdvancedOutput::StartStreaming(obs_service_t *service)
{
653 654 655 656 657
	if (!useStreamEncoder ||
	    (!ffmpegRecording && !obs_output_active(fileOutput))) {
		UpdateStreamSettings();
	}

658 659
	UpdateAudioSettings();

J
jp9000 已提交
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	if (!Active())
		SetupOutputs();

	obs_output_set_service(streamOutput, service);

	bool reconnect = config_get_bool(main->Config(), "AdvOut", "Reconnect");
	int retryDelay = config_get_int(main->Config(), "AdvOut", "RetryDelay");
	int maxRetries = config_get_int(main->Config(), "AdvOut", "MaxRetries");
	if (!reconnect)
		maxRetries = 0;

	obs_output_set_reconnect_settings(streamOutput, maxRetries,
			retryDelay);

	if (obs_output_start(streamOutput)) {
		activeRefs++;
		return true;
	}

	return false;
}

bool AdvancedOutput::StartRecording()
{
684 685 686 687 688 689 690 691
	if (!useStreamEncoder) {
		if (!ffmpegRecording) {
			UpdateRecordingSettings();
		}
	} else if (!obs_output_active(streamOutput)) {
		UpdateStreamSettings();
	}

692 693
	UpdateAudioSettings();

J
jp9000 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
	if (!Active())
		SetupOutputs();

	if (!ffmpegRecording) {
		const char *path = config_get_string(main->Config(),
				"AdvOut", "RecFilePath");

		os_dir_t *dir = path ? os_opendir(path) : nullptr;

		if (!dir) {
			QMessageBox::information(main,
					QTStr("Output.BadPath.Title"),
					QTStr("Output.BadPath.Text"));
			return false;
		}

		os_closedir(dir);

		string strPath;
		strPath += path;

		char lastChar = strPath.back();
		if (lastChar != '/' && lastChar != '\\')
			strPath += "/";

		strPath += GenerateTimeDateFilename("flv");

		obs_data_t *settings = obs_data_create();
		obs_data_set_string(settings, "path", strPath.c_str());

		obs_output_update(fileOutput, settings);

		obs_data_release(settings);
	}

	if (obs_output_start(fileOutput)) {
		activeRefs++;
		return true;
	}

	return false;
}

void AdvancedOutput::StopStreaming()
{
	obs_output_stop(streamOutput);
}

void AdvancedOutput::StopRecording()
{
	obs_output_stop(fileOutput);
}

bool AdvancedOutput::StreamingActive() const
{
	return obs_output_active(streamOutput);
}

bool AdvancedOutput::RecordingActive() const
{
	return obs_output_active(fileOutput);
}

/* ------------------------------------------------------------------------ */

J
jp9000 已提交
759 760 761 762
BasicOutputHandler *CreateSimpleOutputHandler(OBSBasic *main)
{
	return new SimpleOutput(main);
}
J
jp9000 已提交
763 764 765 766 767

BasicOutputHandler *CreateAdvancedOutputHandler(OBSBasic *main)
{
	return new AdvancedOutput(main);
}