summaryrefslogtreecommitdiffstats
path: root/src/plugins/multimedia/windows/mediacapture/qwindowsmediadevicesession.cpp
blob: b13599444cb86b14eba1abd5312fd3956b7275ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qwindowsmediadevicesession_p.h"

#include "qwindowsmediadevicereader_p.h"
#include "private/qwindowsmultimediautils_p.h"
#include "private/qplatformvideosink_p.h"
#include <qvideosink.h>
#include <QtCore/qdebug.h>
#include <qaudioinput.h>
#include <qaudiooutput.h>

QT_BEGIN_NAMESPACE

QWindowsMediaDeviceSession::QWindowsMediaDeviceSession(QObject *parent)
    : QObject(parent)
{
    m_mediaDeviceReader = new QWindowsMediaDeviceReader(this);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::streamingStarted,
            this, &QWindowsMediaDeviceSession::handleStreamingStarted);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::streamingStopped,
            this, &QWindowsMediaDeviceSession::handleStreamingStopped);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::streamingError,
            this, &QWindowsMediaDeviceSession::handleStreamingError);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::videoFrameChanged,
            this, &QWindowsMediaDeviceSession::handleVideoFrameChanged);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::recordingStarted,
            this, &QWindowsMediaDeviceSession::recordingStarted);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::recordingStopped,
            this, &QWindowsMediaDeviceSession::recordingStopped);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::recordingError,
            this, &QWindowsMediaDeviceSession::recordingError);
    connect(m_mediaDeviceReader, &QWindowsMediaDeviceReader::durationChanged,
            this, &QWindowsMediaDeviceSession::durationChanged);
}

QWindowsMediaDeviceSession::~QWindowsMediaDeviceSession()
{
    delete m_mediaDeviceReader;
}

bool QWindowsMediaDeviceSession::isActive() const
{
    return m_active;
}

bool QWindowsMediaDeviceSession::isActivating() const
{
    return m_activating;
}

void QWindowsMediaDeviceSession::setActive(bool active)
{
    if ((active && (m_active || m_activating)) || (!active && !m_active && !m_activating))
        return;

    if (active) {
        auto camId = QString::fromUtf8(m_activeCameraDevice.id());
        auto micId = m_audioInput ? QString::fromUtf8(m_audioInput->device().id()) : QString();
        if (!camId.isEmpty() || !micId.isEmpty()) {
            if (m_mediaDeviceReader->activate(camId, m_cameraFormat, micId)) {
                m_activating = true;
            } else {
                emit streamingError(MF_E_NOT_AVAILABLE);
            }
        } else {
            qWarning() << Q_FUNC_INFO << "Camera ID and Microphone ID both undefined.";
        }
    } else {
        m_mediaDeviceReader->deactivate();
        m_active = false;
        m_activating = false;
        emit activeChanged(m_active);
        emit readyForCaptureChanged(m_active);
    }
}

void QWindowsMediaDeviceSession::reactivate()
{
    if (m_active || m_activating) {
        pauseRecording();
        setActive(false);
        setActive(true);
        resumeRecording();
    }
}

void QWindowsMediaDeviceSession::setActiveCamera(const QCameraDevice &camera)
{
    m_activeCameraDevice = camera;
    reactivate();
}

QCameraDevice QWindowsMediaDeviceSession::activeCamera() const
{
    return m_activeCameraDevice;
}

void QWindowsMediaDeviceSession::setCameraFormat(const QCameraFormat &cameraFormat)
{
    m_cameraFormat = cameraFormat;
}

void QWindowsMediaDeviceSession::setVideoSink(QVideoSink *surface)
{
    m_surface = surface;
}

void QWindowsMediaDeviceSession::handleStreamingStarted()
{
    if (m_activating) {
        m_active = true;
        m_activating = false;
        emit activeChanged(m_active);
        emit readyForCaptureChanged(m_active);
    }
}

void QWindowsMediaDeviceSession::handleStreamingStopped()
{
    m_active = false;
    emit activeChanged(m_active);
    emit readyForCaptureChanged(m_active);
}

void QWindowsMediaDeviceSession::handleStreamingError(int errorCode)
{
    if (m_surface)
        m_surface->platformVideoSink()->setVideoFrame(QVideoFrame());
    emit streamingError(errorCode);
}

void QWindowsMediaDeviceSession::handleVideoFrameChanged(const QVideoFrame &frame)
{
    if (m_surface)
        m_surface->platformVideoSink()->setVideoFrame(frame);
    emit videoFrameChanged(frame);
}

void QWindowsMediaDeviceSession::setAudioInputMuted(bool muted)
{
    m_mediaDeviceReader->setInputMuted(muted);
}

void QWindowsMediaDeviceSession::setAudioInputVolume(float volume)
{
    m_mediaDeviceReader->setInputVolume(volume);
}

void QWindowsMediaDeviceSession::audioInputDeviceChanged()
{
    reactivate();
}

void QWindowsMediaDeviceSession::setAudioOutputMuted(bool muted)
{
    m_mediaDeviceReader->setOutputMuted(muted);
}

void QWindowsMediaDeviceSession::setAudioOutputVolume(float volume)
{
    m_mediaDeviceReader->setOutputVolume(volume);
}

void QWindowsMediaDeviceSession::audioOutputDeviceChanged()
{
    if (m_active || m_activating)
        m_mediaDeviceReader->setAudioOutput(QString::fromUtf8(m_audioOutput->device().id()));
}

void QWindowsMediaDeviceSession::setAudioInput(QAudioInput *input)
{
    if (m_audioInput == input)
        return;
    if (m_audioInput)
        m_audioInput->disconnect(this);
    m_audioInput = input;

    audioInputDeviceChanged();

    if (!m_audioInput)
        return;
    connect(m_audioInput, &QAudioInput::mutedChanged, this, &QWindowsMediaDeviceSession::setAudioInputMuted);
    connect(m_audioInput, &QAudioInput::volumeChanged, this, &QWindowsMediaDeviceSession::setAudioInputVolume);
    connect(m_audioInput, &QAudioInput::deviceChanged, this, &QWindowsMediaDeviceSession::audioInputDeviceChanged);
}

void QWindowsMediaDeviceSession::setAudioOutput(QAudioOutput *output)
{
    if (m_audioOutput == output)
        return;
    if (m_audioOutput)
        m_audioOutput->disconnect(this);
    m_audioOutput = output;
    if (!m_audioOutput) {
        m_mediaDeviceReader->setAudioOutput({});
        return;
    }

    m_mediaDeviceReader->setAudioOutput(QString::fromUtf8(m_audioOutput->device().id()));

    connect(m_audioOutput, &QAudioOutput::mutedChanged, this, &QWindowsMediaDeviceSession::setAudioOutputMuted);
    connect(m_audioOutput, &QAudioOutput::volumeChanged, this, &QWindowsMediaDeviceSession::setAudioOutputVolume);
    connect(m_audioOutput, &QAudioOutput::deviceChanged, this, &QWindowsMediaDeviceSession::audioOutputDeviceChanged);
}

QMediaRecorder::Error QWindowsMediaDeviceSession::startRecording(QMediaEncoderSettings &settings, const QString &fileName, bool audioOnly)
{
    GUID container = audioOnly ? QWindowsMultimediaUtils::containerForAudioFileFormat(settings.mediaFormat().fileFormat())
                               : QWindowsMultimediaUtils::containerForVideoFileFormat(settings.mediaFormat().fileFormat());
    GUID videoFormat = QWindowsMultimediaUtils::videoFormatForCodec(settings.videoCodec());
    GUID audioFormat = QWindowsMultimediaUtils::audioFormatForCodec(settings.audioCodec());

    QSize res = settings.videoResolution();
    UINT32 width, height;
    if (res.width() > 0 && res.height() > 0) {
        width = UINT32(res.width());
        height = UINT32(res.height());
    } else {
        width = m_mediaDeviceReader->frameWidth();
        height = m_mediaDeviceReader->frameHeight();
        settings.setVideoResolution(QSize(int(width), int(height)));
    }

    qreal frameRate = settings.videoFrameRate();
    if (frameRate <= 0) {
        frameRate = m_mediaDeviceReader->frameRate();
        settings.setVideoFrameRate(frameRate);
    }

    auto quality = settings.quality();

    UINT32 videoBitRate = 0;
    if (settings.videoBitRate() > 0) {
        videoBitRate = UINT32(settings.videoBitRate());
    } else {
        videoBitRate = estimateVideoBitRate(videoFormat, width, height, frameRate, quality);
        settings.setVideoBitRate(int(videoBitRate));
    }

    UINT32 audioBitRate = 0;
    if (settings.audioBitRate() > 0) {
        audioBitRate = UINT32(settings.audioBitRate());
    } else {
        audioBitRate = estimateAudioBitRate(audioFormat, quality);
        settings.setAudioBitRate(int(audioBitRate));
    }

    return m_mediaDeviceReader->startRecording(fileName, container, audioOnly ? GUID_NULL : videoFormat,
                                               videoBitRate, width, height, frameRate,
                                               audioFormat, audioBitRate);
}

