?? emm.c
字號:
/*-------------------------------------------------------------------------------------*/
/*emm.c */
/*-------------------------------------------------------------------------------------*/
#include "psos.h"
#include "pna.h"
#include "repid.h"
#include "caproc.h"
#define MAX_EMMG_BUFFER_LENGTH (1024*4)
#define MAX_EMMG_CONNECTED 3
/*char emmgdatagrambuf[MAX_EMMG_BUFFER_LENGTH];*/
int g_emm_datagram_length;
extern CA_CONFIG g_ca_config;
CA_EMMG_CHANNEL_INFO g_emmg_channel_info[3];
CA_EMMG_STREAM_INFO g_emmg_stream_info[3];
/*CA_EMMG_STREAM_BW_REQUEST g_emmg_stram_bw_request[3];*/
/*CA_EMMG_DATA_PROVISION g_emmg_data_provision;*/
/*對于EMM發起的連接列表*/
CA_EMM_TCPMEMBER g_Emm_TcpMember[MAX_EMMG_CONNECTED];
int g_Emmg_connected[3];
unsigned long ulTidEmmTcp[3];
int g_socket_emmg=0;
int g_socket_emmg1=0; /*added by xu*/
int g_socket_emmg2=0; /*added by xu*/
/*-------------------------------------------------------*/
/* added by johnnyling 2004-08-24 */
/*-------------------------------------------------------*/
#define EMM_MAX_PAYLOD (188 - 1 - sizeof(TS_TAG))
char g_emm_ts_buf[3][188];
int g_emm_buf_off[3] = {0,0,0};
/*fd_set g_read_mask;*/
/*-------------------------------------------------------*/
/* added by johnnyling 2004-08-24 */
/*-------------------------------------------------------*/
/*將EMM數據送到FIFO中*/
void send_emm(char *buf,int buflen,unsigned short pid,char datatype,unsigned char ucCACount)
{
int i;
int tsnum;
TS_TAG *pTsHead;
char *pSection;
short offset;
char *pts;
char tspkt[188];
short rst;
int first;
static int continue_counter[3]={0,0,0};
if(datatype)
{/* ts packet */
tsnum= buflen / 188;
for(i=0;i<tsnum;i++)
{
pts= buf + i*188;
/*獲取PTS信息及PID*/
pTsHead=(TS_TAG *)pts;
pTsHead->PID=pid;
pTsHead->Continuity_counter=continue_counter[ucCACount];
ts_send(pts);
/*-------------------------------------------------*/
/* added by johnnyling 2004-02-16 */
/*---------------------------------------------------*/
if(continue_counter[ucCACount]==15)
continue_counter[ucCACount]=0;
else
continue_counter[ucCACount]++;
}
}
else
{/*將段打包成TS包寫到FIFO中*/
pSection = buf;
offset = 0;
rst=buflen;
first=1;
while(rst>0)
{
pTsHead=(TS_TAG *)tspkt;
MemSet(tspkt,188,0xFF);
pTsHead->Sync_byte = 0x47;
pTsHead->Transport_err_indicator = 0;
if(first)
pTsHead->Payload_unit_start_indicator=1;
else
pTsHead->Payload_unit_start_indicator=0;
pTsHead->Transport_priority =0;
pTsHead->PID =pid;
pTsHead->Transport_scambling_control =0;
pTsHead->Adaption_field_control =1;
pTsHead->Continuity_counter =continue_counter[ucCACount];
if(continue_counter[ucCACount]==15)
continue_counter[ucCACount]=0;
else
continue_counter[ucCACount]++;
if(first)
{/*第一個包*/
*(char *)((char *)pTsHead + sizeof(TS_TAG))=0; /* pointer field */
if(rst<=(188 - 1 - sizeof(TS_TAG)))
{/*長度小于183字節*/
MemCopy((char *)pTsHead + sizeof(TS_TAG) + 1,buf + offset,rst);
offset+=rst;
ts_send(tspkt);
break;
}
else
{/*大于183字節,要多次發送*/
MemCopy((char *)pTsHead + sizeof(TS_TAG) + 1,buf + offset,188-1-sizeof(TS_TAG));
offset+=188-1-sizeof(TS_TAG);
ts_send(tspkt);
rst-=188 - 1 - sizeof(TS_TAG);
}
first=0;
}
else
{
if(rst<=(188 - sizeof(TS_TAG)))
{
MemCopy((char *)pTsHead + sizeof(TS_TAG),buf + offset,rst);
offset+=rst;
ts_send(tspkt);
break;
}
else
{
MemCopy((char *)pTsHead + sizeof(TS_TAG),buf + offset,188 - sizeof(TS_TAG));
offset+=188 - sizeof(TS_TAG);
ts_send(tspkt);
rst-=188 - sizeof(TS_TAG);
}
}
}
}
}
/*
void send_emm_datagram(char *buf,int buflen,unsigned short pid,char datatype)
{
SECTION_TAG *pSection;
char *pbuf;
int rst=buflen;
static cnt=0;
pSection=(SECTION_TAG *)buf;
while(rst>0)
{
while((pSection->TableID<0x80)||(pSection->TableID>0x89))
{
pSection=(SECTION_TAG *)((char *)pSection + 1);
rst--;
if(rst<=0)
return;
}
send_emm((char *)pSection,pSection->SectionLength + 3,pid,datatype);
rst-=pSection->SectionLength + 3;
pSection=(SECTION_TAG *)((char *)pSection + pSection->SectionLength + 3);
}
}
*/
/*---------------------------------------------------------------------------*/
/* updated by johnnyling 2004-02-16 */
/* support ts packet emm */
/*---------------------------------------------------------------------------*/
#if 0
void send_emm_datagram(char *buf,int buflen,unsigned short pid,char datatype)
{
SECTION_TAG *pSection;
char *pbuf;
int rst=buflen;
static cnt=0;
if(datatype)
{
send_emm(buf,buflen,pid,datatype);
}
else
{
pSection=(SECTION_TAG *)buf;
/*-----------------------------------------------------------------------------*/
/*去掉中視聯section前面的垃圾 updated by johnnyling 2004-02-17 */
/*-----------------------------------------------------------------------------*/
if((g_ca_config.Para_Config.CA_system_id>=0x4900)&&( g_ca_config.Para_Config.CA_system_id<=0x49ff))
{
while(rst>0)
{
while((pSection->TableID<0x80)||(pSection->TableID>0x89))
{
pSection=(SECTION_TAG *)((char *)pSection + 1);
rst--;
if(rst<=0)
return;
}
send_emm((char *)pSection,pSection->SectionLength + 3,pid,datatype);
rst-=pSection->SectionLength + 3;
pSection=(SECTION_TAG *)((char *)pSection + pSection->SectionLength + 3);
}
}
else
send_emm((char *)pSection,pSection->SectionLength + 3,pid,datatype);
}
}
#else
/*發送EMM數據*/
void send_emm_datagram(char *buf,int buflen,unsigned short pid,unsigned char ucCACount)
{
SECTION_TAG *pSection;
char *pbuf;
int rst=buflen;
char *pTemp;
static cnt=0;
if(g_emmg_channel_info[ucCACount].section_TSpket_flag)
{/*表示為TS流,直接發送*/
send_emm(buf,buflen,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
}
else
{/*接收的為段,需打包成TS包*/
pSection=(SECTION_TAG *)buf;
/*-----------------------------------------------------------------------------*/
/*去掉中視聯section前面的垃圾 updated by johnnyling 2004-02-17 */
/*-----------------------------------------------------------------------------*/
if((g_ca_config.Para_Config[ucCACount].CA_system_id>=0x4900)&&( g_ca_config.Para_Config[ucCACount].CA_system_id<=0x49ff))
{
while(rst>0)
{
while((pSection->TableID<0x80)||(pSection->TableID>0x89))
{/*如PID不正確,逐個往后查找*/
pSection=(SECTION_TAG *)((char *)pSection + 1);
rst--;
if(rst<=0)
return;
}
if(pSection->SectionLength + 3>=EMM_MAX_PAYLOD)
{
/*------------------------------------------------------------------*/
/* 先發送緩沖的SECTION */
/*------------------------------------------------------------------*/
if(g_emm_buf_off[ucCACount])
send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
g_emm_buf_off[ucCACount]=0;
/*------------------------------------------------------------------*/
/* 先發送緩沖的SECTION */
/*------------------------------------------------------------------*/
send_emm((char *)pSection,pSection->SectionLength + 3,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
}
else if(pSection->SectionLength + 3 + g_emm_buf_off[ucCACount] >= EMM_MAX_PAYLOD)
{
/*------------------------------------------------------------------*/
/* 先發送緩沖的SECTION */
/*------------------------------------------------------------------*/
if(g_emm_buf_off[ucCACount])
send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
g_emm_buf_off[ucCACount]=0;
/*------------------------------------------------------------------*/
/*緩沖本次的SECTION */
/*------------------------------------------------------------------*/
memcpy(g_emm_ts_buf[ucCACount],pSection,pSection->SectionLength + 3);
g_emm_buf_off[ucCACount]=pSection->SectionLength + 3;
}
else
{
/*------------------------------------------------------------------*/
/*緩沖本次的SECTION */
/*------------------------------------------------------------------*/
memcpy(g_emm_ts_buf[ucCACount] + g_emm_buf_off[ucCACount],pSection,pSection->SectionLength + 3);
g_emm_buf_off[ucCACount]+=buflen;
}
rst-=pSection->SectionLength + 3;
pSection=(SECTION_TAG *)((char *)pSection + pSection->SectionLength + 3);
}
}
else
{/*當數據小于183字節時,緩存下來,做為下次發送*/
if(buflen>=EMM_MAX_PAYLOD)
{
/*------------------------------------------------------------------*/
/* 先發送緩沖的SECTION */
/*------------------------------------------------------------------*/
if(g_emm_buf_off[ucCACount])
{
send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
}
g_emm_buf_off[ucCACount]=0;
/*------------------------------------------------------------------*/
/* 先發送緩沖的SECTION */
/*------------------------------------------------------------------*/
send_emm((char *)pSection,pSection->SectionLength + 3,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
}
else if(buflen+g_emm_buf_off[ucCACount]>=EMM_MAX_PAYLOD)
{
/*------------------------------------------------------------------*/
/* 先發送緩沖的SECTION */
/*------------------------------------------------------------------*/
if(g_emm_buf_off[ucCACount])
{
send_emm((char *)g_emm_ts_buf[ucCACount],g_emm_buf_off[ucCACount],pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
}
g_emm_buf_off[ucCACount]=0;
/*------------------------------------------------------------------*/
/*緩沖本次的SECTION */
/*------------------------------------------------------------------*/
memcpy(g_emm_ts_buf[ucCACount],buf,buflen);
g_emm_buf_off[ucCACount]=buflen;
}
else
{
send_emm(buf,buflen,pid,g_emmg_channel_info[ucCACount].section_TSpket_flag,ucCACount);
g_emm_buf_off[ucCACount] = 0;
}
}
}
}
#endif
/*發送通道測試信息*/
int emmgsendchannelstatus(int socket,unsigned char ucCACount)
{
volatile CA_MESSAGE_HEAD *pHead;
volatile CA_MESSAGE_ITEM_HEAD *pItemHead;
int MsgLen=0;
int i;
int rc;
char EmmgSendBuf[2048];
pHead=(CA_MESSAGE_HEAD *)EmmgSendBuf;
pHead->protocol_version=0x02;
pHead->message_type =EMMG2MUX_CHANNEL_STATUS;
pHead->message_length =0;
pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pHead + sizeof(CA_MESSAGE_HEAD));
pItemHead->parameter_type=EMMG2MUX_client_ID;
pItemHead->parameter_length=4;
*(unsigned long *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD))=g_emmg_channel_info[ucCACount].client_ID;
pHead->message_length +=sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length;
pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length);
pItemHead->parameter_type=EMMG2MUX_data_channel_ID;
pItemHead->parameter_length=2;
*(unsigned short *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD))=g_emmg_channel_info[ucCACount].data_channel_ID;
pHead->message_length +=sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length;
pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length);
pItemHead->parameter_type=EMMG2MUX_section_TSpkt_flag;
pItemHead->parameter_length=1;
*(unsigned char *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD))=g_emmg_channel_info[ucCACount].section_TSpket_flag;
pHead->message_length +=sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length;
pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pItemHead + sizeof(CA_MESSAGE_ITEM_HEAD) + pItemHead->parameter_length);
MsgLen=pHead->message_length + sizeof(CA_MESSAGE_HEAD);
/*將數據發送到EMMG*/
/* rc=ca_send(socket,EmmgSendBuf,MsgLen,g_ca_config.Para_Config[ucCACount].Network_Delay);*/
rc=send(socket,EmmgSendBuf,MsgLen,0);
if(rc!=MsgLen)
return -1;
else
{
caSendLog(LOGTYPE_NORMAL,"emmg: Send channel status message,channelid=%d.",g_emmg_channel_info[ucCACount].data_channel_ID);
Display("emmg: Send channel status message,clientId=%d.",g_emmg_channel_info[ucCACount].client_ID);
return rc;
}
}
/*發送流狀態信息*/
int emmgsendstreamstatus(int socket,unsigned char ucCACount)
{
CA_MESSAGE_HEAD *pHead;
CA_MESSAGE_ITEM_HEAD *pItemHead;
int MsgLen=0;
int i;
int rc;
char EmmgSendBuf[2048];
pHead=(CA_MESSAGE_HEAD *)EmmgSendBuf;
pHead->protocol_version=0x02;
pHead->message_type =EMMG2MUX_STREAM_STATUS;
pHead->message_length =0;
pItemHead=(CA_MESSAGE_ITEM_HEAD *)((char *)pHead + sizeof(CA_MESSAGE_HEAD));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -