-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrtsp_h265_file.cpp
391 lines (336 loc) · 11.5 KB
/
rtsp_h265_file.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// RTSP Server
#include <thread>
#include <memory>
#include <iostream>
#include <string>
#include "xop/RtspServer.h"
#include "net/Timer.h"
#include "bs.h"
#include "h265_stream.h"
static volatile bool s_spnet_loop;
static pthread_mutex_t s_main_lock;
static pthread_cond_t s_frame_cond;
class H265File
{
public:
H265File(int buf_size=500000);
~H265File();
bool Open(const char *path);
void Close();
bool IsOpened() const
{ return (m_file != NULL); }
int ReadFrame(char* in_buf, int in_buf_size, bool* end);
private:
FILE* m_file = NULL;
uint8_t* m_buf = NULL;
int m_buf_size = 0;
int m_bytes_used = 0;
int m_count = 0;
};
H265File::H265File(int buf_size)
: m_buf_size(buf_size)
{
m_buf = new uint8_t[m_buf_size];
}
H265File::~H265File()
{
delete [] m_buf;
}
bool H265File::Open(const char *path)
{
m_file = fopen(path, "rb");
if(m_file == NULL) {
return false;
}
return true;
}
void H265File::Close()
{
if(m_file) {
fclose(m_file);
m_file = NULL;
m_count = 0;
m_bytes_used = 0;
}
}
int H265File::ReadFrame(char* in_buf, int in_buf_size, bool* end)
{
if(m_file == NULL) {
return -1;
}
int bytes_read = (int)fread(m_buf, 1, m_buf_size, m_file);
if(bytes_read == 0) {
fseek(m_file, 0, SEEK_SET);
m_count = 0;
m_bytes_used = 0;
bytes_read = (int)fread(m_buf, 1, m_buf_size, m_file);
if(bytes_read == 0) {
this->Close();
return -1;
}
}
uint8_t nal_unit;
uint32_t first_slice,no_output,pic_parameter,slice_type;
bool is_find_start = false, is_find_end = false;
int i = 0, start_code = 3;
*end = false;
for (i=0; i<bytes_read-5; i++) {
if(m_buf[i] == 0 && m_buf[i+1] == 0 && m_buf[i+2] == 1) {
start_code = 3;
}
else if(m_buf[i] == 0 && m_buf[i+1] == 0 && m_buf[i+2] == 0 && m_buf[i+3] == 1) {
start_code = 4;
}
else {
continue;
}
/*
00 00 00 01 nalu temporal_id
0 CODED SLICE TRAIL N
1 CODED SLICE TRAIL R
0x13 IDR W
0x14 IDR N
0x20 VPS
0x21 SPS
0x22 PPS
0x27 SEI Prefix
0x28 SEI Suffix
nal_unit = (*nalu_pos&0x7E) >> 1
temporal_id = *(nalu_pos+1)
--------------------------------
00 00 00 01 nalu temporal_id (first_slice+no_output+slice_pic_parameter+slice_reserved+slice_type at one byte)
0 HEVC_SLICE_B
1 HEVC_SLICE_P
2 HEVC_SLICE_I
*(bs->p) = *(nalu_pos+2)
bs_read_u1(bs)
bs_read_u1(bs)
bs_read_ue(bs)
slice_type = bs_read_ue(bs)
*/
//debug
/*printf("ReadFrame1: %02x%02x%02x%02x %02x%02x%02x%02x%02x%02x%02x%02x\n",
*((unsigned char*)(m_buf+0)),
*((unsigned char*)(m_buf+1)),
*((unsigned char*)(m_buf+2)),
*((unsigned char*)(m_buf+3)),
*((unsigned char*)(m_buf+4)),
*((unsigned char*)(m_buf+5)),
*((unsigned char*)(m_buf+6)),
*((unsigned char*)(m_buf+7)),
*((unsigned char*)(m_buf+8)),
*((unsigned char*)(m_buf+9)),
*((unsigned char*)(m_buf+10)),
*((unsigned char*)(m_buf+11)));
if( *((unsigned char*)(m_buf+0)) == 0x00
&&
*((unsigned char*)(m_buf+1)) == 0x00
&&
*((unsigned char*)(m_buf+2)) == 0x00
&&
*((unsigned char*)(m_buf+3)) == 0x01
&&
*((unsigned char*)(m_buf+4)) == 0x10
&&
*((unsigned char*)(m_buf+5)) == 0x01
&&
*((unsigned char*)(m_buf+6)) == 0xff
&&
*((unsigned char*)(m_buf+7)) == 0x22
&&
*((unsigned char*)(m_buf+8)) == 0x2d
&&
*((unsigned char*)(m_buf+9)) == 0x49
&&
*((unsigned char*)(m_buf+10)) == 0xfd
&&
*((unsigned char*)(m_buf+11)) == 0xc2)
printf("found1\n\n");*/
nal_unit = (m_buf[i+start_code] & 0x7E) >> 1;
bs_init(m_buf[i+start_code+2]);
first_slice = bs_read_u1();
if ( nal_unit >= NAL_UNIT_CODED_SLICE_BLA_W_LP && nal_unit <= NAL_UNIT_RESERVED_IRAP_VCL23 )
{
no_output = bs_read_u1();
}
pic_parameter = bs_read_ue();
slice_type = bs_read_ue();
/*SLICE_TRAIL_N SLICE_TRAIL_R IDRW IDRN CRA && HEVC_SLICE_B HEVC_SLICE_I*/
if (nal_unit == 0x20 || nal_unit == 0x21 || nal_unit == 0x22 || nal_unit == 0x27 || nal_unit == 0x28
||
((nal_unit == 0x0 || nal_unit == 0x1 || nal_unit == 0x8 || nal_unit == 0x9 || nal_unit == 0x13 || nal_unit == 0x14 || nal_unit == 0x15) && (slice_type >= 0 && slice_type <= 2))) {
//printf("slice_type:%d\n", slice_type); //debug
is_find_start = true;
i += 4;
break;
}
}
for (; i<bytes_read-5; i++) {
if(m_buf[i] == 0 && m_buf[i+1] == 0 && m_buf[i+2] == 1)
{
start_code = 3;
}
else if(m_buf[i] == 0 && m_buf[i+1] == 0 && m_buf[i+2] == 0 && m_buf[i+3] == 1) {
start_code = 4;
}
else {
continue;
}
//debug
/*printf("ReadFrame2: %02x%02x%02x%02x %02x%02x%02x%02x%02x%02x%02x%02x\n",
*((unsigned char*)(i+m_buf+0)),
*((unsigned char*)(i+m_buf+1)),
*((unsigned char*)(i+m_buf+2)),
*((unsigned char*)(i+m_buf+3)),
*((unsigned char*)(i+m_buf+4)),
*((unsigned char*)(i+m_buf+5)),
*((unsigned char*)(i+m_buf+6)),
*((unsigned char*)(i+m_buf+7)),
*((unsigned char*)(i+m_buf+8)),
*((unsigned char*)(i+m_buf+9)),
*((unsigned char*)(i+m_buf+10)),
*((unsigned char*)(i+m_buf+11)));
if( *((unsigned char*)(i+m_buf+0)) == 0x00
&&
*((unsigned char*)(i+m_buf+1)) == 0x00
&&
*((unsigned char*)(i+m_buf+2)) == 0x00
&&
*((unsigned char*)(i+m_buf+3)) == 0x01
&&
*((unsigned char*)(i+m_buf+4)) == 0x00
&&
*((unsigned char*)(i+m_buf+5)) == 0x01
&&
*((unsigned char*)(i+m_buf+6)) == 0xfe
&&
*((unsigned char*)(i+m_buf+7)) == 0xe2
&&
*((unsigned char*)(i+m_buf+8)) == 0x2d
&&
*((unsigned char*)(i+m_buf+9)) == 0x57
&&
*((unsigned char*)(i+m_buf+10)) == 0xf7
&&
*((unsigned char*)(i+m_buf+11)) == 0x08)
printf("found2\n\n");*/
nal_unit = (m_buf[i+start_code] & 0x7E) >> 1;
bs_init(m_buf[i+start_code+2]);
first_slice = bs_read_u1();
if ( nal_unit >= NAL_UNIT_CODED_SLICE_BLA_W_LP && nal_unit <= NAL_UNIT_RESERVED_IRAP_VCL23 )
{
no_output = bs_read_u1();
}
pic_parameter = bs_read_ue();
slice_type = bs_read_ue();
/*VPS SPS PPS SEI_PREFIX SEI_SUFFIX
SLICE_TRAIL_N SLICE_TRAIL_R IDRW IDRN CRA && HEVC_SLICE_B HEVC_SLICE_I*/
if (nal_unit == 0x20 || nal_unit == 0x21 || nal_unit == 0x22 || nal_unit == 0x27 || nal_unit == 0x28
||
((nal_unit == 0x0 || nal_unit == 0x1 || nal_unit == 0x8 || nal_unit == 0x9 || nal_unit == 0x13 || nal_unit == 0x14 || nal_unit == 0x15) && (slice_type >= 0 && slice_type <= 2))) {
//printf("slice_type:%d\n", slice_type); //debug
is_find_end = true;
break;
}
}
bool flag = false;
if(is_find_start && !is_find_end && m_count>0) {
flag = is_find_end = true;
i = bytes_read;
*end = true;
}
if(!is_find_start || !is_find_end) {
this->Close();
return -1;
}
int size = (i<=in_buf_size ? i : in_buf_size);
memcpy(in_buf, m_buf, size);
if(!flag) {
m_count += 1;
m_bytes_used += i;
}
else {
m_count = 0;
m_bytes_used = 0;
}
fseek(m_file, m_bytes_used, SEEK_SET);
return size;
}
static void SendFrameThread(xop::RtspServer* rtsp_server, xop::MediaSessionId session_id, H265File* h265_file)
{
int buf_size = 2000000;
std::unique_ptr<uint8_t> frame_buf(new uint8_t[buf_size]);
while(s_spnet_loop) {
bool end_of_frame = false;
int frame_size = h265_file->ReadFrame((char*)frame_buf.get(), buf_size, &end_of_frame);
if(frame_size > 0) {
xop::AVFrame videoFrame = {0};
//videoFrame.type = 0;
videoFrame.size = frame_size;
//videoFrame.timestamp = xop::H265Source::GetTimestamp();
xop::H265Source::GetTimestamp(&videoFrame.timeNow, &videoFrame.timestamp);
videoFrame.buffer.reset(new uint8_t[videoFrame.size]);
memcpy(videoFrame.buffer.get(), frame_buf.get(), videoFrame.size);
rtsp_server->PushFrame(session_id, xop::channel_0, videoFrame);
}
else {
break;
}
xop::Timer::Sleep(40);
}
pthread_cond_signal(&s_frame_cond);
}
int main(int argc, char **argv)
{
if(argc != 2) {
printf("Usage: %s test.265\n", argv[0]);
return 0;
}
H265File h265_file;
if(!h265_file.Open(argv[1])) {
printf("Open %s failed.\n", argv[1]);
return 0;
}
pthread_mutex_init(&s_main_lock,NULL);
pthread_cond_init(&s_frame_cond,NULL);
std::string suffix = "live";
std::string ip = "127.0.0.1";
std::string port = "554";
std::string rtsp_url = "rtsp://" + ip + ":" + port + "/" + suffix;
std::shared_ptr<xop::EventLoop> event_loop(new xop::EventLoop());
std::shared_ptr<xop::RtspServer> server = xop::RtspServer::Create(event_loop.get());
if (!server->Start("0.0.0.0", atoi(port.c_str()))) {
printf("RTSP Server listen on %s failed.\n", port.c_str());
return 0;
}
#ifdef AUTH_CONFIG
server->SetAuthConfig("-_-", "admin", "12345");
#endif
xop::MediaSession *session = xop::MediaSession::CreateNew("live");
session->AddSource(xop::channel_0, xop::H265Source::CreateNew());
//session->StartMulticast();
session->AddNotifyConnectedCallback([] (xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port){
printf("RTSP client connect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
});
session->AddNotifyDisconnectedCallback([](xop::MediaSessionId sessionId, std::string peer_ip, uint16_t peer_port) {
printf("RTSP client disconnect, ip=%s, port=%hu \n", peer_ip.c_str(), peer_port);
});
xop::MediaSessionId session_id = server->AddSession(session);
s_spnet_loop = true;
std::thread t1(SendFrameThread, server.get(), session_id, &h265_file);
t1.detach();
std::cout << "Play URL: " << rtsp_url << std::endl;
/*while (1) {
xop::Timer::Sleep(100);
}*/
getchar();
s_spnet_loop = false;
pthread_mutex_lock(&s_main_lock);
pthread_cond_wait(&s_frame_cond, &s_main_lock);
pthread_mutex_unlock(&s_main_lock);
pthread_cond_destroy(&s_frame_cond);
pthread_mutex_destroy(&s_main_lock);
h265_file.Close();
std::cout << "rtsp server exit" << std::endl;
return 0;
}