window-basic-main.cpp 19.0 KB
Newer Older
1
/******************************************************************************
2
    Copyright (C) 2013-2014 by Hugh Bailey <obs.jim@gmail.com>
3
    Copyright (C) 2014 by Zachary Lund <admin@computerquip.com>
4 5 6

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
7
    the Free Software Foundation, either version 2 of the License, or
8 9 10
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 13 14 15 16 17 18
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

J
jp9000 已提交
19
#include <obs.hpp>
J
jp9000 已提交
20
#include <QMessageBox>
21
#include <QShowEvent>
J
jp9000 已提交
22
#include <QFileDialog>
23

24 25 26
#include <util/util.hpp>
#include <util/platform.h>

27
#include "obs-app.hpp"
28
#include "platform.hpp"
29
#include "window-basic-settings.hpp"
30
#include "window-namedialog.hpp"
J
jp9000 已提交
31 32
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"
33

J
jp9000 已提交
34
#include "ui_OBSBasic.h"
35

36
using namespace std;
J
jp9000 已提交
37

J
jp9000 已提交
38 39 40
Q_DECLARE_METATYPE(OBSScene);
Q_DECLARE_METATYPE(OBSSceneItem);

41 42
OBSBasic::OBSBasic(QWidget *parent)
	: OBSMainWindow (parent),
J
jp9000 已提交
43
	  outputTest    (NULL),
44
	  sceneChanging (false),
J
jp9000 已提交
45
	  ui            (new Ui::OBSBasic)
46 47 48 49
{
	ui->setupUi(this);
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
bool OBSBasic::InitBasicConfigDefaults()
{
	config_set_default_int(basicConfig, "Window", "PosX",  -1);
	config_set_default_int(basicConfig, "Window", "PosY",  -1);
	config_set_default_int(basicConfig, "Window", "SizeX", -1);
	config_set_default_int(basicConfig, "Window", "SizeY", -1);

	vector<MonitorInfo> monitors;
	GetMonitors(monitors);

	if (!monitors.size()) {
		OBSErrorBox(NULL, "There appears to be no monitors.  Er, this "
		                  "technically shouldn't be possible.");
		return false;
	}

	uint32_t cx = monitors[0].cx;
	uint32_t cy = monitors[0].cy;

	config_set_default_uint  (basicConfig, "Video", "BaseCX",   cx);
	config_set_default_uint  (basicConfig, "Video", "BaseCY",   cy);

	cx = cx * 10 / 15;
	cy = cy * 10 / 15;
	config_set_default_uint  (basicConfig, "Video", "OutputCX", cx);
	config_set_default_uint  (basicConfig, "Video", "OutputCY", cy);

	config_set_default_uint  (basicConfig, "Video", "FPSType", 0);
	config_set_default_string(basicConfig, "Video", "FPSCommon", "30");
	config_set_default_uint  (basicConfig, "Video", "FPSInt", 30);
	config_set_default_uint  (basicConfig, "Video", "FPSNum", 30);
	config_set_default_uint  (basicConfig, "Video", "FPSDen", 1);

	config_set_default_uint  (basicConfig, "Audio", "SampleRate", 44100);
	config_set_default_string(basicConfig, "Audio", "ChannelSetup",
			"Stereo");
	config_set_default_uint  (basicConfig, "Audio", "BufferingTime", 1000);

J
jp9000 已提交
88 89 90 91 92 93 94 95 96 97 98
	config_set_default_string(basicConfig, "Audio", "DesktopDevice1",
			"default");
	config_set_default_string(basicConfig, "Audio", "DesktopDevice2",
			"disabled");
	config_set_default_string(basicConfig, "Audio", "AuxDevice1",
			"default");
	config_set_default_string(basicConfig, "Audio", "AuxDevice2",
			"disabled");
	config_set_default_string(basicConfig, "Audio", "AuxDevice3",
			"disabled");

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	return true;
}

bool OBSBasic::InitBasicConfig()
{
	BPtr<char> configPath(os_get_config_path("obs-studio/basic/basic.ini"));

	int errorcode = basicConfig.Open(configPath, CONFIG_OPEN_ALWAYS);
	if (errorcode != CONFIG_SUCCESS) {
		OBSErrorBox(NULL, "Failed to open basic.ini: %d", errorcode);
		return false;
	}

	return InitBasicConfigDefaults();
}

115 116 117 118 119 120 121 122
void OBSBasic::OBSInit()
{
	/* make sure it's fully displayed before doing any initialization */
	show();
	App()->processEvents();

	if (!obs_startup())
		throw "Failed to initialize libobs";
123 124
	if (!InitBasicConfig())
		throw "Failed to load basic.ini";
125 126 127
	if (!ResetVideo())
		throw "Failed to initialize video";
	if (!ResetAudio())
128 129
		throw "Failed to initialize audio";

130
	signal_handler_connect(obs_signalhandler(), "source_add",
131
			OBSBasic::SourceAdded, this);
132
	signal_handler_connect(obs_signalhandler(), "source_remove",
133
			OBSBasic::SourceRemoved, this);
134
	signal_handler_connect(obs_signalhandler(), "channel_change",
135 136
			OBSBasic::ChannelChanged, this);

J
jp9000 已提交
137 138
	/* TODO: this is a test, all modules will be searched for and loaded
	 * automatically later */
139
	obs_load_module("test-input");
140
	obs_load_module("obs-ffmpeg");
J
jp9000 已提交
141 142
#ifdef __APPLE__
	obs_load_module("mac-capture");
J
jp9000 已提交
143 144
#elif _WIN32
	obs_load_module("win-wasapi");
J
jp9000 已提交
145
	obs_load_module("win-capture");
J
jp9000 已提交
146
#endif
147 148 149 150 151 152 153 154 155 156 157
}

OBSBasic::~OBSBasic()
{
	/* free the lists before shutting down to remove the scene/item
	 * references */
	ui->sources->clear();
	ui->scenes->clear();
	obs_shutdown();
}

J
jp9000 已提交
158
OBSScene OBSBasic::GetCurrentScene()
159
{
J
jp9000 已提交
160
	QListWidgetItem *item = ui->scenes->currentItem();
J
jp9000 已提交
161
	return item ? item->data(Qt::UserRole).value<OBSScene>() : nullptr;
162 163
}

J
jp9000 已提交
164
OBSSceneItem OBSBasic::GetCurrentSceneItem()
J
jp9000 已提交
165 166
{
	QListWidgetItem *item = ui->sources->currentItem();
J
jp9000 已提交
167
	return item ? item->data(Qt::UserRole).value<OBSSceneItem>() : nullptr;
J
jp9000 已提交
168 169
}

170 171 172 173 174 175 176 177
void OBSBasic::UpdateSources(OBSScene scene)
{
	ui->sources->clear();

	obs_scene_enum_items(scene,
			[] (obs_scene_t scene, obs_sceneitem_t item, void *p)
			{
				OBSBasic *window = static_cast<OBSBasic*>(p);
178
				window->InsertSceneItem(item);
J
jp9000 已提交
179 180

				UNUSED_PARAMETER(scene);
181 182 183 184
				return true;
			}, this);
}

185 186 187 188 189 190 191 192 193 194 195 196
void OBSBasic::InsertSceneItem(obs_sceneitem_t item)
{
	obs_source_t source = obs_sceneitem_getsource(item);
	const char   *name  = obs_source_getname(source);

	QListWidgetItem *listItem = new QListWidgetItem(QT_UTF8(name));
	listItem->setData(Qt::UserRole,
			QVariant::fromValue(OBSSceneItem(item)));

	ui->sources->insertItem(0, listItem);
}

197 198 199
/* Qt callbacks for invokeMethod */

void OBSBasic::AddScene(OBSSource source)
200 201 202
{
	const char *name  = obs_source_getname(source);
	obs_scene_t scene = obs_scene_fromsource(source);
J
jp9000 已提交
203 204

	QListWidgetItem *item = new QListWidgetItem(QT_UTF8(name));
J
jp9000 已提交
205
	item->setData(Qt::UserRole, QVariant::fromValue(OBSScene(scene)));
J
jp9000 已提交
206
	ui->scenes->addItem(item);
207 208

	signal_handler_t handler = obs_source_signalhandler(source);
209
	signal_handler_connect(handler, "item_add",
J
jp9000 已提交
210
			OBSBasic::SceneItemAdded, this);
211
	signal_handler_connect(handler, "item_remove",
J
jp9000 已提交
212
			OBSBasic::SceneItemRemoved, this);
213 214
}

