#include <eXosip2/eXosip.h>
#include <stdio.h>
#include <stdlib.h>
#include <Winsock2.h>
#include <windows.h>
#include "demo.h"
#include "ctrl.h"
#include "refrash.h"
#include "h264.h"
#include <sys/types.h>
#include <string>
#include <vector>
FILE* logfile;
#define RUSULT_ERROR 0
#define RUSULT_WRITE 1
#define PROG_NAME "sipreg"
#define PROG_VER "1.0"
#define UA_STRING "dtagent"
#define SYSLOG_FACILITY LOG_DAEMON
// sip服务器
char eXosip_server_id[30] = "34020000002000000001";
char eXosip_server_ip[20] = "192.168.1.19";
char eXosip_server_port[10] = "15060";
/// local ipc:
char eXosip_ipc_id[30] = "34020000001380000001";
char eXosip_ipc_pwd[20] = "12345678";
char eXosip_ipc_ip[20] = "192.168.1.105";
char eXosip_ipc_media_port[10] = "20000";
char eXosip_ipc_sess_port[10] = "5080";
//char eXosip_alarm_id[30]
char eXosip_alarm_id[30] = "34020000001340000010";
/// rtp: av: localip:
char eXosip_media_ip[30] = "192.168.1.105";
char eXosip_media_port[10] = "15070";
char eXosip_device_name[30] = "zhangsanceshi";
char eXosip_device_manufacturer[30] = "charles";
char eXosip_device_model[30] = "ABC_model2";
char eXosip_device_firmware[30] = "V1.0";
char eXosip_device_encode[10] = "ON";
char eXosip_device_record[10] = "OFF";
char eXosip_status_on[10] = "ON";
char eXosip_status_ok[10] = "OK";
char eXosip_status_online[10] = "ONLINE";
char eXosip_status_guard[10] = "OFFDUTY";
char eXosip_status_time[30] = "2023-05-04T22:31:22";
char aFileNameTime[16] = { 0 };//以时间格式命名的文件名如201805181536(2018/5/15 15:36)
char aFileName[200] = { 0 };//以时间格式命名的带后缀的文件名如201805181536.txt(2018/5/15 15:36)
// 为了适配wvp-GB28181-pro
char eXosip_media_ssrc[10] = "200005269";
void gettime(char* buf)
{
char szContentBuf[200] = "";
time_t timep;
struct tm *p;
#if 0
time(&timep);
p = gmtime(&timep);//格林时间
#else
time(&timep);
p = localtime(&timep);//当地时间
#endif
sprintf(szContentBuf, "%04d%02d%02d%02d%02d%02d", (1900 + p->tm_year), (1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
strcpy(buf, szContentBuf);
}
DEVICE_INFO device_info;
DEVICE_STATUS device_status;
DEVICE_CONTROL device_control;
DT_EXOSIP_CALLBACK dt_eXosip_callback;
BOOL SenAliveFlag = FALSE;
int g_register_id = 0;/*注册ID/用来更新注册或取消注册*/
int g_call_id = 0;/*INVITE连接ID/用来分辨不同的INVITE连接,每个时刻只允许有一个INVITE连接*/
int g_did_realPlay = 0;/*会话ID/用来分辨不同的会话:实时视音频点播*/
int g_did_backPlay = 0;/*会话ID/用来分辨不同的会话:历史视音频回放*/
int g_did_fileDown = 0;/*会话ID/用来分辨不同的会话:视音频文件下载*/
int dt_initDeviceParm(void);
int dt_eXosip_init(eXosip_t *context);/*初始化eXosip*/
int dt_eXosip_register(eXosip_t *context,int expires);/*注册eXosip,手动处理服务器返回的401状态*/
int dt_eXosip_unregister(eXosip_t *context);/*注销eXosip*/
void dt_eXosip_sendEventAlarm(eXosip_t *context,char *alarm_time);/*报警事件通知和分发:发送报警通知*/
void dt_eXosip_sendFileEnd(eXosip_t *context);/*历史视音频回放:发送文件结束*/
void dt_eXosip_paraseMsgBody(eXosip_t *context,eXosip_event_t *p_event);/*解析MESSAGE的XML消息体*/
void dt_eXosip_paraseInviteBody(eXosip_t *context,eXosip_event_t *p_event);/*解析INVITE的SDP消息体,同时保存全局INVITE连接ID和全局会话ID*/
void dt_eXosip_paraseInfoBody(eXosip_t *context,eXosip_event_t *p_event);/*解析INFO的RTSP消息体*/
void dt_eXosip_printEvent(eXosip_t *context,eXosip_event_t *p_event);/*检测并打印事件*/
void dt_eXosip_processEvent(eXosip_t *context);/*消息循环处理*/
void dt_eXosip_launch(eXosip_t *context);/*启动并注册eXosip*/
void CALLBACK TimeProc(
HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime);
DWORD CALLBACK Thread(PVOID pvoid)
{
MSG msg;
PeekMessage(&msg,NULL,WM_USER,WM_USER,PM_NOREMOVE);
UINT timerid = SetTimer(NULL,111,20000,TimeProc);
BOOL bRet;
while ((bRet = GetMessage(&msg,NULL,0,0))!=0)
{
if (bRet == -1)
{
dbg("Error:the thread will quit,error id is %d\n",GetLastError());
break;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
KillTimer(NULL,timerid);
dbg("thread end here/n");
return 0;
}
bool ctrlhandler(DWORD fdwctrltype)
{
switch (fdwctrltype)
{
// handle the ctrl-c signal.
case CTRL_C_EVENT:
dbg("ctrl-c event\n\n");
return(true);
// ctrl-close: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT://右上角关闭事件
dbg("ctrl-close event\n\n");
fflush(logfile);
fclose(logfile);
//system("pause");
return(true);
// pass other signals to the next handler.
case CTRL_BREAK_EVENT:
dbg("ctrl-break event\n\n");
return false;
case CTRL_LOGOFF_EVENT:
dbg("ctrl-logoff event\n\n");
return false;
case CTRL_SHUTDOWN_EVENT:
dbg("ctrl-shutdown event\n\n");
return false;
default:
return false;
}
}
//主函数
int main (int argc, char *argv[])
{
if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ctrlhandler, true) == FALSE)
{
dbg("SetConsoleCtrlHandler failed\r\n");
return 0;
}
gettime(aFileNameTime);//C语言字符串是用字符数组表示的
sprintf(aFileName, "%s%s", aFileNameTime, ".txt");//格式化写到字符串aFileName
logfile = fopen(aFileName, "wt");
device_info.server_id = eXosip_server_id;
device_info.server_ip = eXosip_server_ip;
device_info.server_port = eXosip_server_port;
device_info.ipc_id = eXosip_ipc_id;
device_info.ipc_pwd = eXosip_ipc_pwd;
device_info.ipc_ip = eXosip_ipc_ip;
device_info.ipc_media_port = eXosip_ipc_media_port;
device_info.ipc_sess_port = eXosip_ipc_sess_port;
device_info.alarm_id = eXosip_alarm_id;
device_info.media_ip = eXosip_media_ip;
device_info.media_port = eXosip_media_port;
device_info.device_name = eXosip_device_name;
device_info.device_manufacturer = eXosip_device_manufacturer;
device_info.device_model = eXosip_device_model;
device_info.device_firmware = eXosip_device_firmware;
device_info.device_encode = eXosip_device_encode;
device_info.device_record = eXosip_device_record;
device_status.status_on = eXosip_status_on;
device_status.status_ok = eXosip_status_ok;
device_status.status_online = eXosip_status_online;
device_status.status_guard = eXosip_status_guard;
device_status.status_time = eXosip_status_time;
dt_eXosip_callback.dt_eXosip_deviceControl = dt_eXosip_deviceControl;
dt_eXosip_callback.dt_eXosip_getDeviceInfo = dt_eXosip_getDeviceInfo;
dt_eXosip_callback.dt_eXosip_getDeviceStatus = dt_eXosip_getDeviceStatus;
dt_eXosip_callback.dt_eXosip_getRecordTime = dt_eXosip_getRecordTime;
dt_eXosip_callback.dt_eXosip_mediaControl = dt_eXosip_mediaControl;
dt_eXosip_callback.dt_eXosip_playControl = dt_eXosip_playControl;
//分配一个eXosip_t结构体
eXosip_t *eXosipContext = eXosip_malloc();
if (eXosipContext == NULL)
dbg("mallco failed\n");
int cnt = 0;
while (dt_eXosip_init(eXosipContext) == -1)
{
cnt++;
if (cnt > 10)
{
system("pause");
cnt = 0;
return -1;
}
}
if(dt_eXosip_register(eXosipContext,3600) == 0)
{
dbg("use timer in workthread of console application\n");
CreateThread(NULL,0,Thread,NULL,0,NULL); //MessageLoop, 防止直接退出
dt_eXosip_processEvent(eXosipContext);
}
return 0;
}
int dt_initDeviceParm(void)
{
return 0;
}
/*初始化eXosip*/
int dt_eXosip_init(eXosip_t *context)
{
int ret = 0;
InitiateWinsock();
ret = eXosip_init(context); /*初始化osip和eXosip协议