Moved all bindings to the "bindings" sub-directory

Renamed the CSFML directory to c
Renamed the DSFML directory to d
--> bindings must now be updated to match the new organization!

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1630 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2010-11-09 17:13:17 +00:00
parent 0cc5563cac
commit 0e2297af28
417 changed files with 0 additions and 0 deletions

View file

@ -0,0 +1,72 @@
module client;
import util;
// Specialization of audio recorder for sending recorded audio
// data through the network
class NetworkRecorder : SoundRecorder
{
public:
// Constructor
this(SocketTCP Socket)
{
mySocket = Socket;
}
~this()
{
delete mySocket;
}
protected:
override bool onStart()
{
return true;
}
override void onStop()
{
}
override bool onProcessSamples(short[] samples)
{
// Pack the audio samples into a network packet
Packet PacketOut = new Packet();
PacketOut.set(AudioData);
PacketOut.append((cast(byte*)samples.ptr)[0..samples.length * short.sizeof]);
// Send the audio packet to the server
return mySocket.send(PacketOut) == SocketStatus.DONE;
}
SocketTCP mySocket; ///< Socket used to communicate with the server
}
void runClient(IPAddress adr, int port)
{
// Create a TCP socket for communicating with server
SocketTCP Socket = new SocketTCP();
// Connect to the specified server
if (!Socket.connect(port, adr))
return;
// Wait for user input...
Cout("Press enter to start recording audio").newline;
Cin.get();
// Create a instance of our custom recorder
NetworkRecorder Recorder = new NetworkRecorder(Socket);
// Start capturing audio data
Recorder.start(44100);
Cout("Press enter to stop recording audio").newline;
Cin.get();
Recorder.stop();
// Send a "end-of-stream" packet
Packet PacketOut = new Packet();
PacketOut.set(EndOfStream);
Socket.send(PacketOut);
}

View file

@ -0,0 +1,46 @@
module entry;
import util;
import server;
import client;
int main(char[][] args)
{
char[][] argc = args.dup;
if (argc.length > 1)
{
if ( argc[1] == "-c" &&
argc.length == 4)
{
IPAddress adr = IPAddress(argc[2]);
if (adr.isValid() &&
parse(argc[3]) <= 60000 &&
parse(argc[3]) >= 1000)
{
runClient(adr, parse(argc[3]));
}
else
printUsage();
}
else if ( argc[1] == "-s" &&
argc.length == 3 &&
parse(argc[2]) <= 60000 &&
parse(argc[2]) >= 1000)
{
runServer(parse(argc[2]));
}
else
printUsage();
}
else
printUsage();
return 0;
}
void printUsage()
{
Cout("Usage :\n voip.exe [-c [ip address] | -s] [port] \n -c = run as client\n -s = run as server\n ip address = address of the server\n port = port between 1000 and 65000\n").newline;
}

View file

@ -0,0 +1,162 @@
module server;
import util;
class NetworkAudioStream : SoundStream
{
public:
static this()
{
s_sync = new Object();
}
// Default constructor
this()
{
myListener = new SocketTCP();
myClient = new SocketTCP();
// Set the sound parameters
super(1, 44100);
}
// Destructor
~this()
{
// Close the sockets
delete myClient;
delete myListener;
}
// Run the server, stream audio data from the client
void start(int Port)
{
if (!myHasFinished)
{
// Listen to the given port for incoming connections
if (!myListener.listen(Port))
return;
Cout("Listening").newline;
myListener.accept(myClient);
Cout("New Client").newline;
// Start playback
play();
// Start receiving audio data
receiveLoop();
}
else
{
// Start playback
play();
}
}
protected:
override bool onStart()
{
// Reset the playing offset
myOffset = 0;
return true;
}
override bool onGetData(out short[] data)
{
// We have reached the end of the buffer and all audio data have been played : we can stop playback
if ((myOffset == mySamples.length) && myHasFinished)
return false;
// No new data has arrived since last update : wait until we get some
while (myOffset == mySamples.length && !myHasFinished)
sleep(0.01f);
synchronized(s_sync)
{
myTempBuffer = mySamples[myOffset..mySamples.length];
// Update the playing offset
myOffset += myTempBuffer.length;
}
data = myTempBuffer;
return true;
}
private:
void receiveLoop()
{
while (!myHasFinished)
{
// Get waiting audio data from the network
Packet PacketIn = new Packet();
if (myClient.receive(PacketIn) != SocketStatus.DONE)
break;
// Extract the message ID
ubyte Id;
PacketIn.get(Id);
if (Id == AudioData)
{
// Extract audio samples from the packet, and append it to our samples buffer
synchronized(s_sync)
{
byte* temp = PacketIn.getData().ptr;
temp++;
mySamples ~= (cast(short*)temp)[0..(PacketIn.getDataSize - byte.sizeof ) / short.sizeof];
}
}
else if (Id == EndOfStream)
{
// End of stream reached : we stop receiving audio data
myHasFinished = true;
}
else
{
// Something's wrong...
myHasFinished = true;
}
}
}
SocketTCP myListener;
SocketTCP myClient;
short[] mySamples;
short[] myTempBuffer;
size_t myOffset;
bool myHasFinished;
static Object s_sync;
};
// Launch a server and wait for incoming audio data from
// a connected client
void runServer(int Port)
{
// Build an audio stream to play sound data as it is received through the network
NetworkAudioStream audioStream = new NetworkAudioStream;
audioStream.start(Port);
// Loop until the sound playback is finished
while (audioStream.getStatus() != SoundStatus.STOPPED)
{
// Leave some CPU time for other threads
sleep(0.1f);
}
Cout("Enter to replay").newline;
Cin.get();
// 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() != SoundStatus.STOPPED)
{
// Leave some CPU time for other threads
sleep(0.1f);
}
}

View file

@ -0,0 +1,49 @@
module util;
const ubyte AudioData = 1;
const ubyte EndOfStream = 2;
public import dsfml.system.all;
public import dsfml.audio.all;
public import dsfml.network.all;
version(Tango)
{
public import tango.io.Console;
public import tango.text.convert.Integer;
}
else
{
public import std.stdio;
//simple abstraction of Cout & Cin for phobos
class Cout
{
static Cout s_c;
static this()
{
s_c = new Cout();
}
static Cout opCall(char[] str)
{
writefln("%s", str);
return s_c;
}
void newline()
{
}
}
class Cin
{
static char[] get()
{
return readln();
}
}
public import std.string : atoi;
alias atoi parse;
}