srs_flv_injecter.c
9.3 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
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
/*
The MIT License (MIT)
Copyright (c) 2013-2014 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
gcc srs_flv_injecter.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_flv_injecter
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../../objs/include/srs_librtmp.h"
#include "srs_research_public.h"
#define ERROR_INJECTED 10000
int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc);
int inject_flv(srs_flv_t ic, srs_flv_t oc);
int main(int argc, char** argv)
{
int ret = 0;
// user options.
char* in_flv_file;
char* out_flv_file;
// flv handler
srs_flv_t ic = NULL;
srs_flv_t oc = NULL;
// temp variables.
int tmp_file_size = 0;
char* tmp_file;
if (argc <= 2) {
printf("inject flv file keyframes to metadata\n"
"Usage: %s in_flv_file out_flv_file\n"
" in_flv_file input flv file to inject.\n"
" out_flv_file the inject output file, can be in_flv_file.\n"
"For example:\n"
" %s ../../doc/source.200kbps.768x320.flv injected.flv\n",
argv[0]);
ret = 1;
exit(ret);
return ret;
}
in_flv_file = argv[1];
out_flv_file = argv[2];
tmp_file_size = strlen(out_flv_file) + strlen(".tmp") + 1;
tmp_file = (char*)malloc(tmp_file_size);
snprintf(tmp_file, tmp_file_size, "%s.tmp", out_flv_file);
trace("inject flv file keyframes to metadata.");
trace("srs(simple-rtmp-server) client librtmp library.");
trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision());
trace("input: %s", in_flv_file);
trace("output: %s", out_flv_file);
trace("tmp_file: %s", tmp_file);
ret = process(in_flv_file, tmp_file, &ic, &oc);
srs_flv_close(ic);
srs_flv_close(oc);
if (ret != 0) {
unlink(tmp_file);
if (ret == ERROR_INJECTED) {
ret = 0;
trace("file already injected.");
} else {
trace("error, remove tmp file.");
}
} else {
rename(tmp_file, out_flv_file);
trace("completed, rename to %s", out_flv_file);
}
free(tmp_file);
return ret;
}
int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc)
{
int ret = 0;
if ((*pic = srs_flv_open_read(in_flv_file)) == NULL) {
ret = 2;
trace("open input flv file failed. ret=%d", ret);
return ret;
}
if ((*poc = srs_flv_open_write(out_flv_file)) == NULL) {
ret = 2;
trace("open output flv file failed. ret=%d", ret);
return ret;
}
if ((ret = inject_flv(*pic, *poc)) != 0) {
return ret;
}
return ret;
}
int parse_metadata(char* data, int size, srs_amf0_t* pname, srs_amf0_t* pdata)
{
int ret = 0;
int nparsed = 0;
*pname = srs_amf0_parse(data, size, &nparsed);
if (*pname == NULL || nparsed >= size) {
trace("invalid amf0 name data.");
return -1;
}
*pdata = srs_amf0_parse(data + nparsed, size - nparsed, &nparsed);
if (*pdata == NULL || nparsed > size) {
trace("invalid amf0 value data");
return -1;
}
return ret;
}
int inject_flv(srs_flv_t ic, srs_flv_t oc)
{
int ret = 0;
// flv header
char header[13];
// packet data
char type;
u_int32_t timestamp = 0;
char* data = NULL;
int32_t size;
int64_t offset = 0;
// metadata
srs_amf0_t amf0_name = NULL;
int amf0_name_size = 0;
srs_amf0_t amf0_data = NULL;
srs_amf0_t keyframes = NULL;
srs_amf0_t filepositions = NULL;
srs_amf0_t times = NULL;
// reset to generate metadata
srs_flv_lseek(ic, 0);
if ((ret = srs_flv_read_header(ic, header)) != 0) {
return ret;
}
trace("build keyframe infos from flv");
for (;;) {
offset = srs_flv_tellg(ic);
// tag header
if ((ret = srs_flv_read_tag_header(ic, &type, &size, ×tamp)) != 0) {
if (srs_flv_is_eof(ret)) {
trace("parse completed.");
break;
}
trace("flv get packet failed. ret=%d", ret);
return ret;
}
if (size <= 0) {
trace("invalid size=%d", size);
break;
}
// TODO: FIXME: mem leak when error.
data = (char*)malloc(size);
if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) {
return ret;
}
// data tag
if (type == SRS_RTMP_TYPE_VIDEO) {
if (!srs_flv_is_sequence_header(data, size) && srs_flv_is_keyframe(data, size)) {
srs_amf0_strict_array_append(filepositions, srs_amf0_create_number(offset));
srs_amf0_strict_array_append(times, srs_amf0_create_number(((double)timestamp)/ 1000));
}
} else if (type == SRS_RTMP_TYPE_SCRIPT) {
if ((ret = parse_metadata(data, size, &amf0_name, &amf0_data)) != 0) {
return ret;
}
if (srs_amf0_is_object(amf0_data)) {
keyframes = srs_amf0_object_property(amf0_data, "keyframes");
if (keyframes != NULL) {
return 0;
}
keyframes = srs_amf0_create_ecma_array();
srs_amf0_object_property_set(amf0_data, "keyframes", keyframes);
filepositions = srs_amf0_create_strict_array();
srs_amf0_object_property_set(keyframes, "filepositions", filepositions);
times = srs_amf0_create_strict_array();
srs_amf0_object_property_set(keyframes, "times", times);
} else if (srs_amf0_is_ecma_array(amf0_data)) {
keyframes = srs_amf0_ecma_array_property(amf0_data, "keyframes");
if (keyframes != NULL) {
return 0;
}
keyframes = srs_amf0_create_ecma_array();
srs_amf0_ecma_array_property_set(amf0_data, "keyframes", keyframes);
filepositions = srs_amf0_create_strict_array();
srs_amf0_ecma_array_property_set(keyframes, "filepositions", filepositions);
times = srs_amf0_create_strict_array();
srs_amf0_ecma_array_property_set(keyframes, "times", times);
}
}
free(data);
}
// reset to write injected file
srs_flv_lseek(ic, 0);
if ((ret = srs_flv_read_header(ic, header)) != 0) {
return ret;
}
if ((ret = srs_flv_write_header(oc, header)) != 0) {
return ret;
}
// write metadata
if (amf0_name != NULL && amf0_data != NULL) {
amf0_name_size = srs_amf0_size(amf0_name);
size = amf0_name_size + srs_amf0_size(amf0_data);
data = (char*)malloc(size);
if ((ret = srs_amf0_serialize(amf0_name, data, amf0_name_size)) != 0) {
return ret;
}
if ((ret = srs_amf0_serialize(amf0_data, data + amf0_name_size, size - amf0_name_size)) != 0) {
return ret;
}
if ((ret = srs_flv_write_tag(oc, SRS_RTMP_TYPE_SCRIPT, 0, data, size)) != 0) {
return ret;
}
free(data);
}
trace("build keyframe infos from flv");
for (;;) {
// tag header
if ((ret = srs_flv_read_tag_header(ic, &type, &size, ×tamp)) != 0) {
if (srs_flv_is_eof(ret)) {
trace("parse completed.");
return 0;
}
trace("flv get packet failed. ret=%d", ret);
return ret;
}
if (size <= 0) {
trace("invalid size=%d", size);
break;
}
// TODO: FIXME: mem leak when error.
data = (char*)malloc(size);
if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) {
return ret;
}
// data tag
if (type == SRS_RTMP_TYPE_SCRIPT) {
continue;
}
// copy
if ((ret = srs_flv_write_tag(oc, type, timestamp, data, size)) != 0) {
return ret;
}
free(data);
}
srs_amf0_free(amf0_name);
srs_amf0_free(amf0_data);
return ret;
}