0

I've been trying to serialize a local audio file following the instructions here, only instead of feeding the packets directly to the AudioQueBuffer, I would like send it over bluetooth/wifi using GKSession. I'm getting an EXC_BAD_ACCESS error:

- (void)broadcastServerMusicWithSession:(GKSession *)session playerName:(NSString *)name clients:    (NSArray *)clients
{
    self.isServer = YES;

    _session = session;
    _session.available = NO;
    _session.delegate = self;
    [_session setDataReceiveHandler:self withContext:nil];

    CFURLRef     fileURL = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:@"mozart"                                                                       withExtension:@"mp3"]; // file URL    
    AudioFile *audioFile = [[AudioFile alloc] initWithURL:fileURL];    

    //no we start sending the data over    
    #define MAX_PACKET_COUNT    4096
    SInt64 currentPacketNumber = 0;
    UInt32 numBytes;
    UInt32 numPacketsToRead;
    AudioStreamPacketDescription    packetDescriptions[MAX_PACKET_COUNT];    

    NSInteger bufferByteSize = 4096;
    if (bufferByteSize < audioFile.maxPacketSize) bufferByteSize = audioFile.maxPacketSize; 
    numPacketsToRead = bufferByteSize/audioFile.maxPacketSize;
    UInt32 numPackets = numPacketsToRead;

    BOOL isVBR = ([audioFile audioFormatRef]->mBytesPerPacket == 0) ? YES : NO;

// this should be in a do while (numPackets < numPacketsToRead) loop.. 
// but i took it out just to narrow down the problem

    NSMutableData *myBuffer = [[NSMutableData alloc] initWithCapacity:numPackets * audioFile.maxPacketSize];

    AudioFileReadPackets (
                          audioFile.fileID,
                          NO,
                          &numBytes,
                          isVBR ? packetDescriptions : 0,
                          currentPacketNumber,
                          &numPackets,
                          &myBuffer
                          );

    NSError *error;        
    if (![session sendDataToAllPeers:packetData withDataMode:GKSendDataReliable error:&error]) 
    {
        NSLog(@"Error sending data to clients: %@", error);
    }

I know for a fact that it's the AudioFileReadPackets call that's breaking the code, since it runs fine if I comment that bit out

I've tried doing Zombies profiling using xcode 4.3, but it just crashes.. one useful debugging tip I got though was breaking when the code crashes and outputting the error code using gdb console.. this is what I get:

(lldb) po [$eax className] NSException
error: warning: receiver type 'unsigned int' is not 'id' or interface pointer, consider casting it to 'id'
warning: declaration does not declare anything
error: expected ';' after expression
error: 1 errors parsing expression

but I couldn't do much with it.. any help?

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246

1 Answers1

0

Turns out there are two problems with the above code:

  1. [audioFile audioFormatRef]->mBytesPerPacket == 0 the compiler isn't happy with this one.. if you refer to the code that defines audioFormatRef, it returns a pointer to a format struct, which has a mBytesPerPacket field.. I donno why the above syntax isn't working though

  2. I shouldn't be supplying myBuffer to the AudioFileReadPackets function (type mistmach).. rather I should do something like this:

    NSMutableData *myBuffer = [[NSMutableData alloc] initWithCapacity:numPackets * audioFile.maxPacketSize];
    const void *buffer = [myBuffer bytes];
    AudioFileReadPackets (
                          audioFile.fileID,
                          NO,
                          &numBytes,
                          isVBR ? packetDescriptions : 0,
                          currentPacketNumber,
                          &numPackets,
                          buffer
                          );
    

this works too

void *buffer = [myBuffer mutablebytes];

read this post to explain the difference between those two options

Community
  • 1
  • 1
abbood
  • 23,101
  • 16
  • 132
  • 246