-
Notifications
You must be signed in to change notification settings - Fork 2
/
Media.cpp
356 lines (277 loc) · 7.98 KB
/
Media.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
1999-2000 Copyright (c) mahalo, the nogada worker.. (Amigo~*). All rights reserved.
Copyright (c) Hyung-seok Choi, the Graphics Designer. All rights reserved.
2000/07/20 - This implemented Media play using DirectShow (In DirectMedia)
This support almost all audio-media (like windows media player)
*/
// Media.cpp: implementation of the CMedia class.
//
//////////////////////////////////////////////////////////////////////
#include "Media.h"
#include "main.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMedia::CMedia()
{
ChangeStateTo( Uninitialized );
media.hGraphNotifyEvent = NULL;
media.pGraph = NULL;
// return TRUE;
}
CMedia::~CMedia()
{
if (media.pGraph != NULL) {
media.pGraph->Release();
media.pGraph = NULL;
}
// this event is owned by the filter graph and is thus invalid
media.hGraphNotifyEvent = NULL;
}
BOOL CMedia::RenderFile(LPSTR szFileName)
{
/* WCHAR wPath[MAX_PATH];
// DeleteContents();
if ( !CreateFilterGraph() ) {
return FALSE;
}
MultiByteToWideChar( CP_ACP, 0, szFileName, -1, wPath, MAX_PATH );
media.pMC->RenderFile(wPath);
return TRUE;
*/
HRESULT hr;
WCHAR wPath[MAX_PATH];
DeleteContents();
if ( !CreateFilterGraph() ) {
InitFail(hWnd, FALSE, "CreateFilterGrapy() : ERROR");
return FALSE;
}
MultiByteToWideChar( CP_ACP, 0, szFileName, -1, wPath, MAX_PATH );
SetCursor( LoadCursor( NULL, IDC_WAIT ) );
hr = media.pGraph->RenderFile(wPath, NULL);
SetCursor( LoadCursor( NULL, IDC_ARROW ) );
if (FAILED( hr )) {
//PlayerMessageBox( IDS_CANT_RENDER_FILE );
InitFail(hWnd, FALSE, "RenderFile() : ERROR");
return FALSE;
}
return TRUE;
}
BOOL CMedia::CreateFilterGraph()
{
CoInitialize(NULL);
HRESULT hr;
hr = CoCreateInstance(CLSID_FilterGraph, // CLSID of object
NULL, // Outer unknown
CLSCTX_INPROC_SERVER, // Type of server
IID_IGraphBuilder, // Interface wanted
(void **) &media.pGraph); // Returned object
if (FAILED(hr)){
media.pGraph = NULL;
return FALSE;
}
/*hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC, IID_IMediaControl, (void **)&media.pMC);
if (FAILED(hr)){
media.pMC = NULL;
return FALSE;
} */
hr = media.pGraph->QueryInterface (IID_IMediaEvent, (void **) &media.pME);
//hr = media.pMC->QueryInterface( IID_IMediaEvent, (void **) &media.pME);
if (FAILED(hr)) {
return FALSE;
}
hr = media.pME->GetEventHandle((OAEVENT*) &media.hGraphNotifyEvent);
if (FAILED(hr)) {
return FALSE;
}
hr = media.pGraph->QueryInterface( IID_IMediaPosition, (void**)&media.pMP);
if (FAILED(hr)) {
DeleteContents();
return FALSE;
}
CoUninitialize();
return TRUE;
}
#undef REWIND
#define FROM_START
void CMedia::OnMediaPlay()
{
if( CanPlay() ){
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->QueryInterface(IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
#ifdef REWIND
IMediaPosition * pMP;
hr = media.pGraph->QueryInterface(media.pGraph,
&IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr)) {
// start from last position, but rewind if near the end
REFTIME tCurrent, tLength;
hr = pMP->get_Duration(pMP, &tLength);
if (SUCCEEDED(hr)) {
hr = pMP->get_CurrentPosition(pMP, &tCurrent);
if (SUCCEEDED(hr)) {
// within 1sec of end? (or past end?)
if ((tLength - tCurrent) < 1) {
pMP->put_CurrentPosition(pMP, 0);
}
}
}
pMP->Release(pMP);
}
#endif
// Ask the filter graph to play and release the interface
hr = pMC->Run();
pMC->Release();
if( SUCCEEDED(hr) ){
ChangeStateTo( Playing );
return;
}
}
// Inform the user that an error occurred
//PlayerMessageBox( IDS_CANT_PLAY );
InitFail(hWnd, FALSE, "OnMediaPlay() : ERROR");
}
}
void CMedia::DeleteContents()
{
if (media.pGraph != NULL) {
media.pGraph->Release();
media.pGraph = NULL;
}
// this event is owned by the filter graph and is thus invalid
media.hGraphNotifyEvent = NULL;
ChangeStateTo( Uninitialized );
}
BOOL CMedia::CanPlay()
{
return (media.state == Stopped || media.state == Paused);
}
BOOL CMedia::CanStop()
{
return (media.state == Playing || media.state == Paused);
}
void CMedia::ChangeStateTo(State newState)
{
media.state = newState;
}
void CMedia::OpenMediaFile(LPSTR szFile)
{
RenderFile(szFile);
ChangeStateTo(Stopped);
}
void CMedia::OnMediaPause()
{
if( CanPause() ){
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->QueryInterface(IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
// Ask the filter graph to pause and release the interface
hr = pMC->Pause();
pMC->Release();
if( SUCCEEDED(hr) ){
ChangeStateTo( Paused );
return;
}
}
// Inform the user that an error occurred
//PlayerMessageBox( IDS_CANT_PAUSE );
InitFail(hWnd, FALSE, "OnMediaPause() : ERROR");
}
}
void CMedia::OnMediaAbortStop()
{
if(CanStop())
{
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->QueryInterface(IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
#ifdef FROM_START
IMediaPosition * pMP;
#endif
// Ask the filter graph to stop and release the interface
hr = pMC->Stop();
pMC->Release();
#ifdef FROM_START
// if we want always to play from the beginning
// then we should seek back to the start here
// (on app or user stop request, and also after EC_COMPLETE).
hr = media.pGraph->QueryInterface(
IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr)) {
pMP->put_CurrentPosition(0);
pMP->Release();
}
// no visible rewind or we will re-show the window!
#endif
if( SUCCEEDED(hr) ){
ChangeStateTo( Stopped );
return;
}
}
// Inform the user that an error occurred
//PlayerMessageBox( IDS_CANT_STOP );
InitFail(hWnd, FALSE, "OnMediaStop() : ERROR");
}
}
void CMedia::OnMediaStop()
{
if( CanStop() ){
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->QueryInterface(IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
#ifdef FROM_START
IMediaPosition * pMP;
OAFilterState state;
// Ask the filter graph to pause
hr = pMC->Pause();
// if we want always to play from the beginning
// then we should seek back to the start here
// (on app or user stop request, and also after EC_COMPLETE).
hr = media.pGraph->QueryInterface(IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr)) {
pMP->put_CurrentPosition(0);
pMP->Release();
}
// wait for pause to complete
pMC->GetState(INFINITE, &state);
#endif
// now really do the stop
pMC->Stop();
pMC->Release();
ChangeStateTo( Stopped );
return;
}
// Inform the user that an error occurred
//PlayerMessageBox( IDS_CANT_STOP );
InitFail(hWnd, FALSE, "OnMediaStop() : ERROR");
}
}
BOOL CMedia::CanPause()
{
return (media.state == Playing || media.state == Paused);
}
REFTIME CMedia::GetCurrentPosition()
{
HRESULT hr;
REFTIME r_time;
IMediaPosition * pMP;
hr = media.pGraph->QueryInterface(IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr)) {
pMP->get_CurrentPosition(&r_time);
pMP->Release();
}
return r_time;
}