window-basic-main-outputs.cpp 21.0 KB
Newer Older
J
jp9000 已提交
1 2
#include <string>
#include <QMessageBox>
3
#include "audio-encoders.hpp"
J
jp9000 已提交
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
#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);
39
	int code = (int)calldata_int(params, "code");
J
jp9000 已提交
40

41 42
	QMetaObject::invokeMethod(output->main,
			"RecordingStop", Q_ARG(int, code));
J
jp9000 已提交
43 44 45 46 47 48 49
	output->activeRefs--;

	UNUSED_PARAMETER(params);
}

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

50 51
static bool CreateAACEncoder(OBSEncoder &res, string &id, int bitrate,
		const char *name, size_t idx)
P
Palana 已提交
52
{
53 54 55 56 57
	const char *id_ = GetAACEncoderForBitrate(bitrate);
	if (!id_) {
		id.clear();
		res = nullptr;
		return false;
P
Palana 已提交
58 59
	}

60 61 62 63 64 65 66 67 68 69 70 71
	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;
P
Palana 已提交
72 73 74 75
}

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

J
jp9000 已提交
76 77 78 79
struct SimpleOutput : BasicOutputHandler {
	OBSEncoder             aac;
	OBSEncoder             h264;

80 81
	string                 aacEncoderID;

J
jp9000 已提交
82 83 84 85 86
	SimpleOutput(OBSBasic *main_);

	virtual void Update() override;

	void SetupOutputs();
87
	int GetAudioBitrate() const;
J
jp9000 已提交
88 89 90 91 92 93 94 95 96 97 98 99

	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",
100
			nullptr, nullptr);
J
jp9000 已提交
101 102
	if (!streamOutput)
		throw "Failed to create stream output (simple output)";
103
	obs_output_release(streamOutput);
J
jp9000 已提交
104

105
	fileOutput = obs_output_create("ffmpeg_muxer", "simple_file_output",
106
			nullptr, nullptr);
J
jp9000 已提交
107 108
	if (!fileOutput)
		throw "Failed to create recording output (simple output)";
109
	obs_output_release(fileOutput);
J
jp9000 已提交
110

111 112
	h264 = obs_video_encoder_create("obs_x264", "simple_h264", nullptr,
			nullptr);
J
jp9000 已提交
113 114
	if (!h264)
		throw "Failed to create h264 encoder (simple output)";
115
	obs_encoder_release(h264);
J
jp9000 已提交
116

117 118
	if (!CreateAACEncoder(aac, aacEncoderID, GetAudioBitrate(),
				"simple_aac", 0))
J
jp9000 已提交
119 120
		throw "Failed to create audio encoder (simple output)";

121
	startStreaming.Connect(obs_output_get_signal_handler(streamOutput),
J
jp9000 已提交
122
			"start", OBSStartStreaming, this);
123
	stopStreaming.Connect(obs_output_get_signal_handler(streamOutput),
J
jp9000 已提交
124 125
			"stop", OBSStopStreaming, this);

126
	startRecording.Connect(obs_output_get_signal_handler(fileOutput),
J
jp9000 已提交
127
			"start", OBSStartRecording, this);
128
	stopRecording.Connect(obs_output_get_signal_handler(fileOutput),
J
jp9000 已提交
129 130 131
			"stop", OBSStopRecording, this);
}

132 133 134 135 136
int SimpleOutput::GetAudioBitrate() const
{
	return config_get_uint(main->Config(), "SimpleOutput", "ABitrate");
}

J
jp9000 已提交
137 138 139 140 141 142 143 144
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",
145
			"VBufsize");
146
	int audioBitrate = GetAudioBitrate();
J
jp9000 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
	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);

172 173 174
	obs_service_apply_encoder_settings(main->GetService(),
			h264Settings, aacSettings);

175 176 177 178 179 180
	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 已提交
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
	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");
231 232
	const char *format = config_get_string(main->Config(),
			"SimpleOutput", "RecFormat");
J
jp9000 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

	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 += "/";

252
	strPath += GenerateTimeDateFilename(format);
J
jp9000 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

	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 已提交
296 297 298 299 300 301 302 303
struct AdvancedOutput : BasicOutputHandler {
	OBSEncoder             aacTrack[4];
	OBSEncoder             h264Streaming;
	OBSEncoder             h264Recording;

	bool                   ffmpegRecording;
	bool                   useStreamEncoder;

304 305
	string                 aacEncoderID[4];

J
jp9000 已提交
306 307 308 309
	AdvancedOutput(OBSBasic *main_);

	inline void UpdateStreamSettings();
	inline void UpdateRecordingSettings();
310
	inline void UpdateAudioSettings();
J
jp9000 已提交
311 312 313 314 315 316
	virtual void Update() override;

	inline void SetupStreaming();
	inline void SetupRecording();
	inline void SetupFFmpeg();
	void SetupOutputs();
317
	int GetAudioBitrate(size_t i) const;
J
jp9000 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330

	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];

J
jp9000 已提交
331
	int ret = GetProfilePath(fullPath, sizeof(fullPath), jsonFile);
