[Live-devel] One-frame delay in h264 streaming app

Алексей Алексеевич Карнаухов karnaukhov at physics.msu.ru
Mon Apr 7 10:45:44 PDT 2014


Hello.
I`m implementing low-latency h264 rtsp source. Based on this code
http://stackoverflow.com/questions/13863673/how-to-write-a-live555-framedsource-to-allow-me-to-stream-h-264-live
I wrote subclass of FramedSource and able to watch this stream in my client
app. But even if I put 1 frame per second setting in x264 codec, I have
exactly one frame-delayed data in my client. This delay is 100% on server
side, because MultiFramedRTPSink::sendPacketIfNecessary() sending
compressed data of previous frame (i just compare first several bytes of
data in fOutBuf with those I wrote in fTo).

What can I do to send rtp packets immediately after filling fTo buffer?

Here is my deliverFrame implementation:


void H264FramedSource::deliverFrame()
{
    if(nals_count==0)
    {
        clr=clr+1;
        if(clr==25) clr = 0;
        envir() << "color=\t"<<clr<<"\r\n" ;
        memset(frame_buf, 0, W*H*3);
        for(int i=0; i<clr; i++) memset(&frame_buf[W*3*i*23], 200, W*11*3);
        AddToBuffer(frame_buf, W*H*3);
    }
    x264_nal_t nalToDeliver;

    if (fPlayTimePerFrame > 0 && fPreferredFrameSize > 0) {
        if (fPresentationTime.tv_sec == 0 && fPresentationTime.tv_usec ==
0) {
            // This is the first frame, so use the current time:
            gettimeofday(&fPresentationTime, NULL);
        } else {
            // Increment by the play time of the previous data:
            unsigned uSeconds   = fPresentationTime.tv_usec + fLastPlayTime;
            fPresentationTime.tv_sec += uSeconds/1000000;
            fPresentationTime.tv_usec = uSeconds%1000000;
        }

        // Remember the play time of this data:
        fLastPlayTime = (fPlayTimePerFrame*fFrameSize)/fPreferredFrameSize;
        fDurationInMicroseconds = fLastPlayTime;
    } else {
        // We don't know a specific play time duration for this data,
        // so just record the current time as being the 'presentation time':
        gettimeofday(&fPresentationTime, NULL);
    }

fPresentationTime.tv_usec;

    if(nals_count>0)//!m_queue.empty())
    {
        nalToDeliver = nals_queue[nal_num];

        uint8_t* newFrameDataStart = (uint8_t*)0xD15EA5E;

        newFrameDataStart = (uint8_t*)(nalToDeliver.p_payload);
        unsigned newFrameSize = nalToDeliver.i_payload;

        nal_num++;
        nals_count--;
        // Deliver the data here:
        if (newFrameSize > fMaxSize) {
            fFrameSize = fMaxSize;
            fNumTruncatedBytes = newFrameSize - fMaxSize;
            envir() << "truncate!!!\r\n" << fNumTruncatedBytes << "\r\n";
        }
        else {
            fFrameSize = newFrameSize;
        }

        memcpy(fTo, nalToDeliver.p_payload, fFrameSize);

        if(nals_count==0) nal_num=0;
    nextTask() = envir().taskScheduler().scheduleDelayedTask(0,
                (TaskFunc*)FramedSource::afterGetting, this);


    }else{
        nal_num = 0;
    }

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.live555.com/pipermail/live-devel/attachments/20140407/bb15e55b/attachment.html>


More information about the live-devel mailing list