Audio Queues

Moderator
Posts: 623
Joined: 2007.09
Post: #16
* FlamingHairball begs TPX to stick around Rasp

- Lincoln Green
http://www.binkworks.com/
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #17
Posting an update:

I fixed two bugs in the code. The first one was a memory leak where I forgot to free the packet descriptions in the close method (oops), and the second one is that the callback must return if the track is already closed so that it doesn't accidentally reference a non-existent queue. I don't recall reading specifically about the callback issue in the documentation [update: yeah it's in there but no explanation accompanies], but the Apple example code does the same thing. I actually had one crash because of this in the last few weeks, so hopefully the early return in the callback fixes it.

I've been beating on this with the Leaks and ObjectAlloc tools in Instruments (I don't think Instruments is under NDA is it?). Anyway, it *appears* that there may be some memory leaks created internally by Audio Queues. In the worst case that I've seen (in the Leaks tool), I might be leaking upwards of 200k per hour, which is certainly no show-stopper, but sucks all the same. The issues appear to start happening only with some files, but not with others, and not if it's played as the first track after launch. I can't quite fully comprehend or explain when it's happening or not, sorry. I have not even the foggiest clue how to fix it because I've been unable to find anything wrong with my implementation. That doesn't mean it's correct though. I figure it's worth mentioning this so that anyone else who uses it is warned to keep an eye on memory leaks.

Another thing I've noticed is that it appears that Audio Queues may not be releasing resources internally on the Mac, as it should. While these unreleased resources do not show up as memory leaks, they certainly are as far as I'm concerned. The application I've been testing with, does grow by a few megabytes over time, and that is a bummer, but it doesn't *appear* to be my fault.

I'd file a bug or two on these memory leak issues but I'm not entirely sure I understand exactly what I'm seeing in instruments. If someone else has more information on this situation I'd be happy to hear from you. Smile
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #18
After more testing, I am 60% convinced that I'm doing something wrong with Audio Queues, which is causing the (relatively minor) memory leaks. I don't want *any* if I can fix it, so for those who are using this, please stay tuned. I am at the point now where I'll either get to the bottom of it, or email Apple about it, or file a bug. I have a few leads I'm working on right now. Again, it's not an outrageous problem (although it could be, depending on your memory leak sensitivity), but I *am* working on it and will reach one of those three outcomes within the next day or two (my assumption). The project I'm working on right now requires that the memory leaks are essentially none, although I don't know how much will be acceptable, so this is important to me to resolve ASAP.
Quote this message in a reply
Sage
Posts: 1,199
Joined: 2004.10
Post: #19
I don't know if Apple has a dedicated mailing list for whichever audio API you're using ( CoreAudio, I assume ) but if they do, I bet you can get help there.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #20
That's a good suggestion. I checked the Core Audio archives, but no dice. If I can come up with a good question to ask there I will. Right now, I'm comparing with Apple's example code. Some of their code leaks too, but some of it doesn't. I'm obviously pretty interested in the stuff that doesn't. Unfortunately, their sample which doesn't leak, doesn't have all the needed features -- tricky, tricky. Wink

Last I remember before I shut the day down yesterday was that I'm somehow spawning threads through Audio Queues and they aren't spawning any threads at all. That's the first thing I want to figure out. I don't mind threaded programming, but it's a little more complicated when dealing with mystery synchronization schemes going on behind the scenes. I didn't see anything in the documentation explaining this. Hopefully in the next few hours I'll get a breakthrough.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #21
Testing Apple's sample code more, it appears that the sample I thought wasn't leaking is leaking after all, and it's leaking the same stuff. Looks like bug report time...

I'm wondering if it is possible to get around leaks showing up by hanging on to queues for the life of the program. While that certainly isn't ideal, it might suffice to satisfy leak sensitivity.

On another note: I think I might try changing the way GBMusicTrack handles track endings, although I don't know that it will offer anything better than the way I'm already doing it. Apple's sample code looks to show a smoother way of doing it, but unfortunately, their code doesn't seem to take into account that AppKit is not thread-safe, so I'll have to contend with that along the way. We'll see what happens.

