日期:2014-05-20 浏览次数:20881 次
// C0, C1, C2, S0, S1, S2 int RTMP_LiveConn::Handshake() { RTMP_UINT8 buf_C0[1]; RTMP_UINT8 buf_S0[1]; int n; buf_C0[0] = RTMP_VERSION; m_pLogger->Write(LOG_NORMAL,"Send C0."); if(m_iSock.Send(buf_C0, 1) <0) { printf("failed to send C0.\n"); return -1; } // 起时时间 RTMP_UINT32 timestamp = 0; m_iSysClock.Reset(); RTMP_UINT8 buf_C1[1536]; if(1) { RTMP_Encoder enc(buf_C1); enc.PutUint32(timestamp); enc.PutPadding(4, 0); } m_pLogger->Write(LOG_NORMAL,"Send C1."); if(m_iSock.Send(buf_C1, 1536) <0) { printf("failed to send C1.\n"); return -1; } // 接收S0 m_pLogger->Write(LOG_NORMAL,"Recv S0."); n = m_iSock.Recv(buf_S0,1); if(n != 1) { printf("failed to recv S0.\n"); return -1; } if(buf_S0[0] != RTMP_VERSION) { printf("S0 error. \n"); return -1; } // 接收S1 m_pLogger->Write(LOG_NORMAL,"Recv S1."); RTMP_UINT8 buf_S1[1536]; TryRecvAll(1536); n = m_nBytesNum; if(n != 1536) { printf("error: S1 length %d bytes.\n", n); return -1; } memcpy(buf_S1, m_pBytes, n); // 发送C2 (C2=S1) RTMP_UINT8 buf_C2[1536]; memcpy(buf_C2, buf_S1, 1536); if(1) { RTMP_Encoder enc(buf_C2); enc.Skip(4); // 计算时间 RTMP_UINT32 timestamp = m_iSysClock.Update(); enc.PutUint32(timestamp); } m_pLogger->Write(LOG_NORMAL,"Send C2."); if(m_iSock.Send(buf_C2, 1536) < 0) { printf("failed to send C2.\n"); return -1; } // 接收S2 (SHOULD == C1) m_pLogger->Write(LOG_NORMAL,"Recv S2."); RTMP_UINT8 buf_S2[1536]; TryRecvAll(1536); n = m_nBytesNum; if(n != 1536) { printf("error: S2 length %d bytes.", n); return -1; } memcpy(buf_S2, m_pBytes, n); if(memcmp(buf_C1+8, buf_S2+8, 1528) != 0) { printf("S2 != C1 \n"); return -1; } timestamp = 0; if(1) { RTMP_Decoder dec(buf_S2+4); timestamp = dec.GetUint32(); printf("time delta = %d ms\n", timestamp); } return 0; }