Simple audio over ip application
-
I need to write simple application, which will send audio data (from wav or, maybe, mp3 file) to network over udp(rtp) protocol and listen it in some of audio player like winamp etc.I found Broadcast Sender, which uses Udp Socket, but how I can put audio data in udp datagram ? So is there any solution in Qt or other non Qt libraries (desirable crossplatform library) ?
-
I found a library which can handle net streams - live 555. And now I need some help. I'm using theirs example in which we receive stream and write it to a file. I found the place when the data is writing into file it looks like this:
@void SpeakerSink::addData(unsigned char const* data, unsigned dataSize,
struct timeval presentationTime) {
if (fPerFrameFileNameBuffer != NULL) {
// Special case: Open a new file on-the-fly for this frame
sprintf(fPerFrameFileNameBuffer, "%s-%lu.lu", fPerFrameFileNamePrefix,
presentationTime.tv_sec, presentationTime.tv_usec);
fOutFid = OpenOutputFile(envir(), fPerFrameFileNameBuffer);
}// Write to our file:
#ifdef TEST_LOSS
static unsigned const framesPerPacket = 10;
static unsigned const frameCount = 0;
static Boolean const packetIsLost;
if ((frameCount++)%framesPerPacket == 0) {
packetIsLost = (our_random() == 0); // simulate 10% packet loss #####
}if (!packetIsLost)
#endif
if (fOutFid != NULL && data != NULL) {
fwrite(data, 1, dataSize, fOutFid); // here we write data to file
}
}
@
So now I need to play this stream to sound card out. I try to write simple QIODevice buffer:
@#ifndef AUDIOBUFFER_H
#define AUDIOBUFFER_H#include <QAudioFormat>
#include <QIODevice>
#include <QMutex>class AudioBuffer : public QIODevice
{
Q_OBJECT
public:
struct block
{
char * ptr;
qint64 len;
int index;
};
//----------------------------------------------------------------------------------------------------------------------------------
AudioBuffer(/const QAudioFormat &format,/ QObject *parent = NULL) : QIODevice(parent)
{
// const char * someData = "12MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMWQuhaSSSSSSSSSSSSSSSSSSSSSSSSSSss";
// writeData(someData,sizeof(someData));
}
//----------------------------------------------------------------------------------------------------------------------------------
~AudioBuffer()
{
clear();
}
//----------------------------------------------------------------------------------------------------------------------------------
void start()
{
if(!isOpen())
open(QIODevice::ReadWrite);
}
//----------------------------------------------------------------------------------------------------------------------------------
void stop()
{
close();
}
//----------------------------------------------------------------------------------------------------------------------------------
qint64 readData(char * data, qint64 maxlen)
{
QMutexLocker mutexLocker(&m_mutex);start(); if(m_buffer.isEmpty()) return 0; int length = maxlen; int size = 0; while (m_buffer.isEmpty() == false) { block bl = m_buffer.at(0); if (bl.len <= length) { memcpy(data+size, bl.ptr, bl.len); m_buffer.removeAt(0); delete [] bl.ptr; length -= bl.len; size += bl.len; continue; } memcpy(data+size, bl.ptr, length); bl.len -= length; m_buffer[0] = bl; memcpy(bl.ptr, bl.ptr+length, bl.len); size += length; length = 0; break; } if (length > 0) memset(data + (maxlen - length), 0xCC, length); return maxlen; }
//----------------------------------------------------------------------------------------------------------------------------------
qint64 writeData(const char *data, qint64 len)
{QMutexLocker mutexLocker(&m_mutex); start(); char * ptr = new char[len]; memcpy(ptr,data,len); block bl; bl.ptr = ptr; bl.len = len; m_buffer.append(bl); return len; }
//----------------------------------------------------------------------------------------------------------------------------------
void clear(){
QMutexLocker mutexLocker(&m_mutex);for(int i = 0; i<m_buffer.count(); i++) { block bl = m_buffer.at(i); delete [] bl.ptr; } m_buffer.clear(); }
//----------------------------------------------------------------------------------------------------------------------------------
private:
QList<block> m_buffer;
QMutex m_mutex;
};#endif//AUDIOBUFFER_H@
And play it using something like this@ AudioBuffer * m_buf = new AudioBuffer();
QAudioFormat format; // Set up the format, eg. format.setFrequency(44100); format.setChannels(2); format.setSampleSize(16); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::UnSignedInt); QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice()); if (!info.isFormatSupported(format)) { qWarning()<<"raw audio format not supported by backend, cannot play audio."; return; } m_buf->start(); m_output = new QAudioOutput(format); m_output->start(m_buf);@
and replace fwrite(data, 1, dataSize, fOutFid); by m_buf->writeData((const char *)data,dataSize); but I hear no sound.
-
And one more thing: when i place breakpoints in writeData method and readData method of Audio buffer class. I reach breakpoint in writeData evry time when put the data to buffer, but never reach breakpoint in readData method. Is it means that my Audio output doesn't read any data from buffer ?