四、連線和數(shù)據(jù)傳輸
主要使用的API: IOTCAPIs+AVAPIs
流程圖:

(一)連線
連線先使用IOTC_Get_SessionID獲取一個(gè)空閑的sid,然后調(diào)用IOTC_Connect_ByUIDEx去連線設(shè)備。
IOTC_Connect_ByUIDEx連線成功(即 >= 0),則返回本次連線的session ID,失敗則返回相關(guān)報(bào)錯(cuò),常見為-90,-42,-19等。
tmpSID = IOTC_Get_SessionID();
if(tmpSID < 0) {
printf(" [%s:%d] IOTC_Get_SessionID failed ret[%d]\n", __FUNCTION__, __LINE__, tmpSID);
pthread_exit(0);
}
printf(" [%s:%d] IOTC_Get_SessionID tmpSID[%d]\n", __FUNCTION__, __LINE__, tmpSID);
IOTCConnectInput connect_input;
connect_input.cb = sizeof(connect_input);
connect_input.authentication_type = AUTHENTICATE_BY_KEY;
connect_input.timeout = 20; //連線超時(shí)時(shí)間
memcpy(connect_input.auth_key, gDemoConfig.iotc_auth_key, IOTC_AUTH_KEY_LENGTH);
SID = IOTC_Connect_ByUIDEx(ServerInfo->UID, tmpSID, &connect_input);
其他參考API:
1. IOTC_Connect_ByUID_Parallel
2. IOTC_Connect_Stop_BySID
3. IOTC_Session_Close
(二)創(chuàng)建傳輸通道
因?yàn)镮OTCAPIs通道為不可靠傳輸,所以通常會(huì)創(chuàng)建一個(gè)AV通道,專門用以可靠傳輸音視頻和信令數(shù)據(jù)。創(chuàng)建方法如下:
avClientStartEx成功則返回通道ID(avIndex),失敗常見錯(cuò)誤:-20011,-20015等。
AVClientStartInConfig avClientInConfig;
AVClientStartOutConfig avClientOutConfig;
memset(&avClientInConfig, 0, sizeof(avClientInConfig));
avClientInConfig.cb = sizeof(AVClientStartInConfig);
avClientInConfig.iotc_channel_id = 0;//av通道需使用的iotc channel id,范圍[0,31]
avClientInConfig.iotc_session_id = SID;//相關(guān)的session id,參考上一步連線
avClientInConfig.timeout_sec = 30;
avClientInConfig.resend = 1; //1 為開啟重傳,0為關(guān)閉重傳
if (ENABLE_DTLS)
avClientInConfig.security_mode = AV_SECURITY_DTLS;//使用DTLS加密
else
avClientInConfig.security_mode = AV_SECURITY_SIMPLE;//使用TUTK加密
if (AUTH_BY_TOKEN) { //使用token的方式驗(yàn)證,可以傳更長的數(shù)據(jù)的驗(yàn)證數(shù)據(jù)
avClientInConfig.account_or_identity = gDemoConfig.av_identity;
avClientInConfig.password_or_token = gDemoConfig.av_token;
avClientInConfig.auth_type = AV_AUTH_TOKEN;
} else { //使用密碼的方式驗(yàn)證,驗(yàn)證數(shù)據(jù)更短
avClientInConfig.account_or_identity = gDemoConfig.av_account;
avClientInConfig.password_or_token = gDemoConfig.av_password;
avClientInConfig.auth_type = AV_AUTH_PASSWORD;
}
avClientOutConfig.cb = sizeof(AVClientStartOutConfig);
avIndex = avClientStartEx(&avClientInConfig, &avClientOutConfig); //開啟通道
其他參考API:
1. avClientStart
2. avClientStart2
3. avServStop
4. avServExit
(三)數(shù)據(jù)傳輸
AVAPIs提供了透傳的通道,針對(duì)不同的數(shù)據(jù)類型,提供了不同的傳輸API對(duì):
- 圖像:
avSendFrameData / avRecvFrameData2 - 聲音:
avSendAudioData / avRecvAudioData - IO控制:
avSendIOCtrl / avRecvIOCtrl(io指令的定義,可以參考SDK Readme.html的AV Module Reference of AV IO Control部分,也可以自定義格式)
1. 接收視頻
SMsgAVIoctrlAVStream ioMsg;
memset(&ioMsg, 0, sizeof(SMsgAVIoctrlAVStream));
if((ret = avSendIOCtrl(avIndex, IOTYPE_USER_IPCAM_START, (char *)&ioMsg, sizeof(SMsgAVIoctrlAVStream))) < 0)
{
printf("start_ipcam_stream failed[%d]\n", ret);
return 0;
}
unsigned int frmNo;
int outBufSize = 0;
int outFrmSize = 0;
int outFrmInfoSize = 0;
bool need_key_frame = false;
int video_buf_size = VIDEO_BUF_SIZE;
char *buf = new char[VIDEO_BUF_SIZE];
FRAMEINFO_t frameInfo;
while(1){
int ret = avRecvFrameData2(avIndex, buf, video_buf_size, &outBufSize, &outFrmSize, (char *)&frameInfo, sizeof(FRAMEINFO_t), &outFrmInfoSize, &frmNo);
if(ret <= 0){
if(ret == AV_ER_DATA_NOREADY) {
usleep(5 * 1000);
continue;
}
else if(ret == AV_ER_LOSED_THIS_FRAME || ret == AV_ER_INCOMPLETE_FRAME) {
printf("Recv video, Lost video frame NO[%d]\n", frmNo);
need_key_frame = true;//中途丟幀,需要等I幀
}
else if(ret == AV_ER_BUFPARA_MAXSIZE_INSUFF){
printf("Video buffer is too small to store frame[%d]\n", frmNo);
//加大視頻buffer,或者frameInfo的大小,這兩個(gè)太小,都可能出現(xiàn)-20001。
resizeVideoBuffer();//or resizeFrameInfoBuffer()
need_key_frame = true;//需要等I幀
continue;
}
else{
printf("Recv video, error code[%d],will break;\n",ret);
break;
}
}
if(need_key_frame){
if(frameInfo.flags = IPC_FRAME_FLAG_IFRAME){
need_key_frame = false;
}
else{
continue;
}
}
//實(shí)際接收的幀大小:ret
//數(shù)據(jù)處理。
//把數(shù)據(jù)拷貝到解碼隊(duì)列,初始化播放器,解碼顯示圖像。
//第一幀需要從I幀開始處理,不然會(huì)解碼失敗。
}
delete[] buf;
buf = NULL;
2. 接收音頻
if((ret = avSendIOCtrl(avIndex, IOTYPE_USER_IPCAM_AUDIOSTART, (char *)&ioMsg, sizeof(SMsgAVIoctrlAVStream))) < 0)
{
printf("start_ipcam_stream failed[%d]\n", ret);
return 0;
}
char buf[AUDIO_BUF_SIZE];
FRAMEINFO_t frameInfo;
while(1){
int ret = avRecvAudioData(avIndex, buf, AUDIO_BUF_SIZE, (char *)&frameInfo, sizeof(FRAMEINFO_t), &frmNo);
if(ret <= 0){
if(ret == AV_ER_DATA_NOREADY) {
usleep(5 * 1000);
continue;
}
else if(ret == AV_ER_LOSED_THIS_FRAME || ret == AV_ER_INCOMPLETE_FRAME) {
printf("Recv video, Lost audio frame NO[%d]\n", frmNo);
#if _ENABLE_FILL_LAST_FRAME_
//因?yàn)橐纛l本身是線性的,所以中途有丟一幀,可以用上一幀來填充。
memcpy(dst_audio_buf,last_audio_frame,last_audio_size);
#else
continue;
#endif
}
else{
printf("Recv audio, error code[%d],will break;\n",ret);
break;
}
}
//實(shí)際接收的幀大小:ret
//數(shù)據(jù)處理。
//把數(shù)據(jù)拷貝到解碼隊(duì)列,初始化播放器,解碼播放聲音。
}
3. 對(duì)講
需要注意的問題
1. 視頻和音頻的接收需要分開不同線程處理,以免互相影響。
2. 接收和解碼播放需要分開不同線程處理,以免互相影響。
3. 視頻第一幀需從I幀開始處理,如果中途丟幀,需要等下一個(gè)I幀才繼續(xù)處理。