如何實現(xiàn)音視頻同步 (live555)

live555中視頻和音頻是分別進行編碼的椿访,如何實現(xiàn)兩者的同步呢?
如果可以做到讓視頻和音頻的時間戳,都與NTP時間保持同步埠通,就可達到音視頻同步的目的赎离。

Network Time Protocol (NTP) is a networking protocol for clock synchronization between computer systems overpacket-switched, variable-latency data networks.

在live555中是如何實現(xiàn)這種機制的呢?
總體思路是:

  • RTSP服務端利用RTCP協(xié)議中的Sender Report將NTP Timestamp發(fā)送到RTSP客戶端逛犹。
  • RTSP客戶端(數(shù)據(jù)的接收方)把A/V的RTP時間戳同步到RTCP的絕對時間(NTP Timestamp)端辱,實現(xiàn)A/V同步
    這個絕對時間就是當前時間距離Jan 1 1900 00:00:00的差值虽画。

首先看一下未加入同步機制的時間戳代碼:

void RTPReceptionStats::noteIncomingPacket(u_int16_t seqNum, 
                                           u_int32_t rtpTimestamp,
                                           unsigned timestampFrequency,
                                           Boolean useForJitterCalculation,
                                           struct timeval& resultPresentationTime,
                                           Boolean& resultHasBeenSyncedUsingRTCP,
                                           unsigned packetSize) 
{
    ...

    // Record the inter-packet delay
    struct timeval timeNow;
    gettimeofday(&timeNow, NULL);

    ...

    // Return the 'presentation time' that corresponds to "rtpTimestamp":
    if (fSyncTime.tv_sec == 0 && fSyncTime.tv_usec == 0) 
    {
        // This is the first timestamp that we've seen, so use the current
        // 'wall clock' time as the synchronization time.  (This will be
        // corrected later when we receive RTCP SRs.)
        fSyncTimestamp = rtpTimestamp; // 首個RTP Timestamp
        fSyncTime      = timeNow; // 使用當前系統(tǒng)時間作為初始參考時間戳
    }

    int timestampDiff = rtpTimestamp - fSyncTimestamp;

    // Note: This works even if the timestamp wraps around
    // (as long as "int" is 32 bits)

    // Divide this by the timestamp frequency to get real time:
    double timeDiff = timestampDiff/(double)timestampFrequency;

    // Add this to the 'sync time' to get our result:
    unsigned const million = 1000000;
    unsigned seconds, uSeconds;

    if (timeDiff >= 0.0) 
    {
        // 計算時間戳
        seconds  = fSyncTime.tv_sec  + (unsigned)(timeDiff);
        uSeconds = fSyncTime.tv_usec + (unsigned)((timeDiff - (unsigned)timeDiff)*million);

        if (uSeconds >= million) 
        {
            uSeconds -= million;
            ++seconds;
        }
    } 
    else 
    {
        timeDiff = -timeDiff;
        seconds  = fSyncTime.tv_sec  - (unsigned)(timeDiff);
        uSeconds = fSyncTime.tv_usec - (unsigned)((timeDiff - (unsigned)timeDiff)*million);
        if ((int)uSeconds < 0) 
        {
            uSeconds += million;
            --seconds;
        }
    }

    resultPresentationTime.tv_sec  = seconds;
    resultPresentationTime.tv_usec = uSeconds;
    resultHasBeenSyncedUsingRTCP   = fHasBeenSynchronized;

    // Save these as the new synchronization timestamp & time:
    fSyncTimestamp = rtpTimestamp;
    fSyncTime      = resultPresentationTime;

    fPreviousPacketRTPTimestamp = rtpTimestamp;
}

其中有兩個重要的參數(shù): fSyncTimestampfSyncTime;

class RTPReceptionStats {
...

private:
  // Used to convert from RTP timestamp to 'wall clock' time:
  Boolean fHasBeenSynchronized;
  u_int32_t fSyncTimestamp;
  struct timeval fSyncTime;
};
  • fSyncTimestamp
    RTP Timestamp, 默認第N幀的rtpTimestamp為第N+1幀的fSyncTimestamp舞蔽。
  • fSyncTime
    'wall clock' time, 默認第N幀的'wall clock' time為第N+1幀的fSyncTime

RTPReceptionStats::noteIncomingPacket的實質(zhì)是:
將 RTP timestamp 轉(zhuǎn)換為 'wall clock' time码撰。

獲取首個RTP時渗柿,將系統(tǒng)時間作為首個'wall clock' time
后續(xù),當RTP timestamp發(fā)生變化時朵栖,要將變化的部分轉(zhuǎn)換為real time:

