-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathApp.cpp
327 lines (271 loc) · 12.9 KB
/
App.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "media/eme/EmeFactory.h"
#include "media/MediaEngineWrapper.h"
#include "media/MediaFoundationHelpers.h"
#include "TestContent.h"
using namespace winrt;
using namespace Windows;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation::Numerics;
using namespace Windows::UI;
using namespace Windows::UI::Core;
using namespace Windows::UI::Composition;
using namespace Windows::Web::Http;
using namespace Windows::Data::Xml::Dom;
using namespace Windows::Security::Cryptography;
using namespace Windows::Storage::Streams;
namespace abi
{
using namespace ABI::Windows::UI::Composition;
}
namespace winrt
{
using namespace Windows::UI::Composition;
}
struct App : implements<App, IFrameworkViewSource, IFrameworkView>
{
// Media members
media::MFPlatformRef m_mfPlatform;
winrt::com_ptr<media::MediaEngineWrapper> m_mediaEngineWrapper;
// EME media members
std::shared_ptr<media::eme::MediaKeySession> m_mediaKeySession;
// Composition members
wil::critical_section m_compositionLock;
winrt::Windows::Foundation::Size m_windowSize{};
CompositionTarget m_target{ nullptr };
Visual m_videoVisual{ nullptr };
IFrameworkView CreateView()
{
return *this;
}
void ReportError(HRESULT result, const char* message)
{
// An application would typically display an error to the user.
// This sample just logs the error.
LOG_HR_MSG(result, message);
}
// EME callbacks
void OnEMEKeyMessage(MF_MEDIAKEYSESSION_MESSAGETYPE /*messageType*/, gsl::span<uint8_t> message, LPCWSTR destinationUrl)
{
// Parse challenge value and request headers from key message
winrt::hstring messageString{ reinterpret_cast<wchar_t*>(const_cast<BYTE*>(message.data())), static_cast<winrt::hstring::size_type>(message.size() / sizeof(wchar_t)) };
XmlDocument messageXml;
messageXml.LoadXml(messageString);
XmlNodeList challengeNodeList = messageXml.GetElementsByTagName(L"Challenge");
if (challengeNodeList.Size() == 0)
{
ReportError(E_UNEXPECTED, "License acquisition failure (invalid challenge data)");
THROW_HR(E_UNEXPECTED);
}
winrt::hstring challengeBase64 = challengeNodeList.GetAt(0).InnerText();
// Extract header names/values
XmlNodeList headerNameNodes = messageXml.GetElementsByTagName(L"name");
XmlNodeList headerValueNodes = messageXml.GetElementsByTagName(L"value");
if (headerNameNodes.Size() != headerValueNodes.Size())
{
ReportError(E_UNEXPECTED, "License acquisition failure (invalid challenge data)");
THROW_HR(E_UNEXPECTED);
}
// Initialize HTTP POST request with headers and challenge data from key message
HttpClient httpClient;
IBuffer challengeBuffer = CryptographicBuffer::DecodeFromBase64String(challengeBase64);
HttpBufferContent postContent(challengeBuffer);
auto headers{ httpClient.DefaultRequestHeaders() };
auto contentHeaders{ postContent.Headers() };
for (uint32_t i = 0; i < headerNameNodes.Size(); i++)
{
winrt::hstring headerName = headerNameNodes.GetAt(i).InnerText();
winrt::hstring headerValue = headerValueNodes.GetAt(i).InnerText();
// Special handling required for the 'Content-Type' header
if (std::wstring(headerName.c_str()) == std::wstring(L"Content-Type"))
{
contentHeaders.ContentType(Headers::HttpMediaTypeHeaderValue::Parse(headerValue));
}
else
{
headers.Insert(headerName, headerValue);
}
}
IBuffer responseBuffer{};
try
{
winrt::Windows::Foundation::Uri requestUri{ destinationUrl };
HttpResponseMessage responseMessage = httpClient.PostAsync(requestUri, postContent).get();
responseMessage.EnsureSuccessStatusCode();
responseBuffer = responseMessage.Content().ReadAsBufferAsync().get();
}
catch (winrt::hresult_error const& ex)
{
ReportError(ex.code(), "License acquisition failure (invalid response)");
throw ex;
}
// Pass response data to update() method on the MediaKeySession
m_mediaKeySession->update(gsl::span<uint8_t>(responseBuffer.data(), responseBuffer.Length()));
}
void Initialize(CoreApplicationView const &)
{
m_mfPlatform.Startup();
// Initialize playback on MTA MF work queue thread
// Ensure that the callback lambda holds a reference to this app instance to prevent the instance from being destroyed prior to the lambda executing
winrt::com_ptr<App> selfRef;
selfRef.copy_from(this);
media::MFPutWorkItem([&, selfRef]() {
try
{
InitializePlayback();
}
catch(...)
{
ReportError(wil::ResultFromCaughtException(), "Playback initialization error");
}
});
}
void InitializePlayback()
{
// Setup EME
media::eme::CdmConfigurationProperties configProperties = {};
// Use the local cache folder which is not backed up / synced to the cloud for CDM storage
configProperties.storagePath = Windows::Storage::ApplicationData::Current().LocalCacheFolder().Path().c_str();
media::eme::EmeFactory emeFactory(configProperties);
// Request access to the Playready key system.
// This is equivalent to the following Javascript EME call:
// Navigator.requestMediaKeySystemAccess("com.microsoft.playready.recommendation", [{ initDataTypes: ['cenc'], persistentState: 'optional', distinctiveIdentifier: 'required'])
std::vector<media::eme::MediaKeySystemConfiguration> keySystemConfigs(1);
keySystemConfigs[0].initDataTypes().push_back(L"cenc");
keySystemConfigs[0].persistentState(MF_MEDIAKEYS_REQUIREMENT_OPTIONAL);
keySystemConfigs[0].distinctiveId(MF_MEDIAKEYS_REQUIREMENT_REQUIRED);
std::shared_ptr<media::eme::MediaKeySystemAccess> keySystemAccess = emeFactory.requestMediaKeySystemAccess(L"com.microsoft.playready.recommendation", keySystemConfigs);
if(!keySystemAccess)
{
ReportError(MF_E_UNSUPPORTED_SERVICE, "Key system configuration not supported.");
return;
}
// Create MediaKeys, MediaKeySession and setup the onmessage callback on the session (to handle license acquisition requests)
std::shared_ptr<media::eme::MediaKeys> mediaKeys = keySystemAccess->createMediaKeys();
m_mediaKeySession = mediaKeys->createSession();
m_mediaKeySession->onmessage(std::bind(&App::OnEMEKeyMessage, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
// Asynchronously acquire license using hardcoded init data - usually an application would find this init data within the media container (e.g in the PSSH box of a MP4 file) or acquire it out-of-band (e.g from an adaptive streaming manifest)
std::vector<uint8_t> initData(sizeof(TestContent::c_initData));
memcpy(&initData[0], &TestContent::c_initData[0], initData.size());
m_mediaKeySession->generateRequest(L"cenc", initData);
// Create a source resolver to create an IMFMediaSource for the content URL.
// This will create an instance of an inbuilt OS media source for playback.
// An application can skip this step and instantiate a custom IMFMediaSource implementation instead.
winrt::com_ptr<IMFSourceResolver> sourceResolver;
THROW_IF_FAILED(MFCreateSourceResolver(sourceResolver.put()));
constexpr uint32_t sourceResolutionFlags = MF_RESOLUTION_MEDIASOURCE | MF_RESOLUTION_READ;
MF_OBJECT_TYPE objectType = {};
winrt::com_ptr<IMFMediaSource> mediaSource;
std::wstring contentUri(TestContent::c_contentUrl);
THROW_IF_FAILED(sourceResolver->CreateObjectFromURL(contentUri.c_str(), sourceResolutionFlags, nullptr, &objectType, reinterpret_cast<IUnknown**>(mediaSource.put_void())));
// Callbacks invoked by the media engine wrapper
auto onInitialized = std::bind(&App::OnMediaInitialized, this);
auto onError = std::bind(&App::OnMediaError, this, std::placeholders::_1, std::placeholders::_2);
// Create and initialize the MediaEngineWrapper which manages media playback
m_mediaEngineWrapper = winrt::make_self<media::MediaEngineWrapper>(onInitialized, onError, nullptr, nullptr, nullptr);
m_mediaEngineWrapper->Initialize(mediaSource.get(), mediaKeys);
}
// MediaEngineWrapper initialization callback which is invoked once the media has been loaded and a DCOMP surface handle is available
void OnMediaInitialized()
{
// Create video visual and add it to the DCOMP tree
SetupVideoVisual();
// Start playback
m_mediaEngineWrapper->StartPlayingFrom(0);
}
void SetupVideoVisual()
{
auto lock = m_compositionLock.lock();
// Complete setting up video visual if we have a surface from the media engine and the visual tree has been initialized
HANDLE videoSurfaceHandle = m_mediaEngineWrapper ? m_mediaEngineWrapper->GetSurfaceHandle() : NULL;
if (!m_videoVisual && videoSurfaceHandle != NULL && m_target)
{
Compositor compositor = m_target.Compositor();
// Create sprite visual for video
SpriteVisual visual = compositor.CreateSpriteVisual();
visual.Offset(
{
0.0f,
0.0f,
0.0f,
});
// Use the ABI ICompositorInterop interface to create an ABI composition surface using the video surface handle from the media engine
winrt::com_ptr<abi::ICompositorInterop> compositorInterop{ compositor.as<abi::ICompositorInterop>() };
winrt::com_ptr<abi::ICompositionSurface> abiCompositionSurface;
THROW_IF_FAILED(compositorInterop->CreateCompositionSurfaceForHandle(videoSurfaceHandle, abiCompositionSurface.put()));
// Convert from ABI ICompositionSurface type to winrt CompositionSurface
CompositionVisualSurface compositionSurface{ nullptr };
winrt::copy_from_abi(compositionSurface, abiCompositionSurface.get());
// Setup surface brush with surface from MediaEngineWrapper
CompositionSurfaceBrush surfaceBrush{ compositor.CreateSurfaceBrush() };
surfaceBrush.Surface(compositionSurface);
visual.Brush(surfaceBrush);
// Insert video visual intro tree
m_videoVisual = visual;
UpdateVideoSize();
m_target.Root(m_videoVisual);
}
}
void UpdateVideoSize()
{
auto lock = m_compositionLock.lock();
// If the window has not been initialized yet, use a default size of 640x480
const bool windowInitialized = m_windowSize.Width != 0 && m_windowSize.Height != 0;
float width = windowInitialized ? m_windowSize.Width : 640;
float height = windowInitialized ? m_windowSize.Height : 480;
if (m_videoVisual)
{
m_videoVisual.Size({
width,
height
});
}
if (m_mediaEngineWrapper)
{
// Call into media engine wrapper on MTA thread to resize the video surface
media::RunSyncInMTA([&]() {
m_mediaEngineWrapper->OnWindowUpdate(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
});
}
}
void OnMediaError(MF_MEDIA_ENGINE_ERR /*error*/, HRESULT hr)
{
ReportError(hr, "Media playback error.");
}
void Load(hstring const&)
{
}
void Uninitialize()
{
}
void Run()
{
CoreWindow window = CoreWindow::GetForCurrentThread();
window.Activate();
CoreDispatcher dispatcher = window.Dispatcher();
dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
}
void OnWindowSizeChanged(IInspectable const&, WindowSizeChangedEventArgs const& args)
{
auto lock = m_compositionLock.lock();
m_windowSize = args.Size();
UpdateVideoSize();
}
void SetWindow(CoreWindow const & window)
{
auto lock = m_compositionLock.lock();
Compositor compositor;
m_target = compositor.CreateTargetForCurrentView();
m_windowSize = { window.Bounds().Width, window.Bounds().Height };
UpdateVideoSize();
window.SizeChanged({ this, &App::OnWindowSizeChanged });
// If the mediaengine has been initialized, setup the video visual
SetupVideoVisual();
}
};
int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
{
CoreApplication::Run(make<App>());
}