Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwindowsmediadevicereader_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QWINDOWSMEDIADEVICEREADER_H
5#define QWINDOWSMEDIADEVICEREADER_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtMultimedia/qcameradevice.h>
19#include <QtMultimedia/qmediarecorder.h>
20#include <QtMultimedia/qvideoframe.h>
21#include <QtCore/qmutex.h>
22#include <QtCore/qobject.h>
23#include <QtCore/qtimer.h>
24#include <QtCore/qwaitcondition.h>
25
26#include <QtCore/private/qcomobject_p.h>
27#include <QtCore/private/qcomptr_p.h>
28
29#include <mfapi.h>
30#include <mferror.h>
31#include <mfidl.h>
32#include <mfreadwrite.h>
33
34QT_BEGIN_NAMESPACE
35
36class QVideoSink;
37
39 : public QObject,
42{
44public:
45 explicit QWindowsMediaDeviceReader(QObject *parent = nullptr);
47
48 //from IMFSourceReaderCallback
49 STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex,
50 DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample) override;
51 STDMETHODIMP OnFlush(DWORD dwStreamIndex) override;
52 STDMETHODIMP OnEvent(DWORD dwStreamIndex, IMFMediaEvent *pEvent) override;
53
54 //from IMFSinkWriterCallback
55 STDMETHODIMP OnFinalize(HRESULT hrStatus) override;
56 STDMETHODIMP OnMarker(DWORD dwStreamIndex, LPVOID pvContext) override;
57
58 bool activate(const QString &cameraId,
59 const QCameraFormat &cameraFormat,
60 const QString &microphoneId);
61 void deactivate();
62
63 QMediaRecorder::Error startRecording(const QString &fileName, const GUID &container,
64 const GUID &videoFormat, UINT32 videoBitRate, UINT32 width,
65 UINT32 height, qreal frameRate, const GUID &audioFormat,
66 UINT32 audioBitRate);
67 void stopRecording();
68 bool pauseRecording();
69 bool resumeRecording();
70
71 UINT32 frameWidth() const;
72 UINT32 frameHeight() const;
73 qreal frameRate() const;
74 void setInputMuted(bool muted);
75 void setInputVolume(qreal volume);
76 void setOutputMuted(bool muted);
77 void setOutputVolume(qreal volume);
78 bool setAudioOutput(const QString &audioOutputId);
79
83 void streamingError(HRESULT errorCode);
86 void recordingError(HRESULT errorCode);
87 void durationChanged(qint64 duration);
88 void videoFrameChanged(const QVideoFrame &frame);
89
90private slots:
91 void updateDuration();
92
93private:
94 HRESULT createSource(const QString &deviceId, bool video, ComPtr<IMFMediaSource> &source);
95 HRESULT createAggregateReader(const ComPtr<IMFMediaSource> &firstSource,
96 const ComPtr<IMFMediaSource> &secondSource,
97 ComPtr<IMFMediaSource> &aggregateSource,
98 ComPtr<IMFSourceReader> &sourceReader);
99 HRESULT createVideoMediaType(const GUID &format, UINT32 bitRate, UINT32 width, UINT32 height,
100 qreal frameRate, ComPtr<IMFMediaType> &mediaType);
101 HRESULT createAudioMediaType(const GUID &format, UINT32 bitRate, ComPtr<IMFMediaType> &mediaType);
102 HRESULT initAudioType(const ComPtr<IMFMediaType> &mediaType, UINT32 channels, UINT32 samplesPerSec, bool flt);
103 HRESULT prepareVideoStream(DWORD mediaTypeIndex);
104 HRESULT prepareAudioStream();
105 HRESULT initSourceIndexes();
106 HRESULT updateSinkInputMediaTypes();
107 HRESULT startMonitoring();
108 void stopMonitoring();
109 void releaseResources();
110 void stopStreaming();
111 DWORD findMediaTypeIndex(const QCameraFormat &reqFormat);
112
113 QMutex m_mutex;
114 QWaitCondition m_hasFinalized;
115 ComPtr<IMFMediaSource> m_videoSource;
116 ComPtr<IMFMediaType> m_videoMediaType;
117 ComPtr<IMFMediaSource> m_audioSource;
118 ComPtr<IMFMediaType> m_audioMediaType;
119 ComPtr<IMFMediaSource> m_aggregateSource;
120 ComPtr<IMFSourceReader> m_sourceReader;
121 ComPtr<IMFSinkWriter> m_sinkWriter;
122 ComPtr<IMFMediaSink> m_monitorSink;
123 ComPtr<IMFSinkWriter> m_monitorWriter;
124 QString m_audioOutputId;
125 DWORD m_sourceVideoStreamIndex = MF_SOURCE_READER_INVALID_STREAM_INDEX;
126 DWORD m_sourceAudioStreamIndex = MF_SOURCE_READER_INVALID_STREAM_INDEX;
127 DWORD m_sinkVideoStreamIndex = MF_SINK_WRITER_INVALID_STREAM_INDEX;
128 DWORD m_sinkAudioStreamIndex = MF_SINK_WRITER_INVALID_STREAM_INDEX;
129 UINT32 m_frameWidth = 0;
130 UINT32 m_frameHeight = 0;
131 qreal m_frameRate = 0.0;
132 LONG m_stride = 0;
133 bool m_active = false;
134 bool m_streaming = false;
135 bool m_recording = false;
136 bool m_firstFrame = false;
137 bool m_paused = false;
138 bool m_pauseChanging = false;
139 bool m_inputMuted = false;
140 bool m_outputMuted = false;
141 qreal m_inputVolume = 1.0;
142 qreal m_outputVolume = 1.0;
143 QVideoFrameFormat::PixelFormat m_pixelFormat = QVideoFrameFormat::Format_Invalid;
144 LONGLONG m_timeOffset = 0;
145 LONGLONG m_pauseTime = 0;
146 LONGLONG m_lastTimestamp = 0;
147 QTimer m_durationTimer;
148 qint64 m_currentDuration = -1;
149 qint64 m_lastDuration = -1;
150 QVideoFrameFormat::ColorRange m_colorRange = QVideoFrameFormat::ColorRange_Unknown;
151 QVideoFrameFormat::ColorSpace m_colorSpace = QVideoFrameFormat::ColorSpace_Undefined;
152};
153
154QT_END_NAMESPACE
155
156#endif // QWINDOWSMEDIADEVICEREADER_H
void recordingError(HRESULT errorCode)
void durationChanged(qint64 duration)
bool activate(const QString &cameraId, const QCameraFormat &cameraFormat, const QString &microphoneId)
void videoFrameChanged(const QVideoFrame &frame)
void streamingError(HRESULT errorCode)
bool setAudioOutput(const QString &audioOutputId)
QMediaRecorder::Error startRecording(const QString &fileName, const GUID &container, const GUID &videoFormat, UINT32 videoBitRate, UINT32 width, UINT32 height, qreal frameRate, const GUID &audioFormat, UINT32 audioBitRate)
STDMETHODIMP OnFlush(DWORD dwStreamIndex) override
STDMETHODIMP OnFinalize(HRESULT hrStatus) override
STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex, DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample) override
STDMETHODIMP OnMarker(DWORD dwStreamIndex, LPVOID pvContext) override
STDMETHODIMP OnEvent(DWORD dwStreamIndex, IMFMediaEvent *pEvent) override
Combined button and popup list for selecting options.