Hi Wesley,
On Tue, 2008-03-04 at 15:36 +0100, Wesley S. wrote:
> I need some help with the shoutcast code.
> I'm having some multithreading issues that I don't know how to solve
> on my own.
>
> Right now EngineShoutcast is created on the heap in EngineSidechain
> somewhere.
> EngineSidechain is responsable for filling a buffer with audio data.
> When this buffer is filled, EngineShoutcast's process() function is
> called.
> The EngineShoutcast process() function basically just passes all audio
> data on to an EncoderVorbis that it is tied to.
> The EncoderVorbis then gives encoded samples back to EngineShoutcast.
> Finally, EngineShoutcast uses libshout to send the data to a
> Shout/Icecast server.
>
> Libshout, however, is very buggy, and I want to implement my own
> Icecast/Shoutcast interface,
> based on QTcpSocket and by borrowing some code from DarkIce.
>
> But here is the first problem.
> If I'm not mistaken (I know little about multithreading), the thread
> where EngineShoutcast is running does not have an event loop,
> which is needed to make event-based processing for the QTcpSocket work
> like it should.
>
> How should I fix that?
>
> Create the QTcpSocket outside of EngineShoutcast in the main thread?
> or put the whole EngineShoutcast in the main thread and only put the
> EncoderVorbis in the sidechain somehow?
> or maybe I'm just missing the big picture.
>
When we use libshout in non-blocking mode, it means libshout is creating
an extra thread that's taking care of buffering the compressed audio and
sending it to the server.
What I think you want to do is create a class (we'll call it Shoutcaster
for now), that mimics the behaviour of libshout. The Shoutcaster class
should have a member function called "send" that submits compressed
audio to the class, which buffers it until it has the required amount of
data to send. The Shoutcaster class should subclass QThread, and it's
main processing function (run() in QT land) should sleep (using a
QWaitCondition) until the required number of samples have been submitted
to it/buffered.
When it's buffer is full, it should send it via a TCP socket to the
Shoutcast server. After that, it should loop back and wait on the
QWaitCondition until the buffer has been filled again.
I'll try to demonstrate with some pseudo-code:
class Shoutcaster
{
QWaitCondition waitForFullBuffer;
QMutex bufferLock;
}
Shoutcaster::send(samples)
{
add samples to buffer
if (buffer == full)
waitForFullBuffer.wake();
}
Shoutcaster::run()
{
while (true) //loop forever
{
waitForFullBuffer.wait();
//When woken, send data over socket to Shoutcast server
//(do QT TCP socket sending stuff here)
}
}
When using the QWaitCondition, make sure you use it in conjuction with a
mutex. The docs for QWaitCondition demonstrate this (first example at
the top):
http://doc.trolltech.com/4.3/qwaitcondition.html
You'll also need to use a QMutex around the code in "send", in order to
safeguard against multiple threads accessing the buffer at the same
time. See: http://doc.trolltech.com/4.3/qmutex.html
If you can master QMutex and QWaitCondition (it's not too difficult),
you'll know about as much about multithreaded programming as I do. :)
Lastly, in case this wasn't clear, you should do whatever you need to do
to set up an event loop in the Shoutcaster thread. I think that should
fix your problem. You'll find that threading is involved with a lot of
socket programming, especially if you want to do non-blocking stuff.
Hope this helps, and feel free ask for clarification. If anyone sees any
obvious holes in my design, feel free to make them known. :)
Thanks,
Albert
> Any help is appreciated,
>
> --
> Wesley Stessens <[EMAIL PROTECTED]>
> Human Knowledge Belongs To The World - Antitrust (2001)
> http://wesley.debianbox.be
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Microsoft
> Defy all challenges. Microsoft(R) Visual Studio 2008.
> http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
> _______________________________________________ Mixxx-devel mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mixxx-devel
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Mixxx-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mixxx-devel