J
jp9000 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	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;

J
jp9000 已提交
357 358
	OBSData streamEncSettings = GetDataFromJsonFile("streamEncoder.json");
	OBSData recordEncSettings = GetDataFromJsonFile("recordEncoder.json");
J
jp9000 已提交
359 360

	streamOutput = obs_output_create("rtmp_output", "adv_stream",
361
			nullptr, nullptr);
J
jp9000 已提交
362 363
	if (!streamOutput)
		throw "Failed to create stream output (advanced output)";
364
	obs_output_release(streamOutput);
J
jp9000 已提交
365 366 367

	if (ffmpegRecording) {
		fileOutput = obs_output_create("ffmpeg_output",
368
				"adv_ffmpeg_output", nullptr, nullptr);
J
jp9000 已提交
369 370 371
		if (!fileOutput)
			throw "Failed to create recording FFmpeg output "
			      "(advanced output)";
372
		obs_output_release(fileOutput);
J
jp9000 已提交
373
	} else {
374 375
		fileOutput = obs_output_create("ffmpeg_muxer",
				"adv_file_output", nullptr, nullptr);
J
jp9000 已提交
376 377 378
		if (!fileOutput)
			throw "Failed to create recording output "
			      "(advanced output)";
379
		obs_output_release(fileOutput);
J
jp9000 已提交
380 381 382

		if (!useStreamEncoder) {
			h264Recording = obs_video_encoder_create(recordEncoder,
383 384
					"recording_h264", recordEncSettings,
					nullptr);
J
jp9000 已提交
385 386 387
			if (!h264Recording)
				throw "Failed to create recording h264 "
				      "encoder (advanced output)";
388
			obs_encoder_release(h264Recording);
J
jp9000 已提交
389 390 391 392
		}
	}

	h264Streaming = obs_video_encoder_create(streamEncoder,
393
			"streaming_h264", streamEncSettings, nullptr);
J
jp9000 已提交
394 395 396
	if (!h264Streaming)
		throw "Failed to create streaming h264 encoder "
		      "(advanced output)";
397
	obs_encoder_release(h264Streaming);
J
jp9000 已提交
398 399 400 401 402

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

403 404
		if (!CreateAACEncoder(aacTrack[i], aacEncoderID[i],
					GetAudioBitrate(i), name, i))
J
jp9000 已提交
405 406 407 408
			throw "Failed to create audio encoder "
			      "(advanced output)";
	}

409
	startStreaming.Connect(obs_output_get_signal_handler(streamOutput),
J
jp9000 已提交
410
			"start", OBSStartStreaming, this);
411
	stopStreaming.Connect(obs_output_get_signal_handler(streamOutput),
J
jp9000 已提交
412 413
			"stop", OBSStopStreaming, this);

414
	startRecording.Connect(obs_output_get_signal_handler(fileOutput),
J
jp9000 已提交
415
			"start", OBSStartRecording, this);
416
	stopRecording.Connect(obs_output_get_signal_handler(fileOutput),
J
jp9000 已提交
417 418 419 420 421
			"stop", OBSStopRecording, this);
}

void AdvancedOutput::UpdateStreamSettings()
{
422 423 424
	bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
			"ApplyServiceSettings");

J
jp9000 已提交
425
	OBSData settings = GetDataFromJsonFile("streamEncoder.json");
426 427 428 429 430

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

431 432 433 434 435 436 437
	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 已提交
438 439 440 441 442
	obs_encoder_update(h264Streaming, settings);
}

inline void AdvancedOutput::UpdateRecordingSettings()
{
J
jp9000 已提交
443
	OBSData settings = GetDataFromJsonFile("recordEncoder.json");
J
jp9000 已提交
444 445 446 447 448 449
	obs_encoder_update(h264Recording, settings);
}

void AdvancedOutput::Update()
{
	UpdateStreamSettings();
450
	if (!useStreamEncoder && !ffmpegRecording)
J
jp9000 已提交
451
		UpdateRecordingSettings();
452
	UpdateAudioSettings();
J
jp9000 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
}

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;

470 471 472 473 474
	if (rescale && rescaleRes && *rescaleRes) {
		if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
			cx = 0;
			cy = 0;
		}
J
jp9000 已提交
475 476 477 478 479 480 481 482 483
	}

	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;
484
		for (; i < trackCount; i++)
J
jp9000 已提交
485 486 487 488
			obs_output_set_audio_encoder(streamOutput, aacTrack[i],
					i);
		for (; i < 4; i++)
			obs_output_set_audio_encoder(streamOutput, nullptr, i);
489

J
jp9000 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503
	} 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");
504
	int tracks = config_get_int(main->Config(), "AdvOut", "RecTracks");
J
jp9000 已提交
505 506 507
	obs_data_t *settings = obs_data_create();
	unsigned int cx = 0;
	unsigned int cy = 0;
508
	int idx = 0;
J
jp9000 已提交
509 510 511 512

	if (useStreamEncoder) {
		obs_output_set_video_encoder(fileOutput, h264Streaming);
	} else {
513 514 515 516 517
		if (rescale && rescaleRes && *rescaleRes) {
			if (sscanf(rescaleRes, "%ux%u", &cx, &cy) != 2) {
				cx = 0;
				cy = 0;
			}
J
jp9000 已提交
518 519 520 521 522 523 524
		}

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

525 526
	for (int i = 0; i < MAX_AUDIO_MIXES; i++) {
		if ((tracks & (1<<i)) != 0) {
J
jp9000 已提交
527
			obs_output_set_audio_encoder(fileOutput, aacTrack[i],
528 529
					idx++);
		}
J
jp9000 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	}

	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");
546 547 548 549
	const char *formatName = config_get_string(main->Config(), "AdvOut",
			"FFFormat");
	const char *mimeType = config_get_string(main->Config(), "AdvOut",
			"FFFormatMimeType");
J
jp9000 已提交
550 551
	const char *vEncoder = config_get_string(main->Config(), "AdvOut",
			"FFVEncoder");
552 553
	int vEncoderId = config_get_int(main->Config(), "AdvOut",
			"FFVEncoderId");
J
jp9000 已提交
554 555 556 557 558 559 560 561
	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");
562 563
	int aEncoderId = config_get_int(main->Config(), "AdvOut",
			"FFAEncoderId");
J
jp9000 已提交
564 565 566 567 568
	const char *aEncCustom = config_get_string(main->Config(), "AdvOut",
			"FFACustom");
	obs_data_t *settings = obs_data_create();

	obs_data_set_string(settings, "url", url);
569 570
	obs_data_set_string(settings, "format_name", formatName);
	obs_data_set_string(settings, "format_mime_type", mimeType);
J
jp9000 已提交
571 572
	obs_data_set_int(settings, "video_bitrate", vBitrate);
	obs_data_set_string(settings, "video_encoder", vEncoder);
573
	obs_data_set_int(settings, "video_encoder_id", vEncoderId);
J
jp9000 已提交
574 575 576
	obs_data_set_string(settings, "video_settings", vEncCustom);
	obs_data_set_int(settings, "audio_bitrate", aBitrate);
	obs_data_set_string(settings, "audio_encoder", aEncoder);
577
	obs_data_set_int(settings, "audio_encoder_id", aEncoderId);
J
jp9000 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	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);
}

604
inline void AdvancedOutput::UpdateAudioSettings()
J
jp9000 已提交
605 606 607 608 609 610 611 612 613
{
	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");
614 615
	bool applyServiceSettings = config_get_bool(main->Config(), "AdvOut",
			"ApplyServiceSettings");
616 617
	int streamTrackIndex = config_get_int(main->Config(), "AdvOut",
			"TrackIndex");
J
jp9000 已提交
618 619
	obs_data_t *settings[4];

620
	for (size_t i = 0; i < 4; i++) {
J
jp9000 已提交
621
		settings[i] = obs_data_create();
622 623
		obs_data_set_int(settings[i], "bitrate", GetAudioBitrate(i));
	}
J
jp9000 已提交
624 625 626 627 628 629 630

	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++) {
631
		if (applyServiceSettings && (int)(i + 1) == streamTrackIndex)
632 633 634
			obs_service_apply_encoder_settings(main->GetService(),
					nullptr, settings[i]);

J
jp9000 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
		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();
}

658 659 660 661 662 663 664 665 666
int AdvancedOutput::GetAudioBitrate(size_t i) const
{
	const char *names[] = {
		"Track1Bitrate", "Track2Bitrate",
		"Track3Bitrate", "Track4Bitrate",
	};
	return config_get_uint(main->Config(), "AdvOut", names[i]);
}

J
jp9000 已提交
667 668
bool AdvancedOutput::StartStreaming(obs_service_t *service)
{
669 670 671 672 673
	if (!useStreamEncoder ||
	    (!ffmpegRecording && !obs_output_active(fileOutput))) {
		UpdateStreamSettings();
	}

674 675
	UpdateAudioSettings();

J
jp9000 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
	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()
{
700 701 702 703 704 705 706 707
	if (!useStreamEncoder) {
		if (!ffmpegRecording) {
			UpdateRecordingSettings();
		}
	} else if (!obs_output_active(streamOutput)) {
		UpdateStreamSettings();
	}

708 709
	UpdateAudioSettings();

J
jp9000 已提交
710 711 712 713 714 715
	if (!Active())
		SetupOutputs();

	if (!ffmpegRecording) {
		const char *path = config_get_string(main->Config(),
				"AdvOut", "RecFilePath");
716 717
		const char *format = config_get_string(main->Config(),
				"AdvOut", "RecFormat");
J
jp9000 已提交
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736

		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 += "/";

737
		strPath += GenerateTimeDateFilename(format);
J
jp9000 已提交
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

		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 已提交
777 778 779 780
BasicOutputHandler *CreateSimpleOutputHandler(OBSBasic *main)
{
	return new SimpleOutput(main);
}
J
jp9000 已提交
781 782 783 784 785

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