Back to index

libsfml  1.6+dfsg2
Functions
VoIP.cpp File Reference
#include <iomanip>
#include <iostream>
#include <cstdlib>

Go to the source code of this file.

Functions

void DoClient (unsigned short Port)
 Create a client, connect it to a running server and start sending him audio data.
void DoServer (unsigned short Port)
 Launch a server and wait for incoming audio data from a connected client.
int main ()
 Entry point of application.

Function Documentation

void DoClient ( unsigned short  Port)

Create a client, connect it to a running server and start sending him audio data.

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

Definition at line 63 of file Client.cpp.

{
    // Check that the device can capture audio
    if (sf::SoundRecorder::CanCapture() == false)
    {
        std::cout << "Sorry, audio capture is not supported by your system" << std::endl;
        return;
    }

    // Ask for server address
    sf::IPAddress ServerAddress;
    do
    {
        std::cout << "Type address or name of the server to connect to : ";
        std::cin  >> ServerAddress;
    }
    while (!ServerAddress.IsValid());

    // Create a TCP socket for communicating with server
    sf::SocketTCP Socket;

    // Connect to the specified server
    if (Socket.Connect(Port, ServerAddress) != sf::Socket::Done)
        return;
    std::cout << "Connected to server " << ServerAddress << std::endl;

    // Wait for user input...
    std::cin.ignore(10000, '\n');
    std::cout << "Press enter to start recording audio";
    std::cin.ignore(10000, '\n');

    // Create a instance of our custom recorder
    NetworkRecorder Recorder(Socket);

    // Start capturing audio data
    Recorder.Start(44100);
    std::cout << "Recording... press enter to stop";
    std::cin.ignore(10000, '\n');
    Recorder.Stop();

    // Send a "end-of-stream" packet
    sf::Packet PacketOut;
    PacketOut << EndOfStream;
    Socket.Send(PacketOut);

    // Close the socket when we're done
    Socket.Close();
}

Here is the caller graph for this function:

void DoServer ( unsigned short  Port)

Launch a server and wait for incoming audio data from a connected client.

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

Definition at line 185 of file Server.cpp.

{
    // Build an audio stream to play sound data as it is received through the network
    NetworkAudioStream AudioStream;
    AudioStream.Start(Port);

    // Loop until the sound playback is finished
    while (AudioStream.GetStatus() != sf::SoundStream::Stopped)
    {
        // Leave some CPU time for other threads
        sf::Sleep(0.1f);
    }

    std::cin.ignore(10000, '\n');

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to replay the sound..." << std::endl;
    std::cin.ignore(10000, '\n');

    // Replay the sound (just to make sure replaying the received data is OK)
    AudioStream.Play();

    // Loop until the sound playback is finished
    while (AudioStream.GetStatus() != sf::SoundStream::Stopped)
    {
        // Leave some CPU time for other threads
        sf::Sleep(0.1f);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( )

Entry point of application.

Returns:
Application exit code

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

Definition at line 24 of file VoIP.cpp.

{
    // Choose a random port for opening sockets (ports < 1024 are reserved)
    const unsigned short Port = 2435;

    // Client or server ?
    char Who;
    std::cout << "Do you want to be a server ('s') or a client ('c') ? ";
    std::cin  >> Who;

    if (Who == 's')
    {
        // Run as a server
        DoServer(Port);
    }
    else
    {
        // Run as a client
        DoClient(Port);
    }

    // Wait until the user presses 'enter' key
    std::cout << "Press enter to exit..." << std::endl;
    std::cin.ignore(10000, '\n');

    return EXIT_SUCCESS;
}

Here is the call graph for this function: