[1.4] startiPhoneAudioStream, when used after a movie ends, crashes the engine. -NOT RESOLVED
by Dave Calabrese · in iTorque 2D · 02/14/2011 (10:19 am) · 8 replies
I've discovered that in the v1.4 build of iTorque2D, attempting to play an audio file using startiPhoneAudioStream, will cause an engine crash.
Here are the facts...
1.) Call a movie file to play
2.) Wait for it to end. Go do stuff. Take some time, doesn't matter how long or how short. Now try to play some music.
-- CRASH --
You will see the following log in your console:
Tracing through the code, we go into SoundEngine.mm, to line 740, with the following code:
Within that code, the offending line is this:
The 'result' returned by that is -38. If you do not play a movie beforehand, this will return as 0. -38 translates to "kAudioFileNotOpenError". AudioFileGetProperty is part of the iPhone APIs, so for whatever reason we can't step into it using XCode to see exactly what is going on.
But what we know is this...
- Playing music without playing a movie works fine.
- Playing music after playing a movie causes the APIs to refuse to load the file, however silently fails to load the file, informs the system that it isn't open, which will later on cause a crash.
The EXACT crash is actually caused by the fact that maxPacketSize is returned as 0. At a later point in the code, maxPacketSize is divded by another number. Since maxPacketSize is 0, the universe implodes. Loading a music file without playing a movie will properly load the audio file, properly setting maxPacketSize to a valid number, and hence preventing the destruction of the universe through a gaping black-hole.
And that's where I am on this one so far. A pretty nasty bug, and I'm kinda stuck on it.
Here are the facts...
1.) Call a movie file to play
playiPhoneMovie("game/data/video/video", 0, 0);2.) Wait for it to end. Go do stuff. Take some time, doesn't matter how long or how short. Now try to play some music.
startiPhoneAudioStream("~/data/audio/music.mp3",true);-- CRASH --
You will see the following log in your console:
Error opening file: -43 Error opening file: -43 Error getting file format: -38 Error getting file data size: -38 Error getting file data info: -38 Error creating queue: 1718449215 Error adding isRunning property listener to queue: -50 Error setting up queue: -50
Tracing through the code, we go into SoundEngine.mm, to line 740, with the following code:
OSStatus SetupBuffers(BG_FileInfo *inFileInfo)
{
int numBuffersToQueue = kNumberBuffers;
UInt32 maxPacketSize;
UInt32 size = sizeof(maxPacketSize);
// we need to calculate how many packets we read at a time, and how big a buffer we need
// we base this on the size of the packets in the file and an approximate duration for each buffer
// first check to see what the max size of a packet is - if it is bigger
// than our allocation default size, that needs to become larger
OSStatus result = AudioFileGetProperty(inFileInfo->mAFID, kAudioFilePropertyPacketSizeUpperBound, &size, &maxPacketSize);
AssertNoError("Error getting packet upper bound size", end);
bool isFormatVBR = (inFileInfo->mFileFormat.mBytesPerPacket == 0 || inFileInfo->mFileFormat.mFramesPerPacket == 0);
CalculateBytesForTime(inFileInfo->mFileFormat, maxPacketSize, 0.5/*seconds*/, &mBufferByteSize, &mNumPacketsToRead);
// if the file is smaller than the capacity of all the buffer queues, always load it at once
if ((mBufferByteSize * numBuffersToQueue) > inFileInfo->mFileDataSize)
inFileInfo->mLoadAtOnce = true;
if (inFileInfo->mLoadAtOnce)
{
UInt64 theFileNumPackets;
size = sizeof(UInt64);
result = AudioFileGetProperty(inFileInfo->mAFID, kAudioFilePropertyAudioDataPacketCount, &size, &theFileNumPackets);
AssertNoError("Error getting packet count for file", end);
mNumPacketsToRead = (UInt32)theFileNumPackets;
mBufferByteSize = inFileInfo->mFileDataSize;
numBuffersToQueue = 1;
}
else
{
mNumPacketsToRead = mBufferByteSize / maxPacketSize;
}
if (isFormatVBR)
mPacketDescs = new AudioStreamPacketDescription [mNumPacketsToRead];
else
mPacketDescs = NULL; // we don't provide packet descriptions for constant bit rate formats (like linear PCM)
// allocate the queue's buffers
for (int i = 0; i < numBuffersToQueue; ++i)
{
result = AudioQueueAllocateBuffer(mQueue, mBufferByteSize, &mBuffers[i]);
AssertNoError("Error allocating buffer for queue", end);
QueueCallback (this, mQueue, mBuffers[i]);
if (inFileInfo->mLoadAtOnce)
inFileInfo->mFileDataInQueue = true;
}
end:
return result;
}Within that code, the offending line is this:
OSStatus result = AudioFileGetProperty(inFileInfo->mAFID, kAudioFilePropertyPacketSizeUpperBound, &size, &maxPacketSize);
The 'result' returned by that is -38. If you do not play a movie beforehand, this will return as 0. -38 translates to "kAudioFileNotOpenError". AudioFileGetProperty is part of the iPhone APIs, so for whatever reason we can't step into it using XCode to see exactly what is going on.
But what we know is this...
- Playing music without playing a movie works fine.
- Playing music after playing a movie causes the APIs to refuse to load the file, however silently fails to load the file, informs the system that it isn't open, which will later on cause a crash.
The EXACT crash is actually caused by the fact that maxPacketSize is returned as 0. At a later point in the code, maxPacketSize is divded by another number. Since maxPacketSize is 0, the universe implodes. Loading a music file without playing a movie will properly load the audio file, properly setting maxPacketSize to a valid number, and hence preventing the destruction of the universe through a gaping black-hole.
And that's where I am on this one so far. A pretty nasty bug, and I'm kinda stuck on it.
About the author
#2
Thanks, Scott / Mich!
02/14/2011 (12:17 pm)
Awesome! If that does work, then I'll just diffmerge changes from 1.4.1 back to our codebase to make sure this is working for our client.Thanks, Scott / Mich!
#3
The fix is in the current 1.4.1 Preview build if you don't want to wait for the Final to get it.
02/14/2011 (1:12 pm)
Yep, looks like this is fixed in 1.4.1. Marking this one as resolved.The fix is in the current 1.4.1 Preview build if you don't want to wait for the Final to get it.
#4
I just finished diffmerging the changes into our codebase, and I'm afraid it hasn't fixed the problem. The exact same problem still occurs. I'm going to have to go back to our 'hack fix' (of using 11mb WAV files vs 500k MP3 files) for the time being.
Mich - Feel free to ping me over IM if you want to work through this with me. I'm pretty sure I merged things in properly (I did a full diffmerge between 1.4.1 -> 1.4), though to be honest I'm not exactly sure where in there the fix was.
02/14/2011 (6:06 pm)
Hey guys,I just finished diffmerging the changes into our codebase, and I'm afraid it hasn't fixed the problem. The exact same problem still occurs. I'm going to have to go back to our 'hack fix' (of using 11mb WAV files vs 500k MP3 files) for the time being.
Mich - Feel free to ping me over IM if you want to work through this with me. I'm pretty sure I merged things in properly (I did a full diffmerge between 1.4.1 -> 1.4), though to be honest I'm not exactly sure where in there the fix was.
#5
02/14/2011 (6:06 pm)
Also set the title of this to 'NOT RESOLVED' until I have this working as expected, to be sure we haven't missed anything.
#6
02/14/2011 (7:53 pm)
So we actually get to a divide by 0 (oh sh-!)? That's definitely not good. I'll ping you tomorrow in chat to work through this Dave. I just fixed some bugs and extended the stability of the code in iPhoneMoviePlayback.
#7
1. Did just merge the source code and not scripts?
2. Are you working with a new project or an old one?
3. What are your XCode build settings?
02/15/2011 (7:16 am)
@Dave - Some questions:1. Did just merge the source code and not scripts?
2. Are you working with a new project or an old one?
3. What are your XCode build settings?
#8
2.) Project originated as a v1.4 project.
3.) Default settings that shipped with iTorque2D.
02/15/2011 (8:34 am)
1.) Merged everything across the board.2.) Project originated as a v1.4 project.
3.) Default settings that shipped with iTorque2D.
Associate Scott Burns
GG Alumni