void QWindowsMediaDeviceSession::stopRecording()
{
    m_mediaDeviceReader->stopRecording();
}

bool QWindowsMediaDeviceSession::pauseRecording()
{
    return m_mediaDeviceReader->pauseRecording();
}

bool QWindowsMediaDeviceSession::resumeRecording()
{
    return m_mediaDeviceReader->resumeRecording();
}

// empirical estimate of the required video bitrate (for H.264)
quint32 QWindowsMediaDeviceSession::estimateVideoBitRate(const GUID &videoFormat, quint32 width, quint32 height,
                                                         qreal frameRate, QMediaRecorder::Quality quality)
{
    Q_UNUSED(videoFormat);

    qreal bitsPerPixel;
    switch (quality) {
    case QMediaRecorder::Quality::VeryLowQuality:
        bitsPerPixel = 0.08;
        break;
    case QMediaRecorder::Quality::LowQuality:
        bitsPerPixel = 0.2;
        break;
    case QMediaRecorder::Quality::NormalQuality:
        bitsPerPixel = 0.3;
        break;
    case QMediaRecorder::Quality::HighQuality:
        bitsPerPixel = 0.5;
        break;
    case QMediaRecorder::Quality::VeryHighQuality:
        bitsPerPixel = 0.8;
        break;
    default:
        bitsPerPixel = 0.3;
    }

    // Required bitrate is not linear on the number of pixels; small resolutions
    // require more BPP, thus the minimum values, to try to compensate it.
    quint32 pixelsPerSec = quint32(qMax(width, 320u) * qMax(height, 240u) * qMax(frameRate, 6.0));
    return pixelsPerSec * bitsPerPixel;
}

quint32 QWindowsMediaDeviceSession::estimateAudioBitRate(const GUID &audioFormat, QMediaRecorder::Quality quality)
{
    if (audioFormat == MFAudioFormat_AAC) {
        // Bitrates supported by the AAC encoder are 96K, 128K, 160K, 192K.
        switch (quality) {
        case QMediaRecorder::Quality::VeryLowQuality:
            return 96000;
        case QMediaRecorder::Quality::LowQuality:
            return 96000;
        case QMediaRecorder::Quality::NormalQuality:
            return 128000;
        case QMediaRecorder::Quality::HighQuality:
            return 160000;
        case QMediaRecorder::Quality::VeryHighQuality:
            return 192000;
        default:
            return 128000;
        }
    } else if (audioFormat == MFAudioFormat_MP3) {
        // Bitrates supported by the MP3 encoder are
        // 32K, 40K, 48K, 56K, 64K, 80K, 96K, 112K, 128K, 160K, 192K, 224K, 256K, 320K.
        switch (quality) {
        case QMediaRecorder::Quality::VeryLowQuality:
            return 48000;
        case QMediaRecorder::Quality::LowQuality:
            return 96000;
        case QMediaRecorder::Quality::NormalQuality:
            return 128000;
        case QMediaRecorder::Quality::HighQuality:
            return 224000;
        case QMediaRecorder::Quality::VeryHighQuality:
            return 320000;
        default:
            return 128000;
        }
    } else if (audioFormat == MFAudioFormat_WMAudioV8) {
        // Bitrates supported by the Windows Media Audio 8 encoder
        switch (quality) {
        case QMediaRecorder::Quality::VeryLowQuality:
            return 32000;
        case QMediaRecorder::Quality::LowQuality:
            return 96000;
        case QMediaRecorder::Quality::NormalQuality:
            return 192000;
        case QMediaRecorder::Quality::HighQuality:
            return 256016;
        case QMediaRecorder::Quality::VeryHighQuality:
            return 320032;
        default:
            return 192000;
        }
    } else if (audioFormat == MFAudioFormat_WMAudioV9) {
        // Bitrates supported by the Windows Media Audio 9 encoder
        switch (quality) {
        case QMediaRecorder::Quality::VeryLowQuality:
            return 32000;
        case QMediaRecorder::Quality::LowQuality:
            return 96000;
        case QMediaRecorder::Quality::NormalQuality:
            return 192000;
        case QMediaRecorder::Quality::HighQuality:
            return 256016;
        case QMediaRecorder::Quality::VeryHighQuality:
            return 384000;
        default:
            return 192000;
        }
    }
    return 0;  // Use default for format
}

QT_END_NAMESPACE

#include "moc_qwindowsmediadevicesession_p.cpp"