redribben

updated

Oct 22nd, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void MyPropertyListenerProc(void                        *inUserData,
  2.                             AudioFileStreamID           inAudioFileStream,
  3.                             AudioFileStreamPropertyID   inPropertyID,
  4.                             UInt32                      *ioFlags) {
  5.     BMHttpAudioStreamManager *manager = (__bridge BMHttpAudioStreamManager*) inUserData;
  6.     [manager myPropertyListenerProcForStreamID:inAudioFileStream forPropertyID:inPropertyID forIOFlags:ioFlags];
  7. }
  8.  
  9. - (void) myPropertyListenerProcForStreamID:(AudioFileStreamID) inAudioFileStream
  10.                        forPropertyID:(AudioFileStreamPropertyID) inPropertyID
  11.                           forIOFlags:(UInt32 *) ioFlags {
  12.     // this is called by audio file stream when it finds property values
  13.         OSStatus err = noErr;
  14.     printf("found property '%c%c%c%c'\n", (char)(inPropertyID>>24)&255, (char)(inPropertyID>>16)&255, (char)(inPropertyID>>8)&255, (char)inPropertyID&255);
  15.    
  16.     switch (inPropertyID) {
  17.         case kAudioFileStreamProperty_ReadyToProducePackets:{
  18.             // the file stream parser is now ready to produce audio packets.
  19.             // get the stream format.
  20.             AudioStreamBasicDescription asbd;
  21.             UInt32 asbdSize = sizeof(asbd);
  22.             err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_DataFormat, &asbdSize, &asbd);
  23.             if (err) {
  24.                 PRINTERROR("get kAudioFileStreamProperty_DataFormat");
  25.                 _audioData->failed = true;
  26.                 return;
  27.             }
  28.            
  29.             // create the audio queue
  30.             err = AudioQueueNewOutput(&asbd, MyAudioQueueOutputCallback, (__bridge void *)(self), NULL, NULL, 0, &(_audioData->audioQueue));
  31.             if (err) { PRINTERROR("AudioQueueNewOutput");
  32. #warning ERROR 1718449215 is printed here: kAudioFormatUnsupportedDataFormatError
  33.                 _audioData->failed = true;
  34.                 return;
  35.             }
  36.            
  37.             // allocate audio queue buffers
  38.             for (unsigned int i = 0; i < kNumAQBufs; ++i) {
  39.                 err = AudioQueueAllocateBuffer(_audioData->audioQueue, kAQBufSize, &_audioData->audioQueueBuffer[i]);
  40.                 if (err) { PRINTERROR("AudioQueueAllocateBuffer");
  41.                     _audioData->failed = true;
  42.                     break;
  43.                 }
  44.             }
  45.  
  46.             // get the cookie size
  47.             UInt32 cookieSize;
  48.             Boolean writable;
  49.             err = AudioFileStreamGetPropertyInfo(inAudioFileStream, kAudioFileStreamProperty_MagicCookieData, &cookieSize, &writable);
  50.             if (err) {
  51.                 PRINTERROR("ERROR: info kAudioFileStreamProperty_MagicCookieData");
  52.                 NSLog(@"Error %@ occured, with code %d", [self checkstatus:err], err);
  53.                 return;
  54.             }
  55.             printf("cookieSize %d\n", (unsigned int)cookieSize);
  56.            
  57.             // get the cookie data
  58.             void* cookieData = calloc(1, cookieSize);
  59.             err = AudioFileStreamGetProperty(inAudioFileStream, kAudioFileStreamProperty_MagicCookieData, &cookieSize, cookieData);
  60.             if (err == noErr) {
  61.                 PRINTERROR("get kAudioFileStreamProperty_MagicCookieData");
  62.                 NSLog(@"%@", [self checkstatus:err]);
  63.                 free(cookieData);
  64.                 return;
  65.             }
  66.            
  67.             // set the cookie on the queue.
  68.             err = AudioQueueSetProperty(_audioData->audioQueue, kAudioQueueProperty_MagicCookie, cookieData, cookieSize);
  69.             free(cookieData);
  70.             if (err) {
  71.                 PRINTERROR("set kAudioQueueProperty_MagicCookie");
  72.                 NSLog(@"%@", [self checkstatus:err]);
  73.                 return;
  74.             }
  75.            
  76.             // listen for kAudioQueueProperty_IsRunning
  77.             err = AudioQueueAddPropertyListener(_audioData->audioQueue, kAudioQueueProperty_IsRunning, MyAudioQueueIsRunningCallback, _audioData);
  78.             if (err) { PRINTERROR("AudioQueueAddPropertyListener");
  79.                 NSLog(@"%@", [self checkstatus:err]);
  80.                 _audioData->failed = true;
  81.                 break;
  82.             }
  83.            
  84.            
  85.         }
  86.         case kAudioFileStreamProperty_DataOffset:{
  87.             //            if (inPropertyID == kAudioFileStreamProperty_DataOffset) {
  88.             SInt64 offset;
  89.             UInt32 offsetSize = sizeof(offset);
  90.            
  91.             err = AudioFileStreamGetProperty(inAudioFileStream,
  92.                                              kAudioFileStreamProperty_DataOffset,
  93.                                              &offsetSize,
  94.                                              &offset);
  95.             if (dataOffset == 0) {
  96.                 dataOffset = offset;
  97.             }
  98.             else {
  99.                 NSLog(@"offset");
  100.             }
  101.            
  102.             if (err)
  103.                 NSLog(@"get kAudioFileStreamProperty_DataOffset failed: %d", err);
  104.             break;
  105.         }
  106.         case kAudioFileStreamProperty_DataFormat:
  107.         case kAudioFileStreamProperty_FileFormat:
  108.             break;
  109.            
  110.         default:
  111.         {
  112.             /*NSLog(@"Audio stream unhandled property change: %c%c%c%c",
  113.              (propertyID>>24)&255,
  114.              (propertyID>>16)&255,
  115.              (propertyID>>8)&255,
  116.              propertyID&255);*/
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment