-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMFMediaPlayer.h
225 lines (177 loc) · 6.42 KB
/
MFMediaPlayer.h
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
//////////////////////////////////////////////////////////////////////////
//
// MFMediaPlayer.h : Playback helper class.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// refer to:
// a. https://github.com/microsoft/Windows-classic-samples/tree/main/Samples/Win7Samples/multimedia/mediafoundation/protectedplayback
// b. https://docs.microsoft.com/en-us/windows/win32/medfound/seeking--fast-forward--and-reverse-play
//
//////////////////////////////////////////////////////////////////////////
#pragma once
#include <windows.h>
// C RunTime Header Files
#include <tchar.h>
#include <commdlg.h> // OpenFile dialog
#include <assert.h>
// Media Foundation headers
#include <mfapi.h>
#include <mfobjects.h>
#include <mfidl.h>
#include <mferror.h>
#include <nserror.h> // More DRM errors.
// EVR headers
#include <evr.h>
// Safe string functions
#include <strsafe.h>
#include "MFUtils.h"
#include <functional>
#include <string>
using namespace MFUtils;
enum PlayerState
{
Closed = 0, // No session.
Ready, // Session was created, ready to open a file.
OpenPending, // Session is opening a file.
Started, // Session is playing a file.
Paused, // Session is paused.
Stopped, // Session is stopped (ready to play).
Closing // Application has closed the session, but is waiting for MESessionClosed.
};
#define CMD_PENDING 0x01
#define CMD_PENDING_SEEK 0x02
#define CMD_PENDING_RATE 0x04
using SampleEventCallback = std::function<void(uint8_t* buffer, size_t size)>;
class MFMediaPlayer : public IMFAsyncCallback
{
enum Command
{
CmdNone = 0,
CmdStop,
CmdStart,
CmdPause,
CmdSeek,
};
class CritSec
{
private:
CRITICAL_SECTION m_criticalSection;
public:
CritSec() { InitializeCriticalSection(&m_criticalSection); }
~CritSec() { DeleteCriticalSection(&m_criticalSection); }
void Lock() { EnterCriticalSection(&m_criticalSection); }
void Unlock() { LeaveCriticalSection(&m_criticalSection); }
};
class AutoLock
{
private:
CritSec* m_pCriticalSection;
public:
AutoLock(CritSec& crit)
{
m_pCriticalSection = &crit;
m_pCriticalSection->Lock();
}
~AutoLock() { m_pCriticalSection->Unlock(); }
};
public:
SampleEventCallback SampleEvent;
static HRESULT CreateInstance(HWND hEvent, MFMediaPlayer** ppPlayer);
// IUnknown methods
STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
STDMETHODIMP_(ULONG) AddRef();
STDMETHODIMP_(ULONG) Release();
// IMFAsyncCallback methods
STDMETHODIMP GetParameters(DWORD*, DWORD*)
{
// Implementation of this method is optional.
return E_NOTIMPL;
}
STDMETHODIMP Invoke(IMFAsyncResult* pAsyncResult);
// Playback
HRESULT OpenURL(const WCHAR* sURL);
HRESULT Shutdown();
HRESULT HandleEvent(UINT_PTR pUnkPtr);
PlayerState GetState() const { return m_state; }
UINT32 GetVideoWidth() const { return m_uVideoWidth; }
UINT32 GetVideoHeight() const { return m_uVideoHeight; }
// Video functionality
void SetLooping(BOOL bLooping) { m_bLooping = bLooping; }
BOOL CanSeek() const;
MFTIME GetDuration() const;
MFTIME GetCurrentPosition() const;
// will reply if play ended
HRESULT SetPosition(MFTIME hnsPosition);
BOOL CanScrub() const;
HRESULT Scrub(BOOL bScrub);
BOOL CanFastForward() const;
BOOL CanRewind() const;
HRESULT SetRate(float fRate);
HRESULT FastForward();
HRESULT Rewind();
HRESULT Play();
HRESULT Pause();
HRESULT Stop();
protected:
HRESULT SetPositionInternal(const MFTIME& hnsPosition);
HRESULT StartPlayback(const MFTIME* hnsPosition);
HRESULT CommitRateChange(float fRate, BOOL bThin);
float GetNominalRate() const;
HRESULT UpdatePendingCommands(Command cmd);
protected:
// Constructor is private. Use static CreateInstance method to instantiate.
MFMediaPlayer(HWND hEvent);
// Destructor is private. Caller should call Release.
virtual ~MFMediaPlayer();
HRESULT CreateOutputNode(IMFStreamDescriptor* pSourceSD, IMFTopologyNode** ppNode);
HRESULT Initialize();
HRESULT CreateSession();
HRESULT CloseSession();
HRESULT CreateMediaSource(const WCHAR* sURL);
HRESULT CreateTopologyFromSource(IMFTopology** ppTopology);
HRESULT AddBranchToPartialTopology(IMFTopology* pTopology, IMFPresentationDescriptor* pSourcePD, DWORD iStream);
// Media event handlers
HRESULT OnTopologyReady(IMFMediaEvent* pEvent);
HRESULT OnPlayEnded(IMFMediaEvent* pEvent);
HRESULT OnSessionStart(HRESULT hr);
HRESULT OnSessionStop(HRESULT hr);
HRESULT OnSessionPause(HRESULT hr);
HRESULT OnSessionEnded(HRESULT hr);
long m_nRefCount; // Reference count.
TComPtr<IMFMediaSession> m_pSession;
TComPtr<IMFMediaSource> m_pSource;
TComPtr<IMFPresentationDescriptor> m_PresentDescriptor;
TComPtr<IMFRateControl> m_RateControl;
TComPtr<IMFRateSupport> m_RateSupport;
TComPtr<IMFPresentationClock> m_pClock;
DWORD m_caps = 0; // Session caps.
MFTIME m_hnsDuration = 0; // Duration of the current presentation.
BOOL m_bCanScrub = FALSE; // Does the current session support rate = 0.
float m_fPrevRate = 1.0f;
// Describes the current or requested state, with respect to seeking and
// playback rate.
struct SeekState
{
Command command;
float fRate; // Playback rate
BOOL bThin; // Thinned playback?
MFTIME hnsStart; // Start position
};
SeekState m_nominal{CmdStop, 1.0, FALSE, 0}; // Current nominal state.
SeekState m_request{CmdNone, 1.0, FALSE, 0}; // Pending request state.
BOOL m_bPending = FALSE; // Is a request pending?
mutable CritSec m_critsec; // Protects the seeking and rate-change states.
HWND m_hwndEvent; // App window to receive events.
PlayerState m_state; // Current state of the media session.
HANDLE m_hCloseEvent; // Event to wait on while closing
UINT32 m_uVideoWidth = 0;
UINT32 m_uVideoHeight = 0;
BOOL m_bLooping = FALSE;
BOOL m_bPlayOnOpen = TRUE;
};