all repos — mgba @ d2f205aa9e7fdf2de299e01e7fab649c2ad6dc51

mGBA Game Boy Advance Emulator

src/platform/qt/GIFView.cpp (view raw)

 1/* Copyright (c) 2013-2014 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "GIFView.h"
 7
 8#ifdef USE_FFMPEG
 9
10#include "CoreController.h"
11#include "GBAApp.h"
12#include "LogController.h"
13
14#include <QMap>
15
16using namespace QGBA;
17
18GIFView::GIFView(QWidget* parent)
19	: QWidget(parent)
20{
21	m_ui.setupUi(this);
22
23	connect(m_ui.start, &QAbstractButton::clicked, this, &GIFView::startRecording);
24	connect(m_ui.stop, &QAbstractButton::clicked, this, &GIFView::stopRecording);
25
26	connect(m_ui.selectFile, &QAbstractButton::clicked, this, &GIFView::selectFile);
27	connect(m_ui.filename, &QLineEdit::textChanged, this, &GIFView::setFilename);
28
29	FFmpegEncoderInit(&m_encoder);
30	FFmpegEncoderSetAudio(&m_encoder, nullptr, 0);
31	FFmpegEncoderSetContainer(&m_encoder, "gif");
32}
33
34GIFView::~GIFView() {
35	stopRecording();
36}
37
38void GIFView::setController(std::shared_ptr<CoreController> controller) {
39	connect(controller.get(), &CoreController::stopping, this, &GIFView::stopRecording);
40	connect(this, &GIFView::recordingStarted, controller.get(), &CoreController::setAVStream);
41	connect(this, &GIFView::recordingStopped, controller.get(), &CoreController::clearAVStream, Qt::DirectConnection);
42	QSize size(controller->screenDimensions());
43	FFmpegEncoderSetDimensions(&m_encoder, size.width(), size.height());
44}
45
46void GIFView::startRecording() {
47	FFmpegEncoderSetVideo(&m_encoder, "gif", 0, m_ui.frameskip->value());
48	if (!FFmpegEncoderOpen(&m_encoder, m_filename.toUtf8().constData())) {
49		LOG(QT, ERROR) << tr("Failed to open output GIF file: %1").arg(m_filename);
50		return;
51	}
52	m_ui.start->setEnabled(false);
53	m_ui.stop->setEnabled(true);
54	m_ui.frameskip->setEnabled(false);
55	emit recordingStarted(&m_encoder.d);
56}
57
58void GIFView::stopRecording() {
59	emit recordingStopped();
60	FFmpegEncoderClose(&m_encoder);
61	m_ui.stop->setEnabled(false);
62	m_ui.start->setEnabled(true);
63	m_ui.frameskip->setEnabled(true);
64}
65
66void GIFView::selectFile() {
67	QString filename = GBAApp::app()->getSaveFileName(this, tr("Select output file"), tr("Graphics Interchange Format (*.gif)"));
68	if (!filename.isEmpty()) {
69		m_ui.filename->setText(filename);
70		if (!FFmpegEncoderIsOpen(&m_encoder)) {
71			m_ui.start->setEnabled(true);
72		}
73	}
74}
75
76void GIFView::setFilename(const QString& fname) {
77	m_filename = fname;
78}
79
80#endif