~phablet-team/+junk/qtmultimedia

« back to all changes in this revision

Viewing changes to src/plugins/winrt/qwinrtplayerrenderercontrol.cpp

  • Committer: Jim Hodapp
  • Date: 2015-05-15 19:17:49 UTC
  • Revision ID: jim.hodapp@canonical.com-20150515191749-r4xausjaaphme9ok
Initial import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
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 Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include "qwinrtplayerrenderercontrol.h"
 
43
 
 
44
#include <QtCore/qfunctions_winrt.h>
 
45
#include <QtCore/QGlobalStatic>
 
46
#include <QtMultimedia/QAbstractVideoSurface>
 
47
#include <QtMultimedia/QVideoSurfaceFormat>
 
48
#include <QtGui/QGuiApplication>
 
49
#include <QtGui/QOpenGLTexture>
 
50
#include <QtGui/QOpenGLContext>
 
51
 
 
52
#define EGL_EGLEXT_PROTOTYPES
 
53
#include <EGL/egl.h>
 
54
#include <EGL/eglext.h>
 
55
 
 
56
#include <d3d11.h>
 
57
#include <mfapi.h>
 
58
#include <mfmediaengine.h>
 
59
#include <wrl.h>
 
60
using namespace Microsoft::WRL;
 
61
 
 
62
QT_USE_NAMESPACE
 
63
 
 
64
template <typename T>
 
65
class D3DDeviceLocker
 
66
{
 
67
public:
 
68
    D3DDeviceLocker(IMFDXGIDeviceManager *manager, HANDLE deviceHandle, T **device)
 
69
        : m_manager(manager), m_deviceHandle(deviceHandle)
 
70
    {
 
71
        HRESULT hr = m_manager->LockDevice(m_deviceHandle, IID_PPV_ARGS(device), TRUE);
 
72
        if (FAILED(hr))
 
73
            qErrnoWarning(hr, "Failed to lock the D3D device");
 
74
    }
 
75
 
 
76
    ~D3DDeviceLocker()
 
77
    {
 
78
        HRESULT hr = m_manager->UnlockDevice(m_deviceHandle, FALSE);
 
79
        if (FAILED(hr))
 
80
            qErrnoWarning(hr, "Failed to unlock the D3D device");
 
81
    }
 
82
 
 
83
private:
 
84
    IMFDXGIDeviceManager *m_manager;
 
85
    HANDLE m_deviceHandle;
 
86
};
 
87
 
 
88
class QWinRTPlayerRendererControlPrivate
 
89
{
 
90
public:
 
91
    ComPtr<IMFMediaEngineEx> engine;
 
92
    ComPtr<IMFDXGIDeviceManager> manager;
 
93
    quint32 resetToken;
 
94
    HANDLE deviceHandle;
 
95
};
 
96
 
 
97
QWinRTPlayerRendererControl::QWinRTPlayerRendererControl(IMFMediaEngineEx *engine, IMFDXGIDeviceManager *manager, quint32 resetToken, QObject *parent)
 
98
    : QWinRTAbstractVideoRendererControl(QSize(), parent), d_ptr(new QWinRTPlayerRendererControlPrivate)
 
99
{
 
100
    Q_D(QWinRTPlayerRendererControl);
 
101
 
 
102
    d->engine = engine;
 
103
    d->manager = manager;
 
104
    d->resetToken = resetToken;
 
105
    d->deviceHandle = 0;
 
106
}
 
107
 
 
108
QWinRTPlayerRendererControl::~QWinRTPlayerRendererControl()
 
109
{
 
110
    shutdown();
 
111
}
 
112
 
 
113
bool QWinRTPlayerRendererControl::ensureReady()
 
114
{
 
115
    Q_D(QWinRTPlayerRendererControl);
 
116
 
 
117
    HRESULT hr;
 
118
    hr = d->manager->TestDevice(d->deviceHandle);
 
119
    if (hr == MF_E_DXGI_NEW_VIDEO_DEVICE || FAILED(hr)) {
 
120
        d->manager->CloseDeviceHandle(d->deviceHandle);
 
121
 
 
122
        hr = d->manager->ResetDevice(d3dDevice(), d->resetToken);
 
123
        RETURN_FALSE_IF_FAILED("Failed to reset the DXGI manager device");
 
124
 
 
125
        hr = d->manager->OpenDeviceHandle(&d->deviceHandle);
 
126
        RETURN_FALSE_IF_FAILED("Failed to open device handle");
 
127
    }
 
128
 
 
129
    return SUCCEEDED(hr);
 
130
}
 
131
 
 
132
bool QWinRTPlayerRendererControl::render(ID3D11Texture2D *texture)
 
133
{
 
134
    Q_D(QWinRTPlayerRendererControl);
 
135
 
 
136
    if (!ensureReady())
 
137
        return false;
 
138
 
 
139
    HRESULT hr;
 
140
    LONGLONG ts;
 
141
    hr = d->engine->OnVideoStreamTick(&ts);
 
142
    RETURN_FALSE_IF_FAILED("Failed to obtain video stream tick");
 
143
    if (hr == S_FALSE) // Frame not available, nothing to do
 
144
        return false;
 
145
 
 
146
    ComPtr<ID3D11Device> device;
 
147
    D3DDeviceLocker<ID3D11Device> locker(d->manager.Get(), d->deviceHandle, device.GetAddressOf());
 
148
 
 
149
    MFVideoNormalizedRect sourceRect = { 0, 0, 1, 1 };
 
150
    const QSize frameSize = size();
 
151
    RECT destRect = { 0, 0, frameSize.width(), frameSize.height() };
 
152
    MFARGB borderColor = { 255, 255, 255, 255 };
 
153
    hr = d->engine->TransferVideoFrame(texture, &sourceRect, &destRect, &borderColor);
 
154
    RETURN_FALSE_IF_FAILED("Failed to transfer video frame to DXGI surface");
 
155
    return true;
 
156
}