GB28281實戰一(海思平臺)
阿新 • • 發佈:2021-01-16
最近專案中要IPC裝置實現GB28281接入,記錄一下開發過程,平臺是海思2521D,應該可以在海思類似平臺上同樣實現
一.協議關聯庫準備
由於需要用到SIP協議,所以這裡用到了開源的OSIP/EXOSIP,由於編譯時需要用到openSSL庫,所以還需要下載openSSL1.0的原始碼(不能是openSSL1.1,不然exoSIP編不過去)。
官網下載的包,簡單的修改了一下Makefile,讓編譯能通過,在編譯庫時一定要先編譯openssl,osip庫,再編譯eXosip庫。
當時編譯的幾個庫:https://download.csdn.net/download/jieting6377/14045610
實際送檢還需要對原始碼進行修改,後面再說。
二.裝置準備
由於裝置需要完成推流,回放,裝置雲臺控制,檔案上傳等功能,所以首先要對手頭的裝置有一個很熟悉的瞭解,至少能夠進行推流等工作。
三.OSIP/EXOSIP程式碼邏輯
首先是協議棧初始化,因為SIP傳輸層是支援UDP/TCP還有組播的,這裡我選擇的是IPPROTO_UDP,會話埠IPC_PORT需要根據需要定義,並繫結。
/*初始化eXosip*/ int eXosip_init(struct eXosip_t *eXosip) { int ret = 0; ret = eXosip_init(eXosip);/*初始化osip和eXosip協議棧*/ if (0 != ret) { printf("Couldn't initialize eXosip!\r\n"); return -1; } printf("eXosip_init success!\r\n"); ret = eXosip_listen_addr(context,IPPROTO_UDP, NULL, atoi(IPC_PORT), AF_INET, 0); if (0 != ret)/*傳輸層初始化失敗*/ { eXosip_quit(eXosip); printf("eXosip_listen_addr error!\r\n"); return -1; } printf("eXosip_listen_addr success!\r\n"); return 0; }
然後就是SIP註冊了,首先是註冊
int dt_eXosip_register(struct eXosip_t *eXosip,int expires) /*expires/註冊訊息過期時間,單位為秒*/
{
int ret = 0;
eXosip_event_t *event = NULL;
osip_message_t *reg = NULL;
char sipserver[200];/*sip:主叫使用者名稱@被叫IP地址*/
char sipclient[200];/*sip:被叫IP地址:被叫IP埠*/
memset(sipserver, 0, 100);
memset(sipclient, 0, 100);
snprintf(sipserver, sizeof(sipserver),"sip:% [email protected]%s:%s", device_info.ipc_id, device_info.ipc_ip,device_info.ipc_sess_port);
printf("sip:%[email protected]%s:%s\n", device_info.ipc_id, device_info.ipc_ip, device_info.ipc_sess_port);
snprintf(sipclient,sizeof(sipclient), "sip:%[email protected]%s:%s", device_info.server_id,device_info.server_ip, device_info.server_port);
printf("sip:%[email protected]%s:%s\n", device_info.server_id, device_info.server_ip, device_info.server_port);
/*傳送不帶認證資訊的註冊請求*/
retry:
eXosip_masquerade_contact (eXosip, device_info.ipc_ip, atoi(device_info.ipc_sess_port));
eXosip_set_user_agent (eXosip, UA_STRING);
// if (eXosip_add_authentication_info (context, device_info.ipc_id, device_info.ipc_id, device_info.ipc_pwd, "MD5", NULL)) {
// printf("eXosip_add_authentication_info failed");
// exit (1);
// } //增加鑑權資訊,這裡未使用
eXosip_lock(eXosip);
g_register_id = eXosip_register_build_initial_register(eXosip,sipserver, sipclient, NULL, expires, ®);
//osip_message_set_authorization(reg, "Capability algorithm=\"H:MD5\"");
if(0 > g_register_id)
{
eXosip_unlock(eXosip);
printf("eXosip_register_build_initial_register error!\r\n");
return -1;
}
printf("eXosip_register_build_initial_register success!\r\n");
ret = eXosip_register_send_register(eXosip,g_register_id, reg);
eXosip_unlock(eXosip);
if(0 != ret)
{
printf("eXosip_register_send_register no authorization error!\r\n");
system("pause");return -1;
}
printf("eXosip_register_send_register no authorization success!\r\n");
printf("g_register_id=%d\r\n", g_register_id);
for(;;)
{
event = eXosip_event_wait(eXosip,0, 50);/*偵聽訊息的到來*/
if(NULL == event )/*沒有接收到訊息*/
{
eXosip_execute(eXosip);
eXosip_automatic_action (eXosip);
osip_usleep (1000);
continue;
}
if(EXOSIP_REGISTRATION_FAILURE == event->type)/*註冊失敗*/
{
printf("<EXOSIP_REGISTRATION_FAILURE>\r\n");
printf("event->rid=%d\r\n", event->rid);
/*收到伺服器返回的註冊失敗/401未認證狀態*/
if((NULL != event->response)&&(401 == event->response->status_code))
{
reg = NULL;
/*傳送攜帶認證資訊的註冊請求*/
eXosip_lock(eXosip);
eXosip_clear_authentication_info(eXosip);/*清除認證資訊*/
eXosip_add_authentication_info(eXosip,device_info.ipc_id, device_info.ipc_id, device_info.ipc_pwd, "MD5", NULL) ;/*新增主叫使用者的認證資訊*/
//eXosip_add_authentication_info(eXosip, device_info.ipc_id, device_info.ipc_id, device_info.ipc_pwd, NULL, NULL);
eXosip_register_build_register(eXosip,event->rid, expires, ®);
ret = eXosip_register_send_register(eXosip,event->rid, reg);
eXosip_unlock(eXosip);
if(0 != ret)
{
printf("eXosip_register_send_register authorization error!\r\n");
return -1;
}
printf("eXosip_register_send_register authorization success!\r\n");
}
else/*真正的註冊失敗*/
{
printf("EXOSIP_REGISTRATION_FAILURE error!\r\n");
usleep(3000);
goto retry;/*重新註冊*/
}
}
else if(EXOSIP_REGISTRATION_SUCCESS == event->type)
{
/*收到伺服器返回的註冊成功*/
eXosip_execute (eXosip);
eXosip_automatic_action (eXosip);
printf("<EXOSIP_REGISTRATION_SUCCESS>\r\n");
g_register_id = event->rid;/*儲存註冊成功的註冊ID*/
printf("g_register_id=%d\r\n", g_register_id);
break;
}
}
eXosip_event_free (event);
return 0;
}
wireshark抓包截圖:
接下來就是事件處理流程了