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
avfvideorenderercontrol.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#include "avfdisplaylink_p.h"
6#include <avfvideobuffer_p.h>
7#include "qavfhelpers_p.h"
8
9#include <QtMultimedia/qvideoframeformat.h>
10
11#include <avfvideosink_p.h>
12#include <rhi/qrhi.h>
13
14#include <QtCore/qdebug.h>
15
16#import <AVFoundation/AVFoundation.h>
17#include <CoreVideo/CVPixelBuffer.h>
18#include <CoreVideo/CVImageBuffer.h>
19
21
22@interface SubtitleDelegate : NSObject <AVPlayerItemLegibleOutputPushDelegate>
23{
26
27- (void)legibleOutput:(AVPlayerItemLegibleOutput *)output
28 didOutputAttributedStrings:(NSArray<NSAttributedString *> *)strings
29 nativeSampleBuffers:(NSArray *)nativeSamples
30 forItemTime:(CMTime)itemTime;
31
32@end
33
34@implementation SubtitleDelegate
35
36-(id)initWithRenderer: (AVFVideoRendererControl *)renderer
37{
38 if (!(self = [super init]))
39 return nil;
40
42
43 return self;
44}
45
46- (void)legibleOutput:(AVPlayerItemLegibleOutput *)output
47 didOutputAttributedStrings:(NSArray<NSAttributedString *> *)strings
48 nativeSampleBuffers:(NSArray *)nativeSamples
49 forItemTime:(CMTime)itemTime
50{
52 for (NSAttributedString *s : strings) {
53 if (!text.isEmpty())
54 text += QChar::LineSeparator;
55 text += QString::fromNSString(s.string);
56 }
58}
59
60@end
61
62
64 : QObject(parent)
65{
66 m_displayLink = new AVFDisplayLink(this);
67 connect(m_displayLink, SIGNAL(tick(CVTimeStamp)), SLOT(updateVideoFrame(CVTimeStamp)));
68}
69
72#ifdef QT_DEBUG_AVF
74#endif
75 m_displayLink->stop();
76 if (m_videoOutput)
77 [m_videoOutput release];
78 if (m_subtitleOutput)
79 [m_subtitleOutput release];
80 if (m_subtitleDelegate)
81 [m_subtitleDelegate release];
82}
83
86#ifdef QT_DEBUG_AVF
87 qDebug() << "reconfigure";
88#endif
89 if (!m_layer) {
90 m_displayLink->stop();
91 return;
92 }
93
94 QMutexLocker locker(&m_mutex);
95
96 m_displayLink->start();
97
99}
100
103 if (m_layer == layer)
104 return;
105
106 AVPlayerLayer *plLayer = playerLayer();
107 if (plLayer) {
108 if (m_videoOutput)
109 [[[plLayer player] currentItem] removeOutput:m_videoOutput];
110
111 if (m_subtitleOutput)
112 [[[plLayer player] currentItem] removeOutput:m_subtitleOutput];
113 }
114
115 if (!layer && m_sink)
117
119}
120
123 m_rotation = rotation;
124}
125
128 m_mirrored = mirrored;
129}
130
131void AVFVideoRendererControl::updateVideoFrame(const CVTimeStamp &ts)
132{
133 Q_UNUSED(ts);
134
135 if (!m_sink)
136 return;
137
138 if (!m_layer)
139 return;
140
141 auto *layer = playerLayer();
142 if (!layer.readyForDisplay)
143 return;
145
147 size_t width, height;
148 CVPixelBufferRef pixelBuffer = copyPixelBufferFromLayer(width, height);
149 if (!pixelBuffer)
150 return;
151 AVFVideoBuffer *buffer = new AVFVideoBuffer(this, pixelBuffer);
152// qDebug() << "Got pixelbuffer with format" << fmt << Qt::hex << CVPixelBufferGetPixelFormatType(pixelBuffer);
153 CVPixelBufferRelease(pixelBuffer);
154
155 frame = QVideoFrame(buffer, buffer->videoFormat());
156 frame.setRotation(m_rotation);
157 frame.setMirrored(m_mirrored);
159}
160
161CVPixelBufferRef AVFVideoRendererControl::copyPixelBufferFromLayer(size_t& width, size_t& height)
162{
163 AVPlayerLayer *layer = playerLayer();
164 //Is layer valid
165 if (!layer) {
166#ifdef QT_DEBUG_AVF
167 qWarning("copyPixelBufferFromLayer: invalid layer");
168#endif
169 return nullptr;
170 }
171
172 AVPlayerItem * item = [[layer player] currentItem];
173
174 if (!m_videoOutput) {
175 if (!m_outputSettings)
177 m_videoOutput = [[AVPlayerItemVideoOutput alloc] initWithPixelBufferAttributes:m_outputSettings];
178 [m_videoOutput setDelegate:nil queue:nil];
179 }
180 if (!m_subtitleOutput) {
181 m_subtitleOutput = [[AVPlayerItemLegibleOutput alloc] init];
182 m_subtitleDelegate = [[SubtitleDelegate alloc] initWithRenderer:this];
183 [m_subtitleOutput setDelegate:m_subtitleDelegate queue:dispatch_get_main_queue()];
184 }
185 if (![item.outputs containsObject:m_videoOutput])
186 [item addOutput:m_videoOutput];
187 if (![item.outputs containsObject:m_subtitleOutput])
188 [item addOutput:m_subtitleOutput];
189
190 CFTimeInterval currentCAFrameTime = CACurrentMediaTime();
191 CMTime currentCMFrameTime = [m_videoOutput itemTimeForHostTime:currentCAFrameTime];
192 // happens when buffering / loading
193 if (CMTimeCompare(currentCMFrameTime, kCMTimeZero) < 0) {
194 return nullptr;
195 }
196
197 if (![m_videoOutput hasNewPixelBufferForItemTime:currentCMFrameTime])
199
200 CVPixelBufferRef pixelBuffer = [m_videoOutput copyPixelBufferForItemTime:currentCMFrameTime
201 itemTimeForDisplay:nil];
202 if (!pixelBuffer) {
203#ifdef QT_DEBUG_AVF
204 qWarning("copyPixelBufferForItemTime returned nil");
205 CMTimeShow(currentCMFrameTime);
206#endif
207 return nullptr;
208 }
209
210 width = CVPixelBufferGetWidth(pixelBuffer);
211 height = CVPixelBufferGetHeight(pixelBuffer);
212// auto f = CVPixelBufferGetPixelFormatType(pixelBuffer);
213// char fmt[5];
214// memcpy(fmt, &f, 4);
215// fmt[4] = 0;
216// qDebug() << "copyPixelBuffer" << f << fmt << width << height;
217 return pixelBuffer;
218}
219
220#include "moc_avfvideorenderercontrol_p.cpp"
void setSubtitleText(const QString &subtitle)
void setVideoRotation(QtVideo::Rotation)
void setLayer(CALayer *layer) override
AVFVideoRendererControl(QObject *parent=nullptr)
virtual void setLayer(CALayer *layer)
NSDictionary * m_outputSettings
virtual void setOutputSettings()
AVFVideoSink * m_sink
\inmodule QtCore
\inmodule QtCore
Definition qmutex.h:313
\inmodule QtCore
Definition qobject.h:103
virtual void setVideoFrame(const QVideoFrame &frame)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
The QVideoFrame class represents a frame of video data.
Definition qvideoframe.h:27
QString text
AVFVideoRendererControl * m_renderer
#define Q_FUNC_INFO
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
EGLOutputLayerEXT layer
#define qDebug
[1]
Definition qlogging.h:164
#define qWarning
Definition qlogging.h:166
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
GLint GLsizei GLsizei height
GLsizei const GLchar ** strings
[1]
GLenum GLuint id
[7]
GLenum GLuint buffer
GLint GLsizei width
GLdouble s
[6]
Definition qopenglext.h:235
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
#define Q_UNUSED(x)
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QObject::connect nullptr
QGraphicsItem * item
QFrame frame
[0]
QSvgRenderer * renderer
[0]