Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio streaming hangs when OnGetData() is slow #180

Closed
bastienleonard opened this issue Feb 24, 2012 · 3 comments
Closed

Audio streaming hangs when OnGetData() is slow #180

bastienleonard opened this issue Feb 24, 2012 · 3 comments

Comments

@bastienleonard
Copy link

Example:

#include <iostream>

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>


class CustomStream : public sf::SoundStream
{
public:
    CustomStream(const std::string& path)
    {
        sound_buffer.LoadFromFile(path);
        unsigned int channel_count = sound_buffer.GetChannelCount();
        unsigned int sample_rate = sound_buffer.GetSampleRate();
        buffer = sound_buffer.GetSamples();
        Initialize(channel_count, sample_rate);
    }

    virtual bool OnGetData(Chunk& chunk)
    {
        const int BUFFER_SIZE = 4096;
        chunk.Samples = buffer;
        chunk.SampleCount = BUFFER_SIZE;
        buffer += BUFFER_SIZE;
        sf::Sleep(sf::Seconds(5));

        if (buffer > sound_buffer.GetSamples() + sound_buffer.GetSampleCount())
        {
            chunk.SampleCount = sound_buffer.GetSampleCount() % BUFFER_SIZE;
            return false;
        }

        return true;
    }

    virtual void OnSeek(sf::Time time_offset)
    {
        std::cout << "Time offset: " << time_offset.AsMilliseconds() << '\n';
    }

private:
    sf::SoundBuffer sound_buffer;
    const sf::Int16 *buffer;
};


int main()
{
    sf::RenderWindow app(sf::VideoMode(800, 600, 32), "Title");
    app.SetFramerateLimit(60);
    sf::Event event;
    CustomStream stream("music.ogg");
    stream.Play();
    stream.SetPlayingOffset(sf::Milliseconds(0));

    while (app.IsOpen())
    {
        while (app.PollEvent(event))
        {
            switch (event.Type)
            {
            case sf::Event::Closed:
                app.Close();
                break;
            default:
                break;
            }
        }

        app.Clear(sf::Color::White);
        app.Display();
    }

    return 0;
}

Note that the program doesn't hang if stream.SetPlayingOffset(sf::Milliseconds(0)) is removed.
My guess is that when SetPlayingOffset() stops the stream, the call to Thread.Wait() never returns.
However it should return at some point; it's just slow in this case because of sf::Sleep(sf::Seconds(5)).

Platform: GNU/Linux, SFML commit #e7256e3324a196bb1432786c678f195c9ffb982b.

For the record, I encountered this bug while adding streaming to my Python binding.
Currently the API is very slow, and I was wondering why my program hangs when testing seeking with SetPlayingOffset().

@LaurentGomila
Copy link
Member

So the program blocks forever right after stream.SetPlayingOffset(sf::Milliseconds(0))?

I can't reproduce it on Windows, the program behaves the same with or without this line. I had to add a std::cout in OnGetData, because with pauses of 5 seconds every 4096 samples, we can hardly hear something and check whether the program runs or not.

@bastienleonard
Copy link
Author

Well this is embarassing, I can't reproduce it either. I guess I was too happy to find out why my binding hangs for no reason. :D I often get a segmentation fault when closing the program, however.

@LaurentGomila
Copy link
Member

I often get a segmentation fault when closing the program, however

This is probably bug #30.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants