Search code examples
cgstreamerhttp-live-streaming

Fast start for fdsink: first 5 MB async, following bytes synchronous


I wrote a little HTTP video streaming server using GStreamer. Essentially the client does a GET request and receives a continuous HTTP stream.

The stream should be sent synchronously, i.e. at the same speed as the bitrate is. Problem is that some players (mplayer is a prominent example) don't buffer variable bitrate content well, thus lacking every other second.

I want to circumvent the buffer underruns by transmitting the first, say, 5 MB immediately, ignoring the pipeline's clock. The rest of the stream should be transmitted at the appropriate speed.

I figured setting the fdsink sync=TRUE for the first 5 MB, and sync=FALSE from then on should do the trick, but that does not work, as the fdsink patiently waits for the pipeline clock to catch up to the already sent data. In my test with a very low bitrate, there is no data transmitted for quite some seconds.

My fdsink reader thread currently looks like this:

static void *readerThreadFun(void*) {
    int fastStart = TRUE;
    g_object_set(G_OBJECT(fdsink0), "sync", FALSE, NULL);
    for(uint64_t position = 0;;) {
        // (On the other side there is node.js,
        // that's why I don't do the HTTP chunking here)
        ssize_t readCount = splice(gstreamerFd, NULL, remoteFd,
                NULL, 1<<20, SPLICE_F_MOVE|SPLICE_F_MORE);
        if(readCount == 0) {
            break;
        } else if(readCount < 0) {
            goto error;
        }
        position += readCount;
        if(fastStart && position >= 5*1024*1024) {
            fastStart = FALSE;
            g_object_set(G_OBJECT(fdsink0), "sync", TRUE, NULL);
        }
    }
    ...
}

How can I make GStreamer "forget" the duration the wall clock has to catch up with? Is there some "reset" function? Am I misunderstanding sync? Is there another method to realize a "fast start" in GStreamer?


Solution

  • That's not quite the solution I was looking for:

    gst_base_sink_set_ts_offset(GST_BASE_SINK(fdsink0), -10ll*1000*1000*1000);

    The sink will stream the first 10 seconds immediately.