on e2ced2d help sign in
unity8/tests/mocks/QtMultimedia/videooutput.cpp
127 lines 4.2 kB history raw
1/*
2 * Copyright (C) 2016 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "videooutput.h"
18#include "mediaplayer.h"
20#include <paths.h>
22#include <QQuickView>
23#include <QQmlComponent>
24#include <QGuiApplication>
25#include <QQmlProperty>
26#include <QQmlEngine>
28VideoOutput::VideoOutput(QQuickItem *parent)
29 : QQuickItem(parent)
30 , m_source(nullptr)
31 , m_qmlContentComponent(nullptr)
32 , m_qmlItem(nullptr)
36void VideoOutput::setSource(QObject *source)
38 if (m_source != source) {
39 if (m_source) disconnect(m_source, 0, this, 0);
41 m_source = source;
42 Q_EMIT sourceChanged();
44 MediaPlayer* mediaPlayer = qobject_cast<MediaPlayer*>(source);
45 if (mediaPlayer) {
46 connect(mediaPlayer, &MediaPlayer::positionChanged, this, &VideoOutput::updateProperties);
47 connect(mediaPlayer, &MediaPlayer::playbackStateChanged, this, &VideoOutput::updateProperties);
48 connect(mediaPlayer->metaData(), SIGNAL(metaDataChanged()), this, SLOT(updateProperties()));
49 }
50 }
53void VideoOutput::itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value)
55 if (change == QQuickItem::ItemSceneChange && !m_qmlContentComponent) {
56 QWindowList list = QGuiApplication::topLevelWindows();
57 if (list.isEmpty()) return;
59 // The assumptions I make here really should hold.
60 QQuickView *quickView =
61 qobject_cast<QQuickView*>(list[0]);
63 m_qmlContentComponent = new QQmlComponent(quickView->engine(),
64 QString("%1/QtMultimedia/VideoSurface.qml").arg(mockPluginsDir()));
66 switch (m_qmlContentComponent->status()) {
67 case QQmlComponent::Ready:
68 createQmlContentItem();
69 break;
70 case QQmlComponent::Loading:
71 connect(m_qmlContentComponent, &QQmlComponent::statusChanged,
72 this, &VideoOutput::onComponentStatusChanged);
73 break;
74 case QQmlComponent::Error:
75 printComponentErrors();
76 qFatal("VideoOutput: failed to create content component.");
77 break;
78 default:
79 qFatal("VideoOutput: Unhandled component status");
80 }
81 }
82 QQuickItem::itemChange(change, value);
85void VideoOutput::printComponentErrors()
87 QList<QQmlError> errors = m_qmlContentComponent->errors();
88 for (int i = 0; i < errors.count(); ++i) {
89 qDebug() << errors[i];
90 }
93void VideoOutput::onComponentStatusChanged(QQmlComponent::Status status)
95 if (status == QQmlComponent::Ready) {
96 createQmlContentItem();
97 }
100void VideoOutput::createQmlContentItem()
102 m_qmlItem = qobject_cast<QQuickItem*>(m_qmlContentComponent->create());
103 m_qmlItem->setParentItem(this);
105 updateProperties();
108void VideoOutput:: updateProperties()
110 if (!m_qmlItem) return;
112 MediaPlayer* mediaPlayer = qobject_cast<MediaPlayer*>(m_source);
113 if (!mediaPlayer) return;
115 QQmlProperty playbackStateProperty(m_qmlItem, "playbackState");
116 playbackStateProperty.write(QVariant::fromValue<int>(mediaPlayer->playbackState()));
118 QQmlProperty positionProperty(m_qmlItem, "position");
119 positionProperty.write(QVariant::fromValue(mediaPlayer->position()));
121 QQmlProperty propResSource(mediaPlayer->metaData(), "resolution");
122 QQmlProperty propResDest(m_qmlItem, "resolution");
124 if (propResSource.isValid() && propResDest.isValid()) {
125 propResDest.write(propResSource.read());
126 }