| 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 | // |
| 5 | // W A R N I N G |
| 6 | // ------------- |
| 7 | // |
| 8 | // This file is not part of the Qt API. It exists purely as an |
| 9 | // implementation detail. This header file may change from version to |
| 10 | // version without notice, or even be removed. |
| 11 | // |
| 12 | // We mean it. |
| 13 | // |
| 14 | |
| 15 | #ifndef QPLATFORMMEDIARECORDER_H |
| 16 | #define QPLATFORMMEDIARECORDER_H |
| 17 | |
| 18 | #include <QtCore/qurl.h> |
| 19 | #include <QtCore/qsize.h> |
| 20 | #if QT_CONFIG(mimetype) |
| 21 | #include <QtCore/qmimetype.h> |
| 22 | #endif |
| 23 | #include <QtCore/qpointer.h> |
| 24 | #include <QtCore/qiodevice.h> |
| 25 | |
| 26 | #include <QtMultimedia/qmediarecorder.h> |
| 27 | #include <QtMultimedia/qmediametadata.h> |
| 28 | #include <QtMultimedia/qmediaformat.h> |
| 29 | #include <QtMultimedia/private/qerrorinfo_p.h> |
| 30 | #include <QtCore/private/qglobal_p.h> |
| 31 | |
| 32 | QT_BEGIN_NAMESPACE |
| 33 | |
| 34 | class QUrl; |
| 35 | QT_END_NAMESPACE |
| 36 | |
| 37 | QT_BEGIN_NAMESPACE |
| 38 | |
| 39 | class Q_MULTIMEDIA_EXPORT QMediaEncoderSettings |
| 40 | { |
| 41 | QMediaRecorder::EncodingMode m_encodingMode = QMediaRecorder::ConstantQualityEncoding; |
| 42 | QMediaRecorder::Quality m_quality = QMediaRecorder::NormalQuality; |
| 43 | |
| 44 | QMediaFormat m_format; |
| 45 | int m_audioBitrate = -1; |
| 46 | int m_audioSampleRate = -1; |
| 47 | int m_audioChannels = -1; |
| 48 | |
| 49 | QSize m_videoResolution = QSize(-1, -1); |
| 50 | int m_videoFrameRate = -1; |
| 51 | int m_videoBitRate = -1; |
| 52 | public: |
| 53 | |
| 54 | QMediaFormat mediaFormat() const { return m_format; } |
| 55 | void setMediaFormat(const QMediaFormat &format) { m_format = format; } |
| 56 | void resolveFormat(QMediaFormat::ResolveFlags flags = QMediaFormat::NoFlags) |
| 57 | { m_format.resolveForEncoding(flags); } |
| 58 | |
| 59 | QMediaFormat::FileFormat fileFormat() const { return mediaFormat().fileFormat(); } |
| 60 | QMediaFormat::VideoCodec videoCodec() const { return mediaFormat().videoCodec(); } |
| 61 | QMediaFormat::AudioCodec audioCodec() const { return mediaFormat().audioCodec(); } |
| 62 | #if QT_CONFIG(mimetype) |
| 63 | QMimeType mimeType() const { return mediaFormat().mimeType(); } |
| 64 | #endif |
| 65 | QString preferredSuffix() const |
| 66 | { |
| 67 | #if QT_CONFIG(mimetype) |
| 68 | return mimeType().preferredSuffix(); |
| 69 | #else |
| 70 | return QStringLiteral(""); |
| 71 | #endif |
| 72 | } |
| 73 | |
| 74 | QMediaRecorder::EncodingMode encodingMode() const { return m_encodingMode; } |
| 75 | void setEncodingMode(QMediaRecorder::EncodingMode mode) { m_encodingMode = mode; } |
| 76 | |
| 77 | QMediaRecorder::Quality quality() const { return m_quality; } |
| 78 | void setQuality(QMediaRecorder::Quality quality) { m_quality = quality; } |
| 79 | |
| 80 | QSize videoResolution() const { return m_videoResolution; } |
| 81 | void setVideoResolution(const QSize &size) { m_videoResolution = size; } |
| 82 | |
| 83 | qreal videoFrameRate() const { return m_videoFrameRate; } |
| 84 | void setVideoFrameRate(qreal rate) { m_videoFrameRate = rate; } |
| 85 | |
| 86 | int videoBitRate() const { return m_videoBitRate; } |
| 87 | void setVideoBitRate(int bitrate) { m_videoBitRate = bitrate; } |
| 88 | |
| 89 | int audioBitRate() const { return m_audioBitrate; } |
| 90 | void setAudioBitRate(int bitrate) { m_audioBitrate = bitrate; } |
| 91 | |
| 92 | int audioChannelCount() const { return m_audioChannels; } |
| 93 | void setAudioChannelCount(int channels) { m_audioChannels = channels; } |
| 94 | |
| 95 | int audioSampleRate() const { return m_audioSampleRate; } |
| 96 | void setAudioSampleRate(int rate) { m_audioSampleRate = rate; } |
| 97 | |
| 98 | bool operator==(const QMediaEncoderSettings &other) const |
| 99 | { |
| 100 | return m_format == other.m_format && |
| 101 | m_encodingMode == other.m_encodingMode && |
| 102 | m_quality == other.m_quality && |
| 103 | m_audioBitrate == other.m_audioBitrate && |
| 104 | m_audioSampleRate == other.m_audioSampleRate && |
| 105 | m_audioChannels == other.m_audioChannels && |
| 106 | m_videoResolution == other.m_videoResolution && |
| 107 | m_videoFrameRate == other.m_videoFrameRate && |
| 108 | m_videoBitRate == other.m_videoBitRate; |
| 109 | } |
| 110 | |
| 111 | bool operator!=(const QMediaEncoderSettings &other) const |
| 112 | { return !operator==(other); } |
| 113 | }; |
| 114 | |
| 115 | class Q_MULTIMEDIA_EXPORT QPlatformMediaRecorder |
| 116 | { |
| 117 | public: |
| 118 | virtual ~QPlatformMediaRecorder() {} |
| 119 | |
| 120 | virtual bool isLocationWritable(const QUrl &location) const = 0; |
| 121 | |
| 122 | virtual QMediaRecorder::RecorderState state() const { return m_state; } |
| 123 | virtual void record(QMediaEncoderSettings &settings) = 0; |
| 124 | virtual void pause(); |
| 125 | virtual void resume(); |
| 126 | virtual void stop() = 0; |
| 127 | |
| 128 | virtual qint64 duration() const { return m_duration; } |
| 129 | |
| 130 | virtual void setMetaData(const QMediaMetaData &) {} |
| 131 | virtual QMediaMetaData metaData() const { return {}; } |
| 132 | |
| 133 | QMediaRecorder::Error error() const { return m_error.code(); } |
| 134 | QString errorString() const { return m_error.description(); } |
| 135 | |
| 136 | QUrl outputLocation() const { return m_outputLocation; } |
| 137 | virtual void setOutputLocation(const QUrl &location) { m_outputLocation = location; } |
| 138 | QUrl actualLocation() const { return m_actualLocation; } |
| 139 | void clearActualLocation() { m_actualLocation.clear(); } |
| 140 | void clearError() { updateError(error: QMediaRecorder::NoError, errorString: QString()); } |
| 141 | |
| 142 | QIODevice *outputDevice() const { return m_outputDevice; } |
| 143 | void setOutputDevice(QIODevice *device) { m_outputDevice = device; } |
| 144 | |
| 145 | virtual void updateAutoStop() { } |
| 146 | |
| 147 | protected: |
| 148 | explicit QPlatformMediaRecorder(QMediaRecorder *parent); |
| 149 | |
| 150 | void stateChanged(QMediaRecorder::RecorderState state); |
| 151 | void durationChanged(qint64 position); |
| 152 | void actualLocationChanged(const QUrl &location); |
| 153 | void updateError(QMediaRecorder::Error error, const QString &errorString); |
| 154 | void metaDataChanged(); |
| 155 | |
| 156 | QMediaRecorder *mediaRecorder() { return q; } |
| 157 | |
| 158 | QString findActualLocation(const QMediaEncoderSettings &settings) const; |
| 159 | |
| 160 | private: |
| 161 | QMediaRecorder *q = nullptr; |
| 162 | QErrorInfo<QMediaRecorder::Error> m_error; |
| 163 | QUrl m_actualLocation; |
| 164 | QUrl m_outputLocation; |
| 165 | QPointer<QIODevice> m_outputDevice; |
| 166 | qint64 m_duration = 0; |
| 167 | |
| 168 | QMediaRecorder::RecorderState m_state = QMediaRecorder::StoppedState; |
| 169 | }; |
| 170 | |
| 171 | QT_END_NAMESPACE |
| 172 | |
| 173 | #endif |
| 174 |
