www.pudn.com > RTP通用开发库(for Linux).rar > rtcpcname.c


/*-------------------------------------------------------------------------
 * rtcpcname.c - rtcpcnameadd, rtcpcnamerem
 *-------------------------------------------------------------------------
 */

#include 
#include 
#include 
#include 
#include 
#include 
#include 

/*------------------------------------------------------------------------
 * rtcpnameadd - insert a stream * in a list of streams with same cname
 *------------------------------------------------------------------------
 */
int
rtcpcnameadd(struct session *psn, struct stream *pstm)
{

	struct stream		*first;
	struct cnamelist	*pcnl;

	if (pthread_mutex_lock(&psn->sn_cnamemutex) != 0)
		return ERROR;

	/*
	 * Lookup the list for the cname pstm->stm_cname.
	 */
	pcnl = (struct cnamelist  *) htget(psn->sn_cnames, (unsigned int) pstm->stm_cname);

	if (pcnl != NULL) {
		/*
		 * Insert pstm into the list of struct stream *'s
		 * for cname pstm->stm_cname.
		 */
		first = pcnl->cn_stream;
		pstm->stm_cnamenext = first->stm_cnamenext;
		first->stm_cnamenext = pstm;
		pstm->stm_cnameprev = first;
		if (pstm->stm_cnamenext != NULL)
			pstm->stm_cnamenext->stm_cnameprev = pstm;
	} else {
		/*
		 * If the list does not exist, create it.
		 */
		pcnl = malloc(sizeof(struct cnamelist));
		memset(pcnl, 0, sizeof(struct cnamelist));
    
		pcnl->cn_stream = pstm;
		pstm->stm_cnamenext = NULL;
		pstm->stm_cnameprev = NULL;

		htadd(psn->sn_cnames, (unsigned int) strdup(pstm->stm_cname), pcnl);
	}

	pthread_mutex_unlock(&psn->sn_cnamemutex);
	return OK;
}

/*------------------------------------------------------------------------
 * rtcpnamerem - remove a stream * from a list of streams with same cname
 *------------------------------------------------------------------------
 */
int
rtcpcnamerem(struct session *psn, struct stream *pstm)
{

	/*
	 * Adjust pointers in the next and previous 
	 * nodes in the threading.
	 */
	if (pthread_mutex_lock(&psn->sn_cnamemutex) != 0)
		return ERROR;

	if (pstm->stm_cnamenext != NULL)
		pstm->stm_cnamenext->stm_cnameprev = pstm->stm_cnameprev;

	if (pstm->stm_cnameprev != NULL)
		pstm->stm_cnameprev->stm_cnamenext = pstm->stm_cnamenext;

	/*
	 * Check if the last source in the list was removed.
	 * If the list is empty, delete the cname from the 
	 * cname hashtable.
	 */
	if (pstm->stm_cnameprev == NULL &&
	    pstm->stm_cnamenext == NULL)
		htdel(psn->sn_cnames, (unsigned int) pstm->stm_cname);
  
	pthread_mutex_unlock(&psn->sn_cnamemutex);
  
	return OK;  
}