1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "videosettings.h"
52#include "ui_videosettings.h"
53
54#include <QComboBox>
55#include <QDebug>
56#include <QMediaRecorder>
57#include <QMediaService>
58
59
60VideoSettings::VideoSettings(QMediaRecorder *mediaRecorder, QWidget *parent) :
61 QDialog(parent),
62 ui(new Ui::VideoSettingsUi),
63 mediaRecorder(mediaRecorder)
64{
65 ui->setupUi(this);
66
67 //audio codecs
68 ui->audioCodecBox->addItem(atext: tr(s: "Default audio codec"), auserData: QVariant(QString()));
69 const QStringList supportedAudioCodecs = mediaRecorder->supportedAudioCodecs();
70 for (const QString &codecName : supportedAudioCodecs) {
71 QString description = mediaRecorder->audioCodecDescription(codecName);
72 ui->audioCodecBox->addItem(atext: codecName + ": " + description, auserData: QVariant(codecName));
73 }
74
75 //sample rate:
76 const QList<int>supportedAudioSampleRates = mediaRecorder->supportedAudioSampleRates();
77 for (int sampleRate : supportedAudioSampleRates)
78 ui->audioSampleRateBox->addItem(atext: QString::number(sampleRate), auserData: QVariant(sampleRate));
79
80 ui->audioQualitySlider->setRange(min: 0, max: int(QMultimedia::VeryHighQuality));
81
82 //video codecs
83 ui->videoCodecBox->addItem(atext: tr(s: "Default video codec"), auserData: QVariant(QString()));
84 const QStringList supportedVideoCodecs = mediaRecorder->supportedVideoCodecs();
85 for (const QString &codecName : supportedVideoCodecs) {
86 QString description = mediaRecorder->videoCodecDescription(codecName);
87 ui->videoCodecBox->addItem(atext: codecName + ": " + description, auserData: QVariant(codecName));
88 }
89
90 ui->videoQualitySlider->setRange(min: 0, max: int(QMultimedia::VeryHighQuality));
91
92
93 ui->videoResolutionBox->addItem(atext: tr(s: "Default"));
94 const QList<QSize> supportedResolutions = mediaRecorder->supportedResolutions();
95 for (const QSize &resolution : supportedResolutions) {
96 ui->videoResolutionBox->addItem(atext: QString("%1x%2").arg(a: resolution.width()).arg(a: resolution.height()),
97 auserData: QVariant(resolution));
98 }
99
100 ui->videoFramerateBox->addItem(atext: tr(s: "Default"));
101 const QList<qreal> supportedFrameRates = mediaRecorder->supportedFrameRates();
102 for (qreal rate : supportedFrameRates) {
103 QString rateString = QString("%1").arg(a: rate, fieldWidth: 0, fmt: 'f', prec: 2);
104 ui->videoFramerateBox->addItem(atext: rateString, auserData: QVariant(rate));
105 }
106
107 //containers
108 ui->containerFormatBox->addItem(atext: tr(s: "Default container"), auserData: QVariant(QString()));
109 const QStringList formats = mediaRecorder->supportedContainers();
110 for (const QString &format : formats) {
111 ui->containerFormatBox->addItem(atext: format + ": " + mediaRecorder->containerDescription(format),
112 auserData: QVariant(format));
113 }
114}
115
116VideoSettings::~VideoSettings()
117{
118 delete ui;
119}
120
121void VideoSettings::changeEvent(QEvent *e)
122{
123 QDialog::changeEvent(e);
124 switch (e->type()) {
125 case QEvent::LanguageChange:
126 ui->retranslateUi(VideoSettingsUi: this);
127 break;
128 default:
129 break;
130 }
131}
132
133QAudioEncoderSettings VideoSettings::audioSettings() const
134{
135 QAudioEncoderSettings settings = mediaRecorder->audioSettings();
136 settings.setCodec(boxValue(ui->audioCodecBox).toString());
137 settings.setQuality(QMultimedia::EncodingQuality(ui->audioQualitySlider->value()));
138 settings.setSampleRate(boxValue(ui->audioSampleRateBox).toInt());
139 return settings;
140}
141
142void VideoSettings::setAudioSettings(const QAudioEncoderSettings &audioSettings)
143{
144 selectComboBoxItem(box: ui->audioCodecBox, value: QVariant(audioSettings.codec()));
145 selectComboBoxItem(box: ui->audioSampleRateBox, value: QVariant(audioSettings.sampleRate()));
146 ui->audioQualitySlider->setValue(audioSettings.quality());
147}
148
149QVideoEncoderSettings VideoSettings::videoSettings() const
150{
151 QVideoEncoderSettings settings = mediaRecorder->videoSettings();
152 settings.setCodec(boxValue(ui->videoCodecBox).toString());
153 settings.setQuality(QMultimedia::EncodingQuality(ui->videoQualitySlider->value()));
154 settings.setResolution(boxValue(ui->videoResolutionBox).toSize());
155 settings.setFrameRate(boxValue(ui->videoFramerateBox).value<qreal>());
156
157 return settings;
158}
159
160void VideoSettings::setVideoSettings(const QVideoEncoderSettings &videoSettings)
161{
162 selectComboBoxItem(box: ui->videoCodecBox, value: QVariant(videoSettings.codec()));
163 selectComboBoxItem(box: ui->videoResolutionBox, value: QVariant(videoSettings.resolution()));
164 ui->videoQualitySlider->setValue(videoSettings.quality());
165
166 //special case for frame rate
167 for (int i = 0; i < ui->videoFramerateBox->count(); ++i) {
168 qreal itemRate = ui->videoFramerateBox->itemData(index: i).value<qreal>();
169 if (qFuzzyCompare(p1: itemRate, p2: videoSettings.frameRate())) {
170 ui->videoFramerateBox->setCurrentIndex(i);
171 break;
172 }
173 }
174}
175
176QString VideoSettings::format() const
177{
178 return boxValue(ui->containerFormatBox).toString();
179}
180
181void VideoSettings::setFormat(const QString &format)
182{
183 selectComboBoxItem(box: ui->containerFormatBox, value: QVariant(format));
184}
185
186QVariant VideoSettings::boxValue(const QComboBox *box) const
187{
188 int idx = box->currentIndex();
189 if (idx == -1)
190 return QVariant();
191
192 return box->itemData(index: idx);
193}
194
195void VideoSettings::selectComboBoxItem(QComboBox *box, const QVariant &value)
196{
197 for (int i = 0; i < box->count(); ++i) {
198 if (box->itemData(index: i) == value) {
199 box->setCurrentIndex(i);
200 break;
201 }
202 }
203}
204

source code of qtmultimedia/examples/multimediawidgets/camera/videosettings.cpp