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 "qgstreamercaptureservice.h"
41#include "qgstreamercapturesession.h"
42#include "qgstreamerrecordercontrol.h"
43#include "qgstreamermediacontainercontrol.h"
44#include "qgstreameraudioencode.h"
45#include "qgstreamervideoencode.h"
46#include "qgstreamerimageencode.h"
47#include "qgstreamercameracontrol.h"
48#include <private/qgstreamerbushelper_p.h>
49#include "qgstreamercapturemetadatacontrol.h"
50
51#if defined(USE_GSTREAMER_CAMERA)
52#include "qgstreamerv4l2input.h"
53#endif
54
55#include "qgstreamerimagecapturecontrol.h"
56#include <private/qgstreameraudioinputselector_p.h>
57#include <private/qgstreamervideoinputdevicecontrol_p.h>
58#include <private/qgstreameraudioprobecontrol_p.h>
59
60#include <private/qgstreamervideorenderer_p.h>
61#include <private/qgstreamervideowindow_p.h>
62
63#if defined(HAVE_WIDGETS)
64#include <private/qgstreamervideowidget_p.h>
65#endif
66
67#include <qmediaserviceproviderplugin.h>
68
69QT_BEGIN_NAMESPACE
70
71QGstreamerCaptureService::QGstreamerCaptureService(const QString &service, QObject *parent)
72 : QMediaService(parent)
73 , m_captureSession(0)
74 , m_cameraControl(0)
75#if defined(USE_GSTREAMER_CAMERA)
76 , m_videoInput(0)
77#endif
78 , m_metaDataControl(0)
79 , m_audioInputSelector(0)
80 , m_videoInputDevice(0)
81 , m_videoOutput(0)
82 , m_videoRenderer(0)
83 , m_videoWindow(0)
84#if defined(HAVE_WIDGETS)
85 , m_videoWidgetControl(0)
86#endif
87 , m_imageCaptureControl(0)
88 , m_audioProbeControl(0)
89{
90 if (service == Q_MEDIASERVICE_AUDIOSOURCE) {
91 m_captureSession = new QGstreamerCaptureSession(QGstreamerCaptureSession::Audio, this);
92 }
93
94#if defined(USE_GSTREAMER_CAMERA)
95 if (service == Q_MEDIASERVICE_CAMERA) {
96 m_captureSession = new QGstreamerCaptureSession(QGstreamerCaptureSession::AudioAndVideo, this);
97 m_cameraControl = new QGstreamerCameraControl(m_captureSession);
98 m_videoInput = new QGstreamerV4L2Input(this);
99 m_captureSession->setVideoInput(m_videoInput);
100 m_videoInputDevice = new QGstreamerVideoInputDeviceControl(this);
101
102 connect(m_videoInputDevice, SIGNAL(selectedDeviceChanged(QString)),
103 m_videoInput, SLOT(setDevice(QString)));
104
105 if (m_videoInputDevice->deviceCount())
106 m_videoInput->setDevice(m_videoInputDevice->deviceName(m_videoInputDevice->selectedDevice()));
107
108 m_videoRenderer = new QGstreamerVideoRenderer(this);
109
110 m_videoWindow = new QGstreamerVideoWindow(this);
111 // If the GStreamer video sink is not available, don't provide the video window control since
112 // it won't work anyway.
113 if (!m_videoWindow->videoSink()) {
114 delete m_videoWindow;
115 m_videoWindow = 0;
116 }
117
118#if defined(HAVE_WIDGETS)
119 m_videoWidgetControl = new QGstreamerVideoWidgetControl(this);
120
121 // If the GStreamer video sink is not available, don't provide the video widget control since
122 // it won't work anyway. QVideoWidget will fall back to QVideoRendererControl in that case.
123 if (!m_videoWidgetControl->videoSink()) {
124 delete m_videoWidgetControl;
125 m_videoWidgetControl = 0;
126 }
127#endif
128 m_imageCaptureControl = new QGstreamerImageCaptureControl(m_captureSession);
129 }
130#endif
131
132 m_audioInputSelector = new QGstreamerAudioInputSelector(this);
133 connect(sender: m_audioInputSelector, SIGNAL(activeInputChanged(QString)), receiver: m_captureSession, SLOT(setCaptureDevice(QString)));
134
135 if (m_captureSession && m_audioInputSelector->availableInputs().size() > 0)
136 m_captureSession->setCaptureDevice(m_audioInputSelector->defaultInput());
137
138 m_metaDataControl = new QGstreamerCaptureMetaDataControl(this);
139 connect(sender: m_metaDataControl, SIGNAL(metaDataChanged(QMap<QByteArray,QVariant>)),
140 receiver: m_captureSession, SLOT(setMetaData(QMap<QByteArray,QVariant>)));
141}
142
143QGstreamerCaptureService::~QGstreamerCaptureService()
144{
145}
146
147QMediaControl *QGstreamerCaptureService::requestControl(const char *name)
148{
149 if (!m_captureSession)
150 return 0;
151
152 if (qstrcmp(str1: name,QAudioInputSelectorControl_iid) == 0)
153 return m_audioInputSelector;
154
155 if (qstrcmp(str1: name,QVideoDeviceSelectorControl_iid) == 0)
156 return m_videoInputDevice;
157
158 if (qstrcmp(str1: name,QMediaRecorderControl_iid) == 0)
159 return m_captureSession->recorderControl();
160
161 if (qstrcmp(str1: name,QAudioEncoderSettingsControl_iid) == 0)
162 return m_captureSession->audioEncodeControl();
163
164 if (qstrcmp(str1: name,QVideoEncoderSettingsControl_iid) == 0)
165 return m_captureSession->videoEncodeControl();
166
167 if (qstrcmp(str1: name,QImageEncoderControl_iid) == 0)
168 return m_captureSession->imageEncodeControl();
169
170
171 if (qstrcmp(str1: name,QMediaContainerControl_iid) == 0)
172 return m_captureSession->mediaContainerControl();
173
174 if (qstrcmp(str1: name,QCameraControl_iid) == 0)
175 return m_cameraControl;
176
177 if (qstrcmp(str1: name,QMetaDataWriterControl_iid) == 0)
178 return m_metaDataControl;
179
180 if (qstrcmp(str1: name, QCameraImageCaptureControl_iid) == 0)
181 return m_imageCaptureControl;
182
183 if (qstrcmp(str1: name,QMediaAudioProbeControl_iid) == 0) {
184 if (!m_audioProbeControl) {
185 m_audioProbeControl = new QGstreamerAudioProbeControl(this);
186 m_captureSession->addProbe(probe: m_audioProbeControl);
187 }
188 m_audioProbeControl->ref.ref();
189 return m_audioProbeControl;
190 }
191
192 if (!m_videoOutput) {
193 if (qstrcmp(str1: name, QVideoRendererControl_iid) == 0) {
194 m_videoOutput = m_videoRenderer;
195 } else if (qstrcmp(str1: name, QVideoWindowControl_iid) == 0) {
196 m_videoOutput = m_videoWindow;
197 }
198#if defined(HAVE_WIDGETS)
199 else if (qstrcmp(str1: name, QVideoWidgetControl_iid) == 0) {
200 m_videoOutput = m_videoWidgetControl;
201 }
202#endif
203
204 if (m_videoOutput) {
205 m_captureSession->setVideoPreview(m_videoOutput);
206 return m_videoOutput;
207 }
208 }
209
210 return 0;
211}
212
213void QGstreamerCaptureService::releaseControl(QMediaControl *control)
214{
215 if (!control) {
216 return;
217 } else if (control == m_videoOutput) {
218 m_videoOutput = 0;
219 m_captureSession->setVideoPreview(0);
220 } else if (control == m_audioProbeControl && !m_audioProbeControl->ref.deref()) {
221 m_captureSession->removeProbe(probe: m_audioProbeControl);
222 delete m_audioProbeControl;
223 m_audioProbeControl = 0;
224 }
225}
226
227QT_END_NAMESPACE
228

source code of qtmultimedia/src/plugins/gstreamer/mediacapture/qgstreamercaptureservice.cpp