Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qffmpegmediaintegration.cpp
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#include <QtMultimedia/private/qplatformmediaplugin_p.h>
5#include <qcameradevice.h>
13#include "qffmpegaudioinput_p.h"
15#include "qffmpegresampler_p.h"
17#include "qffmpegconverter_p.h"
18
19#ifdef Q_OS_MACOS
20#include <VideoToolbox/VideoToolbox.h>
21
23#include "qcgwindowcapture_p.h"
24#include "qavfscreencapture_p.h"
25#endif
26
27#ifdef Q_OS_DARWIN
28#include "qavfcamera_p.h"
29
30#elif defined(Q_OS_WINDOWS)
31#include "qwindowscamera_p.h"
35#include "qgdiwindowcapture_p.h"
36#endif
37
38#ifdef Q_OS_ANDROID
39# include "jni.h"
41# include "qandroidcamera_p.h"
43extern "C" {
44# include <libavutil/log.h>
45# include <libavcodec/jni.h>
46}
47#endif
48
49#if QT_CONFIG(linux_v4l)
50#include "qv4l2camera_p.h"
52#endif
53
54#if QT_CONFIG(cpp_winrt)
56#endif
57
58#if QT_CONFIG(xlib)
61#endif
62
63#if QT_CONFIG(eglfs)
65#endif
66
68
70{
73
74public:
78
80 {
81 if (name == u"ffmpeg")
82 return new QFFmpegMediaIntegration;
83 return nullptr;
84 }
85};
86
87bool thread_local FFmpegLogsEnabledInThread = true;
88static bool UseCustomFFmpegLogger = false;
89
90static void qffmpegLogCallback(void *ptr, int level, const char *fmt, va_list vl)
91{
93 return;
94
96 return av_log_default_callback(ptr, level, fmt, vl);
97
98 // filter logs above the chosen level and AV_LOG_QUIET (negative level)
99 if (level < 0 || level > av_log_get_level())
100 return;
101
102 QString message = QStringLiteral("FFmpeg log: %1").arg(QString::vasprintf(fmt, vl));
103 if (message.endsWith("\n"))
104 message.removeLast();
105
106 if (level == AV_LOG_DEBUG || level == AV_LOG_TRACE)
107 qDebug() << message;
108 else if (level == AV_LOG_VERBOSE || level == AV_LOG_INFO)
109 qInfo() << message;
110 else if (level == AV_LOG_WARNING)
111 qWarning() << message;
112 else if (level == AV_LOG_ERROR || level == AV_LOG_FATAL || level == AV_LOG_PANIC)
113 qCritical() << message;
114}
115
116static void setupFFmpegLogger()
117{
118 if (qEnvironmentVariableIsSet("QT_FFMPEG_DEBUG")) {
119 av_log_set_level(AV_LOG_DEBUG);
121 }
122
123 av_log_set_callback(&qffmpegLogCallback);
124}
125
127{
128 if (backend == u"grabwindow")
130
131#if QT_CONFIG(eglfs)
132 if (backend == u"eglfs")
133 return new QEglfsScreenCapture;
134#endif
135
136#if QT_CONFIG(xlib)
137 if (backend == u"x11")
139#elif defined(Q_OS_WINDOWS)
140 if (backend == u"dxgi")
141 return new QFFmpegScreenCaptureDxgi;
142#elif defined(Q_OS_MACOS)
143 if (backend == u"avf")
144 return new QAVFScreenCapture;
145#endif
146 return nullptr;
147}
148
150{
151 if (backend == u"grabwindow")
153
154#if QT_CONFIG(xlib)
155 if (backend == u"x11")
157#elif defined(Q_OS_WINDOWS)
158 if (backend == u"gdi")
159 return new QGdiWindowCapture;
160#if QT_CONFIG(cpp_winrt)
161 if (backend == u"uwp")
162 return new QFFmpegWindowCaptureUwp;
163#endif
164#elif defined(Q_OS_MACOS)
165 if (backend == u"cg")
166 return new QCGWindowCapture;
167#endif
168 return nullptr;
169}
170
173{
175
176#ifndef QT_NO_DEBUG
177 qDebug() << "Available HW decoding frameworks:";
179 qDebug() << " " << av_hwdevice_get_type_name(type);
180
181 qDebug() << "Available HW encoding frameworks:";
183 qDebug() << " " << av_hwdevice_get_type_name(type);
184#endif
185}
186
187QMaybe<QPlatformAudioDecoder *> QFFmpegMediaIntegration::createAudioDecoder(QAudioDecoder *decoder)
188{
189 return new QFFmpegAudioDecoder(decoder);
190}
191
192QMaybe<std::unique_ptr<QPlatformAudioResampler>>
194 const QAudioFormat &outputFormat)
195{
196 return { std::make_unique<QFFmpegResampler>(inputFormat, outputFormat) };
197}
198
199QMaybe<QPlatformMediaCaptureSession *> QFFmpegMediaIntegration::createCaptureSession()
200{
201 return new QFFmpegMediaCaptureSession();
202}
203
205{
206 return new QFFmpegMediaPlayer(player);
207}
208
210{
211#ifdef Q_OS_DARWIN
212 return new QAVFCamera(camera);
213#elif defined(Q_OS_ANDROID)
214 return new QAndroidCamera(camera);
215#elif QT_CONFIG(linux_v4l)
216 return new QV4L2Camera(camera);
217#elif defined(Q_OS_WINDOWS)
218 return new QWindowsCamera(camera);
219#else
221 return nullptr;//new QFFmpegCamera(camera);
222#endif
223}
224
226{
227 static const QString screenCaptureBackend = qgetenv("QT_SCREEN_CAPTURE_BACKEND").toLower();
228
229 if (!screenCaptureBackend.isEmpty()) {
230 if (auto screenCapture = createScreenCaptureByBackend(screenCaptureBackend))
231 return screenCapture;
232
233 qWarning() << "Not supported QT_SCREEN_CAPTURE_BACKEND:" << screenCaptureBackend;
234 }
235
236#if QT_CONFIG(xlib)
239#endif
240
241#if QT_CONFIG(eglfs)
243 return new QEglfsScreenCapture;
244#endif
245
246#if defined(Q_OS_WINDOWS)
247 return new QFFmpegScreenCaptureDxgi;
248#elif defined(Q_OS_MACOS) // TODO: probably use it for iOS as well
249 return new QAVFScreenCapture;
250#else
252#endif
253}
254
256{
257 static const QString windowCaptureBackend = qgetenv("QT_WINDOW_CAPTURE_BACKEND").toLower();
258
259 if (!windowCaptureBackend.isEmpty()) {
260 if (auto windowCapture = createWindowCaptureByBackend(windowCaptureBackend))
261 return windowCapture;
262
263 qWarning() << "Not supported QT_WINDOW_CAPTURE_BACKEND:" << windowCaptureBackend;
264 }
265
266#if QT_CONFIG(xlib)
269#endif
270
271#if defined(Q_OS_WINDOWS)
272# if QT_CONFIG(cpp_winrt)
274 return new QFFmpegWindowCaptureUwp;
275# endif
276
277 return new QGdiWindowCapture;
278#elif defined(Q_OS_MACOS) // TODO: probably use it for iOS as well
279 return new QCGWindowCapture;
280#else
282#endif
283}
284
286{
287 return new QFFmpegMediaRecorder(recorder);
288}
289
291{
292#if defined(Q_OS_ANDROID)
294#else
296#endif
297}
298
300{
301 return new QFFmpegVideoSink(sink);
302}
303
305{
306 return new QFFmpegAudioInput(input);
307}
308
310 const QVideoFrameFormat &destFormat)
311{
312 return convertFrame(srcFrame, destFormat);
313}
314
319
321{
322#if defined(Q_OS_ANDROID)
323 return new QAndroidVideoDevices(this);
324#elif QT_CONFIG(linux_v4l)
325 return new QV4L2CameraDevices(this);
326#elif defined Q_OS_DARWIN
327 return new QAVFVideoDevices(this);
328#elif defined(Q_OS_WINDOWS)
329 return new QWindowsVideoDevices(this);
330#else
331 return nullptr;
332#endif
333}
334
336{
337#if QT_CONFIG(xlib)
339 return new QX11CapturableWindows;
340#elif defined Q_OS_MACOS
341 return new QCGCapturableWindows;
342#elif defined(Q_OS_WINDOWS)
343 return new QWinCapturableWindows;
344#endif
345 return nullptr;
346}
347
348#ifdef Q_OS_ANDROID
349
350Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/)
351{
352 static bool initialized = false;
353 if (initialized)
354 return JNI_VERSION_1_6;
355 initialized = true;
356
358 void *environment;
359 if (vm->GetEnv(&environment, JNI_VERSION_1_6))
360 return JNI_ERR;
361
362 // setting our javavm into ffmpeg.
363 if (av_jni_set_java_vm(vm, nullptr))
364 return JNI_ERR;
365
367 return JNI_ERR;
368
369 return JNI_VERSION_1_6;
370}
371#endif
372
374
375#include "qffmpegmediaintegration.moc"
QMediaPlayer player
Definition audio.cpp:213
static bool registerNativeMethods()
The QAudioDecoder class implements decoding audio.
The QAudioFormat class stores audio stream parameter information.
\qmltype AudioInput \instantiates QAudioInput
Definition qaudioinput.h:19
The QCamera class provides interface for system camera devices.
Definition qcamera.h:28
\inmodule QtMultimedia
QMaybe< std::unique_ptr< QPlatformAudioResampler > > createAudioResampler(const QAudioFormat &inputFormat, const QAudioFormat &outputFormat) override
QPlatformMediaFormatInfo * createFormatInfo() override
QPlatformSurfaceCapture * createScreenCapture(QScreenCapture *) override
QMaybe< QPlatformMediaPlayer * > createPlayer(QMediaPlayer *player) override
QMaybe< QPlatformVideoSink * > createVideoSink(QVideoSink *sink) override
QVideoFrame convertVideoFrame(QVideoFrame &srcFrame, const QVideoFrameFormat &destFormat) override
QMaybe< QPlatformAudioInput * > createAudioInput(QAudioInput *input) override
QMaybe< QPlatformAudioDecoder * > createAudioDecoder(QAudioDecoder *decoder) override
QMaybe< QPlatformImageCapture * > createImageCapture(QImageCapture *) override
QMaybe< QPlatformMediaRecorder * > createRecorder(QMediaRecorder *) override
QMaybe< QPlatformMediaCaptureSession * > createCaptureSession() override
QMaybe< QPlatformCamera * > createCamera(QCamera *) override
QPlatformVideoDevices * createVideoDevices() override
QPlatformCapturableWindows * createCapturableWindows() override
QPlatformSurfaceCapture * createWindowCapture(QWindowCapture *) override
QPlatformMediaIntegration * create(const QString &name) override
static const std::vector< AVHWDeviceType > & decodingDeviceTypes()
static const std::vector< AVHWDeviceType > & encodingDeviceTypes()
\inmodule QtMultimedia
The QMediaPlayer class allows the playing of a media files.
\inmodule QtMultimedia
\inmodule QtMultimedia
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1
Definition qstring.cpp:7357
The QVideoFrameFormat class specifies the stream format of a video presentation surface.
The QVideoFrame class represents a frame of video data.
Definition qvideoframe.h:27
The QVideoSink class represents a generic sink for video data.
Definition qvideosink.h:22
\inmodule QtMultimedia
QMediaRecorder * recorder
Definition camera.cpp:20
QCamera * camera
Definition camera.cpp:19
QImageCapture * imageCapture
Definition camera.cpp:21
Combined button and popup list for selecting options.
#define Q_DECL_EXPORT
QVideoFrame convertFrame(QVideoFrame &src, const QVideoFrameFormat &dstFormat)
static bool UseCustomFFmpegLogger
static QPlatformSurfaceCapture * createScreenCaptureByBackend(QString backend)
static QPlatformSurfaceCapture * createWindowCaptureByBackend(QString backend)
bool thread_local FFmpegLogsEnabledInThread
static void qffmpegLogCallback(void *ptr, int level, const char *fmt, va_list vl)
static void setupFFmpegLogger()
QT_END_NAMESPACE JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
#define qCritical
Definition qlogging.h:167
#define qInfo
Definition qlogging.h:165
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
static ControlElement< T > * ptr(QWidget *widget)
GLenum GLuint GLint level
GLenum type
GLuint GLsizei const GLchar * message
GLuint name
GLsizei GLenum GLboolean sink
GLenum GLenum GLenum input
#define QPlatformMediaPlugin_iid
#define QStringLiteral(str)
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_UNUSED(x)
QVideoFrameFormat::PixelFormat fmt