Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jetty-users] Issue flushing response buffer with audio streaming servlet

Hi,

I'm using Jetty 7.6.3 embedded on Android

I have a servlet serving realtime audio PCM data to a client playing this audio. The PCM audio is read from a fifo input stream and copied to the response's OutputStream.
The fifo is populated with PCM data by another independent subsystem.

Everything is working fine when there is audio in the fifo, but not when it becomes empty. When it becomes empty (no realtime audio available), the Servlet must detect this condition and start sending silent audio chunks in the response's OutputStream. That's a requirement I can't change for the client to maintain the connection
to the Servlet (otherwise it disconnects on no data).

Detecting that there is no audio available can take some time: typically if no data is available from the fifo for 1s,
the Servlet start writing silent audio chunks to the reponse.

The problem is that when writing the first silence chunk, there is some unflushed buffering that get in the way, resulting in an audio glitch on the playing client which could be described as:

audio playing.... | 1s silence | 200ms-500ms small unwanted audio segment | silence....
            |
            |
            ----  > That's the problem


To make things clearer, in pusedo code the reponse data writing loop looks like this:

while(true) {

    if(isNoAudioDetected()) {
        // if we get here, no data has been written to reponse during 1s
         while(noAudioAvailable()) {
// problem: first call to this will flush old audio data written 1s before by response.write(data), resulting in the unwanted audio segment above
                response.write(silent_chunk);
        }
     }

    data = read(fifo);
    response.getOutPutStream().write(data);
    // response.flushBuffer();    <= doesn't fix the problem
// response.getOutPutStream().flush(); <= doesn't fix the problem either
}


Of course I tought that a call to flushBuffer() just after writing audio data would fix the problem. Yet it doesn't seem to have no effect. I've tried various things to no avail.
Could it possibly be a bug in Jetty ?
Is there a way to discard (not flush) response data not yet written to the socket ?

I even thought it could be the client socket buffering getting in the way, but even hacking SocketConnector to setup a socket with minimal buffer size with Socket.setReceiveBufferSize()
did nothing (this size is 512K on Android, the minimum is 32K).









Back to the top