-
Notifications
You must be signed in to change notification settings - Fork 4
/
wmastream.cpp
378 lines (301 loc) · 12.4 KB
/
wmastream.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//-----------------------------------------------------------------------------
// File: WMAStream.cpp
//
// Desc: Class for streaming wave file playback.
//
// Hist: 12.15.00 - New for December XDK release
// 03.15.01 - Updated for April XDK
// 07.12.02 - Updated for August 2002 XDK
// 10.04.02 - Cleaned up for the November 2002 XDK release
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <assert.h>
#include <xtl.h>
#include "WMAStream.h"
//-----------------------------------------------------------------------------
// Name: CWMAFileStream()
// Desc: Object constructor.
//-----------------------------------------------------------------------------
CWMAFileStream::CWMAFileStream()
{
m_pSourceXMO = NULL;
m_pDestXMO = NULL;
m_pvSourceBuffer = NULL;
m_hrOpenResult = E_PENDING;
m_dwFileProgress = 0;
}
//-----------------------------------------------------------------------------
// Name: ~CWMAFileStream()
// Desc: Object destructor.
//-----------------------------------------------------------------------------
CWMAFileStream::~CWMAFileStream()
{
if( m_pSourceXMO )
m_pSourceXMO->Release();
if( m_pDestXMO )
m_pDestXMO->Release();
if( m_pvSourceBuffer )
delete[] m_pvSourceBuffer;
}
//-----------------------------------------------------------------------------
// Name: Initialize()
// Desc: Initializes the WMA file streaming object
//-----------------------------------------------------------------------------
HRESULT CWMAFileStream::Initialize( const CHAR* strFileName )
{
// Allocate data buffers. The source XMO (WMA decoder) is synchronous,
// so we only need to worry about having enough buffer space for the
// destination XMO (DirectSound Stream).
m_pvSourceBuffer = new BYTE[ WMASTRM_SOURCE_PACKET_BYTES * WMASTRM_PACKET_COUNT ];
if( NULL == m_pvSourceBuffer )
return E_OUTOFMEMORY;
// Create the decoder
WMAXMODECODERPARAMETERS Params = {0};
Params.pszFileName = strFileName;
Params.dwFileOffset = 0;
Params.dwLookaheadBufferSize = WMASTRM_LOOKAHEAD_SIZE;
// Note that this call could block, as it must call CreateFile. To avoid
// having this call block, the title can pass in an already-opened
// file handle instead of a file name.
HRESULT hr = XWmaDecoderCreateMediaObject( (LPCWMAXMODECODERPARAMETERS)&Params, &m_pSourceXMO );
if( FAILED(hr) )
{
m_hrOpenResult = E_FAIL;
return hr;
}
// Set the decoder status, so that Process() knows
// to create the DSound stream with the correct
// wave format when the decoder is ready
m_dwLastDecoderStatus = 0;
// Initialize packet status array
ZeroMemory( m_adwStatus, sizeof(m_adwStatus) );
return S_OK;
}
extern int startTime;
//-----------------------------------------------------------------------------
// Name: CreateStream()
// Desc: Creates the Direct Sound stream
//-----------------------------------------------------------------------------
HRESULT CWMAFileStream::CreateStream()
{
// Get the total wave file duration. We'll use this to calculate how far
// along in the file we are during processing.
HRESULT hr = m_pSourceXMO->GetFileHeader( &m_WmaFileHeader );
if( FAILED( hr ) )
{
m_hrOpenResult = E_FAIL;
return hr;
}
// get some info about the wma content
WMAXMOFileContDesc desc = {0};
desc.pTitle = m_strWmaTitle;
desc.wTitleLength = sizeof( m_strWmaTitle );
desc.pAuthor = m_strWmaAuthor;
desc.wAuthorLength = sizeof( m_strWmaAuthor );
desc.pDescription = m_strWmaDescription;
desc.wDescriptionLength = sizeof( m_strWmaDescription );
hr = m_pSourceXMO->GetFileContentDescription( &desc );
if( FAILED( hr ) )
{
m_hrOpenResult = E_FAIL;
return hr;
}
// Create an appropriate WAVEFORMATEX struct
XAudioCreatePcmFormat( (WORD)m_WmaFileHeader.dwNumChannels, m_WmaFileHeader.dwSampleRate,
(WORD)16, &m_wfxSourceFormat );
// Create the stream
DSSTREAMDESC dssd;
ZeroMemory( &dssd, sizeof( dssd ) );
dssd.dwMaxAttachedPackets = WMASTRM_PACKET_COUNT;
dssd.lpwfxFormat = &m_wfxSourceFormat;
hr = DirectSoundCreateStream( &dssd, &m_pDestXMO );
if( FAILED(hr) )
{
m_hrOpenResult = E_FAIL;
return hr;
}
#if _DEBUG
// We expect the source XMO to be synchronous and read-only and the
// destination XMO to be asynchronous write-only. Assert that all
// of this is true and check the packet sizes for compatibility.
XMEDIAINFO xmi = {0};
hr = m_pSourceXMO->GetInfo( &xmi );
if( FAILED(hr) )
{
m_hrOpenResult = E_FAIL;
return hr;
}
assert( xmi.dwFlags & XMO_STREAMF_FIXED_SAMPLE_SIZE );
assert( xmi.dwMaxLookahead == 0 );
assert( xmi.dwOutputSize );
assert( xmi.dwInputSize == 0 );
assert( WMASTRM_SOURCE_PACKET_BYTES % xmi.dwOutputSize == 0 );
hr = m_pDestXMO->GetInfo( &xmi );
if( FAILED(hr) )
{
m_hrOpenResult = E_FAIL;
return hr;
}
assert( xmi.dwFlags == ( XMO_STREAMF_FIXED_SAMPLE_SIZE | XMO_STREAMF_INPUT_ASYNC ) );
assert( WMASTRM_SOURCE_PACKET_BYTES * WMASTRM_PACKET_COUNT >= xmi.dwMaxLookahead );
assert( WMASTRM_SOURCE_PACKET_BYTES % xmi.dwInputSize == 0 );
assert( xmi.dwOutputSize == 0 );
#endif // _DEBUG
m_hrOpenResult = S_OK;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Process()
// Desc: Performs any work necessary to keep the stream playing.
//-----------------------------------------------------------------------------
HRESULT CWMAFileStream::Process( DWORD* pdwPercentCompleted /* = NULL */ )
{
DWORD dwPacketIndex;
HRESULT hr;
// Let the decoder do some work - this is where it schedules its
// asynchronous file I/O.
m_pSourceXMO->DoWork();
// Since the WMA header is read asynchronously, we need to wait
// until the decoder says it's ready to produce output. Once
// the decoder says it's ready, we can create our stream and
// start playback. It makes things a little more complex, but
// it's better than blocking every time we start streaming a
// new WMA file.
if( ( m_dwLastDecoderStatus & XMO_STATUSF_ACCEPT_OUTPUT_DATA ) == 0 )
{
hr = m_pSourceXMO->GetStatus( &m_dwLastDecoderStatus );
if( FAILED( hr ) )
return hr;
// The decoder still isn't ready - bail
if( ( m_dwLastDecoderStatus & XMO_STATUSF_ACCEPT_OUTPUT_DATA ) == 0 )
return S_OK;
// If we get here, the decoder is ready to produce output.
// Create our stream and start processing.
if( FAILED( CreateStream() ) )
return E_FAIL;
}
// Find a free packet, re-fill with decoded data and submit to the stream.
// If there are no free packets, we don't have anything to do.
while( FindFreePacket( &dwPacketIndex ) )
{
// Grab data from the source XMO (WMA decoder)
hr = ProcessSource( dwPacketIndex );
if( FAILED(hr) )
return hr;
// Send the data to the destination XMO (DirectSound Stream)
hr = ProcessRenderer( dwPacketIndex );
if( FAILED(hr) )
return hr;
}
// Calculate the completion percentage based on the total amount of
// data we've played back.
if( pdwPercentCompleted )
{
FLOAT fMsecCompleted = m_dwFileProgress * 1000.0f / m_wfxSourceFormat.nAvgBytesPerSec;
(*pdwPercentCompleted) = DWORD( 100 * fMsecCompleted / m_WmaFileHeader.dwDuration );
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: FindFreePacket()
// Desc: Finds a render packet available for processing.
//-----------------------------------------------------------------------------
BOOL CWMAFileStream::FindFreePacket( DWORD* pdwPacketIndex )
{
for( DWORD dwPacketIndex = 0; dwPacketIndex < WMASTRM_PACKET_COUNT; dwPacketIndex++ )
{
if( XMEDIAPACKET_STATUS_PENDING != m_adwStatus[ dwPacketIndex ] )
{
if( pdwPacketIndex )
(*pdwPacketIndex) = dwPacketIndex;
return TRUE;
}
}
return FALSE;
}
//-----------------------------------------------------------------------------
// Name: ProcessSource()
// Desc: Reads data from the source XMO (WMA decoder).
//-----------------------------------------------------------------------------
HRESULT CWMAFileStream::ProcessSource( DWORD dwPacketIndex )
{
DWORD dwTotalSourceUsed = 0;
DWORD dwSourceUsed;
HRESULT hr;
// We're going to read a full packet's worth of data into the source
// buffer. Since we're playing in an infinite loop, we'll just spin
// until we've read enough data, even if that means wrapping around the
// end of the file.
XMEDIAPACKET xmp = {0};
xmp.pvBuffer = (BYTE*)m_pvSourceBuffer + (dwPacketIndex * WMASTRM_SOURCE_PACKET_BYTES);
xmp.dwMaxSize = WMASTRM_SOURCE_PACKET_BYTES;
xmp.pdwCompletedSize = &dwSourceUsed;
xmp.pdwStatus = &m_adwStatus[ dwPacketIndex ];
while( dwTotalSourceUsed < WMASTRM_SOURCE_PACKET_BYTES )
{
// Read from the source
hr = m_pSourceXMO->Process( NULL, &xmp );
if( FAILED(hr) )
return hr;
// Add the amount read to the total
dwTotalSourceUsed += dwSourceUsed;
// If we read less than the amount requested, it's because we hit
// the end of the file. Seek back to the start and keep going.
if( dwSourceUsed < xmp.dwMaxSize )
{
xmp.pvBuffer = (BYTE*)xmp.pvBuffer + dwSourceUsed;
xmp.dwMaxSize = xmp.dwMaxSize - dwSourceUsed;
hr = m_pSourceXMO->Flush();
if( FAILED(hr) )
return hr;
m_dwFileProgress = 0;
};
}
// Update the file progress. We keep track in bytes and do the bytes->ms
// and/or ms->percentage calculation at the last step, so as not to
// lose precision
m_dwFileProgress += dwTotalSourceUsed;
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: ProcessRenderer()
// Desc: Sends data to the destination XMO (DirectSound stream).
//-----------------------------------------------------------------------------
HRESULT CWMAFileStream::ProcessRenderer( DWORD dwPacketIndex )
{
// There's a full packet's worth of data ready for us to send to the
// renderer. We want to track the status of this packet since the
// destination XMO is asynchronous and we need to know when the packet is
// completed.
XMEDIAPACKET xmp = {0};
xmp.pvBuffer = (BYTE*)m_pvSourceBuffer + (dwPacketIndex * WMASTRM_SOURCE_PACKET_BYTES );
xmp.dwMaxSize = WMASTRM_SOURCE_PACKET_BYTES;
xmp.pdwStatus = &m_adwStatus[ dwPacketIndex ];
// Submit for playback
return m_pDestXMO->Process( &xmp, NULL );
}
//-----------------------------------------------------------------------------
// Name: Pause()
// Desc: Pauses and resumes stream playback
//-----------------------------------------------------------------------------
VOID CWMAFileStream::Pause( DWORD dwPause )
{
m_pDestXMO->Pause( dwPause );
}
VOID CWMAFileStream::Seek( DWORD time, LPDWORD timeActual )
{
/* DWORD dwTotalSourceUsed = 0;
DWORD dwSourceUsed;
HRESULT hr;
XMEDIAPACKET xmp = {0};
xmp.pvBuffer = (BYTE*)m_pvSourceBuffer + (0 * WMASTRM_SOURCE_PACKET_BYTES);
xmp.dwMaxSize = WMASTRM_SOURCE_PACKET_BYTES;
xmp.pdwCompletedSize = &dwSourceUsed;
xmp.pdwStatus = &m_adwStatus[ 0];
for(int i=0;i<100;i++)
hr = m_pSourceXMO->Process( NULL, &xmp );
*/
// m_pSourceXMO->Seek(1024*1024, FILE_BEGIN, &xx);
// m_pSourceXMO->SeekToTime(time, timeActual);
}