comparison src/cdrom/macosx/CDPlayer.cpp @ 775:08b7fc2b5225

Fixed playback problems with MacOSX 10.1
author Sam Lantinga <slouken@libsdl.org>
date Mon, 05 Jan 2004 12:08:38 +0000
parents 336603031bab
children f8d5ddc7aef1
comparison
equal deleted inserted replaced
774:0c3e00cc9580 775:08b7fc2b5225
54 54
55 static bool playBackWasInit = false; 55 static bool playBackWasInit = false;
56 static AudioUnit theUnit; 56 static AudioUnit theUnit;
57 static AudioFilePlayer* thePlayer = NULL; 57 static AudioFilePlayer* thePlayer = NULL;
58 static CDPlayerCompletionProc completionProc = NULL; 58 static CDPlayerCompletionProc completionProc = NULL;
59 static pthread_mutex_t apiMutex; 59 static SDL_mutex *apiMutex = NULL;
60 static pthread_t callbackThread; 60 static SDL_sem *callbackSem;
61 static pthread_mutex_t callbackMutex;
62 static volatile int runCallBackThread;
63 static int initMutex = SDL_TRUE;
64 static SDL_CD* theCDROM; 61 static SDL_CD* theCDROM;
65 62
66 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ 63 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
67 // Prototypes 64 // Prototypes
68 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ 65 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
69 66
70 #pragma mark -- Prototypes -- 67 #pragma mark -- Prototypes --
71 68
72 OSStatus CheckInit (); 69 static OSStatus CheckInit ();
73 70
74 OSStatus MatchAUFormats (AudioUnit theUnit, UInt32 theInputBus); 71 static OSStatus MatchAUFormats (AudioUnit theUnit, UInt32 theInputBus);
75 72
76 void FilePlayNotificationHandler (void* inRefCon, OSStatus inStatus); 73 static void FilePlayNotificationHandler (void* inRefCon, OSStatus inStatus);
77 74
78 void* RunCallBackThread (void* inRefCon); 75 static int RunCallBackThread (void* inRefCon);
79 76
80 77
81 #pragma mark -- Public Functions -- 78 #pragma mark -- Public Functions --
82 79
83 void Lock () 80 void Lock ()
84 { 81 {
85 if (initMutex) { 82 if (!apiMutex) {
86 83 apiMutex = SDL_CreateMutex();
87 pthread_mutexattr_t attr; 84 }
88 85 SDL_mutexP(apiMutex);
89 pthread_mutexattr_init (&attr);
90 pthread_mutex_init (&apiMutex, &attr);
91 pthread_mutexattr_destroy (&attr);
92
93 initMutex = SDL_FALSE;
94 }
95
96 pthread_mutex_lock (&apiMutex);
97 } 86 }
98 87
99 void Unlock () 88 void Unlock ()
100 { 89 {
101 pthread_mutex_unlock (&apiMutex); 90 SDL_mutexV(apiMutex);
102 } 91 }
103 92
104 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ 93 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
105 // DetectAudioCDVolumes 94 // DetectAudioCDVolumes
106 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ 95 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
353 NULL, 342 NULL,
354 &rootDirectory); 343 &rootDirectory);
355 344
356 if (result != noErr) { 345 if (result != noErr) {
357 SDL_SetError ("ListTrackFiles: FSGetVolumeInfo returned %d", result); 346 SDL_SetError ("ListTrackFiles: FSGetVolumeInfo returned %d", result);
358 goto bail; 347 return result;
359 } 348 }
360 349
361 result = FSOpenIterator (&rootDirectory, kFSIterateFlat, &iterator); 350 result = FSOpenIterator (&rootDirectory, kFSIterateFlat, &iterator);
362 if (result == noErr) { 351 if (result == noErr) {
363 do 352 do
400 } 389 }
401 } while(noErr == result); 390 } while(noErr == result);
402 FSCloseIterator (iterator); 391 FSCloseIterator (iterator);
403 } 392 }
404 393
405 result = 0; 394 return 0;
406 bail:
407 return result;
408 } 395 }
409 396
410 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ 397 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
411 // LoadFile 398 // LoadFile
412 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ 399 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
561 theCDROM = cdrom; 548 theCDROM = cdrom;
562 completionProc = proc; 549 completionProc = proc;
563 thePlayer->SetNotifier (FilePlayNotificationHandler, cdrom); 550 thePlayer->SetNotifier (FilePlayNotificationHandler, cdrom);
564 } 551 }
565 552
553 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
554 // GetCurrentFrame
555 //ΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡΡ
566 556
567 int GetCurrentFrame () 557 int GetCurrentFrame ()
568 { 558 {
569 int frame; 559 int frame;
570 560
577 } 567 }
578 568
579 569
580 #pragma mark -- Private Functions -- 570 #pragma mark -- Private Functions --
581 571
582 OSStatus CheckInit () 572 static OSStatus CheckInit ()
583 { 573 {
584 if (playBackWasInit) 574 if (playBackWasInit)
585 return 0; 575 return 0;
586 576
587 OSStatus result = noErr; 577 OSStatus result = noErr;
588 578
589 579 // Create the callback semaphore
590 // Create the callback mutex 580 callbackSem = SDL_CreateSemaphore(0);
591 pthread_mutexattr_t attr; 581
592 pthread_mutexattr_init (&attr);
593 pthread_mutex_init (&callbackMutex, &attr);
594 pthread_mutexattr_destroy (&attr);
595 pthread_mutex_lock (&callbackMutex);
596
597 // Start callback thread 582 // Start callback thread
598 pthread_attr_t attr1; 583 SDL_CreateThread(RunCallBackThread, NULL);
599 pthread_attr_init (&attr1);
600 pthread_create (&callbackThread, &attr1, RunCallBackThread, NULL);
601 pthread_attr_destroy (&attr1);
602 584
603 try { 585 try {
604 ComponentDescription desc; 586 ComponentDescription desc;
605 587
606 desc.componentType = kAudioUnitComponentType; 588 desc.componentType = kAudioUnitComponentType;
642 624
643 return 0; 625 return 0;
644 } 626 }
645 627
646 628
647 OSStatus MatchAUFormats (AudioUnit theUnit, UInt32 theInputBus) 629 static OSStatus MatchAUFormats (AudioUnit theUnit, UInt32 theInputBus)
648 { 630 {
649 AudioStreamBasicDescription theDesc; 631 AudioStreamBasicDescription theDesc;
650 UInt32 size = sizeof (theDesc); 632 UInt32 size = sizeof (theDesc);
651 OSStatus result = AudioUnitGetProperty (theUnit, 633 OSStatus result = AudioUnitGetProperty (theUnit,
652 kAudioUnitProperty_StreamFormat, 634 kAudioUnitProperty_StreamFormat,
664 size); 646 size);
665 647
666 return result; 648 return result;
667 } 649 }
668 650
669 void FilePlayNotificationHandler(void * inRefCon, OSStatus inStatus) 651 static void FilePlayNotificationHandler(void * inRefCon, OSStatus inStatus)
670 { 652 {
671 if (inStatus == kAudioFilePlay_FileIsFinished) { 653 if (inStatus == kAudioFilePlay_FileIsFinished) {
672 654
673 // notify non-CA thread to perform the callback 655 // notify non-CA thread to perform the callback
674 pthread_mutex_unlock (&callbackMutex); 656 SDL_SemPost(callbackSem);
675 657
676 } else if (inStatus == kAudioFilePlayErr_FilePlayUnderrun) { 658 } else if (inStatus == kAudioFilePlayErr_FilePlayUnderrun) {
677 659
678 SDL_SetError ("CDPlayer Notification: buffer underrun"); 660 SDL_SetError ("CDPlayer Notification: buffer underrun");
679 } else if (inStatus == kAudioFilePlay_PlayerIsUninitialized) { 661 } else if (inStatus == kAudioFilePlay_PlayerIsUninitialized) {
683 665
684 SDL_SetError ("CDPlayer Notification: unknown error %ld", inStatus); 666 SDL_SetError ("CDPlayer Notification: unknown error %ld", inStatus);
685 } 667 }
686 } 668 }
687 669
688 void* RunCallBackThread (void *param) 670 static int RunCallBackThread (void *param)
689 { 671 {
690 runCallBackThread = 1; 672 for (;;) {
691 673
692 while (runCallBackThread) { 674 SDL_SemWait(callbackSem);
693
694 pthread_mutex_lock (&callbackMutex);
695 675
696 if (completionProc && theCDROM) { 676 if (completionProc && theCDROM) {
697 #if DEBUG_CDROM 677 #if DEBUG_CDROM
698 printf ("callback!\n"); 678 printf ("callback!\n");
699 #endif 679 #endif
703 printf ("callback?\n"); 683 printf ("callback?\n");
704 #endif 684 #endif
705 } 685 }
706 } 686 }
707 687
708 runCallBackThread = -1;
709
710 #if DEBUG_CDROM 688 #if DEBUG_CDROM
711 printf ("thread dying now...\n"); 689 printf ("thread dying now...\n");
712 #endif 690 #endif
713 691
714 return NULL; 692 return 0;
715 } 693 }
716 694
717 }; // extern "C" 695 }; // extern "C"