1
/****************************************************************************
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the Qt Toolkit.
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.
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.
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.
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.
40
****************************************************************************/
42
#include "qwinrtplayerrenderercontrol.h"
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>
52
#define EGL_EGLEXT_PROTOTYPES
54
#include <EGL/eglext.h>
58
#include <mfmediaengine.h>
60
using namespace Microsoft::WRL;
68
D3DDeviceLocker(IMFDXGIDeviceManager *manager, HANDLE deviceHandle, T **device)
69
: m_manager(manager), m_deviceHandle(deviceHandle)
71
HRESULT hr = m_manager->LockDevice(m_deviceHandle, IID_PPV_ARGS(device), TRUE);
73
qErrnoWarning(hr, "Failed to lock the D3D device");
78
HRESULT hr = m_manager->UnlockDevice(m_deviceHandle, FALSE);
80
qErrnoWarning(hr, "Failed to unlock the D3D device");
84
IMFDXGIDeviceManager *m_manager;
85
HANDLE m_deviceHandle;
88
class QWinRTPlayerRendererControlPrivate
91
ComPtr<IMFMediaEngineEx> engine;
92
ComPtr<IMFDXGIDeviceManager> manager;
97
QWinRTPlayerRendererControl::QWinRTPlayerRendererControl(IMFMediaEngineEx *engine, IMFDXGIDeviceManager *manager, quint32 resetToken, QObject *parent)
98
: QWinRTAbstractVideoRendererControl(QSize(), parent), d_ptr(new QWinRTPlayerRendererControlPrivate)
100
Q_D(QWinRTPlayerRendererControl);
103
d->manager = manager;
104
d->resetToken = resetToken;
108
QWinRTPlayerRendererControl::~QWinRTPlayerRendererControl()
113
bool QWinRTPlayerRendererControl::ensureReady()
115
Q_D(QWinRTPlayerRendererControl);
118
hr = d->manager->TestDevice(d->deviceHandle);
119
if (hr == MF_E_DXGI_NEW_VIDEO_DEVICE || FAILED(hr)) {
120
d->manager->CloseDeviceHandle(d->deviceHandle);
122
hr = d->manager->ResetDevice(d3dDevice(), d->resetToken);
123
RETURN_FALSE_IF_FAILED("Failed to reset the DXGI manager device");
125
hr = d->manager->OpenDeviceHandle(&d->deviceHandle);
126
RETURN_FALSE_IF_FAILED("Failed to open device handle");
129
return SUCCEEDED(hr);
132
bool QWinRTPlayerRendererControl::render(ID3D11Texture2D *texture)
134
Q_D(QWinRTPlayerRendererControl);
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
146
ComPtr<ID3D11Device> device;
147
D3DDeviceLocker<ID3D11Device> locker(d->manager.Get(), d->deviceHandle, device.GetAddressOf());
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");