215
void OBSBasic::RemoveScene(OBSSource source)
J
jp9000 已提交
216 217 218
{
	const char *name = obs_source_getname(source);

J
jp9000 已提交
219 220 221
	QListWidgetItem *sel = ui->scenes->currentItem();
	QList<QListWidgetItem*> items = ui->scenes->findItems(QT_UTF8(name),
			Qt::MatchExactly);
J
jp9000 已提交
222

J
jp9000 已提交
223 224 225 226
	if (sel != nullptr) {
		if (items.contains(sel))
			ui->sources->clear();
		delete sel;
J
jp9000 已提交
227
	}
228 229
}

230
void OBSBasic::AddSceneItem(OBSSceneItem item)
231
{
J
jp9000 已提交
232
	obs_scene_t  scene  = obs_sceneitem_getscene(item);
233
	obs_source_t source = obs_sceneitem_getsource(item);
J
jp9000 已提交
234

235 236
	if (GetCurrentScene() == scene)
		InsertSceneItem(item);
J
jp9000 已提交
237 238

	sourceSceneRefs[source] = sourceSceneRefs[source] + 1;
239 240
}

241
void OBSBasic::RemoveSceneItem(OBSSceneItem item)
242
{
J
jp9000 已提交
243
	obs_scene_t scene = obs_sceneitem_getscene(item);
244

J
jp9000 已提交
245
	if (GetCurrentScene() == scene) {
B
BtbN 已提交
246
		for (int i = 0; i < ui->sources->count(); i++) {
J
jp9000 已提交
247 248
			QListWidgetItem *listItem = ui->sources->item(i);
			QVariant userData = listItem->data(Qt::UserRole);
J
jp9000 已提交
249

J
jp9000 已提交
250
			if (userData.value<OBSSceneItem>() == item) {
J
jp9000 已提交
251
				delete listItem;
J
jp9000 已提交
252 253
				break;
			}
254 255
		}
	}
J
jp9000 已提交
256 257 258 259 260 261 262 263

	obs_source_t source = obs_sceneitem_getsource(item);

	int scenes = sourceSceneRefs[source] - 1;
	if (scenes == 0) {
		obs_source_remove(source);
		sourceSceneRefs.erase(source);
	}
264 265
}

266
void OBSBasic::UpdateSceneSelection(OBSSource source)
267 268 269 270 271
{
	if (source) {
		obs_source_type type;
		obs_source_gettype(source, &type, NULL);

J
jp9000 已提交
272
		if (type != OBS_SOURCE_TYPE_SCENE)
J
jp9000 已提交
273
			return;
274

J
jp9000 已提交
275 276 277 278 279 280
		obs_scene_t scene = obs_scene_fromsource(source);
		const char *name = obs_source_getname(source);

		QList<QListWidgetItem*> items =
			ui->scenes->findItems(QT_UTF8(name), Qt::MatchExactly);

281 282 283 284 285
		if (items.count()) {
			sceneChanging = true;
			ui->scenes->setCurrentItem(items.first());
			sceneChanging = false;

J
jp9000 已提交
286
			UpdateSources(scene);
287
		}
J
jp9000 已提交
288
	}
289 290 291 292 293 294 295 296 297
}

/* OBS Callbacks */

void OBSBasic::SceneItemAdded(void *data, calldata_t params)
{
	OBSBasic *window = static_cast<OBSBasic*>(data);

	obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");
J
jp9000 已提交
298

299 300
	QMetaObject::invokeMethod(window, "AddSceneItem",
			Q_ARG(OBSSceneItem, OBSSceneItem(item)));
J
jp9000 已提交
301 302
}

303
void OBSBasic::SceneItemRemoved(void *data, calldata_t params)
304
{
305
	OBSBasic *window = static_cast<OBSBasic*>(data);
306

307 308
	obs_sceneitem_t item = (obs_sceneitem_t)calldata_ptr(params, "item");

309 310
	QMetaObject::invokeMethod(window, "RemoveSceneItem",
			Q_ARG(OBSSceneItem, OBSSceneItem(item)));
311 312 313 314 315
}

void OBSBasic::SourceAdded(void *data, calldata_t params)
{
	obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
316 317 318 319

	obs_source_type type;
	obs_source_gettype(source, &type, NULL);

J
jp9000 已提交
320
	if (type == OBS_SOURCE_TYPE_SCENE)
321 322 323
		QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
				"AddScene",
				Q_ARG(OBSSource, OBSSource(source)));
324 325
}

