1/**************************************************************************** 2** 3** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies). 4** Contact: https://www.qt.io/licensing/ 5** 6** This file is part of the Qt Toolkit. 7** 8** $QT_BEGIN_LICENSE:LGPL$ 9** Commercial License Usage 10** Licensees holding valid commercial Qt licenses may use this file in 11** accordance with the commercial license agreement provided with the 12** Software or, alternatively, in accordance with the terms contained in 13** a written agreement between you and The Qt Company. For licensing terms 14** and conditions see https://www.qt.io/terms-conditions. For further 15** information use the contact form at https://www.qt.io/contact-us. 16** 17** GNU Lesser General Public License Usage 18** Alternatively, this file may be used under the terms of the GNU Lesser 19** General Public License version 3 as published by the Free Software 20** Foundation and appearing in the file LICENSE.LGPL3 included in the 21** packaging of this file. Please review the following information to 22** ensure the GNU Lesser General Public License version 3 requirements 23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 24** 25** GNU General Public License Usage 26** Alternatively, this file may be used under the terms of the GNU 27** General Public License version 2.0 or (at your option) the GNU General 28** Public license version 3 or any later version approved by the KDE Free 29** Qt Foundation. The licenses are as published by the Free Software 30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 31** included in the packaging of this file. Please review the following 32** information to ensure the GNU General Public License requirements will 33** be met: https://www.gnu.org/licenses/gpl-2.0.html and 34** https://www.gnu.org/licenses/gpl-3.0.html. 35** 36** $QT_END_LICENSE$ 37** 38****************************************************************************/ 39 40#include "avfvideowidget.h" 41 42#import <AVFoundation/AVFoundation.h> 43#import <QuartzCore/CATransaction.h> 44 45#if defined(Q_OS_MACOS) 46#import <AppKit/AppKit.h> 47#else 48#import <UIKit/UIKit.h> 49#endif 50 51#include <QtCore/QDebug> 52#include <QtGui/QResizeEvent> 53#include <QtGui/QPaintEvent> 54#include <QtGui/QPainter> 55 56QT_USE_NAMESPACE 57 58AVFVideoWidget::AVFVideoWidget(QWidget *parent) 59 : QWidget(parent) 60 , m_aspectRatioMode(Qt::KeepAspectRatio) 61 , m_playerLayer(nullptr) 62 , m_nativeView(nullptr) 63{ 64 setAutoFillBackground(false); 65} 66 67AVFVideoWidget::~AVFVideoWidget() 68{ 69#ifdef QT_DEBUG_AVF 70 qDebug() << Q_FUNC_INFO; 71#endif 72 73 if (m_playerLayer) { 74 [m_playerLayer removeFromSuperlayer]; 75 [m_playerLayer release]; 76 } 77} 78 79QSize AVFVideoWidget::sizeHint() const 80{ 81 return m_nativeSize; 82} 83 84Qt::AspectRatioMode AVFVideoWidget::aspectRatioMode() const 85{ 86 return m_aspectRatioMode; 87} 88 89void AVFVideoWidget::setAspectRatioMode(Qt::AspectRatioMode mode) 90{ 91 if (m_aspectRatioMode != mode) { 92 m_aspectRatioMode = mode; 93 94 updateAspectRatio(); 95 } 96} 97 98void AVFVideoWidget::setPlayerLayer(AVPlayerLayer *layer) 99{ 100 if (m_playerLayer == layer) 101 return; 102 103 if (!m_nativeView) { 104 //make video widget a native window 105#if defined(Q_OS_OSX) 106 m_nativeView = (NSView*)this->winId(); 107 [m_nativeView setWantsLayer:YES]; 108#else 109 m_nativeView = (UIView*)this->winId(); 110#endif 111 } 112 113 if (m_playerLayer) { 114 [m_playerLayer removeFromSuperlayer]; 115 [m_playerLayer release]; 116 } 117 118 m_playerLayer = layer; 119 120 CALayer *nativeLayer = [m_nativeView layer]; 121 122 if (layer) { 123 [layer retain]; 124 125 m_nativeSize = QSize(m_playerLayer.bounds.size.width, 126 m_playerLayer.bounds.size.height); 127 128 updateAspectRatio(); 129 [nativeLayer addSublayer:m_playerLayer]; 130 updatePlayerLayerBounds(this->size()); 131 } 132#ifdef QT_DEBUG_AVF 133 NSArray *sublayers = [nativeLayer sublayers]; 134 qDebug() << "playerlayer: " << "at z:" << [m_playerLayer zPosition] 135 << " frame: " << m_playerLayer.frame.size.width << "x" << m_playerLayer.frame.size.height; 136 qDebug() << "superlayer: " << "at z:" << [nativeLayer zPosition] 137 << " frame: " << nativeLayer.frame.size.width << "x" << nativeLayer.frame.size.height; 138 int i = 0; 139 for (CALayer *layer in sublayers) { 140 qDebug() << "layer " << i << ": at z:" << [layer zPosition] 141 << " frame: " << layer.frame.size.width << "x" << layer.frame.size.height; 142 i++; 143 } 144#endif 145 146} 147 148void AVFVideoWidget::resizeEvent(QResizeEvent *event) 149{ 150 updatePlayerLayerBounds(event->size()); 151 QWidget::resizeEvent(event); 152} 153 154void AVFVideoWidget::paintEvent(QPaintEvent *event) 155{ 156 QPainter painter(this); 157 painter.fillRect(rect(), Qt::black); 158 159 QWidget::paintEvent(event); 160} 161 162void AVFVideoWidget::updateAspectRatio() 163{ 164 if (m_playerLayer) { 165 switch (m_aspectRatioMode) { 166 case Qt::IgnoreAspectRatio: 167 [m_playerLayer setVideoGravity:AVLayerVideoGravityResize]; 168 break; 169 case Qt::KeepAspectRatio: 170 [m_playerLayer setVideoGravity:AVLayerVideoGravityResizeAspect]; 171 break; 172 case Qt::KeepAspectRatioByExpanding: 173 [m_playerLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; 174 break; 175 default: 176 break; 177 } 178 } 179} 180 181void AVFVideoWidget::updatePlayerLayerBounds(const QSize &size) 182{ 183 [CATransaction begin]; 184 [CATransaction setDisableActions: YES]; // disable animation/flicks 185 m_playerLayer.bounds = QRect(QPoint(0, 0), size).toCGRect(); 186 [CATransaction commit]; 187} 188