-
Notifications
You must be signed in to change notification settings - Fork 173
/
main.cpp
324 lines (290 loc) · 9.74 KB
/
main.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
/*
Copyright (c) 2014-2024 EasyDarwin.org All rights reserved.
Github: https://github.com/easydarwin
WEChat: EasyDarwin
Website: https://www.easydarwin.org
*/
#include <stdio.h>
#include <string.h>
#include "getopt.h"
#include "EasyRTSPClientAPI.h"
FILE* fVideo = NULL;
FILE* fAudio = NULL;
char* fRTSPURL = NULL; //rtsp source addrs
int fTransType = 0; //0 : TCP 1 : UDP
bool fSaveFile = true; //true : save file false : don't save
Easy_Handle fRTSPHandle = 0;
int Easy_APICALL __RTSPClientCallBack( int _chid, void *_chPtr, int _frameType, char *_pBuf, EASY_FRAME_INFO* _frameInfo)
{
if (_frameType == EASY_SDK_VIDEO_FRAME_FLAG)
{
if(_frameInfo->codec == EASY_SDK_VIDEO_CODEC_H264)
{
if(fSaveFile)
{
if(fVideo == NULL)
{
char filename[100] = {0};
sprintf(filename, "./video_%s.H264", fTransType?"udp":"tcp");
fVideo = ::fopen(filename,"wb");
}
::fwrite(_pBuf, 1, _frameInfo->length, fVideo);
}
/*
|H.264£ºSPS+PPS+IDR|
|---------------------|----------------|-------------------------------------|
| | | |
0-----------------reserved1--------reserved2-------------------------------length
*/
if (_frameInfo->type == EASY_SDK_VIDEO_FRAME_I)
{
char sps[2048] = { 0 };
char pps[2048] = { 0 };
char* IFrame = NULL;
unsigned int spsLen,ppsLen,iFrameLen = 0;
spsLen = _frameInfo->reserved1; // SPS
ppsLen = _frameInfo->reserved2 - _frameInfo->reserved1; // PPS
iFrameLen = _frameInfo->length - spsLen - ppsLen; // IDR
memcpy(sps, _pBuf, spsLen); //SPS
memcpy(pps, _pBuf+spsLen, ppsLen); //PPS
IFrame = _pBuf + spsLen + ppsLen; //IDR
printf("Get I H264(%d * %d) SPS/PPS/IDR Len:%d/%d/%d \ttimestamp:%u.%u\n",_frameInfo->width, _frameInfo->height, spsLen, ppsLen, iFrameLen, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
else if (_frameInfo->type == EASY_SDK_VIDEO_FRAME_P)
{
printf("Get P H264(%d * %d) Len:%d \ttimestamp:%u.%u\n",_frameInfo->width, _frameInfo->height, _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
}
/*
|-----vps-----|-----sps-----|-----pps-----|-----I Frame-----|
| | | | |
0--------reserved1-----reserved2------reserved3-----------length
*/
else if(_frameInfo->codec == EASY_SDK_VIDEO_CODEC_H265)
{
if(fSaveFile)
{
if(fVideo == NULL)
{
char filename[100] = {0};
sprintf(filename, "./video_%s.H265", fTransType?"udp":"tcp");
fVideo = ::fopen(filename,"wb");
}
::fwrite(_pBuf, 1, _frameInfo->length, fVideo);
}
if (_frameInfo->type == EASY_SDK_VIDEO_FRAME_I)
{
char vps[2048] = { 0 };
char sps[2048] = { 0 };
char pps[2048] = { 0 };
char* IFrame = NULL;
unsigned int vpsLen,spsLen,ppsLen,iFrameLen = 0;
vpsLen = _frameInfo->reserved1; //VPS
spsLen = _frameInfo->reserved2 - _frameInfo->reserved1; // SPS
ppsLen = _frameInfo->reserved3 - _frameInfo->reserved2; // PPS
iFrameLen = _frameInfo->length - vpsLen - spsLen - ppsLen;// IDR
memcpy(vps, _pBuf, vpsLen);
memcpy(sps, _pBuf+vpsLen, spsLen); //SPS
memcpy(pps, _pBuf+vpsLen+spsLen, ppsLen); //PPS
IFrame = _pBuf + vpsLen + spsLen + ppsLen; //IDR
printf("Get I H265(%d * %d) VPS/SPS/PPS/IDR Len:%d/%d/%d/%d \ttimestamp:%u.%u\n",_frameInfo->width, _frameInfo->height, vpsLen, spsLen, ppsLen, iFrameLen, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
else if (_frameInfo->type == EASY_SDK_VIDEO_FRAME_P)
{
printf("Get P H265(%d * %d) Len:%d \ttimestamp:%u.%u\n",_frameInfo->width, _frameInfo->height, _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
}
else if (_frameInfo->codec == EASY_SDK_VIDEO_CODEC_MJPEG)
{
if(fSaveFile)
{
if(fVideo == NULL)
{
char filename[100] = {0};
sprintf(filename, "./video_%s.mjpeg", fTransType?"udp":"tcp");
fVideo = ::fopen(filename,"wb");
}
::fwrite(_pBuf, 1, _frameInfo->length, fVideo);
}
printf("Get MJPEG Len:%d \ttimestamp:%u.%u\n", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
else if (_frameInfo->codec == EASY_SDK_VIDEO_CODEC_MPEG4)
{
if(fSaveFile)
{
if(fVideo == NULL)
{
char filename[100] = {0};
sprintf(filename, "./video_%s.mpeg4", fTransType?"udp":"tcp");
fVideo = ::fopen(filename,"wb");
}
::fwrite(_pBuf, 1, _frameInfo->length, fVideo);
}
printf("Get MPEG4 Len:%d \ttimestamp:%u.%u\n", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
}
else if (_frameType == EASY_SDK_AUDIO_FRAME_FLAG)
{
if (_frameInfo->codec == EASY_SDK_AUDIO_CODEC_AAC)
{
if(fSaveFile)
{
if(fAudio == NULL)
{
char filename[100] = {0};
sprintf(filename, "./audio_%s.aac", fTransType?"udp":"tcp");
fAudio = ::fopen(filename,"wb");
}
}
printf("Get AAC Len:%d \ttimestamp:%u.%u\n", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
else if (_frameInfo->codec == EASY_SDK_AUDIO_CODEC_G711A)
{
if(fSaveFile)
{
if(fAudio == NULL)
{
char filename[100] = {0};
sprintf(filename, "./audio_%s.pcma", fTransType?"udp":"tcp");
fAudio = ::fopen(filename,"wb");
}
}
printf("Get PCMA Len:%d \ttimestamp:%u.%u\n", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
else if (_frameInfo->codec == EASY_SDK_AUDIO_CODEC_G711U)
{
if(fSaveFile)
{
if(fAudio == NULL)
{
char filename[100] = {0};
sprintf(filename, "./audio_%s.pcmu", fTransType?"udp":"tcp");
fAudio = ::fopen(filename,"wb");
}
}
printf("Get PCMU Len:%d \ttimestamp:%u.%u\n", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
else if (_frameInfo->codec == EASY_SDK_AUDIO_CODEC_G726)
{
if(fSaveFile)
{
if(fAudio == NULL)
{
char filename[100] = {0};
sprintf(filename, "./audio_%s.g726", fTransType?"udp":"tcp");
fAudio = ::fopen(filename,"wb");
}
}
printf("Get G.726 Len:%d \ttimestamp:%u.%u\n", _frameInfo->length, _frameInfo->timestamp_sec, _frameInfo->timestamp_usec);
}
if(fSaveFile)
::fwrite(_pBuf, 1, _frameInfo->length, fAudio);
}
else if (_frameType == EASY_SDK_EVENT_FRAME_FLAG)
{
if (NULL == _pBuf && NULL == _frameInfo)
{
printf("Connecting:%s ...\n", fRTSPURL);
}
else if (NULL != _frameInfo && _frameInfo->codec == EASY_SDK_EVENT_CODEC_ERROR)
{
printf("Error:%s, %d :%s ...\n", fRTSPURL, EasyRTSP_GetErrCode(fRTSPHandle), _pBuf?_pBuf:"null" );
}
else if (NULL != _frameInfo && _frameInfo->codec == EASY_SDK_EVENT_CODEC_EXIT)
{
printf("Exit:%s,Error:%d ...\n", fRTSPURL, EasyRTSP_GetErrCode(fRTSPHandle));
}
}
else if (_frameType == EASY_SDK_MEDIA_INFO_FLAG)
{
if(_pBuf != NULL)
{
EASY_MEDIA_INFO_T mediainfo;
memset(&mediainfo, 0x00, sizeof(EASY_MEDIA_INFO_T));
memcpy(&mediainfo, _pBuf, sizeof(EASY_MEDIA_INFO_T));
printf("RTSP DESCRIBE Get Media Info: video:%u fps:%u audio:%u channel:%u sampleRate:%u \n",
mediainfo.u32VideoCodec, mediainfo.u32VideoFps, mediainfo.u32AudioCodec, mediainfo.u32AudioChannel, mediainfo.u32AudioSamplerate);
}
}
return 0;
}
void PrintUsage(char const* progName)
{
printf("Usage:\n");
printf("--------------------------------------------------------------\n");
printf("%s -d <rtsp-url>[ -m <transport-mode> -s <save-file>]\n", progName);
printf("Help Mode: %s -h \n", progName );
printf("rtsp-url : source rtsp address\ntransport-mode : tcp or udp, default is tcp\nsave-file : yes or no, default is yes\n");
printf("For example: %s -d \"rtsp://admin:[email protected]/11\" -m tcp -s yes\n", progName);
printf("--------------------------------------------------------------\n");
}
int main(int argc, char** argv)
{
printf("\n*****************EasyRTSPClient-v3.1.240724*******************\n");
int activeRet = 0;
int ch;
// We need at least one "rtsp://" URL argument:
if (argc < 2)
{
PrintUsage(argv[0]);
printf("Press Enter exit...\n");
getchar();
return 1;
}
while ((ch = getopt(argc,argv, "h:d:m:s:")) != EOF)
{
switch(ch)
{
case 'h':
PrintUsage(argv[0]);
return 0;
break;
case 'd':
fRTSPURL = optarg;
break;
case 'm':
if((strlen(optarg) == 3) && ((0 == strcmp(optarg, "UDP"))|| (0 == strcmp(optarg, "udp"))))
{
fTransType = 1;
}
else
{
fTransType = 0;
}
break;
case 's':
if((strlen(optarg) == 2) && ((0 == strcmp(optarg, "NO"))|| (0 == strcmp(optarg, "no"))))
{
fSaveFile = false;
}
else
{
fSaveFile = true;
}
break;
default:
break;
}
}
activeRet = EasyRTSP_Activate("https://www.easydarwin.org/");
if(activeRet < 0)
{
printf("EasyRTSP_Activate fail:%d!\n", activeRet);
getchar();
return -1;
}
int init=EasyRTSP_Init(&fRTSPHandle);
if (NULL == fRTSPHandle) return 0;
EasyRTSP_SetCallback(fRTSPHandle, __RTSPClientCallBack);
unsigned int mediaType = EASY_SDK_VIDEO_FRAME_FLAG | EASY_SDK_AUDIO_FRAME_FLAG;
if(fTransType == 0)
EasyRTSP_OpenStream(fRTSPHandle, 0, fRTSPURL, EASY_RTP_OVER_TCP, mediaType, NULL, NULL, NULL, 1000, 0, 0x00, 0);
else
EasyRTSP_OpenStream(fRTSPHandle, 0, fRTSPURL, EASY_RTP_OVER_UDP, mediaType, NULL, NULL, NULL, 1000, 0, 0x01, 3);
printf("Press any key exit...\n");
getchar();
EasyRTSP_CloseStream(fRTSPHandle);
EasyRTSP_Deinit(&fRTSPHandle);
fRTSPHandle = NULL;
return 0;
}