summaryrefslogtreecommitdiffstats
path: root/src/qtmultimediaquicktools/qdeclarativevideooutput_window.cpp
blob: 0d465da5522d0064d9618aa2f10200ffb5ec3413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/****************************************************************************
**
** Copyright (C) 2016 Research In Motion
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** 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 The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qdeclarativevideooutput_window_p.h"
#include "qdeclarativevideooutput_p.h"
#include <QtQuick/qquickwindow.h>
#include <QtMultimedia/qmediaservice.h>
#include <QtMultimedia/qvideowindowcontrol.h>

QT_BEGIN_NAMESPACE

QDeclarativeVideoWindowBackend::QDeclarativeVideoWindowBackend(QDeclarativeVideoOutput *parent)
    : QDeclarativeVideoBackend(parent),
      m_visible(true)
{
}

QDeclarativeVideoWindowBackend::~QDeclarativeVideoWindowBackend()
{
    releaseSource();
    releaseControl();
}

bool QDeclarativeVideoWindowBackend::init(QMediaService *service)
{
    if (QMediaControl *control = service->requestControl(QVideoWindowControl_iid)) {
        if ((m_videoWindowControl = qobject_cast<QVideoWindowControl *>(control))) {
            if (q->window())
                m_videoWindowControl->setWinId(q->window()->winId());
            m_service = service;
            QObject::connect(m_videoWindowControl.data(), SIGNAL(nativeSizeChanged()),
                             q, SLOT(_q_updateNativeSize()));
            return true;
        }
    }
    return false;
}

void QDeclarativeVideoWindowBackend::itemChange(QQuickItem::ItemChange change,
                                                const QQuickItem::ItemChangeData &changeData)
{
    if (!m_videoWindowControl)
        return;

    switch (change) {
    case QQuickItem::ItemVisibleHasChanged:
        m_visible = changeData.boolValue;
        updateGeometry();
        break;
    case QQuickItem::ItemSceneChange:
        if (changeData.window)
            m_videoWindowControl->setWinId(changeData.window->winId());
        else
            m_videoWindowControl->setWinId(0);
        break;
    default: break;
    }
}

void QDeclarativeVideoWindowBackend::releaseSource()
{
}

void QDeclarativeVideoWindowBackend::releaseControl()
{
    if (m_videoWindowControl) {
        m_videoWindowControl->setWinId(0);
        if (m_service)
            m_service->releaseControl(m_videoWindowControl);
        m_videoWindowControl = 0;
    }
}

QSize QDeclarativeVideoWindowBackend::nativeSize() const
{
    return m_videoWindowControl->nativeSize();
}

void QDeclarativeVideoWindowBackend::updateGeometry()
{
    switch (q->fillMode()) {
    case QDeclarativeVideoOutput::PreserveAspectFit:
        m_videoWindowControl->setAspectRatioMode(Qt::KeepAspectRatio); break;
    case QDeclarativeVideoOutput::PreserveAspectCrop:
        m_videoWindowControl->setAspectRatioMode(Qt::KeepAspectRatioByExpanding); break;
    case QDeclarativeVideoOutput::Stretch:
        m_videoWindowControl->setAspectRatioMode(Qt::IgnoreAspectRatio); break;
    };

    const QRectF canvasRect = q->mapRectToScene(QRectF(0, 0, q->width(), q->height()));
    m_videoWindowControl->setDisplayRect(m_visible ? canvasRect.toAlignedRect() : QRect());
}

QSGNode *QDeclarativeVideoWindowBackend::updatePaintNode(QSGNode *oldNode,
                                                         QQuickItem::UpdatePaintNodeData *data)
{
    Q_UNUSED(oldNode);
    Q_UNUSED(data);
    m_videoWindowControl->repaint();
    return 0;
}

QAbstractVideoSurface *QDeclarativeVideoWindowBackend::videoSurface() const
{
    return 0;
}

QRectF QDeclarativeVideoWindowBackend::adjustedViewport() const
{
    // No viewport supported by QVideoWindowControl, so make the viewport the same size
    // as the source
    return QRectF(QPointF(0, 0), nativeSize());
}

QT_END_NAMESPACE