J
jp9000 已提交
326
void OBSBasic::SourceRemoved(void *data, calldata_t params)
327
{
328
	obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
329

J
jp9000 已提交
330 331 332
	obs_source_type type;
	obs_source_gettype(source, &type, NULL);

J
jp9000 已提交
333
	if (type == OBS_SOURCE_TYPE_SCENE)
334 335 336
		QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
				"RemoveScene",
				Q_ARG(OBSSource, OBSSource(source)));
337 338
}

339 340 341
void OBSBasic::ChannelChanged(void *data, calldata_t params)
{
	obs_source_t source = (obs_source_t)calldata_ptr(params, "source");
342
	uint32_t channel = (uint32_t)calldata_int(params, "channel");
343 344

	if (channel == 0)
345 346 347
		QMetaObject::invokeMethod(static_cast<OBSBasic*>(data),
				"UpdateSceneSelection",
				Q_ARG(OBSSource, OBSSource(source)));
348 349
}

350 351
void OBSBasic::RenderMain(void *data, uint32_t cx, uint32_t cy)
{
J
jp9000 已提交
352 353 354 355
	OBSBasic *window = static_cast<OBSBasic*>(data);
	gs_matrix_push();
	gs_matrix_scale3f(window->previewScale, window->previewScale, 1.0f);
	gs_matrix_translate3f(-window->previewX, -window->previewY, 0.0f);
J
jp9000 已提交
356
	obs_render_main_view();
J
jp9000 已提交
357
	gs_matrix_pop();
J
jp9000 已提交
358 359 360

	UNUSED_PARAMETER(cx);
	UNUSED_PARAMETER(cy);
361 362
}

363 364
/* Main class functions */

365
bool OBSBasic::ResetVideo()
J
jp9000 已提交
366 367
{
	struct obs_video_info ovi;
J
jp9000 已提交
368

369
	GetConfigFPS(ovi.fps_num, ovi.fps_den);
J
jp9000 已提交
370 371

	ovi.graphics_module = App()->GetRenderModule();
372
	ovi.base_width     = (uint32_t)config_get_uint(basicConfig,
J
jp9000 已提交
373
			"Video", "BaseCX");
374
	ovi.base_height    = (uint32_t)config_get_uint(basicConfig,
J
jp9000 已提交
375
			"Video", "BaseCY");
376
	ovi.output_width   = (uint32_t)config_get_uint(basicConfig,
J
jp9000 已提交
377
			"Video", "OutputCX");
378
	ovi.output_height  = (uint32_t)config_get_uint(basicConfig,
J
jp9000 已提交
379
			"Video", "OutputCY");
J
jp9000 已提交
380 381 382
	ovi.output_format  = VIDEO_FORMAT_I420;
	ovi.adapter        = 0;
	ovi.gpu_conversion = true;
383

J
jp9000 已提交
384
	QTToGSWindow(ui->preview->winId(), ovi.window);
J
jp9000 已提交
385 386

	//required to make opengl display stuff on osx(?)
J
jp9000 已提交
387
	ResizePreview(ovi.base_width, ovi.base_height);
J
jp9000 已提交
388

J
jp9000 已提交
389 390 391
	QSize size = ui->preview->size();
	ovi.window_width  = size.width();
	ovi.window_height = size.height();
J
jp9000 已提交
392

393 394 395 396 397
	if (!obs_reset_video(&ovi))
		return false;

	obs_add_draw_callback(OBSBasic::RenderMain, this);
	return true;
J
jp9000 已提交
398
}
J
jp9000 已提交
399

400
bool OBSBasic::ResetAudio()
J
jp9000 已提交
401
{
J
jp9000 已提交
402
	struct audio_output_info ai;
403 404 405
	ai.name = "Main Audio Track";
	ai.format = AUDIO_FORMAT_FLOAT;

406
	ai.samples_per_sec = config_get_uint(basicConfig, "Audio",
407 408
			"SampleRate");

409
	const char *channelSetupStr = config_get_string(basicConfig,
410 411 412 413 414 415 416
			"Audio", "ChannelSetup");

	if (strcmp(channelSetupStr, "Mono") == 0)
		ai.speakers = SPEAKERS_MONO;
	else
		ai.speakers = SPEAKERS_STEREO;

417
	ai.buffer_ms = config_get_uint(basicConfig, "Audio", "BufferingTime");
J
jp9000 已提交
418 419

	return obs_reset_audio(&ai);
J
jp9000 已提交
420 421
}

J
jp9000 已提交
422 423 424 425 426
bool OBSBasic::ResetAudioDevices()
{
	return false;
}

J
jp9000 已提交
427
void OBSBasic::ResizePreview(uint32_t cx, uint32_t cy)
428
{
J
jp9000 已提交
429
	double targetAspect, baseAspect;
430 431
	QSize  targetSize;
	int x, y;
J
jp9000 已提交
432

433
	/* resize preview panel to fix to the top section of the window */
J
jp9000 已提交
434
	targetSize   = ui->preview->size();
J
jp9000 已提交
435 436
	targetAspect = double(targetSize.width()) / double(targetSize.height());
	baseAspect   = double(cx) / double(cy);
437

438
	if (targetAspect > baseAspect) {
J
jp9000 已提交
439
		previewScale = float(targetSize.height()) / float(cy);
440 441 442
		cx = targetSize.height() * baseAspect;
		cy = targetSize.height();
	} else {
J
jp9000 已提交
443
		previewScale = float(targetSize.width()) / float(cx);
444 445 446 447
		cx = targetSize.width();
		cy = targetSize.width() / baseAspect;
	}

J
jp9000 已提交
448 449
	previewX = targetSize.width() /2 - cx/2;
	previewY = targetSize.height()/2 - cy/2;
J
jp9000 已提交
450

J
jp9000 已提交
451 452 453 454 455
	if (isVisible()) {
		if (resizeTimer)
			killTimer(resizeTimer);
		resizeTimer = startTimer(100);
	}
J
jp9000 已提交
456 457
}

J
jp9000 已提交
458
void OBSBasic::closeEvent(QCloseEvent *event)
J
jp9000 已提交
459
{
J
jp9000 已提交
460 461
	/* TODO */
	UNUSED_PARAMETER(event);
462 463
}

J
jp9000 已提交
464
void OBSBasic::changeEvent(QEvent *event)
465
{
J
jp9000 已提交
466 467
	/* TODO */
	UNUSED_PARAMETER(event);
468 469
}

J
jp9000 已提交
470
void OBSBasic::resizeEvent(QResizeEvent *event)
471
{
J
jp9000 已提交
472 473 474 475
	struct obs_video_info ovi;

	if (obs_get_video_info(&ovi))
		ResizePreview(ovi.base_width, ovi.base_height);
J
jp9000 已提交
476 477

	UNUSED_PARAMETER(event);
478 479
}

J
jp9000 已提交
480 481 482 483 484 485 486 487 488 489 490
void OBSBasic::timerEvent(QTimerEvent *event)
{
	if (event->timerId() == resizeTimer) {
		killTimer(resizeTimer);
		resizeTimer = 0;

		QSize size = ui->preview->size();
		obs_resize(size.width(), size.height());
	}
}

J
jp9000 已提交
491
void OBSBasic::on_action_New_triggered()
492
{
J
jp9000 已提交
493
	/* TODO */
494 495
}

J
jp9000 已提交
496
void OBSBasic::on_action_Open_triggered()
497
{
J
jp9000 已提交
498
	/* TODO */
499 500
}

J
jp9000 已提交
501
void OBSBasic::on_action_Save_triggered()
502
{
J
jp9000 已提交
503
	/* TODO */
504 505
}

506 507
void OBSBasic::on_scenes_currentItemChanged(QListWidgetItem *current,
		QListWidgetItem *prev)
508 509
{
	obs_source_t source = NULL;
J
jp9000 已提交
510

511 512 513 514
	if (sceneChanging)
		return;

	if (current) {
J
jp9000 已提交
515 516
		obs_scene_t scene;

517
		scene = current->data(Qt::UserRole).value<OBSScene>();
518 519 520
		source = obs_scene_getsource(scene);
	}

521
	/* TODO: allow transitions */
522
	obs_set_output_source(0, source);
523 524

	UNUSED_PARAMETER(prev);
525 526
}

J
jp9000 已提交
527
void OBSBasic::on_scenes_customContextMenuRequested(const QPoint &pos)
528
{
J
jp9000 已提交
529 530
	/* TODO */
	UNUSED_PARAMETER(pos);
531 532
}