For now, need to do more code fiddling and preparing a bug report.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #22
The Audio Queue memory leaks bug described here has been filed as Bug ID# 6117171 in case someone with RADAR access happens to stumble upon this.
Quote this message in a reply
Member
Posts: 50
Joined: 2008.06
Post: #23
Hey, AnotherJake, thanks for the AudioQueue code to look at... I am giving it a try and am using an mp3, as in your code example, and it gets to the line: AudioQueueNewOutput(&dataFormat, BufferCallback, self, nil, nil, 0, &queue); and then seems to get lost in there forever. Is this something you've fixed or am I just stupid and not using it right?

Edit: I tried with a WAV and it works great.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #24
Hmm... It should work with the mp3 just fine. I wonder what's up with that? I'll have to look into it when I get a chance. Is it doing it on the first song or is it after you've played one already?
Quote this message in a reply
Member
Posts: 50
Joined: 2008.06
Post: #25
AnotherJake Wrote:Hmm... It should work with the mp3 just fine. I wonder what's up with that? I'll have to look into it when I get a chance. Is it doing it on the first song or is it after you've played one already?

I've tried a couple mp3's. Does it to all of them. I've only ever tried it on the first sound play ever. I switched to wavs and the problem went away. I can try playing a wav and then an mp3 if you like.

Edit: Nope. Wav and then mp3 still sticks forever on that line.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #26
That definitely should have worked. Looks like the forum contact info is still off Annoyed If you don't mind, could you email your OS version and machine info to me at anotherjake +AT+ mac +DOT+ com ?
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #27
After some email discussion with bruss14 I should remind everyone using it that GBMusicTrack was designed to be used only for background music, and only one track at a time. I should have made that clearer, my fault. Don't try to use it for sound effects like laser beams and explosions and stuff -- use something else like OpenAL for that. Also, if you're looping a background music track it is preferable to set the track to loop rather than release it and initialize a new one every time.

I'm going to be spending some more time on it in the next day or two and hopefully have an update which might improve it.

If anyone has any bugs/comments/questions/suggestions/improvements/etc. please feel free to email me at anotherjake +AT+ mac +DOT+ com.

Also, still no word from Apple on the memory leaks bug report I filed.
Quote this message in a reply
Member
Posts: 50
Joined: 2008.06
Post: #28
AnotherJake Wrote:After some email discussion with bruss14 I should remind everyone using it that GBMusicTrack was designed to be used only for background music, and only one track at a time. I should have made that clearer, my fault. Don't try to use it for sound effects like laser beams and explosions and stuff -- use something else like OpenAL for that. Also, if you're looping a background music track it is preferable to set the track to loop rather than release it and initialize a new one every time.

I'm going to be spending some more time on it in the next day or two and hopefully have an update which might improve it.

If anyone has any bugs/comments/questions/suggestions/improvements/etc. please feel free to email me at anotherjake +AT+ mac +DOT+ com.

Also, still no word from Apple on the memory leaks bug report I filed.

I'd like to add, that has nothing to do with the problem I mentioned in this forum. Just the discussion Jake and I had in email. Another user and I are having problems with m4a and mp3 getting stuck forever in loading. wavs work great though.

Edit: I should also probably add that m4a and mp3's only get stuck in loading forever in a special case. They work great for all other uses so far.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #29
I'm starting the debug/update right now, so hopefully I can get this tracked down soon (tonight/tomorrow?). I fear there may be another bug report to file because I recall having had intermittent crashing problems like this using Apple's sample code too. We'll see how it goes...

bruss14, I may be sending you a known working project later.
Quote this message in a reply
Moderator
Posts: 3,571
Joined: 2003.06
Post: #30
I posted an updated version which I believe handles track endings in a smoother way for Audio Queues. It also makes sure you only have one track playing at a time by default, although you could easily defeat that. It's not really much different than the previous version, but now when the track ends, it's over with and can't be reused, and must be disposed of after the track finished notification gets called. It is not clear to me why (or if) Audio Queues seems to prefer it this way, but it now more closely mimics Apple's sample code, just to be safe.

It's still fine to set the track to repeat, if you want to loop it. Seems like some compressed files might experience a small audio gap in between loops.

Note that this does not fix the issue bruss14 is having. It is suspected at this time that it is a bug in audio toolbox related to a specific machine configuration, but we haven't been able to confirm that yet. I have been completely unable to reproduce the issue on my setup.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Core Audio mutlichannel audio tachyon 2 6,126 Mar 18, 2005 01:04 AM
Last Post: tachyon