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
avfvideosink.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <rhi/qrhi.h>
7#include <QtGui/qopenglcontext.h>
8
9#import <AVFoundation/AVFoundation.h>
10#import <QuartzCore/CATransaction.h>
11
12#include <Foundation/Foundation.h>
13
14#ifdef Q_OS_MACOS
15#import <AppKit/AppKit.h>
16#endif
17
18QT_USE_NAMESPACE
19
24
25AVFVideoSink::~AVFVideoSink() = default;
26
27void AVFVideoSink::setRhi(QRhi *rhi)
28{
29 if (m_rhi == rhi)
30 return;
31 m_rhi = rhi;
32 if (m_interface)
33 m_interface->setRhi(rhi);
34}
35
36void AVFVideoSink::setNativeSize(QSize size)
37{
38 if (size == nativeSize())
39 return;
40 QPlatformVideoSink::setNativeSize(size);
41 if (m_interface)
42 m_interface->nativeSizeChanged();
43}
44
46{
47 m_interface = interface;
48 if (m_interface)
49 m_interface->setRhi(m_rhi);
50}
51
53{
54 if (m_layer)
55 [m_layer release];
56 if (m_outputSettings)
57 [m_outputSettings release];
58 freeTextureCaches();
59}
60
61void AVFVideoSinkInterface::freeTextureCaches()
62{
63 cvMetalTextureCache = {};
64#if defined(Q_OS_MACOS)
65 cvOpenGLTextureCache = {};
66#elif defined(Q_OS_IOS)
67 cvOpenGLESTextureCache = {};
68#endif
69}
70
72{
73 if (sink == m_sink)
74 return;
75
76 if (m_sink)
78
79 m_sink = sink;
80
81 if (m_sink) {
84 }
85}
86
87void AVFVideoSinkInterface::setRhi(QRhi *rhi)
88{
89 if (m_rhi == rhi)
90 return;
91 freeTextureCaches();
92 m_rhi = rhi;
93
94 if (!rhi)
95 return;
96 if (rhi->backend() == QRhi::Metal) {
97 const auto *metal = static_cast<const QRhiMetalNativeHandles *>(rhi->nativeHandles());
98
99 // Create a Metal Core Video texture cache from the pixel buffer.
100 Q_ASSERT(!cvMetalTextureCache);
101 if (CVMetalTextureCacheCreate(
102 kCFAllocatorDefault,
103 nil,
104 (id<MTLDevice>)metal->dev,
105 nil,
106 &cvMetalTextureCache) != kCVReturnSuccess) {
107 qWarning() << "Metal texture cache creation failed";
108 m_rhi = nullptr;
109 }
110 } else if (rhi->backend() == QRhi::OpenGLES2) {
111#if QT_CONFIG(opengl)
112#ifdef Q_OS_MACOS
113 const auto *gl = static_cast<const QRhiGles2NativeHandles *>(rhi->nativeHandles());
114
115 auto nsGLContext = gl->context->nativeInterface<QNativeInterface::QCocoaGLContext>()->nativeContext();
116 auto nsGLPixelFormat = nsGLContext.pixelFormat.CGLPixelFormatObj;
117
118 // Create an OpenGL CoreVideo texture cache from the pixel buffer.
119 if (CVOpenGLTextureCacheCreate(
120 kCFAllocatorDefault,
121 nullptr,
122 reinterpret_cast<CGLContextObj>(nsGLContext.CGLContextObj),
123 nsGLPixelFormat,
124 nil,
125 &cvOpenGLTextureCache)) {
126 qWarning() << "OpenGL texture cache creation failed";
127 m_rhi = nullptr;
128 }
129#endif
130#ifdef Q_OS_IOS
131 // Create an OpenGL CoreVideo texture cache from the pixel buffer.
132 if (CVOpenGLESTextureCacheCreate(
133 kCFAllocatorDefault,
134 nullptr,
135 [EAGLContext currentContext],
136 nullptr,
137 &cvOpenGLESTextureCache)) {
138 qWarning() << "OpenGL texture cache creation failed";
139 m_rhi = nullptr;
140 }
141#endif
142#else
143 m_rhi = nullptr;
144#endif // QT_CONFIG(opengl)
145 }
147}
148
149void AVFVideoSinkInterface::setLayer(CALayer *layer)
150{
151 if (layer == m_layer)
152 return;
153
154 if (m_layer)
155 [m_layer release];
156
157 m_layer = layer;
158 if (m_layer)
159 [m_layer retain];
160
162}
163
165{
166 if (m_outputSettings)
167 [m_outputSettings release];
168 m_outputSettings = nil;
169
170 // Set pixel format
171 NSDictionary *dictionary = nil;
172 if (m_rhi && m_rhi->backend() == QRhi::OpenGLES2) {
173#if QT_CONFIG(opengl)
174 dictionary = @{(NSString *)kCVPixelBufferPixelFormatTypeKey:
175 @(kCVPixelFormatType_32BGRA)
176#ifndef Q_OS_IOS // On iOS this key generates a warning about unsupported key.
177 , (NSString *)kCVPixelBufferOpenGLCompatibilityKey: @true
178#endif // Q_OS_IOS
179 };
180#endif
181 } else {
182 dictionary = @{(NSString *)kCVPixelBufferPixelFormatTypeKey:
183 @[
184 @(kCVPixelFormatType_32BGRA),
185 @(kCVPixelFormatType_32RGBA),
186 @(kCVPixelFormatType_422YpCbCr8),
187 @(kCVPixelFormatType_422YpCbCr8_yuvs),
188 @(kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange),
189 @(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange),
190 @(kCVPixelFormatType_420YpCbCr10BiPlanarVideoRange),
191 @(kCVPixelFormatType_420YpCbCr10BiPlanarFullRange),
192 @(kCVPixelFormatType_OneComponent8),
193 @(kCVPixelFormatType_OneComponent16),
194 @(kCVPixelFormatType_420YpCbCr8Planar),
195 @(kCVPixelFormatType_420YpCbCr8PlanarFullRange)
196 ]
197#ifndef Q_OS_IOS // This key is not supported and generates a warning.
198 , (NSString *)kCVPixelBufferMetalCompatibilityKey: @true
199#endif // Q_OS_IOS
200 };
201 }
202
203 m_outputSettings = [[NSDictionary alloc] initWithDictionary:dictionary];
204}
205
207{
208 if (!m_layer)
209 return;
210 [CATransaction begin];
211 [CATransaction setDisableActions: YES]; // disable animation/flicks
212 m_layer.frame = QRectF(0, 0, nativeSize().width(), nativeSize().height()).toCGRect();
213 m_layer.bounds = m_layer.frame;
214 [CATransaction commit];
215}
216
217#include "moc_avfvideosink_p.cpp"
virtual void setLayer(CALayer *layer)
virtual void setOutputSettings()
virtual void reconfigure()=0
AVFVideoSink * m_sink
void setVideoSink(AVFVideoSink *sink)
void setVideoSinkInterface(AVFVideoSinkInterface *interface)
void setNativeSize(QSize size)
QObject * parent
Definition qobject.h:74
The QVideoSink class represents a generic sink for video data.
Definition qvideosink.h:22