From 700b4cdf42335ad02ff308cddbfc37b8d49a1e71 Mon Sep 17 00:00:00 2001 From: Andy Nichols Date: Mon, 1 Oct 2012 10:20:19 +0200 Subject: Add MediaPlayer support to AVFoundation plugin This plugin would be used on Mac 10.7+ where without the QuickTime C API our QT7 media player performance was crippled. Change-Id: Iaadb1990a8f63393c4cd02d096624e0fed42b40f Reviewed-by: Lars Knoll Reviewed-by: Jason Barron Reviewed-by: Andy Nichols --- .../mediaplayer/avfvideorenderercontrol.mm | 213 +++++++++++++++++++++ 1 file changed, 213 insertions(+) create mode 100644 src/plugins/avfoundation/mediaplayer/avfvideorenderercontrol.mm (limited to 'src/plugins/avfoundation/mediaplayer/avfvideorenderercontrol.mm') diff --git a/src/plugins/avfoundation/mediaplayer/avfvideorenderercontrol.mm b/src/plugins/avfoundation/mediaplayer/avfvideorenderercontrol.mm new file mode 100644 index 000000000..e7d99cd00 --- /dev/null +++ b/src/plugins/avfoundation/mediaplayer/avfvideorenderercontrol.mm @@ -0,0 +1,213 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "avfvideorenderercontrol.h" +#include "avfdisplaylink.h" +#include "avfvideoframerenderer.h" + +#include +#include +#include +#include + +#import + +QT_USE_NAMESPACE + +class TextureVideoBuffer : public QAbstractVideoBuffer +{ +public: + TextureVideoBuffer(GLuint textureId) + : QAbstractVideoBuffer(GLTextureHandle) + , m_textureId(textureId) + {} + + virtual ~TextureVideoBuffer() {} + + MapMode mapMode() const { return NotMapped; } + uchar *map(MapMode, int*, int*) { return 0; } + void unmap() {} + + QVariant handle() const + { + return QVariant::fromValue(m_textureId); + } + +private: + GLuint m_textureId; +}; + +AVFVideoRendererControl::AVFVideoRendererControl(QObject *parent) + : QVideoRendererControl(parent) + , m_surface(0) + , m_playerLayer(0) + , m_frameRenderer(0) + +{ + m_displayLink = new AVFDisplayLink(this); + connect(m_displayLink, SIGNAL(tick(CVTimeStamp)), SLOT(updateVideoFrame(CVTimeStamp))); +} + +AVFVideoRendererControl::~AVFVideoRendererControl() +{ +#ifdef QT_DEBUG_AVF + qDebug() << Q_FUNC_INFO; +#endif + m_displayLink->stop(); + if (m_playerLayer) + [(AVPlayerLayer*)m_playerLayer release]; +} + +QAbstractVideoSurface *AVFVideoRendererControl::surface() const +{ + return m_surface; +} + +void AVFVideoRendererControl::setSurface(QAbstractVideoSurface *surface) +{ +#ifdef QT_DEBUG_AVF + qDebug() << "Set video surface" << surface; +#endif + + //When we have a valid surface, we can setup a frame renderer + //and schedule surface updates with the display link. + if (surface == m_surface) + return; + + QMutexLocker locker(&m_mutex); + + if (m_surface && m_surface->isActive()) + m_surface->stop(); + + m_surface = surface; + + //If the surface changed, then the current frame renderer is no longer valid + if (m_frameRenderer) + delete m_frameRenderer; + + //If there is now no surface to render too + if (m_surface == 0) { + m_displayLink->stop(); + return; + } + + //Surface changed, so we need a new frame renderer + m_frameRenderer = new AVFVideoFrameRenderer(m_surface, this); + + //If we already have a layer, but changed surfaces start rendering again + if (m_playerLayer && !m_displayLink->isActive()) { + m_displayLink->start(); + } + +} + +void AVFVideoRendererControl::setLayer(void *playerLayer) +{ + if (m_playerLayer == playerLayer) + return; + + [(AVPlayerLayer*)playerLayer retain]; + [(AVPlayerLayer*)m_playerLayer release]; + + m_playerLayer = playerLayer; + + //If there is no layer to render, stop scheduling updates + if (m_playerLayer == 0) { + m_displayLink->stop(); + return; + } + + setupVideoOutput(); + + //If we now have both a valid surface and layer, start scheduling updates + if (m_surface && !m_displayLink->isActive()) { + m_displayLink->start(); + } +} + +void AVFVideoRendererControl::updateVideoFrame(const CVTimeStamp &ts) +{ + Q_UNUSED(ts) + + AVPlayerLayer *playerLayer = (AVPlayerLayer*)m_playerLayer; + + if (!playerLayer) { + qWarning("updateVideoFrame called without AVPlayerLayer (which shouldn't happen"); + return; + } + + if (!playerLayer.readyForDisplay) + return; + + GLuint textureId = m_frameRenderer->renderLayerToTexture(playerLayer); + + //Make sure we got a valid texture + if (textureId == 0) { + qWarning("renderLayerToTexture failed"); + return; + } + + QAbstractVideoBuffer *buffer = new TextureVideoBuffer(textureId); + QVideoFrame frame = QVideoFrame(buffer, m_nativeSize, QVideoFrame::Format_BGR32); + + if (m_surface && frame.isValid()) { + if (m_surface->isActive() && m_surface->surfaceFormat().pixelFormat() != frame.pixelFormat()) + m_surface->stop(); + + if (!m_surface->isActive()) { + QVideoSurfaceFormat format(frame.size(), frame.pixelFormat(), QAbstractVideoBuffer::GLTextureHandle); + + if (!m_surface->start(format)) { + qWarning("Failed to activate video surface"); + } + } + + if (m_surface->isActive()) + m_surface->present(frame); + } +} + +void AVFVideoRendererControl::setupVideoOutput() +{ + AVPlayerLayer *playerLayer = (AVPlayerLayer*)m_playerLayer; + if (playerLayer) + m_nativeSize = QSize(playerLayer.bounds.size.width, playerLayer.bounds.size.height); +} -- cgit v1.2.3