Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Use of locks in sf::SoundStream::onGetData  (Read 4517 times)

0 Members and 1 Guest are viewing this topic.

Misopeth

  • Newbie
  • *
  • Posts: 6
    • View Profile
Use of locks in sf::SoundStream::onGetData
« on: February 02, 2016, 03:27:29 pm »
Is it safe to use Locks and/or to allocate memory in sf::SoundStream::onGetData when sub-classing sf::SoundStream?
I mean, in audio callbacks this is usually not allowed as it could cause glitches in the playback, but in the related tutorial (http://www.sfml-dev.org/tutorials/2.3/audio-streams.php) locks are used, so I'm wondering if SFML is already taking care of this.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Use of locks in sf::SoundStream::onGetData
« Reply #1 on: February 02, 2016, 03:40:13 pm »
Like you said it is generally a very bad idea to allocate memory or do any kind of blocking operation in a "realtime" situation. onGetData would be one of them. Where exactly in the tutorial do you see memory being allocated or locks being used?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Use of locks in sf::SoundStream::onGetData
« Reply #2 on: February 02, 2016, 04:13:53 pm »
sf::SoundStream is triple-buffered, so you won't block playback if you spend a little more time in onGetData.
Laurent Gomila - SFML developer

Misopeth

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Use of locks in sf::SoundStream::onGetData
« Reply #3 on: February 02, 2016, 04:28:48 pm »
In the tutorial code there are neither of the two "dangers"; however a race condition might happen as m_currentSample is assigned in onGetData and also in onSeek.
The "Threading issues" section however gives mutexes as an example of protection strategy against concurrent access.
Also, the VOIP example both allocates and uses locks (https://github.com/SFML/SFML/blob/master/examples/voip/Server.cpp#L134).

Speed is not what I'm concerned about; I'm thinking about what could go wrong in acquiring a lock on a mutex, as priority inversion and so on.
I also looked at the code in sf::SoundStream; seems like locks are used in the audio thread.

I'm just pointing out some concerns I have since I am really new to audio programming; I read about using lock-free code in audio programming but I don't know if a careful design could allow the use of locks, as SFML do.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Use of locks in sf::SoundStream::onGetData
« Reply #4 on: February 02, 2016, 08:19:58 pm »
There's nothing wrong with locks, really. Don't overthink this stuff.
Laurent Gomila - SFML developer

Misopeth

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Use of locks in sf::SoundStream::onGetData
« Reply #5 on: February 02, 2016, 09:06:37 pm »
Ok, I'm glad to ear that as avoiding locks really complicates things.

Thank you for your replies (and for the library too, of course).

mynameisjohnj

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Use of locks in sf::SoundStream::onGetData
« Reply #6 on: March 08, 2016, 04:24:08 pm »
Hi All,

I hate to resurrect this thread, but I have kind of a relevant question and feel like there are enough soundstream questions high up at the moment (though I'll make a thread if noone responds).

It's good to know that the sf::SoundStream's onGetData function is passing you the third of three buffers used for streaming, as I wasn't really sure if this was the case. Could this be added to the docs?

Also, would you say the onGetData function is a reasonable place to do some mixing / processing? Nothing too ridiculous, but... well let's just put some numbers to it.

If I push say 2 seconds worth of audio in each onGetData call, that means that while 2 seconds play there are two seconds ready to go and 2 seconds I'll be asked for. Does that mean I've got the time it takes to play those two seconds to process and push new audio?

Sorry if that's difficult to read... let me know if I can be more clear / if I should make a new thread. And by the way, thanks to all the developers for working on SFML. It's people like you and libraries like this that make life worth living.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Use of locks in sf::SoundStream::onGetData
« Reply #7 on: March 08, 2016, 04:30:42 pm »
Quote
Could this be added to the docs?
I don't like to bother people with too many implementation details, but yes, I see that this one may have an impact on user code so it may be worth mentioning it.

Quote
would you say the onGetData function is a reasonable place to do some mixing / processing?
Sure it is.

Quote
Does that mean I've got the time it takes to play those two seconds to process and push new audio?
Indeed.
Laurent Gomila - SFML developer

mynameisjohnj

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Use of locks in sf::SoundStream::onGetData
« Reply #8 on: March 08, 2016, 06:12:32 pm »
Merci!