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