J
jp9000 已提交
533
void OBSBasic::on_actionAddScene_triggered()
534
{
535
	string name;
J
jp9000 已提交
536 537 538
	bool accepted = NameDialog::AskForName(this,
			QTStr("MainWindow.AddSceneDlg.Title"),
			QTStr("MainWindow.AddSceneDlg.Text"),
539 540
			name);

J
jp9000 已提交
541
	if (accepted) {
542 543
		obs_source_t source = obs_get_source_by_name(name.c_str());
		if (source) {
J
jp9000 已提交
544 545 546
			QMessageBox::information(this,
					QTStr("MainWindow.NameExists.Title"),
					QTStr("MainWindow.NameExists.Text"));
547 548

			obs_source_release(source);
J
jp9000 已提交
549
			on_actionAddScene_triggered();
550 551 552
			return;
		}

553
		obs_scene_t scene = obs_scene_create(name.c_str());
554 555
		source = obs_scene_getsource(scene);
		obs_add_source(source);
556
		obs_scene_release(scene);
557 558

		obs_set_output_source(0, source);
559
	}
560 561
}

J
jp9000 已提交
562
void OBSBasic::on_actionRemoveScene_triggered()
563
{
J
jp9000 已提交
564 565
	QListWidgetItem *item = ui->scenes->currentItem();
	if (!item)
J
jp9000 已提交
566 567
		return;

J
jp9000 已提交
568
	QVariant userData = item->data(Qt::UserRole);
J
jp9000 已提交
569
	obs_scene_t scene = userData.value<OBSScene>();
J
jp9000 已提交
570 571
	obs_source_t source = obs_scene_getsource(scene);
	obs_source_remove(source);
572 573
}

J
jp9000 已提交
574
void OBSBasic::on_actionSceneProperties_triggered()
575
{
J
jp9000 已提交
576
	/* TODO */
577 578
}

J
jp9000 已提交
579
void OBSBasic::on_actionSceneUp_triggered()
580
{
J
jp9000 已提交
581
	/* TODO */
582 583
}

J
jp9000 已提交
584
void OBSBasic::on_actionSceneDown_triggered()
585
{
J
jp9000 已提交
586
	/* TODO */
587 588
}

589 590
void OBSBasic::on_sources_currentItemChanged(QListWidgetItem *current,
		QListWidgetItem *prev)
591
{
J
jp9000 已提交
592
	/* TODO */
593 594
	UNUSED_PARAMETER(current);
	UNUSED_PARAMETER(prev);
595 596
}

J
jp9000 已提交
597
void OBSBasic::on_sources_customContextMenuRequested(const QPoint &pos)
598
{
J
jp9000 已提交
599 600
	/* TODO */
	UNUSED_PARAMETER(pos);
601 602
}

603 604 605 606 607 608
void OBSBasic::AddSource(obs_scene_t scene, const char *id)
{
	string name;

	bool success = false;
	while (!success) {
J
jp9000 已提交
609
		bool accepted = NameDialog::AskForName(this,
610 611 612 613
				Str("MainWindow.AddSourceDlg.Title"),
				Str("MainWindow.AddSourceDlg.Text"),
				name);

J
jp9000 已提交
614
		if (!accepted)
615 616
			break;

J
jp9000 已提交
617
		obs_source_t source = obs_get_source_by_name(name.c_str());
618 619 620
		if (!source) {
			success = true;
		} else {
J
jp9000 已提交
621 622 623
			QMessageBox::information(this,
					QTStr("MainWindow.NameExists.Title"),
					QTStr("MainWindow.NameExists.Text"));
624 625 626 627 628
			obs_source_release(source);
		}
	}

	if (success) {
J
jp9000 已提交
629 630
		obs_source_t source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
				id, name.c_str(), NULL);
J
jp9000 已提交
631 632 633

		sourceSceneRefs[source] = 0;

634
		obs_add_source(source);
J
jp9000 已提交
635
		obs_scene_add(scene, source);
636 637 638 639
		obs_source_release(source);
	}
}

J
jp9000 已提交
640
void OBSBasic::AddSourcePopupMenu(const QPoint &pos)
641
{
642
	OBSScene scene = GetCurrentScene();
643
	const char *type;
J
jp9000 已提交
644 645
	bool foundValues = false;
	size_t idx = 0;
646

J
jp9000 已提交
647
	if (!scene)
648 649
		return;

J
jp9000 已提交
650 651
	QMenu popup;
	while (obs_enum_input_types(idx++, &type)) {
J
jp9000 已提交
652 653
		const char *name = obs_source_getdisplayname(
				OBS_SOURCE_TYPE_INPUT,
J
jp9000 已提交
654
				type, App()->GetLocale());
655

J
jp9000 已提交
656 657 658
		QAction *popupItem = new QAction(QT_UTF8(name), this);
		popupItem->setData(QT_UTF8(type));
		popup.addAction(popupItem);
659

J
jp9000 已提交
660
		foundValues = true;
661 662
	}

J
jp9000 已提交
663 664 665 666
	if (foundValues) {
		QAction *ret = popup.exec(pos);
		if (ret)
			AddSource(scene, ret->data().toString().toUtf8());
667 668 669
	}
}

