📄 rtcp.c
字号:
}
rvMutexUnlock(&s->mutex);
return 0;
}
RVVXDAPI
RvInt32 VXDCALLCONV rtcpProcessCompoundRTCPPacket(
IN HRTCPSESSION hRTCP,
IN OUT RV_BUFFER * buf,
IN RvUint64 myTime)
{
rtcpSession *s = (rtcpSession *)hRTCP;
rtcpHeader *head;
RvUint8 *currPtr = buf->buffer, *dataPtr, *compoundEnd;
int hdr_count, hdr_len;
rtcpType hdr_type;
rvMutexLock(&s->mutex);
compoundEnd = buf->buffer + buf->length;
while (currPtr < compoundEnd)
{
if ((compoundEnd + 1 - currPtr) < 1)
{
rvMutexUnlock(&s->mutex);
return ERR_RTCP_ILLEGALPACKET;
}
head = (rtcpHeader*)(currPtr);
ConvertToNetwork(currPtr, 0, 1);
hdr_count = bitfieldGet(head->bits, HEADER_RC, HDR_LEN_RC);
hdr_type = (rtcpType)bitfieldGet(head->bits, HEADER_PT, HDR_LEN_PT);
hdr_len = sizeof(RvUint32) *
(bitfieldGet(head->bits, HEADER_len, HDR_LEN_len));
if ((compoundEnd - currPtr) < hdr_len)
{
rvMutexUnlock(&s->mutex);
return ERR_RTCP_ILLEGALPACKET;
}
dataPtr = (RvUint8 *)head + sizeof(RvUint32);
rtcpProcessRTCPPacket(s, dataPtr, hdr_len, hdr_type, hdr_count,
myTime);
currPtr += hdr_len + sizeof(RvUint32);
}
rvMutexUnlock(&s->mutex);
return 0;
}
/* == Basic RTCP Functions == */
/*=========================================================================**
** == rtcpInit() == **
** **
** Initializes the RTCP module. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
RvInt32 VXDCALLCONV rtcpInit(void)
{
rvCoreInit();
rvHostConstructLocal(&host);
rvSocketEngineConstruct(&engine, SOCKENGINE_TASK_PRIORITY, &rvDefaultAlloc);
rvSocketEngineStart(&engine);
return 0;
}
/*=========================================================================**
** == rtcpInitEx() == **
** **
** Initializes the RTCP module with a specific network interface. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
RvInt32 VXDCALLCONV rtcpInitEx(RvUint32 ip)
{
rvCoreInit();
rvHostConstructIpv4(&host, (RvIpv4Addr*)&ip);
rvSocketEngineConstruct(&engine, SOCKENGINE_TASK_PRIORITY, &rvDefaultAlloc);
rvSocketEngineStart(&engine);
return 0;
}
/*=========================================================================**
** == rtcpEnd() == **
** **
** Shuts down the RTCP module. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
RvInt32 VXDCALLCONV rtcpEnd(void)
{
rvSocketEngineDestruct(&engine);
rvCoreEnd();
return 0;
}
/*=========================================================================**
** == rtcpGetAllocationSize() **
** **
** Calculates an allocation size for RTCP session **
** **
** PARAMETERS: **
** sessionMembers Maximum number of participants in the session **
** **
** **
** RETURNS: **
** If no error occurs, the function returns an allocation size for **
** RTCP session. Otherwise it returns NULL. **
** **
**=========================================================================*/
RVVXDAPI
int VXDCALLCONV rtcpGetAllocationSize(
IN int sessionMembers)
{
return sizeof(rtcpSession);
}
/*=========================================================================**
** == rtcpOpenFrom() == **
** **
** Opens a new RTCP session in provided buffer. **
** **
** PARAMETERS: **
** ssrc The synchronization source value for the RTCP session. **
** **
** port The UDP port number to be used for the RTCP session. **
** **
** cname A unique name representing the source of the RTP data. **
** Must not be NULL. **
** sessionMembers Maximum number of participants in the session **
** **
** buffer pointer to at least rtcpGetAllocationSize byte of memory **
** **
** bufferSize size of the buffer **
** **
** RETURNS: **
** If no error occurs, the function returns a handle for the new **
** RTCP session. Otherwise it returns NULL. **
** **
**=========================================================================*/
RVVXDAPI
HRTCPSESSION VXDCALLCONV rtcpOpenFrom(
IN RvUint32 ssrc,
IN RvUint16 port,
IN char * cname,
IN int sessionMembers,
IN void * buffer,
IN int bufferSize)
{
rtcpSession* s=(rtcpSession*)buffer;
int allocSize=rtcpGetAllocationSize(sessionMembers);
if (!cname || strlen(cname) > MAXSDES || allocSize > bufferSize)
{
return NULL;
}
memset(buffer , 0, allocSize);
s->rtt = 0;
s->numOfRRs = 0;
s->isAllocated=rvFalse;
s->myInfo.ssrc = ssrc;
/* If only one local interface bind to it, otherwise bind to "any" */
if (rvHostGetNumOfAddrs(&host) == 1) {
RvSocketAddr sa;
rvSocketAddrConstructInet(&sa, &host, 0, port);
if (rvSocketConstructUdpEx(&s->socket, &sa) == NULL)
return NULL;
} else {
if(port == 0) {
if (rvSocketConstructUdpAnyPort(&s->socket) == NULL) /* assign a port now */
return NULL;
} else {
if (rvSocketConstructUdp(&s->socket, port) == NULL)
return NULL;
}
}
rvListConstruct(rtcpInfo)(&s->hList, &rvDefaultAlloc);
rvTimerConstruct(&s->tElem, RTCP_TIMEOUT, rtcpTimerCallback, s);
rvMutexConstruct(&s->mutex);
rvSocketListenerConstruct(&s->listener, &s->socket, rtcpReceive_, s);
rvSocketEngineRegisterListener(&engine, &s->listener);
setSDES(RTCP_SDES_CNAME, &(s->myInfo.eCName), (RvUint8*)cname, strlen(cname));
return (HRTCPSESSION)s;
}
/*=========================================================================**
** == rtcpOpen() == **
** **
** Opens a new RTCP session. **
** **
** PARAMETERS: **
** ssrc The synchronization source value for the RTCP session. **
** **
** port The UDP port number to be used for the RTCP session. **
** **
** cname A unique name representing the source of the RTP data. **
** Must not be NULL. **
** **
** RETURNS: **
** If no error occurs, the function returns a handle for the new **
** RTCP session. Otherwise it returns NULL. **
** **
**=========================================================================*/
RVVXDAPI
HRTCPSESSION VXDCALLCONV rtcpOpen(
IN RvUint32 ssrc,
IN RvUint16 port,
IN char * cname)
{
rtcpSession* s;
int allocSize=rtcpGetAllocationSize(MAXRTPSESSIONMEMBERS);
s = (rtcpSession*)rvMemAlloc(allocSize);
if (s==NULL)
return NULL;
memset(s, 0, allocSize);
if((rtcpSession*)rtcpOpenFrom(ssrc, port, cname, MAXRTPSESSIONMEMBERS, (void*)s, allocSize)==NULL)
{
rvMemFree(s);
return NULL;
}
s->isAllocated=rvTrue;
return (HRTCPSESSION)s;
}
/*=========================================================================**
** == rtcpClose() == **
** **
** Closes an RTCP session. **
** **
** PARAMETERS: **
** hRTCP The handle of the RTCP session. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
RvInt32 VXDCALLCONV rtcpClose(
IN HRTCPSESSION hRTCP)
{
rtcpSession *s = (rtcpSession *)hRTCP;
rvSocketEngineUnregisterListener(&engine, &s->listener);
rvSocketListenerDestruct(&s->listener);
rvSocketDestruct(&s->socket);
rvMutexDestruct(&s->mutex);
rvTimerDestruct(&s->tElem);
rvListDestruct(rtcpInfo)(&s->hList);
if(s->isAllocated)
rvMemFree(s);
return 0;
}
/*=========================================================================**
** == rtcpSetRemoteAddress() == **
** **
** Defines the address of the remote peer or of the multicast groop. **
** **
** PARAMETERS: **
** hRTCP The handle of the RTCP session. **
** **
** ip The IP address to which the RTCP packets will be sent. **
** **
** port The UDP port to which the RTCP packets should be sent. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
void VXDCALLCONV rtcpSetRemoteAddress(
IN HRTCPSESSION hRTCP, /* RTCP Session Opaque Handle */
IN RvUint32 ip, /* target ip address */
IN RvUint16 port) /* target UDP port */
{
rtcpSession *s = (rtcpSession *)hRTCP;
rvMutexLock(&s->mutex);
s->ip = ip;
s->port = port;
if (ip&&port)
{
rvTimerReset(&s->tElem, RTCP_TIMEOUT);
}
else
{
rtcpStop(hRTCP);
}
rvMutexUnlock(&s->mutex);
}
/*=========================================================================**
** == rtcpStop() == **
** **
** Stop RTCP transmisions . **
** **
** PARAMETERS: **
** hRTCP The handle of the RTCP session. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
RvInt32 VXDCALLCONV rtcpStop(
IN HRTCPSESSION hRTCP) /* RTCP Session Opaque Handle */
{
rtcpSession *s = (rtcpSession *)hRTCP;
rvMutexLock(&s->mutex);
s->ip = 0;
s->port = 0;
rvListClear(rtcpInfo)(&s->hList);
s->myInfo.collision = 0;
s->myInfo.active = 0;
s->myInfo.timestamp = 0;
memset(&(s->myInfo.eSR),0,sizeof(s->myInfo.eSR));
rvTimerReset(&s->tElem, RTCP_TIMEOUT);
rvMutexUnlock(&s->mutex);
return 0;
}
/*=========================================================================**
** == rtcpRTPPacketRecv() == **
** **
** Informs the RTCP session that a packet was received in the **
** corresponding RTP session. **
** **
** PARAMETERS: **
** hRTCP The handle of the RTCP session. **
** **
** localTimestamp The local timestamp for the received packet. **
** **
** timestamp The RTP timestamp from the received packet. **
** **
** sequence The packet sequence number. **
** **
** RETURNS: **
** A non-negative value upon success, or a negative integer error **
** code. **
** **
**=========================================================================*/
RVVXDAPI
RvInt32 VXDCALLCONV rtcpRTPPacketRecv(
IN HRTCPSESSION hRTCP,
IN RvUint32 ssrc,
IN RvUint32 localTimestamp,
IN RvUint32 timestamp,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -