-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.cc
395 lines (364 loc) · 10.5 KB
/
main.cc
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
392
393
394
395
/**
* @file main.cc
* @brief
* @author Rider Woo
* @version 0.0.1
* @date 2014-08-28
*/
extern "C"{
#include <librtmp/rtmp.h>
#include <librtmp/log.h>
}
#include <iostream>
#include <vector>
#include <unistd.h>
#include <arpa/inet.h>
#include <string.h>
#include <signal.h>
#define uchar unsigned char
struct GlobalContext{
RTMP *rtmp;
RTMPPacket *packet;
FILE *fp;
char *url;
char *flvfile;
std::vector<std::string> flvfiles;
size_t nflvfiles;
uint32_t timestamp_delta;
bool loop;
GlobalContext(){
rtmp = NULL;
packet = NULL;
url = NULL;
flvfile = NULL;
fp = NULL;
timestamp_delta = 0;
flvfiles.clear();
nflvfiles = 0;
loop = 0;
}
~GlobalContext(){
if(rtmp)
{
RTMP_Close(rtmp);
RTMP_Free(rtmp);
rtmp = NULL;
}
if(packet){
RTMPPacket_Free(packet);
delete packet;
packet = NULL;
}
if(fp){
fclose(fp);
fp = NULL;
}
}
};
bool Read8(int &i8, FILE *fp){
if(fread(&i8, 1, 1, fp) != 1){
return false;
}
return true;
}
bool Read16(int &i16, FILE *fp){
if(fread(&i16, 2, 1, fp) != 1){
return false;
}
i16 = htons(i16);
return true;
}
bool Read24(int &i24, FILE *fp){
uchar byte;
if(fread(&byte, 1, 1, fp) != 1){
return false;
}
i24 += byte << 16;
if(fread(&byte, 1, 1, fp) != 1){
return false;
}
i24 += byte << 8;
if(fread(&byte, 1, 1, fp) != 1){
return false;
}
i24 += byte;
return true;
}
bool Read32(int &i32, FILE *fp){
if(fread(&i32, 4, 1, fp) != 1){
return false;
}
i32 = htonl(i32);
return true;
}
bool ReadTime(uint32_t &time, FILE *fp){
if(fread(&time, 4, 1, fp) != 1){
return false;
}
time = ((time>>16&0xff)|(time<<16&0xff0000)|(time&0xff00)|(time&0xff000000));
return true;
}
void print_help(){
std::cout<<"Commands:"<<std::endl;
std::cout<<" -h print help information"<<std::endl;
std::cout<<" -i input flv file(s), splited by comma"<<std::endl;
std::cout<<" -o output rtmp url"<<std::endl;
std::cout<<" -l loop files";
}
int exit_program = 0;
void int_handler(int sig){
exit_program = 1;
}
int connect_remote(GlobalContext &ctx){
std::cout<<"Before allco rtmp context."<<std::endl;
if(ctx.rtmp)
{
RTMP_Close(ctx.rtmp);
RTMP_Free(ctx.rtmp);
ctx.rtmp = NULL;
}
if(ctx.packet){
RTMPPacket_Free(ctx.packet);
delete ctx.packet;
ctx.packet = NULL;
}
// 初始化librtmp参数
ctx.rtmp = RTMP_Alloc();
std::cout<<"Alloced librtmp struct."<<std::endl;
if(!ctx.rtmp){
std::cerr<<"Failed to alloc librtmp struct"<<std::endl;
return -1;
}
RTMP_Init(ctx.rtmp);
ctx.rtmp->Link.timeout = 5;
std::cout<<"Timeout set to 5s."<<std::endl;
// 创建RTMP数据包
ctx.packet = new RTMPPacket;
RTMPPacket_Alloc(ctx.packet, 1024*64);
std::cout<<"Alloced rtmp packet."<<std::endl;
RTMPPacket_Reset(ctx.packet);
// 连接url
RTMP_SetupURL(ctx.rtmp, ctx.url);
std::cout<<"Setup rtmp url ["<<ctx.url<<"]."<<std::endl;
RTMP_EnableWrite(ctx.rtmp);
std::cout<<"Enabled rtmp write."<<std::endl;
// NetConnect
if(!RTMP_Connect(ctx.rtmp, NULL)){
std::cout<<"Failed to do rtmp connect."<<std::endl;
return -1;
}
std::cout<<"RTMP connected."<<std::endl;
// StreamConnect
if(!RTMP_ConnectStream(ctx.rtmp, 0)){
return -1;
}
std::cout<<"Stream connected."<<std::endl;
// 初始化RTMP数据包
ctx.packet->m_hasAbsTimestamp = 0;
ctx.packet->m_nChannel = 0x04;
ctx.packet->m_nInfoField2 = ctx.rtmp->m_stream_id;
return 0;
}
int open_file(GlobalContext &ctx){
if(ctx.fp){
fclose(ctx.fp);
ctx.fp = NULL;
}
std::string file_path;
if(ctx.flvfiles.size()){
while(1){
if(ctx.nflvfiles >= ctx.flvfiles.size()){
if(ctx.loop){
ctx.nflvfiles = 0;
continue;
}
return -1;
}
file_path = ctx.flvfiles[ctx.nflvfiles];
ctx.nflvfiles++;
// 打开flv文件
ctx.fp = fopen(file_path.c_str(), "rb");
if(!ctx.fp){
std::cout<<"Failed to open flv file "<<file_path<<std::endl;
continue;
}
std::cout<<"Succeeded to open flv file "<<file_path<<std::endl;
return 0;
}
}
else {
file_path = ctx.flvfile;
// 打开flv文件
ctx.fp = fopen(file_path.c_str(), "rb");
if(!ctx.fp){
std::cout<<"Failed to open flv file "<<file_path<<std::endl;
return -1;
}
std::cout<<"Succeeded to open flv file "<<file_path<<std::endl;
return 0;
}
}
void parse_multi_files(GlobalContext &ctx){
std::string flvfile = ctx.flvfile;
ctx.flvfiles.clear();
size_t pos = 0;
while(1){
size_t p = flvfile.find(',', pos);
if(p == std::string::npos){
if(pos != flvfile.size()){
std::string file_path = flvfile.substr(pos, p);
std::cout<<"Input file #"<<ctx.flvfiles.size()<<": "<<file_path<<std::endl;
ctx.flvfiles.push_back(file_path);
}
break;
}
if(pos == p){
pos = p+1;
continue;
}
std::string file_path = flvfile.substr(pos, p-pos);
std::cout<<"Input file #"<<ctx.flvfiles.size()<<": "<<file_path<<std::endl;
ctx.flvfiles.push_back(file_path);
pos = p+1;
}
}
int main(int argc, char *argv[])
{
GlobalContext ctx;
// 解析输入参数
int opt;
memset(&ctx, 0, sizeof(ctx));
while((opt = getopt(argc, argv, "hi:o:l")) != -1){
switch(opt){
case 'i':
ctx.flvfile = optarg;
break;
case 'o':
ctx.url = optarg;
break;
case 'h':
print_help();
return 0;
case 'l':
ctx.loop = 1;
break;
default:
break;
}
}
if(!ctx.flvfile){
std::cout<<"Input flv file not found."<<std::endl;
print_help();
return -1;
}
if(std::string(ctx.flvfile).find(',') != std::string::npos){
parse_multi_files(ctx);
}
if(!ctx.url){
std::cout<<"Output RTMP url not found."<<std::endl;
print_help();
return -1;
}
// 捕捉系统信号
signal(SIGINT, int_handler);
signal(SIGTERM, int_handler);
signal(SIGPIPE, SIG_IGN);
signal(SIGHUP, SIG_IGN);
// 设置日志级别
RTMP_LogSetLevel(RTMP_LOGINFO);
std::cout<<"Setting librtmp log level."<<std::endl;
// 起始时间
time_t start = time(0);
// 上一帧时间戳
uint32_t stream_timestamp = 0;
while(!exit_program){
// 打开文件
if(open_file(ctx)){
return -1;
}
// 连接rtmp服务端
if(!ctx.rtmp || (ctx.rtmp && !RTMP_IsConnected(ctx.rtmp))){
if(connect_remote(ctx)){
// 每隔1s,重连rtmp服务端
std::cout<<"Failed to connect to remote rtmp server, retry in 1 second...."<<std::endl;
sleep(1);
continue;
}
}
// 跳过9字节长度
fseek(ctx.fp, 9, SEEK_SET);
// 跳过4字节长度
fseek(ctx.fp, 4, SEEK_CUR);
// 发送文件
while(!exit_program){
time_t cur = time(0);
// 如果发送过快,等待...
if((uint32_t)(cur - start) < (uint32_t)(stream_timestamp/1000)){
usleep(300);
continue;
}
std::cout<<"====================================================="<<std::endl;
std::cout<<"cur: "<<cur<<" start: "<<start<<" stream timestamp: "<<stream_timestamp<<std::endl;
std::cout<<"cur-start = "<<cur-start<<" stream_timestamp/1000 = "<<stream_timestamp/1000<<std::endl;
// 类型
int type = 0;
int datalength = 0;
uint32_t time = 0;
int streamid = 0;
// 从flv文件中读取数据包
std::cout<<"File position: "<<ftell(ctx.fp)<<std::endl;
if(!Read8(type, ctx.fp)){
std::cout<<"Failed to read type"<<std::endl;
break;
}
std::cout<<"type: "<<type<<std::endl;
if(!Read24(datalength, ctx.fp)){
std::cout<<"Failed to read datalength"<<std::endl;
break;
}
std::cout<<"datalength: "<<datalength<<std::endl;
if(!ReadTime(time, ctx.fp)){
std::cout<<"Failed to read time"<<std::endl;
break;
}
std::cout<<"time: "<<time<<std::endl;
time += ctx.timestamp_delta;
if(!Read24(streamid, ctx.fp)){
std::cout<<"Failed to read streamid"<<std::endl;
break;
}
std::cout<<"streamid: "<<streamid<<std::endl;
if(type != 0x08 && type != 0x09){
fseek(ctx.fp, datalength + 4, SEEK_CUR);
continue;
}
if(fread(ctx.packet->m_body, 1, datalength, ctx.fp) != datalength){
std::cout<<"Failed to read body"<<std::endl;
break;
}
ctx.packet->m_headerType = RTMP_PACKET_SIZE_MEDIUM;
ctx.packet->m_nTimeStamp = time;
ctx.packet->m_packetType = type;
ctx.packet->m_nBodySize = datalength;
if(!RTMP_IsConnected(ctx.rtmp)){
std::cout<<"RTMP disconnected."<<std::endl;
break;
}
// 发送RTMP数据包
if(!RTMP_SendPacket(ctx.rtmp, ctx.packet, 0)){
std::cout<<"RTMP packet sent."<<std::endl;
break;
}
int alldatalength = 0;
if(!Read32(alldatalength, ctx.fp)){
std::cout<<"Failed to read all data length"<<std::endl;
break;
}
stream_timestamp = time;
}
// 文件发送结束后,保存已发送timestamp
ctx.timestamp_delta = stream_timestamp;
}
std::cout<<"exit"<<std::endl;
return 0;
}