/
CDPlayer.c
663 lines (508 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "CDPlayer.h"
#include "AudioFilePlayer.h"
25
#include "SDLOSXCAGuard.h"
26
27
// we're exporting these functions into C land for SDL_syscdrom.c
28
//extern "C" {
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
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// Constants
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
#define kAudioCDFilesystemID (UInt16)(('J' << 8) | 'H') // 'JH'; this avoids compiler warning
// XML PList keys
#define kRawTOCDataString "Format 0x02 TOC Data"
#define kSessionsString "Sessions"
#define kSessionTypeString "Session Type"
#define kTrackArrayString "Track Array"
#define kFirstTrackInSessionString "First Track"
#define kLastTrackInSessionString "Last Track"
#define kLeadoutBlockString "Leadout Block"
#define kDataKeyString "Data"
#define kPointKeyString "Point"
#define kSessionNumberKeyString "Session Number"
#define kStartBlockKeyString "Start Block"
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// Globals
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
#pragma mark -- Globals --
55
static int playBackWasInit = 0;
56
57
58
static AudioUnit theUnit;
static AudioFilePlayer* thePlayer = NULL;
static CDPlayerCompletionProc completionProc = NULL;
59
60
static SDL_mutex *apiMutex = NULL;
static SDL_sem *callbackSem;
61
62
63
64
65
66
67
68
static SDL_CD* theCDROM;
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// Prototypes
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
#pragma mark -- Prototypes --
69
static OSStatus CheckInit ();
70
71
static void FilePlayNotificationHandler (void* inRefCon, OSStatus inStatus);
72
73
static int RunCallBackThread (void* inRefCon);
74
75
76
77
78
79
#pragma mark -- Public Functions --
void Lock ()
{
80
81
if (!apiMutex) {
apiMutex = SDL_CreateMutex();
82
}
83
SDL_mutexP(apiMutex);
84
85
86
87
}
void Unlock ()
{
88
SDL_mutexV(apiMutex);
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// DetectAudioCDVolumes
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int DetectAudioCDVolumes(FSVolumeRefNum *volumes, int numVolumes)
{
int volumeIndex;
int cdVolumeCount = 0;
OSStatus result = noErr;
for (volumeIndex = 1; result == noErr || result != nsvErr; volumeIndex++)
{
FSVolumeRefNum actualVolume;
FSVolumeInfo volumeInfo;
memset (&volumeInfo, 0, sizeof(volumeInfo));
result = FSGetVolumeInfo (kFSInvalidVolumeRefNum,
volumeIndex,
&actualVolume,
kFSVolInfoFSInfo,
&volumeInfo,
113
114
NULL,
NULL);
115
116
117
118
if (result == noErr)
{
if (volumeInfo.filesystemID == kAudioCDFilesystemID) // It's an audio CD
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
if (volumes != NULL && cdVolumeCount < numVolumes)
volumes[cdVolumeCount] = actualVolume;
cdVolumeCount++;
}
}
else
{
// I'm commenting this out because it seems to be harmless
//SDL_SetError ("DetectAudioCDVolumes: FSGetVolumeInfo returned %d", result);
}
}
return cdVolumeCount;
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// ReadTOCData
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int ReadTOCData (FSVolumeRefNum theVolume, SDL_CD *theCD)
{
HFSUniStr255 dataForkName;
OSStatus theErr;
SInt16 forkRefNum;
SInt64 forkSize;
Ptr forkData = 0;
ByteCount actualRead;
CFDataRef dataRef = 0;
CFPropertyListRef propertyListRef = 0;
FSRefParam fsRefPB;
FSRef tocPlistFSRef;
const char* error = "Unspecified Error";
// get stuff from .TOC.plist
fsRefPB.ioCompletion = NULL;
fsRefPB.ioNamePtr = "\p.TOC.plist";
fsRefPB.ioVRefNum = theVolume;
fsRefPB.ioDirID = 0;
fsRefPB.newRef = &tocPlistFSRef;
theErr = PBMakeFSRefSync (&fsRefPB);
if(theErr != noErr) {
error = "PBMakeFSRefSync";
goto bail;
}
// Load and parse the TOC XML data
theErr = FSGetDataForkName (&dataForkName);
if (theErr != noErr) {
error = "FSGetDataForkName";
goto bail;
}
theErr = FSOpenFork (&tocPlistFSRef, dataForkName.length, dataForkName.unicode, fsRdPerm, &forkRefNum);
if (theErr != noErr) {
error = "FSOpenFork";
goto bail;
}
theErr = FSGetForkSize (forkRefNum, &forkSize);
if (theErr != noErr) {
error = "FSGetForkSize";
goto bail;
}
// Allocate some memory for the XML data
forkData = NewPtr (forkSize);
if(forkData == NULL) {
error = "NewPtr";
goto bail;
}
theErr = FSReadFork (forkRefNum, fsFromStart, 0 /* offset location */, forkSize, forkData, &actualRead);
if(theErr != noErr) {
error = "FSReadFork";
goto bail;
}
dataRef = CFDataCreate (kCFAllocatorDefault, (UInt8 *)forkData, forkSize);
if(dataRef == 0) {
error = "CFDataCreate";
goto bail;
}
propertyListRef = CFPropertyListCreateFromXMLData (kCFAllocatorDefault,
dataRef,
kCFPropertyListImmutable,
NULL);
if (propertyListRef == NULL) {
error = "CFPropertyListCreateFromXMLData";
goto bail;
}
// Now we got the Property List in memory. Parse it.
// First, make sure the root item is a CFDictionary. If not, release and bail.
if(CFGetTypeID(propertyListRef)== CFDictionaryGetTypeID())
{
CFDictionaryRef dictRef = (CFDictionaryRef)propertyListRef;
CFDataRef theRawTOCDataRef;
CFArrayRef theSessionArrayRef;
CFIndex numSessions;
CFIndex index;
// This is how we get the Raw TOC Data
theRawTOCDataRef = (CFDataRef)CFDictionaryGetValue (dictRef, CFSTR(kRawTOCDataString));
// Get the session array info.
theSessionArrayRef = (CFArrayRef)CFDictionaryGetValue (dictRef, CFSTR(kSessionsString));
// Find out how many sessions there are.
numSessions = CFArrayGetCount (theSessionArrayRef);
// Initialize the total number of tracks to 0
theCD->numtracks = 0;
// Iterate over all sessions, collecting the track data
for(index = 0; index < numSessions; index++)
{
CFDictionaryRef theSessionDict;
CFNumberRef leadoutBlock;
CFArrayRef trackArray;
CFIndex numTracks;
CFIndex trackIndex;
UInt32 value = 0;
theSessionDict = (CFDictionaryRef) CFArrayGetValueAtIndex (theSessionArrayRef, index);
leadoutBlock = (CFNumberRef) CFDictionaryGetValue (theSessionDict, CFSTR(kLeadoutBlockString));
trackArray = (CFArrayRef)CFDictionaryGetValue (theSessionDict, CFSTR(kTrackArrayString));
numTracks = CFArrayGetCount (trackArray);
for(trackIndex = 0; trackIndex < numTracks; trackIndex++) {
CFDictionaryRef theTrackDict;
CFNumberRef trackNumber;
CFNumberRef sessionNumber;
CFNumberRef startBlock;
CFBooleanRef isDataTrack;
UInt32 value;
theTrackDict = (CFDictionaryRef) CFArrayGetValueAtIndex (trackArray, trackIndex);
trackNumber = (CFNumberRef) CFDictionaryGetValue (theTrackDict, CFSTR(kPointKeyString));
sessionNumber = (CFNumberRef) CFDictionaryGetValue (theTrackDict, CFSTR(kSessionNumberKeyString));
startBlock = (CFNumberRef) CFDictionaryGetValue (theTrackDict, CFSTR(kStartBlockKeyString));
isDataTrack = (CFBooleanRef) CFDictionaryGetValue (theTrackDict, CFSTR(kDataKeyString));
// Fill in the SDL_CD struct
int idx = theCD->numtracks++;
CFNumberGetValue (trackNumber, kCFNumberSInt32Type, &value);
theCD->track[idx].id = value;
CFNumberGetValue (startBlock, kCFNumberSInt32Type, &value);
theCD->track[idx].offset = value;
theCD->track[idx].type = (isDataTrack == kCFBooleanTrue) ? SDL_DATA_TRACK : SDL_AUDIO_TRACK;
// Since the track lengths are not stored in .TOC.plist we compute them.
if (trackIndex > 0) {
theCD->track[idx-1].length = theCD->track[idx].offset - theCD->track[idx-1].offset;
}
}
// Compute the length of the last track
CFNumberGetValue (leadoutBlock, kCFNumberSInt32Type, &value);
theCD->track[theCD->numtracks-1].length =
value - theCD->track[theCD->numtracks-1].offset;
// Set offset to leadout track
theCD->track[theCD->numtracks].offset = value;
}
}
theErr = 0;
goto cleanup;
bail:
SDL_SetError ("ReadTOCData: %s returned %d", error, theErr);
theErr = -1;
cleanup:
if (propertyListRef != NULL)
CFRelease(propertyListRef);
if (dataRef != NULL)
CFRelease(dataRef);
if (forkData != NULL)
DisposePtr(forkData);
FSCloseFork (forkRefNum);
return theErr;
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// ListTrackFiles
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int ListTrackFiles (FSVolumeRefNum theVolume, FSRef *trackFiles, int numTracks)
{
OSStatus result = -1;
FSIterator iterator;
ItemCount actualObjects;
FSRef rootDirectory;
FSRef ref;
HFSUniStr255 nameStr;
result = FSGetVolumeInfo (theVolume,
0,
NULL,
kFSVolInfoFSInfo,
NULL,
NULL,
&rootDirectory);
if (result != noErr) {
SDL_SetError ("ListTrackFiles: FSGetVolumeInfo returned %d", result);
345
return result;
346
347
348
349
350
351
352
353
354
355
356
357
358
359
}
result = FSOpenIterator (&rootDirectory, kFSIterateFlat, &iterator);
if (result == noErr) {
do
{
result = FSGetCatalogInfoBulk (iterator, 1, &actualObjects,
NULL, kFSCatInfoNone, NULL, &ref, NULL, &nameStr);
if (result == noErr) {
CFStringRef name;
name = CFStringCreateWithCharacters (NULL, nameStr.unicode, nameStr.length);
// Look for .aiff extension
360
361
if (CFStringHasSuffix (name, CFSTR(".aiff")) ||
CFStringHasSuffix (name, CFSTR(".cdda"))) {
362
363
364
// Extract the track id from the filename
int trackID = 0, i = 0;
365
366
367
368
while (i < nameStr.length && !isdigit(nameStr.unicode[i])) {
++i;
}
while (i < nameStr.length && isdigit(nameStr.unicode[i])) {
369
trackID = 10 * trackID +(nameStr.unicode[i] - '0');
370
++i;
371
}
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#if DEBUG_CDROM
printf("Found AIFF for track %d: '%s'\n", trackID,
CFStringGetCStringPtr (name, CFStringGetSystemEncoding()));
#endif
// Track ID's start at 1, but we want to start at 0
trackID--;
assert(0 <= trackID && trackID <= SDL_MAX_TRACKS);
if (trackID < numTracks)
memcpy (&trackFiles[trackID], &ref, sizeof(FSRef));
}
CFRelease (name);
}
} while(noErr == result);
FSCloseIterator (iterator);
}
392
return 0;
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// LoadFile
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int LoadFile (const FSRef *ref, int startFrame, int stopFrame)
{
int error = -1;
if (CheckInit () < 0)
goto bail;
// release any currently playing file
if (ReleaseFile () < 0)
goto bail;
#if DEBUG_CDROM
printf ("LoadFile: %d %d\n", startFrame, stopFrame);
#endif
414
//try {
415
416
417
// create a new player, and attach to the audio unit
418
thePlayer = new_AudioFilePlayer(ref);
419
420
if (thePlayer == NULL) {
SDL_SetError ("LoadFile: Could not create player");
421
return -3; //throw (-3);
422
423
}
424
425
if (!thePlayer->SetDestination(thePlayer, &theUnit))
goto bail;
426
427
if (startFrame >= 0)
428
thePlayer->SetStartFrame (thePlayer, startFrame);
429
430
if (stopFrame >= 0 && stopFrame > startFrame)
431
thePlayer->SetStopFrame (thePlayer, stopFrame);
432
433
// we set the notifier later
434
//thePlayer->SetNotifier(thePlayer, FilePlayNotificationHandler, NULL);
435
436
437
if (!thePlayer->Connect(thePlayer))
goto bail;
438
439
#if DEBUG_CDROM
440
thePlayer->Print(thePlayer);
441
442
fflush (stdout);
#endif
443
444
445
446
447
//}
//catch (...)
//{
// goto bail;
//}
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
error = 0;
bail:
return error;
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// ReleaseFile
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int ReleaseFile ()
{
int error = -1;
463
464
// (Don't see any way that the original C++ code could throw here.) --ryan.
//try {
465
466
if (thePlayer != NULL) {
467
thePlayer->Disconnect(thePlayer);
468
469
delete_AudioFilePlayer(thePlayer);
470
471
472
thePlayer = NULL;
}
473
474
475
476
477
//}
//catch (...)
//{
// goto bail;
//}
478
479
480
error = 0;
481
// bail:
482
483
484
485
486
487
488
489
490
491
492
493
494
495
return error;
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// PlayFile
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int PlayFile ()
{
OSStatus result = -1;
if (CheckInit () < 0)
goto bail;
496
// try {
497
498
499
// start processing of the audio unit
result = AudioOutputUnitStart (theUnit);
500
if (result) goto bail; //THROW_RESULT("PlayFile: AudioOutputUnitStart")
501
502
503
504
505
506
// }
// catch (...)
// {
// goto bail;
// }
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
result = 0;
bail:
return result;
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// PauseFile
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
int PauseFile ()
{
OSStatus result = -1;
if (CheckInit () < 0)
goto bail;
525
//try {
526
527
528
// stop processing the audio unit
result = AudioOutputUnitStop (theUnit);
529
530
531
532
533
534
if (result) goto bail; //THROW_RESULT("PauseFile: AudioOutputUnitStop")
//}
//catch (...)
//{
// goto bail;
//}
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
result = 0;
bail:
return result;
}
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// SetCompletionProc
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
void SetCompletionProc (CDPlayerCompletionProc proc, SDL_CD *cdrom)
{
assert(thePlayer != NULL);
theCDROM = cdrom;
completionProc = proc;
551
thePlayer->SetNotifier (thePlayer, FilePlayNotificationHandler, cdrom);
552
553
}
554
555
556
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
// GetCurrentFrame
//ÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑÑ
557
558
559
560
561
562
563
564
int GetCurrentFrame ()
{
int frame;
if (thePlayer == NULL)
frame = 0;
else
565
frame = thePlayer->GetCurrentFrame (thePlayer);
566
567
568
569
570
571
572
return frame;
}
#pragma mark -- Private Functions --
573
static OSStatus CheckInit ()
574
575
576
577
578
579
{
if (playBackWasInit)
return 0;
OSStatus result = noErr;
580
581
582
// Create the callback semaphore
callbackSem = SDL_CreateSemaphore(0);
583
// Start callback thread
584
SDL_CreateThread(RunCallBackThread, NULL);
585
586
{ //try {
587
588
589
590
591
592
593
594
595
596
597
ComponentDescription desc;
desc.componentType = kAudioUnitComponentType;
desc.componentSubType = kAudioUnitSubType_Output;
desc.componentManufacturer = kAudioUnitID_DefaultOutput;
desc.componentFlags = 0;
desc.componentFlagsMask = 0;
Component comp = FindNextComponent (NULL, &desc);
if (comp == NULL) {
SDL_SetError ("CheckInit: FindNextComponent returned NULL");
598
if (result) return -1; //throw(internalComponentErr);
599
600
601
}
result = OpenAComponent (comp, &theUnit);
602
if (result) return -1; //THROW_RESULT("CheckInit: OpenAComponent")
603
604
605
// you need to initialize the output unit before you set it as a destination
result = AudioUnitInitialize (theUnit);
606
if (result) return -1; //THROW_RESULT("CheckInit: AudioUnitInitialize")
607
608
609
610
playBackWasInit = true;
}
611
612
613
614
//catch (...)
//{
// return -1;
//}
615
616
617
618
return 0;
}
619
static void FilePlayNotificationHandler(void * inRefCon, OSStatus inStatus)
620
621
622
623
{
if (inStatus == kAudioFilePlay_FileIsFinished) {
// notify non-CA thread to perform the callback
624
SDL_SemPost(callbackSem);
625
626
627
628
629
630
631
632
633
634
635
636
637
} else if (inStatus == kAudioFilePlayErr_FilePlayUnderrun) {
SDL_SetError ("CDPlayer Notification: buffer underrun");
} else if (inStatus == kAudioFilePlay_PlayerIsUninitialized) {
SDL_SetError ("CDPlayer Notification: player is uninitialized");
} else {
SDL_SetError ("CDPlayer Notification: unknown error %ld", inStatus);
}
}
638
static int RunCallBackThread (void *param)
639
{
640
for (;;) {
641
642
SDL_SemWait(callbackSem);
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
if (completionProc && theCDROM) {
#if DEBUG_CDROM
printf ("callback!\n");
#endif
(*completionProc)(theCDROM);
} else {
#if DEBUG_CDROM
printf ("callback?\n");
#endif
}
}
#if DEBUG_CDROM
printf ("thread dying now...\n");
#endif
660
return 0;
661
662
}
663
//}; // extern "C"