Hello everyone!
Last time I`ve had an idea to do a project to recording voice on computer A and sending them to computer B (and of course playing them there) in real-time. Of course I used TcpSockets, SoundRecorder and SoundStream. Everything compiles nicely, but the problem occurs when the program is running. I get an "pure virtual function call" in client.exe and "the program stopped working" in server.exe. Here is the code:
server.cpp
#include "SFML/System.hpp"
#include "SFML/Network.hpp"
#include "SFML/Audio.hpp"
#include <iostream>
using namespace std;
void end()
{
system("pause");
exit(1);
}
class MyStream : public sf::SoundStream
{
sf::TcpSocket socket;
sf::TcpListener listener;
sf::Int16* samples;
protected:
virtual void onSeek(sf::Time timeOffset);
virtual bool onGetData(sf::SoundStream::Chunk& data);
public:
MyStream();
~MyStream();
};
MyStream::MyStream()
{
unsigned short port = 1234;
cout << "Oczekiwanie na klienta..." << endl;
if(listener.listen(port) != sf::Socket::Done)
{
cout << "Blad przy listen!" << endl;
end();
}
if(listener.accept(socket) != sf::Socket::Done)
{
cout << "Blad przy accept!" << endl;
end();
}
cout << "Klient polaczony!" << endl;
initialize(1, 44100);
}
MyStream::~MyStream()
{
delete [] samples;
}
void MyStream::onSeek(sf::Time timeOffset)
{
return;
}
bool MyStream::onGetData(sf::SoundStream::Chunk& data)
{
size_t received, samples_count;
delete [] samples;
if(socket.receive((void*)&samples_count, sizeof(size_t), received) != sf::Socket::Done)
{
cout << "Blad przy odbieraniu ilosci probek!" << endl;
return false;
}
samples = new sf::Int16[samples_count];
if(socket.receive((void*)samples, samples_count*sizeof(sf::Int16), received) != sf::Socket::Done)
{
cout << "Blad przy odbieraniu probek!" << endl;
return false;
}
data.sampleCount = samples_count;
data.samples = samples;
return true;
}
int main()
{
MyStream stream;
stream.play();
}
client.cpp
#include "SFML/System.hpp"
#include "SFML/Network.hpp"
#include "SFML/Audio.hpp"
#include <iostream>
using namespace std;
void end()
{
system("pause");
exit(1);
}
class MyRecorder : public sf::SoundRecorder
{
sf::TcpSocket socket;
public:
MyRecorder();
protected:
virtual bool onProcessSamples(const sf::Int16 *samples, std::size_t sampleCount);
};
MyRecorder::MyRecorder()
{
sf::IpAddress ip = "localhost";
unsigned short port = 1234;
cout << "Proba laczenia z serwerem..." << endl;
if(socket.connect(ip, port) != sf::Socket::Done)
{
cout << "Nie udalo sie naweiazac polaczenia!" << endl;
end();
}
cout << "Polaczenie nawiazane!" << endl;
};
bool MyRecorder::onProcessSamples(const sf::Int16 *samples, std::size_t sampleCount)
{
if(socket.send((void*)sampleCount, sizeof(size_t)) != sf::Socket::Done)
{
cout << "Blad przy wysylaniu liczby probek" << endl;
return false;
}
if(socket.send((void*)samples, sampleCount*sizeof(sf::Int16)) != sf::Socket::Done)
{
cout << "Blad przy wysylaniu probek" << endl;
return false;
}
return true;
}
int main()
{
MyRecorder recorder;
recorder.start();
}
I`ve also discovered that when I delete the "stream.play()" and "recorder.start()" commands everything works fine (but of course in this case my project is useless). Any suggestions about fixing my problem?
PS. Sorry for my English, but I`m from Poland and I can`t speak English very well. If you want to response, please use as simple words as possible :p