main.cpp.28181
3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// Created bxc on 2022/11/25.
//
#include "./sip/SipServer.h"
#include "./rtp/RTPReceiver.h"
#include"./rtp/RTPTcpReceiver.h"
#include"./rtp/RTPUdpReceiver.h"
#include <thread>
#include <chrono>
#include <iostream>
#ifdef __linux__
#include "arpa/inet.h"
#endif
#include "common_header.h"
#define MIN_RTP_PORT 10000
#define MAX_RTP_PORT 60000
using namespace std;
static void RTP_Stream_CallBack(void* userdata, int videoType, char* data, int len, int isKey, uint64_t pts, uint64_t localPts)
{
LOG_INFO("len: {}", len);
// FFGB28181Decoder* decoder = (FFGB28181Decoder*)userdata;
// decoder->stream_callback(videoType, data, len, isKey, pts, localPts);
}
static void RTP_Stream_End_CallBack(void* userdata)
{
LOG_INFO("finished.");
// FFGB28181Decoder* decoder = (FFGB28181Decoder*)userdata;
// decoder->stream_end_callback();
}
bool RequestStream(const char* deviceId, int rtp_port);
// ȡ MIN_RTP_PORT(10000)~MAX_RTP_PORT(60000)֮�������˿�(ż���������������˿ڿ���)
int allocRtpPort() {
int s_rtpPort = MIN_RTP_PORT;
srand((unsigned int)time(NULL));
s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);
if (s_rtpPort % 2)
++s_rtpPort;
int count = 0;
while (true)
{
if (s_rtpPort >= MAX_RTP_PORT) {
s_rtpPort = MIN_RTP_PORT;
count ++;
if (count > 1) {
printf("10000 到 60000 没有可用的port");
}
}
int i = 0;
for (; i < 2; i++) {
sockaddr_in sRecvAddr;
int s = socket(AF_INET, SOCK_DGRAM, 0);
sRecvAddr.sin_family = AF_INET;
sRecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
sRecvAddr.sin_port = htons(s_rtpPort + i);
int nResult = bind(s, (sockaddr *)&sRecvAddr, sizeof(sRecvAddr));
if (nResult != 0) {
break;
}
nResult = close(s);
if (nResult != 0) {
printf("closesocket failed : {}", nResult);
break;
}
}
if (i == 2)
break;
s_rtpPort += 2;
}
return s_rtpPort;
}
bool start_rtp(string deviceId, int m_port) {
RTPReceiver* m_rtpPtr = nullptr;
if(false){
m_rtpPtr = new RTPTcpReceiver();
}else{
m_rtpPtr = new RTPUdpReceiver();
}
if(nullptr == m_rtpPtr){
return false;
}
m_rtpPtr->SetRequestStreamCallback(RequestStream);
m_rtpPtr->SetOutputCallback(RTP_Stream_CallBack, nullptr);
m_rtpPtr->SetVodEndCallback(RTP_Stream_End_CallBack, nullptr);
bool bRet = m_rtpPtr->Open(deviceId);
if(bRet){
// pthread_create(&m_post_decode_thread,0,
// [](void* arg)
// {
// FFGB28181Decoder* a=(FFGB28181Decoder*)arg;
// a->post_decode_thread();
// return (void*)0;
// }
// ,this);
LOG_INFO("rtp open successed.");
}
return bRet;
}
bool RequestStream(const char* deviceId, int rtp_port) {
SipServer* pServer = SipServer::getInstance();
int ret = pServer->RequestInvite_UDP(deviceId, rtp_port);
if (ret > 0)
{
return true;
}
return false;
}
int main(int argc, char *argv[]) {
if (argc <= 1)
{
printf("请输入待获取的摄像头的视频通道编码ID。");
return -1;
}
SipServer* pServer = SipServer::getInstance();
pServer->Init(nullptr);
// const char* sipid = "34020000001310000001";
const char* sipid = argv[1];
char oper = 'g';
while (oper != 'q') {
oper = getchar();
switch (oper)
{
case 'g':
break;
case 'i':
{
int rtp_port = allocRtpPort();
start_rtp(sipid, rtp_port);
pServer->RequestInvite_UDP(sipid, rtp_port);
}
break;
case 'b':
break;
default:
break;
}
}
return 0;
}