J
jp9000 已提交
670
void OBSBasic::on_actionAddSource_triggered()
671
{
J
jp9000 已提交
672
	AddSourcePopupMenu(QCursor::pos());
673 674
}

J
jp9000 已提交
675
void OBSBasic::on_actionRemoveSource_triggered()
676
{
677
	OBSSceneItem item = GetCurrentSceneItem();
J
jp9000 已提交
678 679
	if (item)
		obs_sceneitem_remove(item);
680 681
}

J
jp9000 已提交
682
void OBSBasic::on_actionSourceProperties_triggered()
683 684 685
{
}

J
jp9000 已提交
686
void OBSBasic::on_actionSourceUp_triggered()
687 688
{
}
J
jp9000 已提交
689

J
jp9000 已提交
690
void OBSBasic::on_actionSourceDown_triggered()
691 692 693
{
}

J
jp9000 已提交
694 695 696 697 698 699 700 701 702
void OBSBasic::on_recordButton_clicked()
{
	if (outputTest) {
		obs_output_destroy(outputTest);
		outputTest = NULL;
		ui->recordButton->setText("Start Recording");
	} else {
		QString path = QFileDialog::getSaveFileName(this,
				"Please enter a file name", QString(),
703
				"MP4 Files (*.mp4)");
J
jp9000 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723

		if (path.isNull() || path.isEmpty())
			return;

		obs_data_t data = obs_data_create();
		obs_data_setstring(data, "filename", QT_TO_UTF8(path));

		outputTest = obs_output_create("ffmpeg_output", "test", data);
		obs_data_release(data);

		if (!obs_output_start(outputTest)) {
			obs_output_destroy(outputTest);
			outputTest = NULL;
			return;
		}

		ui->recordButton->setText("Stop Recording");
	}
}

J
jp9000 已提交
724
void OBSBasic::on_settingsButton_clicked()
J
jp9000 已提交
725
{
726 727
	OBSBasicSettings settings(this);
	settings.exec();
J
jp9000 已提交
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 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796

void OBSBasic::GetFPSCommon(uint32_t &num, uint32_t &den) const
{
	const char *val = config_get_string(basicConfig, "Video", "FPSCommon");

	if (strcmp(val, "10") == 0) {
		num = 10;
		den = 1;
	} else if (strcmp(val, "20") == 0) {
		num = 20;
		den = 1;
	} else if (strcmp(val, "25") == 0) {
		num = 25;
		den = 1;
	} else if (strcmp(val, "29.97") == 0) {
		num = 30000;
		den = 1001;
	} else if (strcmp(val, "48") == 0) {
		num = 48;
		den = 1;
	} else if (strcmp(val, "59.94") == 0) {
		num = 60000;
		den = 1001;
	} else if (strcmp(val, "60") == 0) {
		num = 60;
		den = 1;
	} else {
		num = 30;
		den = 1;
	}
}

void OBSBasic::GetFPSInteger(uint32_t &num, uint32_t &den) const
{
	num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSInt");
	den = 1;
}

void OBSBasic::GetFPSFraction(uint32_t &num, uint32_t &den) const
{
	num = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNum");
	den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSDen");
}

void OBSBasic::GetFPSNanoseconds(uint32_t &num, uint32_t &den) const
{
	num = 1000000000;
	den = (uint32_t)config_get_uint(basicConfig, "Video", "FPSNS");
}

void OBSBasic::GetConfigFPS(uint32_t &num, uint32_t &den) const
{
	uint32_t type = config_get_uint(basicConfig, "Video", "FPSType");

	if (type == 1) //"Integer"
		GetFPSInteger(num, den);
	else if (type == 2) //"Fraction"
		GetFPSFraction(num, den);
	else if (false) //"Nanoseconds", currently not implemented
		GetFPSNanoseconds(num, den);
	else
		GetFPSCommon(num, den);
}

config_t OBSBasic::Config() const
{
	return basicConfig;
}