Blame view

src/decoder/gb28181/main.cpp.28181 4.18 KB
c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
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
  //

  // 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;

  

  

  SipServer sipServer;

  

  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() {

  

  	static int s_rtpPort = MIN_RTP_PORT;

  	// if (MIN_RTP_PORT == s_rtpPort)

  	// {

  	// 	srand((unsigned int)time(NULL));

  	// 	s_rtpPort = MIN_RTP_PORT + (rand() % MIN_RTP_PORT);

  	// }

  

  	// if (s_rtpPort % 2)

  	// 	++s_rtpPort;

  

  	while (true)

  	{

  		s_rtpPort = s_rtpPort >= MAX_RTP_PORT ? MIN_RTP_PORT : s_rtpPort;

  

  		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:%d\n", 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;

      }

  

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
108
109
110
111
112
113
114
115
  	m_rtpPtr->SetRequestStreamCallback(RequestStream);

  

  

  

  	m_rtpPtr->SetOutputCallback(RTP_Stream_CallBack, nullptr);

  	m_rtpPtr->SetVodEndCallback(RTP_Stream_End_CallBack, nullptr);

  

  

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
116
      bool bRet = m_rtpPtr->Open(deviceId);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      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) {

  

  	std::vector< DeviceInfo > vec_device = sipServer.GetDeviceList();

  	if (vec_device.size() <= 0)

  	{

  		cout << "no device" << endl;

  		return false;

  	}

  

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
141
  	int ret = sipServer.RequestInvite_TCP_a(vec_device[0].id.c_str(), rtp_port);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
142
143
144
145
146
147
148
149
150
151
152
  	if (ret > 0)

  	{

  		return true;

  	}

  

  	return false;

  }

  

  int main(int argc, char *argv[]) {

  

      ServerInfo info(

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
153
              "SY_Sip_Server",

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
154
155
156
157
158
159
160
161
162
163
164
              "12345678",

              "192.168.60.179",

              15060,

              "34020000002000000002",

              "3402000000",

              "sy123456",

              1800,

              3600);

  

      

      sipServer.Init(info);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  

  	std::this_thread::sleep_for(std::chrono::seconds(5));

  

  	sipServer.cacheCatalog();

  

  	std::vector< DeviceInfo > vec_device;

  	char oper = 'g';

  	while (oper != 'q') {

  		oper = getchar();

  		switch (oper)

  		{

  		case 'g':

  			sipServer.cacheCatalog();

  			break;

  		case 'i':

  			vec_device = sipServer.GetDeviceList();

  			if (vec_device.size() <= 0)

  			{

  				cout << "no device" << endl;

  				break;

  			}

  

  			{

  				int rtp_port = 30026;//allocRtpPort();

  				start_rtp(vec_device[0].id, rtp_port);

  

74d1e6a8   Hu Chunming   完成gb28181大体的代码,未完...
191
  				sipServer.RequestInvite_UDP(vec_device[0].id.c_str(), rtp_port);

c8285c8d   Hu Chunming   GB28181 UDP 有重大进展...
192
193
194
195
196
197
198
199
200
201
202
  			}

  			break;

  		case 'b':

  			break;

  		default:

  			break;

  		}

  	}

  

      return 0;

  }