int timestampDiff = rtpTimestamp - fSyncTimestamp;
 // Divide this by the timestamp frequency to get real time: 
double timeDiff = timestampDiff/(double)timestampFrequency;

然后將該部分改變反映到'wall clock' time上颊亮, 如:

seconds = fSyncTime.tv_sec + (unsigned)(timeDiff); 
uSeconds = fSyncTime.tv_usec + (unsigned)((timeDiff - (unsigned)timeDiff)*million);

可以看出以上的邏輯中,完全取決于系統(tǒng)時間的精確度陨溅,沒有任何校正機制终惑。

live555是在哪里實現(xiàn)時間校正的呢?
答案是利用RTSP客戶端(數(shù)據(jù)的接收者)利用RTCP返回的Sender Report, 然后利用其中的NTP TimestampRTP timestamp, 對fSyncTimestampfSyncTime進行校正。

Part of Sender Report RTCP Packet

校正程序如下:

void RTPReceptionStats::noteIncomingSR(u_int32_t ntpTimestampMSW,
                                       u_int32_t ntpTimestampLSW,
                                       u_int32_t rtpTimestamp) 
{
    fLastReceivedSR_NTPmsw = ntpTimestampMSW;
    fLastReceivedSR_NTPlsw = ntpTimestampLSW;

    gettimeofday(&fLastReceivedSR_time, NULL);

    // Use this SR to update time synchronization information:
    // ntpTimestampMSW : NTP timestamp, most significant word (64位NTP時間戳的高32位)
    fSyncTimestamp      = rtpTimestamp;
    fSyncTime.tv_sec    = ntpTimestampMSW - 0x83AA7E80; // 1/1/1900 -> 1/1/1970

    // ntpTimestampLSW  : NTP timestamp, least significant word (64位NTP時間戳的低32位)
    double microseconds = (ntpTimestampLSW * 15625.0) / 0x04000000; // 10^6/2^32
    fSyncTime.tv_usec   = (unsigned)(microseconds + 0.5);
}

通過Sender Report门扇,分別對視頻和音頻的時間及時進行校正雹有,即可保證視音頻同步。

References:

https://en.wikipedia.org/wiki/Network_Time_Protocol
RTP: A Transport Protocol for Real-Time Applications

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末臼寄,一起剝皮案震驚了整個濱河市霸奕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌吉拳,老刑警劉巖质帅,帶你破解...
    沈念sama閱讀 217,406評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異留攒,居然都是意外死亡临梗,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,732評論 3 393
  • 文/潘曉璐 我一進店門稼跳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來盟庞,“玉大人,你說我怎么就攤上這事汤善∈膊” “怎么了?”我有些...
    開封第一講書人閱讀 163,711評論 0 353
  • 文/不壞的土叔 我叫張陵红淡,是天一觀的道長不狮。 經(jīng)常有香客問我,道長在旱,這世上最難降的妖魔是什么摇零? 我笑而不...
    開封第一講書人閱讀 58,380評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮桶蝎,結(jié)果婚禮上驻仅,老公的妹妹穿的比我還像新娘。我一直安慰自己登渣,他們只是感情好噪服,可當我...
    茶點故事閱讀 67,432評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著胜茧,像睡著了一般粘优。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,301評論 1 301
  • 那天雹顺,我揣著相機與錄音丹墨,去河邊找鬼。 笑死嬉愧,一個胖子當著我的面吹牛带到,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播英染,決...
    沈念sama閱讀 40,145評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼揽惹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了四康?” 一聲冷哼從身側(cè)響起搪搏,我...
    開封第一講書人閱讀 39,008評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎闪金,沒想到半個月后疯溺,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,443評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡哎垦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,649評論 3 334
  • 正文 我和宋清朗相戀三年囱嫩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片漏设。...
    茶點故事閱讀 39,795評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡墨闲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出郑口,到底是詐尸還是另有隱情鸳碧,我是刑警寧澤,帶...
    沈念sama閱讀 35,501評論 5 345
  • 正文 年R本政府宣布犬性,位于F島的核電站瞻离,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏乒裆。R本人自食惡果不足惜套利,卻給世界環(huán)境...
    茶點故事閱讀 41,119評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望鹤耍。 院中可真熱鬧肉迫,春花似錦、人聲如沸惰蜜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,731評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽抛猖。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間财著,已是汗流浹背联四。 一陣腳步聲響...
    開封第一講書人閱讀 32,865評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留撑教,地道東北人朝墩。 一個月前我還...
    沈念sama閱讀 47,899評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像伟姐,于是被迫代替她去往敵國和親收苏。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,724評論 2 354

推薦閱讀更多精彩內(nèi)容