Back to index

libsfml  1.6+dfsg2
Public Member Functions | Private Member Functions | Private Attributes
NetworkAudioStream Class Reference

Customized sound stream for acquiring audio data from the network /////////////////////////////////////////////////////////. More...

Collaboration diagram for NetworkAudioStream:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 NetworkAudioStream ()
 Default constructor.
 ~NetworkAudioStream ()
 Destructor.
void Start (unsigned short Port)
 Run the server, stream audio data from the client.

Private Member Functions

virtual bool OnStart ()
 /see SoundStream::OnStart
virtual bool OnGetData (sf::SoundStream::Chunk &Data)
 /see SoundStream::OnGetData
void ReceiveLoop ()
 Get audio data from the client until playback is stopped.

Private Attributes

sf::SocketTCP myListener
sf::SocketTCP myClient
sf::Mutex myMutex
std::vector< sf::Int16 > mySamples
std::vector< sf::Int16 > myTempBuffer
std::size_t myOffset
bool myHasFinished

Detailed Description

Customized sound stream for acquiring audio data from the network /////////////////////////////////////////////////////////.

Definition at line 19 of file Server.cpp.


Constructor & Destructor Documentation

Default constructor.

/////////////////////////////////////////////////////////

Definition at line 27 of file Server.cpp.

                         :
    myOffset     (0),
    myHasFinished(false)
    {
        // Set the sound parameters
        Initialize(1, 44100);
    }

Here is the call graph for this function:

Destructor.

/////////////////////////////////////////////////////////

Definition at line 39 of file Server.cpp.

    {
        // Close the sockets
        myClient.Close();
        myListener.Close();
    }

Member Function Documentation

virtual bool NetworkAudioStream::OnGetData ( sf::SoundStream::Chunk &  Data) [inline, private, virtual]

/see SoundStream::OnGetData

/////////////////////////////////////////////////////////

Definition at line 95 of file Server.cpp.

    {
        // We have reached the end of the buffer and all audio data have been played : we can stop playback
        if ((myOffset == mySamples.size()) && myHasFinished)
            return false;

        // No new data has arrived since last update : wait until we get some
        while ((myOffset == mySamples.size()) && !myHasFinished)
            sf::Sleep(0.01f);

        // Copy samples into a local buffer to avoid synchronization problems
        // (don't forget that we run in two separate threads)
        {
            sf::Lock Lock(myMutex);
            myTempBuffer.assign(mySamples.begin() + myOffset, mySamples.end());
        }

        // Fill audio data to pass to the stream
        Data.Samples   = &myTempBuffer[0];
        Data.NbSamples = myTempBuffer.size();

        // Update the playing offset
        myOffset += myTempBuffer.size();

        return true;
    }

Here is the call graph for this function:

virtual bool NetworkAudioStream::OnStart ( ) [inline, private, virtual]

/see SoundStream::OnStart

/////////////////////////////////////////////////////////

Definition at line 83 of file Server.cpp.

    {
        // Reset the playing offset
        myOffset = 0;

        return true;
    }
void NetworkAudioStream::ReceiveLoop ( ) [inline, private]

Get audio data from the client until playback is stopped.

/////////////////////////////////////////////////////////

Definition at line 126 of file Server.cpp.

    {
        while (!myHasFinished)
        {
            // Get waiting audio data from the network
            sf::Packet PacketIn;
            if (myClient.Receive(PacketIn) != sf::Socket::Done)
                break;

            // Extract the message ID
            sf::Uint8 Id;
            PacketIn >> Id;

            if (Id == AudioData)
            {
                // Extract audio samples from the packet, and append it to our samples buffer
                const sf::Int16* Samples   = reinterpret_cast<const sf::Int16*>(PacketIn.GetData() + 1);
                std::size_t      NbSamples = (PacketIn.GetDataSize() - 1) / sizeof(sf::Int16);

                // Don't forget that the other thread can access the samples array at any time
                // (so we protect any operation on it with the mutex)
                {
                    sf::Lock Lock(myMutex);
                    std::copy(Samples, Samples + NbSamples, std::back_inserter(mySamples));
                }
            }
            else if (Id == EndOfStream)
            {
                // End of stream reached : we stop receiving audio data
                std::cout << "Audio data has been 100% received !" << std::endl;
                myHasFinished = true;
            }
            else
            {
                // Something's wrong...
                std::cout << "Invalid packet received..." << std::endl;
                myHasFinished = true;
            }
        }
    }

Here is the caller graph for this function:

void NetworkAudioStream::Start ( unsigned short  Port) [inline]

Run the server, stream audio data from the client.

/////////////////////////////////////////////////////////

Definition at line 50 of file Server.cpp.

    {
        if (!myHasFinished)
        {
            // Listen to the given port for incoming connections
            if (!myListener.Listen(Port))
                return;
            std::cout << "Server is listening to port " << Port << ", waiting for connections... " << std::endl;

            // Wait for a connection
            sf::IPAddress ClientAddress;
            myListener.Accept(myClient, &ClientAddress);
            std::cout << "Client connected : " << ClientAddress << std::endl;

            // Start playback
            Play();

            // Start receiving audio data
            ReceiveLoop();
        }
        else
        {
            // Start playback
            Play();
        }
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

sf::SocketTCP NetworkAudioStream::myClient [private]

Definition at line 171 of file Server.cpp.

Definition at line 176 of file Server.cpp.

sf::SocketTCP NetworkAudioStream::myListener [private]

Definition at line 170 of file Server.cpp.

sf::Mutex NetworkAudioStream::myMutex [private]

Definition at line 172 of file Server.cpp.

std::size_t NetworkAudioStream::myOffset [private]

Definition at line 175 of file Server.cpp.

std::vector<sf::Int16> NetworkAudioStream::mySamples [private]

Definition at line 173 of file Server.cpp.

std::vector<sf::Int16> NetworkAudioStream::myTempBuffer [private]

Definition at line 174 of file Server.cpp.


The documentation for this class was generated from the following file: