📄 rtp.c
字号:
return RV_OK;}/************************************************************************************ * rtpGetSSRC * description: Returns the current SSRC (synchronization source value) of the RTP session. * input: hRTP - Handle of the RTP session. * output: none. * return value: If no error occurs, the function returns the current SSRC value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvUint32 RVCALLCONV rtpGetSSRC( IN HRTPSESSION hRTP){ rtpSession *s = (rtpSession *)hRTP; return s->sSrc;}/************************************************************************************ * rtpSetEventHandler * description: Set an Event Handler for the RTP session. The application must set * an Event Handler for each RTP session. * input: hRTP - Handle of the RTP session. * eventHandler - Pointer to the callback function that is called each time a * new RTP packet arrives to the RTP session. * context - The parameter is an application handle that identifies the * particular RTP session. The application passes the handle to * the Event Handler. * output: none. * return value: none. ***********************************************************************************/RVAPIvoid RVCALLCONV rtpSetEventHandler( IN HRTPSESSION hRTP, IN LPRTPEVENTHANDLER eventHandler, IN void * context){ rtpSession *s = (rtpSession *)hRTP; RvBool hasFd, addFd; if (s) { hasFd = (s->eventHandler != NULL); addFd = (eventHandler != NULL); s->eventHandler = eventHandler; s->context = context; /* Seems like we have to add it to our list of fds */ if (addFd & (!hasFd)) { RvStatus status; status = RvSelectGetThreadEngine(logMgr, &s->selectEngine); if ((status != RV_OK) || (s->selectEngine == NULL)) s->selectEngine = rvRtpInstance.selectEngine; RvSocketSetBlocking(&s->socket, RV_FALSE, logMgr); RvFdConstruct(&s->selectFd, &s->socket, logMgr); RvSelectAdd(s->selectEngine, &s->selectFd, RvSelectRead, rtpEvent); } /* Seems like we have to remove it from our list of fds */ if (!addFd & hasFd) { RvSelectRemove(s->selectEngine, &s->selectFd); RvFdDestruct(&s->selectFd); RvSocketSetBlocking(&s->socket, RV_TRUE, logMgr); } }}/************************************************************************************ * rtpSetRemoteAddress * description: Defines the address of the remote peer or the address of a multicast * group to which the RTP stream will be sent. * input: hRTP - Handle of the RTP session. * ip - IP address to which RTP packets should be sent. * port - UDP port to which RTP packets should be sent. * output: none. * return value: none. ***********************************************************************************/RVAPIvoid RVCALLCONV rtpSetRemoteAddress( IN HRTPSESSION hRTP, /* RTP Session Opaque Handle */ IN RvUint32 ip, IN RvUint16 port){ rtpSession* s = (rtpSession *)hRTP; if (s != NULL) { if (s->remoteAddressSet) { RvAddressDestruct(&s->remoteAddress); } RvAddressConstructIpv4(&s->remoteAddress, ip, port); s->remoteAddressSet = RV_TRUE; }}/************************************************************************************ * rtpPack * description: This routine sets the RTP header. * input: hRTP - Handle of the RTP session. * buf - Pointer to buffer containing the RTP packet with room before first * payload byte for RTP header. * len - Length in bytes of buf. * p - A struct of RTP param. * output: none. * return value: If no error occurs, the function returns the non-neagtive value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvInt32 RVCALLCONV rtpPack( IN HRTPSESSION hRTP, IN void * buf, IN RvInt32 len, IN rtpParam * p){ rtpSession *s = (rtpSession *)hRTP; RvUint32 *header; RvUint32 seq; p->sByte-=12; p->len=len - p->sByte; if (s->useSequenceNumber) s->sequenceNumber=p->sequenceNumber; p->sequenceNumber=s->sequenceNumber; seq = s->sequenceNumber; /* sets the fields inside RTP message.*/ header=(RvUint32*)((char*)buf + p->sByte); header[0]=0; header[0]=bitfieldSet(header[0],2,30,2); header[0]=bitfieldSet(header[0],p->marker,23,1); header[0]=bitfieldSet(header[0],p->payload,16,7); header[0]=bitfieldSet(header[0],seq,0,16); header[1]=p->timestamp; header[2]=s->sSrc; /* increment the internal sequence number for this session */ s->sequenceNumber++; /* converts an array of 4-byte integers from host format to network format.*/ ConvertToNetwork(header, 0, 3); return RV_OK;}/************************************************************************************ * rtpWrite * description: This routine sends the RTP packet. * input: hRTP - Handle of the RTP session. * buf - Pointer to buffer containing the RTP packet with room before first * payload byte for RTP header. * len - Length in bytes of buf. * p - A struct of RTP param. * output: none. * return value: If no error occurs, the function returns the non-neagtive value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvInt32 RVCALLCONV rtpWrite( IN HRTPSESSION hRTP, IN void * buf, IN RvInt32 len, IN rtpParam * p){ RvStatus res; rtpSession *s = (rtpSession *)hRTP; res = rtpPack(hRTP, buf, len, p); if (res >= 0) { /* send UDP data through the specified socket to the remote host.*/ res = RvSocketSendBuffer(&s->socket, (RvUint8*)buf+p->sByte, (RvSize_t)p->len, &s->remoteAddress, logMgr, NULL); } if (res == RV_OK) { if ((s->hRTCP != NULL) && (res == RV_OK)) { /* inform the RTCP session about a packet that was sent in the corresponding RTP session.*/ rtcpRTPPacketSent(s->hRTCP, p->len, p->timestamp); } } return res;}RVAPIRvInt32 RVCALLCONV rtpUnpack( IN HRTPSESSION hRTP, IN void *buf, IN RvInt32 len, OUT rtpParam* p){ RvUint32 *header=(RvUint32*)buf; RV_UNUSED_ARG(len); RV_UNUSED_ARG(hRTP); if (p->len < 12) { return RV_ERROR_UNKNOWN; } ConvertFromNetwork(buf, 3, (int)bitfieldGet(header[0], 24, 4)); p->timestamp=header[1]; p->sequenceNumber=(RvUint16)bitfieldGet(header[0],0,16); p->sSrc=header[2]; p->marker=bitfieldGet(header[0],23,1); p->payload=(unsigned char)bitfieldGet(header[0],16,7); p->sByte=12+bitfieldGet(header[0],24,4)*sizeof(RvUint32); if (bitfieldGet(header[0],28,1))/*Extension Bit Set*/ { int xStart=p->sByte / sizeof(RvUint32); ConvertFromNetwork(buf, xStart, 1); p->sByte+=bitfieldGet(header[xStart],0,16)*sizeof(RvUint32); if (p->sByte > p->len) { /* This packet is probably corrupted */ p->sByte = 12; return RV_ERROR_UNKNOWN; } } if (bitfieldGet(header[0],29,1))/*Padding Bit Set*/ { p->len-=((char*)buf)[p->len-1]; } return RV_OK;}/************************************************************************************ * rtpRead * description: This routine sets the header of the RTP message. * input: hRTP - Handle of the RTP session. * buf - Pointer to buffer containing the RTP packet with room before first * payload byte for RTP header. * len - Length in bytes of buf. * * output: p - A struct of RTP param,contain the fields of RTP header. * return value: If no error occurs, the function returns the non-negative value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvInt32 RVCALLCONV rtpRead( IN HRTPSESSION hRTP, IN void *buf, IN RvInt32 len, OUT rtpParam* p){ return rtpReadWithRemoteAddress(hRTP, buf, len, p, NULL, NULL);}/************************************************************************************ * rtpReadEx * description: Receives an RTP packet and updates the corresponding RTCP session. * input: hRTP - Handle of the RTP session. * buf - Pointer to buffer containing the RTP packet with room before first * payload byte for RTP header. * len - Length in bytes of buf. * timestamp - * p - A struct of RTP param,contain the fields of RTP header. * output: none. * return value: If no error occurs, the function returns the non-neagtive value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvInt32 RVCALLCONV rtpReadEx( IN HRTPSESSION hRTP, IN void * buf, IN RvInt32 len, IN RvUint32 timestamp, OUT rtpParam * p){ rtpSession *s = (rtpSession *)hRTP; int retVal; retVal = rtpRead(hRTP, buf, len, p); if (s->hRTCP && retVal >= 0) { /* Informs the RTCP session about a packet that was received in the corresponding RTP session.*/ rtcpRTPPacketRecv(s->hRTCP, p->sSrc, timestamp, p->timestamp, p->sequenceNumber); } return retVal;}/************************************************************************************ * rtpReadWithRemoteAddress * description: This routine reads.RTP packets from the network and passes * to the user the remote address * input: hRTP - Handle of the RTP session. * buf - Pointer to buffer containing the RTP packet with room before first * payload byte for RTP header. * len - Length in bytes of buf. * * output: p - A struct of RTP param,contain the fields of RTP header. * pIP -pointer to IP * pPort - pointer to port * return value: If no error occurs, the function returns the non-negative value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvInt32 RVCALLCONV rtpReadWithRemoteAddress( IN HRTPSESSION hRTP, IN void * buf, IN RvInt32 len, OUT rtpParam* p, OUT RvUint32* pIP, OUT RvUint16* pPort){ rtpSession* s = (rtpSession *)hRTP; RvAddress remoteAddress; RvSize_t messageSize; RvStatus res; RvUint32 ip; res = RvSocketReceiveBuffer(&s->socket, (RvUint8*)buf, (RvSize_t)len, logMgr, &messageSize, &remoteAddress); if (res != RV_OK) return res; ip = RvAddressIpv4GetIp(RvAddressGetIpv4(&remoteAddress)); if (pIP != NULL) { *pIP = ip; } if (pPort!=NULL) { *pPort = RvAddressGetIpPort(&remoteAddress); } RvAddressDestruct(&remoteAddress); p->len = (int)messageSize; ConvertFromNetwork(buf, 0, 3); /* If the SSRC is ours and the is our IP, we should ignore this packet - it probably came in as a multicast packet we sent out. */ if ( (s->sSrc == ((RvUint32*)buf)[2]) && isMyIP(ip) ) return RV_ERROR_UNKNOWN; if (p->len <= 0) return RV_ERROR_UNKNOWN; return rtpUnpack(hRTP, buf, len, p);}/************************************************************************************ * rtpGetPort * description: Returns the current port of the RTP session. * input: hRTP - Handle of the RTP session. * output: none. * return value: If no error occurs, the function returns the current port value. * Otherwise, it returns a negative value. ***********************************************************************************/RVAPIRvUint16 RVCALLCONV rtpGetPort( IN HRTPSESSION hRTP) /* RTP Session Opaque Handle */{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -