1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtMultimedia/private/qtmultimediaglobal_p.h>
41#include "camerabinaudioencoder.h"
42#include "camerabincontainer.h"
43#include <private/qgstutils_p.h>
44
45#include <QtCore/qdebug.h>
46
47QT_BEGIN_NAMESPACE
48
49CameraBinAudioEncoder::CameraBinAudioEncoder(QObject *parent)
50 :QAudioEncoderSettingsControl(parent)
51#if QT_CONFIG(gstreamer_encodingprofiles)
52 , m_codecs(QGstCodecsInfo::AudioEncoder)
53#endif
54{
55}
56
57CameraBinAudioEncoder::~CameraBinAudioEncoder()
58{
59}
60
61QStringList CameraBinAudioEncoder::supportedAudioCodecs() const
62{
63#if QT_CONFIG(gstreamer_encodingprofiles)
64 return m_codecs.supportedCodecs();
65#else
66 return QStringList();
67#endif
68}
69
70QString CameraBinAudioEncoder::codecDescription(const QString &codecName) const
71{
72#if QT_CONFIG(gstreamer_encodingprofiles)
73 return m_codecs.codecDescription(codec: codecName);
74#else
75 Q_UNUSED(codecName)
76 return QString();
77#endif
78}
79
80QList<int> CameraBinAudioEncoder::supportedSampleRates(const QAudioEncoderSettings &, bool *) const
81{
82 //TODO check element caps to find actual values
83
84 return QList<int>();
85}
86
87QAudioEncoderSettings CameraBinAudioEncoder::audioSettings() const
88{
89 return m_audioSettings;
90}
91
92void CameraBinAudioEncoder::setAudioSettings(const QAudioEncoderSettings &settings)
93{
94 if (m_audioSettings != settings) {
95 m_audioSettings = settings;
96 m_actualAudioSettings = settings;
97 emit settingsChanged();
98 }
99}
100
101QAudioEncoderSettings CameraBinAudioEncoder::actualAudioSettings() const
102{
103 return m_actualAudioSettings;
104}
105
106void CameraBinAudioEncoder::setActualAudioSettings(const QAudioEncoderSettings &settings)
107{
108 m_actualAudioSettings = settings;
109}
110
111void CameraBinAudioEncoder::resetActualSettings()
112{
113 m_actualAudioSettings = m_audioSettings;
114}
115
116#if QT_CONFIG(gstreamer_encodingprofiles)
117
118GstEncodingProfile *CameraBinAudioEncoder::createProfile()
119{
120 QString codec = m_actualAudioSettings.codec();
121 QString preset = m_actualAudioSettings.encodingOption(QStringLiteral("preset")).toString();
122 GstCaps *caps;
123
124 if (codec.isEmpty())
125 return 0;
126 else
127 caps = gst_caps_from_string(string: codec.toLatin1());
128
129 GstEncodingProfile *profile = (GstEncodingProfile *)gst_encoding_audio_profile_new(
130 format: caps,
131 preset: !preset.isEmpty() ? preset.toLatin1().constData() : NULL, //preset
132 NULL, //restriction
133 presence: 0); //presence
134
135 gst_caps_unref(caps);
136
137 return profile;
138}
139
140#endif
141
142void CameraBinAudioEncoder::applySettings(GstElement *encoder)
143{
144 GObjectClass * const objectClass = G_OBJECT_GET_CLASS(encoder);
145 const char * const name = qt_gst_element_get_factory_name(element: encoder);
146
147 const bool isVorbis = qstrcmp(str1: name, str2: "vorbisenc") == 0;
148
149 const int bitRate = m_actualAudioSettings.bitRate();
150 if (!isVorbis && bitRate == -1) {
151 // Bit rate is invalid, don't evaluate the remaining conditions unless the encoder is
152 // vorbisenc which is known to accept -1 as an unspecified bitrate.
153 } else if (g_object_class_find_property(oclass: objectClass, property_name: "bitrate")) {
154 g_object_set(G_OBJECT(encoder), first_property_name: "bitrate", bitRate, NULL);
155 } else if (g_object_class_find_property(oclass: objectClass, property_name: "target-bitrate")) {
156 g_object_set(G_OBJECT(encoder), first_property_name: "target-bitrate", bitRate, NULL);
157 }
158
159 if (isVorbis) {
160 static const double qualities[] = { 0.1, 0.3, 0.5, 0.7, 1.0 };
161 g_object_set(G_OBJECT(encoder), first_property_name: "quality", qualities[m_actualAudioSettings.quality()], NULL);
162 }
163}
164
165QT_END_NAMESPACE
166

source code of qtmultimedia/src/plugins/gstreamer/camerabin/camerabinaudioencoder.cpp