Qt 6.x
The Qt SDK
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"
16
17#ifdef Q_OS_MACOS
18#include <VideoToolbox/VideoToolbox.h>
19#endif
20
21#ifdef Q_OS_DARWIN
22#include "qavfcamera_p.h"
23#include "qavfscreencapture_p.h"
24#include "qcgwindowcapture_p.h"
26
27#elif defined(Q_OS_WINDOWS)
28#include "qwindowscamera_p.h"
32#endif
33
34#ifdef Q_OS_ANDROID
35# include "jni.h"
37# include "qandroidcamera_p.h"
38# include "qandroidimagecapture_p.h"
39extern "C" {
40# include <libavutil/log.h>
41# include <libavcodec/jni.h>
42}
43#endif
44
45#if QT_CONFIG(linux_v4l)
46#include "qv4l2camera_p.h"
47#endif
48
49#if QT_CONFIG(cpp_winrt)
51#endif
52
53#if QT_CONFIG(xlib)
56#endif
57
59
61{
64
65public:
68 {}
69
71 {
72 if (name == QLatin1String("ffmpeg"))
73 return new QFFmpegMediaIntegration;
74 return nullptr;
75 }
76};
77
78static void qffmpegLogCallback(void *ptr, int level, const char *fmt, va_list vl)
79{
81
82 // filter logs above the chosen level and AV_LOG_QUIET (negative level)
83 if (level < 0 || level > av_log_get_level())
84 return;
85
86 QString message = QString("FFmpeg log: %1").arg(QString::vasprintf(fmt, vl));
87 if (message.endsWith("\n"))
89
90 if (level == AV_LOG_DEBUG || level == AV_LOG_TRACE)
91 qDebug() << message;
92 else if (level == AV_LOG_VERBOSE || level == AV_LOG_INFO)
93 qInfo() << message;
94 else if (level == AV_LOG_WARNING)
95 qWarning() << message;
96 else if (level == AV_LOG_ERROR || level == AV_LOG_FATAL || level == AV_LOG_PANIC)
97 qCritical() << message;
98}
99
101{
103
104#if defined(Q_OS_ANDROID)
105 m_videoDevices = std::make_unique<QAndroidVideoDevices>(this);
106#elif QT_CONFIG(linux_v4l)
107 m_videoDevices = std::make_unique<QV4L2CameraDevices>(this);
108#elif defined Q_OS_DARWIN
109 m_videoDevices = std::make_unique<QAVFVideoDevices>(this);
110#elif defined(Q_OS_WINDOWS)
111 m_videoDevices = std::make_unique<QWindowsVideoDevices>(this);
112#endif
113
114#if QT_CONFIG(xlib)
116 m_capturableWindows = std::make_unique<QX11CapturableWindows>();
117#elif defined Q_OS_DARWIN
118 m_capturableWindows = std::make_unique<QCGCapturableWindows>();
119#elif defined(Q_OS_WINDOWS)
120 m_capturableWindows = std::make_unique<QWinCapturableWindows>();
121#endif
122
123 if (qEnvironmentVariableIsSet("QT_FFMPEG_DEBUG")) {
124 av_log_set_level(AV_LOG_DEBUG);
125 av_log_set_callback(&qffmpegLogCallback);
126 }
127
128#ifndef QT_NO_DEBUG
129 qDebug() << "Available HW decoding frameworks:";
130 AVHWDeviceType type = AV_HWDEVICE_TYPE_NONE;
131 while ((type = av_hwdevice_iterate_types(type)) != AV_HWDEVICE_TYPE_NONE)
132 qDebug() << " " << av_hwdevice_get_type_name(type);
133#endif
134}
135
137{
138 delete m_formatsInfo;
139}
140
142{
143 return m_formatsInfo;
144}
145
147{
148 return new QFFmpegAudioDecoder(decoder);
149}
150
152{
153 return new QFFmpegMediaCaptureSession();
154}
155
157{
158 return new QFFmpegMediaPlayer(player);
159}
160
162{
163#ifdef Q_OS_DARWIN
164 return new QAVFCamera(camera);
165#elif defined(Q_OS_ANDROID)
166 return new QAndroidCamera(camera);
167#elif QT_CONFIG(linux_v4l)
168 return new QV4L2Camera(camera);
169#elif defined(Q_OS_WINDOWS)
170 return new QWindowsCamera(camera);
171#else
173 return nullptr;//new QFFmpegCamera(camera);
174#endif
175}
176
178{
179#if QT_CONFIG(xlib)
182#endif
183
184#if defined(Q_OS_WINDOWS)
185 return new QFFmpegScreenCaptureDxgi;
186#elif defined(Q_OS_MACOS) // TODO: probably use it for iOS as well
187 return new QAVFScreenCapture;
188#else
190#endif
191}
192
194{
195#if QT_CONFIG(xlib)
198#endif
199
200#if defined(Q_OS_WINDOWS)
201# if QT_CONFIG(cpp_winrt)
202// if (QFFmpegWindowCaptureUwp::isSupported())
203// return new QFFmpegWindowCaptureUwp;
204# endif
205 // TODO: replace with other one
207#elif defined(Q_OS_MACOS) // TODO: probably use it for iOS as well
208 return new QCGWindowCapture;
209#else
211#endif
212}
213
215{
216 return new QFFmpegMediaRecorder(recorder);
217}
218
220{
221#if defined(Q_OS_ANDROID)
223#else
225#endif
226}
227
229{
230 return new QFFmpegVideoSink(sink);
231}
232
234{
235 return new QFFmpegAudioInput(input);
236}
237
238#ifdef Q_OS_ANDROID
239
240Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/)
241{
242 static bool initialized = false;
243 if (initialized)
244 return JNI_VERSION_1_6;
245 initialized = true;
246
248 void *environment;
249 if (vm->GetEnv(&environment, JNI_VERSION_1_6))
250 return JNI_ERR;
251
252 // setting our javavm into ffmpeg.
253 if (av_jni_set_java_vm(vm, nullptr))
254 return JNI_ERR;
255
257 return JNI_ERR;
258
259 return JNI_VERSION_1_6;
260}
261#endif
262
264
265#include "qffmpegmediaintegration.moc"
QMediaPlayer player
Definition audio.cpp:205
static bool registerNativeMethods()
The QAudioDecoder class implements decoding audio.
\qmltype AudioInput \instantiates QAudioInput
Definition qaudioinput.h:19
The QCamera class provides interface for system camera devices.
Definition qcamera.h:28
\inmodule QtMultimedia
QPlatformSurfaceCapture * createScreenCapture(QScreenCapture *) override
QMaybe< QPlatformMediaPlayer * > createPlayer(QMediaPlayer *player) override
QMaybe< QPlatformVideoSink * > createVideoSink(QVideoSink *sink) override
QMaybe< QPlatformAudioInput * > createAudioInput(QAudioInput *input) override
QMaybe< QPlatformAudioDecoder * > createAudioDecoder(QAudioDecoder *decoder) override
QMaybe< QPlatformImageCapture * > createImageCapture(QImageCapture *) override
QMaybe< QPlatformMediaRecorder * > createRecorder(QMediaRecorder *) override
QPlatformMediaFormatInfo * formatInfo() override
QMaybe< QPlatformMediaCaptureSession * > createCaptureSession() override
QMaybe< QPlatformCamera * > createCamera(QCamera *) override
QFFmpegMediaFormatInfo * m_formatsInfo
QPlatformSurfaceCapture * createWindowCapture(QWindowCapture *) override
QPlatformMediaIntegration * create(const QString &name) override
\inmodule QtMultimedia
The QMediaPlayer class allows the playing of a media files.
\inmodule QtMultimedia
std::unique_ptr< QPlatformVideoDevices > m_videoDevices
std::unique_ptr< QPlatformCapturableWindows > m_capturableWindows
\inmodule QtMultimedia
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
static QString vasprintf(const char *format, va_list ap) Q_ATTRIBUTE_FORMAT_PRINTF(1
Definition qstring.cpp:7099
QString & removeLast()
Definition qstring.h:487
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
static void qffmpegLogCallback(void *ptr, int level, const char *fmt, va_list vl)
QT_END_NAMESPACE JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
#define qCritical
Definition qlogging.h:163
#define qInfo
Definition qlogging.h:161
#define qDebug
[1]
Definition qlogging.h:160
#define qWarning
Definition qlogging.h:162
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
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define Q_OBJECT
#define Q_PLUGIN_METADATA(x)
#define Q_UNUSED(x)
QVideoFrameFormat::PixelFormat fmt