-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedia.c
451 lines (357 loc) · 9.72 KB
/
Media.c
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//==========================================================================;
//
// 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) 1992 - 1996 Microsoft Corporation. All Rights Reserved.
//
//--------------------------------------------------------------------------;
#include "stdwin.h"
#include <evcode.h>
#include "cplay.h"
#include "media.h"
// Current multimedia variables
Media media;
BOOL QuitIt = FALSE;
//
// CanPlay
//
// Return true if we can go to a playing state from our current state
//
BOOL CanPlay()
{
return (media.state == Stopped || media.state == Paused);
}
//
// CanStop
//
// Return true if we can go to a stopped state from our current state
//
BOOL CanStop()
{
return (media.state == Playing || media.state == Paused);
}
//
// CanPause
//
// Return true if we can go to a paused state from our current state
//
BOOL CanPause()
{
return (media.state == Playing || media.state == Stopped);
}
//
// IsInitialized
//
// Return true if we have loaded and initialized a multimedia file
//
BOOL IsInitialized()
{
return (media.state != Uninitialized);
}
//
// ChangeStateTo
//
void ChangeStateTo( State newState )
{
media.state = newState;
// update the toolbar
}
//
// InitMedia
//
// Initialization
//
BOOL InitMedia()
{
ChangeStateTo( Uninitialized );
media.hGraphNotifyEvent = NULL;
media.pGraph = NULL;
return TRUE;
}
//
// CreateFilterGraph
//
BOOL CreateFilterGraph()
{
IMediaEvent *pME;
HRESULT hr;
// ASSERT(media.pGraph == NULL);
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;
}
// We use this to find out events sent by the filtergraph
hr = media.pGraph->lpVtbl->QueryInterface(media.pGraph, &IID_IMediaEvent, (void **) &pME);
if (FAILED(hr)) {
DeleteContents();
return FALSE;
}
hr = pME->lpVtbl->GetEventHandle(pME, (OAEVENT*) &media.hGraphNotifyEvent);
pME->lpVtbl->Release( pME );
if (FAILED(hr)) {
DeleteContents();
return FALSE;
}
return TRUE;
} // CreateFilterGraph
// Destruction
//
// DeleteContents
//
void DeleteContents()
{
if (media.pGraph != NULL) {
media.pGraph->lpVtbl->Release( media.pGraph );
media.pGraph = NULL;
}
// this event is owned by the filter graph and is thus invalid
media.hGraphNotifyEvent = NULL;
ChangeStateTo( Uninitialized );
}
//
// RenderFile
//
BOOL RenderFile( LPSTR szFileName )
{
HRESULT hr;
WCHAR wPath[MAX_PATH];
DeleteContents();
if ( !CreateFilterGraph() ) {
// PlayerMessageBox( IDS_CANT_INIT_QUARTZ );
return FALSE;
}
MultiByteToWideChar( CP_ACP, 0, szFileName, -1, wPath, MAX_PATH );
SetCursor( LoadCursor( NULL, IDC_WAIT ) );
hr = media.pGraph->lpVtbl->RenderFile(media.pGraph, wPath, NULL);
SetCursor( LoadCursor( NULL, IDC_ARROW ) );
if (FAILED( hr )) {
// PlayerMessageBox( IDS_CANT_RENDER_FILE );
return FALSE;
}
return TRUE;
} // RenderFile
//
// OpenMediaFile
//
// File..Open has been selected
//
void OpenMediaFile( HWND hwnd, LPSTR szFile )
{
static char szFileName[ _MAX_PATH ];
static char szTitleName[ _MAX_FNAME + _MAX_EXT ];
if( szFile != NULL && RenderFile( szFile ))
{
LPSTR szTitle;
// Work out the full path name and the file name from the
// specified file
GetFullPathName( szFile, _MAX_PATH, szFileName, &szTitle );
strncpy( szTitleName, szTitle, _MAX_FNAME + _MAX_EXT );
szTitleName[ _MAX_FNAME + _MAX_EXT -1 ] = '\0';
ChangeStateTo( Stopped );
}
} // OpenMediaFile
//
// OnMediaPlay
//
// There are two possible UI strategies for an application: you either play
// from the start each time you stop, or you play from the current position,
// in which case you have to explicitly rewind at the end. If you want the
// play from current and rewind at end, enable this code, if you want the
// other option, then enable FROM_START in both OnMediaStop and OnAbortStop.
#undef REWIND
#define FROM_START
void OnMediaPlay()
{
if( CanPlay() ){
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->lpVtbl->QueryInterface(media.pGraph, &IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
#ifdef REWIND
IMediaPosition * pMP;
hr = media.pGraph->lpVtbl->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->lpVtbl->get_Duration(pMP, &tLength);
if (SUCCEEDED(hr)) {
hr = pMP->lpVtbl->get_CurrentPosition(pMP, &tCurrent);
if (SUCCEEDED(hr)) {
// within 1sec of end? (or past end?)
if ((tLength - tCurrent) < 1) {
pMP->lpVtbl->put_CurrentPosition(pMP, 0);
}
}
}
pMP->lpVtbl->Release(pMP);
}
#endif
// Ask the filter graph to play and release the interface
hr = pMC->lpVtbl->Run( pMC );
pMC->lpVtbl->Release( pMC );
if( SUCCEEDED(hr) ){
ChangeStateTo( Playing );
return;
}
}
// Inform the user that an error occurred
// PlayerMessageBox( IDS_CANT_PLAY );
}
} // OnMediaPlay
//
// OnMediaPause
//
void OnMediaPause()
{
if( CanPause() ){
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->lpVtbl->QueryInterface(media.pGraph, &IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
// Ask the filter graph to pause and release the interface
hr = pMC->lpVtbl->Pause( pMC );
pMC->lpVtbl->Release( pMC );
if( SUCCEEDED(hr) ){
ChangeStateTo( Paused );
return;
}
}
// Inform the user that an error occurred
// PlayerMessageBox( IDS_CANT_PAUSE );
}
} // OnMediaPause
//
// OnMediaAbortStop
//
// Called when we get an EC_USER_ABORT or EC_ERROR_ABORT event
//
void OnMediaAbortStop(void)
{
if(CanStop())
{
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->lpVtbl->QueryInterface(media.pGraph, &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->lpVtbl->Stop( pMC );
pMC->lpVtbl->Release( pMC );
#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->lpVtbl->QueryInterface(
media.pGraph,
&IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr)) {
pMP->lpVtbl->put_CurrentPosition(pMP, 0);
pMP->lpVtbl->Release(pMP);
}
// 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 );
}
} // OnMediaAbortStop
//
// OnMediaStop
//
// There are two different ways to stop a graph. We can stop and then when we
// are later paused or run continue from the same position. Alternatively the
// graph can be set back to the start of the media when it is stopped to have
// a more CDPLAYER style interface. These are both offered here conditionally
// compiled using the FROM_START definition. The main difference is that in
// the latter case we put the current position to zero while we change states
//
void OnMediaStop()
{
if( CanStop() ){
HRESULT hr;
IMediaControl *pMC;
// Obtain the interface to our filter graph
hr = media.pGraph->lpVtbl->QueryInterface(media.pGraph, &IID_IMediaControl, (void **) &pMC);
if( SUCCEEDED(hr) ){
#ifdef FROM_START
IMediaPosition * pMP;
OAFilterState state;
// Ask the filter graph to pause
hr = pMC->lpVtbl->Pause( pMC );
// 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->lpVtbl->QueryInterface(media.pGraph,
&IID_IMediaPosition,
(void**) &pMP);
if (SUCCEEDED(hr)) {
pMP->lpVtbl->put_CurrentPosition(pMP, 0);
pMP->lpVtbl->Release(pMP);
}
// wait for pause to complete
pMC->lpVtbl->GetState(pMC, INFINITE, &state);
#endif
// now really do the stop
pMC->lpVtbl->Stop( pMC );
pMC->lpVtbl->Release( pMC );
ChangeStateTo( Stopped );
QuitIt = TRUE;
return;
}
// Inform the user that an error occurred
// PlayerMessageBox( IDS_CANT_STOP );
}
} // OnMediaStop
//
// GetGraphEventHandle
//
// We use this to check for graph events
//
HANDLE GetGraphEventHandle()
{
return media.hGraphNotifyEvent;
} // GetGraphEventHandle
//
// OnGraphNotify
//
// If the event handle is valid, then ask the graph if
// anything has happened (eg the graph has stopped...)
//
void OnGraphNotify()
{
IMediaEvent *pME;
long lEventCode, lParam1, lParam2;
// ASSERT( media.hGraphNotifyEvent != NULL );
if( SUCCEEDED(media.pGraph->lpVtbl->QueryInterface(media.pGraph, &IID_IMediaEvent, (void **) &pME))){
if( SUCCEEDED(pME->lpVtbl->GetEvent(pME, &lEventCode, &lParam1, &lParam2, 0))) {
if (lEventCode == EC_COMPLETE) {
// OnMediaAbortStop();
OnMediaStop();
} else if ((lEventCode == EC_USERABORT) ||
(lEventCode == EC_ERRORABORT)) {
OnMediaAbortStop();
}
}
pME->lpVtbl->Release( pME );
}
}