正在显示
1 个修改的文件
包含
290 行增加
和
290 行删除
trunk/research/librtmp/srs_ingest_flv.c
100755 → 100644
| 1 | -/* | ||
| 2 | -The MIT License (MIT) | ||
| 3 | - | ||
| 4 | -Copyright (c) 2013-2014 winlin | ||
| 5 | - | ||
| 6 | -Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | -this software and associated documentation files (the "Software"), to deal in | ||
| 8 | -the Software without restriction, including without limitation the rights to | ||
| 9 | -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 10 | -the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 11 | -subject to the following conditions: | ||
| 12 | - | ||
| 13 | -The above copyright notice and this permission notice shall be included in all | ||
| 14 | -copies or substantial portions of the Software. | ||
| 15 | - | ||
| 16 | -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 18 | -FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 19 | -COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 20 | -IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 21 | -CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 22 | -*/ | ||
| 23 | -/** | ||
| 24 | -gcc srs_ingest_flv.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_ingest_flv | ||
| 25 | -*/ | ||
| 26 | - | ||
| 27 | -#include <stdio.h> | ||
| 28 | -#include <stdlib.h> | ||
| 29 | -#include <unistd.h> | ||
| 30 | - | ||
| 31 | -#include <sys/types.h> | ||
| 32 | -#include <sys/stat.h> | ||
| 33 | -#include <fcntl.h> | ||
| 34 | - | ||
| 35 | -#include "../../objs/include/srs_librtmp.h" | ||
| 36 | -#include "srs_research_public.h" | ||
| 37 | - | ||
| 38 | -int proxy(int flv_fd, srs_rtmp_t ortmp); | ||
| 39 | -int connect_oc(srs_rtmp_t ortmp); | ||
| 40 | - | ||
| 41 | -int open_flv_file(char* in_flv_file); | ||
| 42 | -void close_flv_file(int flv_fd); | ||
| 43 | -int flv_open_ic(int flv_fd); | ||
| 44 | -int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size); | ||
| 45 | - | ||
| 46 | -int64_t re_create(); | ||
| 47 | -int64_t re_update(int64_t re, u_int32_t time); | ||
| 48 | - | ||
| 49 | -int main(int argc, char** argv) | ||
| 50 | -{ | ||
| 51 | - int ret = 0; | ||
| 52 | - | ||
| 53 | - // user option parse index. | ||
| 54 | - int opt = 0; | ||
| 55 | - // user options. | ||
| 56 | - char* in_flv_file; char* out_rtmp_url; | ||
| 57 | - // rtmp handler | ||
| 58 | - srs_rtmp_t ortmp; | ||
| 59 | - // flv handler | ||
| 60 | - int flv_fd; | ||
| 61 | - | ||
| 62 | - if (argc <= 2) { | ||
| 63 | - printf("ingest flv file and publish to RTMP server\n" | ||
| 64 | - "Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n" | ||
| 65 | - " in_flv_file input flv file, ingest from this file.\n" | ||
| 66 | - " out_rtmp_url output rtmp url, publish to this url.\n" | ||
| 67 | - "For example:\n" | ||
| 68 | - " %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/demo\n", | ||
| 69 | - argv[0]); | ||
| 70 | - ret = 1; | ||
| 71 | - exit(ret); | ||
| 72 | - return ret; | ||
| 73 | - } | ||
| 74 | - | ||
| 75 | - // parse options in FFMPEG format. | ||
| 76 | - while ((opt = getopt(argc, argv, "i:y:")) != -1) { | ||
| 77 | - switch (opt) { | ||
| 78 | - case 'i': | ||
| 79 | - in_flv_file = optarg; | ||
| 80 | - break; | ||
| 81 | - case 'y': | ||
| 82 | - out_rtmp_url = optarg; | ||
| 83 | - break; | ||
| 84 | - default: | ||
| 85 | - break; | ||
| 86 | - } | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - trace("ingest flv file and publish to RTMP server like FFMPEG."); | ||
| 90 | - trace("srs(simple-rtmp-server) client librtmp library."); | ||
| 91 | - trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); | ||
| 92 | - trace("input: %s", in_flv_file); | ||
| 93 | - trace("output: %s", out_rtmp_url); | ||
| 94 | - | ||
| 95 | - flv_fd = open_flv_file(in_flv_file); | ||
| 96 | - if (flv_fd <= 0) { | ||
| 97 | - ret = 2; | ||
| 98 | - trace("open flv file failed. ret=%d", ret); | ||
| 99 | - return ret; | ||
| 100 | - } | ||
| 101 | - | ||
| 102 | - ortmp = srs_rtmp_create(out_rtmp_url); | ||
| 103 | - | ||
| 104 | - ret = proxy(flv_fd, ortmp); | ||
| 105 | - trace("ingest flv to RTMP completed"); | ||
| 106 | - | ||
| 107 | - srs_rtmp_destroy(ortmp); | ||
| 108 | - close_flv_file(flv_fd); | ||
| 109 | - | ||
| 110 | - return ret; | ||
| 111 | -} | ||
| 112 | - | ||
| 113 | -int proxy(int flv_fd, srs_rtmp_t ortmp) | ||
| 114 | -{ | ||
| 115 | - int ret = 0; | ||
| 116 | - | ||
| 117 | - // packet data | ||
| 118 | - int type, size; | ||
| 119 | - u_int32_t timestamp = 0; | ||
| 120 | - char* data = NULL; | ||
| 121 | - // re | ||
| 122 | - int64_t re = re_create(); | ||
| 123 | - | ||
| 124 | - if ((ret = flv_open_ic(flv_fd)) != 0) { | ||
| 125 | - return ret; | ||
| 126 | - } | ||
| 127 | - if ((ret = connect_oc(ortmp)) != 0) { | ||
| 128 | - return ret; | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - trace("start ingest flv to RTMP stream"); | ||
| 132 | - for (;;) { | ||
| 133 | - if ((ret = flv_read_packet(flv_fd, &type, ×tamp, &data, &size)) != 0) { | ||
| 134 | - trace("irtmp get packet failed. ret=%d", ret); | ||
| 135 | - return ret; | ||
| 136 | - } | ||
| 137 | - verbose("irtmp got packet: type=%s, time=%d, size=%d", | ||
| 138 | - srs_type2string(type), timestamp, size); | ||
| 139 | - | ||
| 140 | - if ((ret = srs_write_packet(ortmp, type, timestamp, data, size)) != 0) { | ||
| 141 | - trace("irtmp get packet failed. ret=%d", ret); | ||
| 142 | - return ret; | ||
| 143 | - } | ||
| 144 | - verbose("ortmp sent packet: type=%s, time=%d, size=%d", | ||
| 145 | - srs_type2string(type), timestamp, size); | ||
| 146 | - | ||
| 147 | - re = re_update(re, timestamp); | ||
| 148 | - } | ||
| 149 | - | ||
| 150 | - return ret; | ||
| 151 | -} | ||
| 152 | - | ||
| 153 | -int connect_oc(srs_rtmp_t ortmp) | ||
| 154 | -{ | ||
| 155 | - int ret = 0; | ||
| 156 | - | ||
| 157 | - if ((ret = srs_simple_handshake(ortmp)) != 0) { | ||
| 158 | - trace("ortmp simple handshake failed. ret=%d", ret); | ||
| 159 | - return ret; | ||
| 160 | - } | ||
| 161 | - trace("ortmp simple handshake success"); | ||
| 162 | - | ||
| 163 | - if ((ret = srs_connect_app(ortmp)) != 0) { | ||
| 164 | - trace("ortmp connect vhost/app failed. ret=%d", ret); | ||
| 165 | - return ret; | ||
| 166 | - } | ||
| 167 | - trace("ortmp connect vhost/app success"); | ||
| 168 | - | ||
| 169 | - if ((ret = srs_publish_stream(ortmp)) != 0) { | ||
| 170 | - trace("ortmp publish stream failed. ret=%d", ret); | ||
| 171 | - return ret; | ||
| 172 | - } | ||
| 173 | - trace("ortmp publish stream success"); | ||
| 174 | - | ||
| 175 | - return ret; | ||
| 176 | -} | ||
| 177 | - | ||
| 178 | -int64_t re_create() | ||
| 179 | -{ | ||
| 180 | - return 0; | ||
| 181 | -} | ||
| 182 | -int64_t re_update(int64_t re, u_int32_t time) | ||
| 183 | -{ | ||
| 184 | - if (time - re > 500) { | ||
| 185 | - usleep((time - re) * 1000); | ||
| 186 | - return time; | ||
| 187 | - } | ||
| 188 | - | ||
| 189 | - return re; | ||
| 190 | -} | ||
| 191 | - | ||
| 192 | -int open_flv_file(char* in_flv_file) | ||
| 193 | -{ | ||
| 194 | - return open(in_flv_file, O_RDONLY); | ||
| 195 | -} | ||
| 196 | - | ||
| 197 | -void close_flv_file(int fd) | ||
| 198 | -{ | ||
| 199 | - if (fd > 0) { | ||
| 200 | - close(fd); | ||
| 201 | - } | ||
| 202 | -} | ||
| 203 | - | ||
| 204 | -int flv_open_ic(int flv_fd) | ||
| 205 | -{ | ||
| 206 | - int ret = 0; | ||
| 207 | - | ||
| 208 | - char h[13]; // 9+4 | ||
| 209 | - | ||
| 210 | - if (read(flv_fd, h, sizeof(h)) != sizeof(h)) { | ||
| 211 | - ret = -1; | ||
| 212 | - trace("read flv header failed. ret=%d", ret); | ||
| 213 | - return ret; | ||
| 214 | - } | ||
| 215 | - | ||
| 216 | - if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') { | ||
| 217 | - ret = -1; | ||
| 218 | - trace("input is not a flv file. ret=%d", ret); | ||
| 219 | - return ret; | ||
| 220 | - } | ||
| 221 | - | ||
| 222 | - return ret; | ||
| 223 | -} | ||
| 224 | - | ||
| 225 | -int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size) | ||
| 226 | -{ | ||
| 227 | - int ret = 0; | ||
| 228 | - | ||
| 229 | - char th[11]; // tag header | ||
| 230 | - char ts[4]; // tag size | ||
| 231 | - | ||
| 232 | - int32_t data_size = 0; | ||
| 233 | - u_int32_t time = 0; | ||
| 234 | - | ||
| 235 | - char* pp; | ||
| 236 | - | ||
| 237 | - // read tag header | ||
| 238 | - if (read(flv_fd, th, sizeof(th)) != sizeof(th)) { | ||
| 239 | - ret = -1; | ||
| 240 | - trace("read flv tag header failed. ret=%d", ret); | ||
| 241 | - return ret; | ||
| 242 | - } | ||
| 243 | - | ||
| 244 | - // Reserved UB [2] | ||
| 245 | - // Filter UB [1] | ||
| 246 | - // TagType UB [5] | ||
| 247 | - *type = (int)(th[0] & 0x1F); | ||
| 248 | - | ||
| 249 | - // DataSize UI24 | ||
| 250 | - pp = (char*)&data_size; | ||
| 251 | - pp[2] = th[1]; | ||
| 252 | - pp[1] = th[2]; | ||
| 253 | - pp[0] = th[3]; | ||
| 254 | - | ||
| 255 | - // Timestamp UI24 | ||
| 256 | - pp = (char*)&time; | ||
| 257 | - pp[2] = th[4]; | ||
| 258 | - pp[1] = th[5]; | ||
| 259 | - pp[0] = th[6]; | ||
| 260 | - | ||
| 261 | - // TimestampExtended UI8 | ||
| 262 | - pp[3] = th[7]; | ||
| 263 | - | ||
| 264 | - *timestamp = time; | ||
| 265 | - | ||
| 266 | - // check data size. | ||
| 267 | - if (data_size <= 0) { | ||
| 268 | - ret = -1; | ||
| 269 | - trace("invalid data size. size=%d, ret=%d", data_size, ret); | ||
| 270 | - return ret; | ||
| 271 | - } | ||
| 272 | - | ||
| 273 | - // read tag data. | ||
| 274 | - *size = data_size; | ||
| 275 | - *data = (char*)malloc(data_size); | ||
| 276 | - if (read(flv_fd, *data, data_size) != data_size) { | ||
| 277 | - ret = -1; | ||
| 278 | - trace("read flv tag data failed. size=%d, ret=%d", data_size, ret); | ||
| 279 | - return ret; | ||
| 280 | - } | ||
| 281 | - | ||
| 282 | - // ignore 4bytes tag size. | ||
| 283 | - if (read(flv_fd, ts, sizeof(ts)) != sizeof(ts)) { | ||
| 284 | - ret = -1; | ||
| 285 | - trace("read flv tag size failed. ret=%d", ret); | ||
| 286 | - return ret; | ||
| 287 | - } | ||
| 288 | - | ||
| 289 | - return ret; | ||
| 290 | -} | 1 | +/* |
| 2 | +The MIT License (MIT) | ||
| 3 | + | ||
| 4 | +Copyright (c) 2013-2014 winlin | ||
| 5 | + | ||
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| 7 | +this software and associated documentation files (the "Software"), to deal in | ||
| 8 | +the Software without restriction, including without limitation the rights to | ||
| 9 | +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| 10 | +the Software, and to permit persons to whom the Software is furnished to do so, | ||
| 11 | +subject to the following conditions: | ||
| 12 | + | ||
| 13 | +The above copyright notice and this permission notice shall be included in all | ||
| 14 | +copies or substantial portions of the Software. | ||
| 15 | + | ||
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| 18 | +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| 19 | +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| 20 | +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| 21 | +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| 22 | +*/ | ||
| 23 | +/** | ||
| 24 | +gcc srs_ingest_flv.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_ingest_flv | ||
| 25 | +*/ | ||
| 26 | + | ||
| 27 | +#include <stdio.h> | ||
| 28 | +#include <stdlib.h> | ||
| 29 | +#include <unistd.h> | ||
| 30 | + | ||
| 31 | +#include <sys/types.h> | ||
| 32 | +#include <sys/stat.h> | ||
| 33 | +#include <fcntl.h> | ||
| 34 | + | ||
| 35 | +#include "../../objs/include/srs_librtmp.h" | ||
| 36 | +#include "srs_research_public.h" | ||
| 37 | + | ||
| 38 | +int proxy(int flv_fd, srs_rtmp_t ortmp); | ||
| 39 | +int connect_oc(srs_rtmp_t ortmp); | ||
| 40 | + | ||
| 41 | +int open_flv_file(char* in_flv_file); | ||
| 42 | +void close_flv_file(int flv_fd); | ||
| 43 | +int flv_open_ic(int flv_fd); | ||
| 44 | +int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size); | ||
| 45 | + | ||
| 46 | +int64_t re_create(); | ||
| 47 | +int64_t re_update(int64_t re, u_int32_t time); | ||
| 48 | + | ||
| 49 | +int main(int argc, char** argv) | ||
| 50 | +{ | ||
| 51 | + int ret = 0; | ||
| 52 | + | ||
| 53 | + // user option parse index. | ||
| 54 | + int opt = 0; | ||
| 55 | + // user options. | ||
| 56 | + char* in_flv_file; char* out_rtmp_url; | ||
| 57 | + // rtmp handler | ||
| 58 | + srs_rtmp_t ortmp; | ||
| 59 | + // flv handler | ||
| 60 | + int flv_fd; | ||
| 61 | + | ||
| 62 | + if (argc <= 2) { | ||
| 63 | + printf("ingest flv file and publish to RTMP server\n" | ||
| 64 | + "Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n" | ||
| 65 | + " in_flv_file input flv file, ingest from this file.\n" | ||
| 66 | + " out_rtmp_url output rtmp url, publish to this url.\n" | ||
| 67 | + "For example:\n" | ||
| 68 | + " %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/demo\n", | ||
| 69 | + argv[0]); | ||
| 70 | + ret = 1; | ||
| 71 | + exit(ret); | ||
| 72 | + return ret; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + // parse options in FFMPEG format. | ||
| 76 | + while ((opt = getopt(argc, argv, "i:y:")) != -1) { | ||
| 77 | + switch (opt) { | ||
| 78 | + case 'i': | ||
| 79 | + in_flv_file = optarg; | ||
| 80 | + break; | ||
| 81 | + case 'y': | ||
| 82 | + out_rtmp_url = optarg; | ||
| 83 | + break; | ||
| 84 | + default: | ||
| 85 | + break; | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + trace("ingest flv file and publish to RTMP server like FFMPEG."); | ||
| 90 | + trace("srs(simple-rtmp-server) client librtmp library."); | ||
| 91 | + trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision()); | ||
| 92 | + trace("input: %s", in_flv_file); | ||
| 93 | + trace("output: %s", out_rtmp_url); | ||
| 94 | + | ||
| 95 | + flv_fd = open_flv_file(in_flv_file); | ||
| 96 | + if (flv_fd <= 0) { | ||
| 97 | + ret = 2; | ||
| 98 | + trace("open flv file failed. ret=%d", ret); | ||
| 99 | + return ret; | ||
| 100 | + } | ||
| 101 | + | ||
| 102 | + ortmp = srs_rtmp_create(out_rtmp_url); | ||
| 103 | + | ||
| 104 | + ret = proxy(flv_fd, ortmp); | ||
| 105 | + trace("ingest flv to RTMP completed"); | ||
| 106 | + | ||
| 107 | + srs_rtmp_destroy(ortmp); | ||
| 108 | + close_flv_file(flv_fd); | ||
| 109 | + | ||
| 110 | + return ret; | ||
| 111 | +} | ||
| 112 | + | ||
| 113 | +int proxy(int flv_fd, srs_rtmp_t ortmp) | ||
| 114 | +{ | ||
| 115 | + int ret = 0; | ||
| 116 | + | ||
| 117 | + // packet data | ||
| 118 | + int type, size; | ||
| 119 | + u_int32_t timestamp = 0; | ||
| 120 | + char* data = NULL; | ||
| 121 | + // re | ||
| 122 | + int64_t re = re_create(); | ||
| 123 | + | ||
| 124 | + if ((ret = flv_open_ic(flv_fd)) != 0) { | ||
| 125 | + return ret; | ||
| 126 | + } | ||
| 127 | + if ((ret = connect_oc(ortmp)) != 0) { | ||
| 128 | + return ret; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + trace("start ingest flv to RTMP stream"); | ||
| 132 | + for (;;) { | ||
| 133 | + if ((ret = flv_read_packet(flv_fd, &type, ×tamp, &data, &size)) != 0) { | ||
| 134 | + trace("irtmp get packet failed. ret=%d", ret); | ||
| 135 | + return ret; | ||
| 136 | + } | ||
| 137 | + verbose("irtmp got packet: type=%s, time=%d, size=%d", | ||
| 138 | + srs_type2string(type), timestamp, size); | ||
| 139 | + | ||
| 140 | + if ((ret = srs_write_packet(ortmp, type, timestamp, data, size)) != 0) { | ||
| 141 | + trace("irtmp get packet failed. ret=%d", ret); | ||
| 142 | + return ret; | ||
| 143 | + } | ||
| 144 | + verbose("ortmp sent packet: type=%s, time=%d, size=%d", | ||
| 145 | + srs_type2string(type), timestamp, size); | ||
| 146 | + | ||
| 147 | + re = re_update(re, timestamp); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + return ret; | ||
| 151 | +} | ||
| 152 | + | ||
| 153 | +int connect_oc(srs_rtmp_t ortmp) | ||
| 154 | +{ | ||
| 155 | + int ret = 0; | ||
| 156 | + | ||
| 157 | + if ((ret = srs_simple_handshake(ortmp)) != 0) { | ||
| 158 | + trace("ortmp simple handshake failed. ret=%d", ret); | ||
| 159 | + return ret; | ||
| 160 | + } | ||
| 161 | + trace("ortmp simple handshake success"); | ||
| 162 | + | ||
| 163 | + if ((ret = srs_connect_app(ortmp)) != 0) { | ||
| 164 | + trace("ortmp connect vhost/app failed. ret=%d", ret); | ||
| 165 | + return ret; | ||
| 166 | + } | ||
| 167 | + trace("ortmp connect vhost/app success"); | ||
| 168 | + | ||
| 169 | + if ((ret = srs_publish_stream(ortmp)) != 0) { | ||
| 170 | + trace("ortmp publish stream failed. ret=%d", ret); | ||
| 171 | + return ret; | ||
| 172 | + } | ||
| 173 | + trace("ortmp publish stream success"); | ||
| 174 | + | ||
| 175 | + return ret; | ||
| 176 | +} | ||
| 177 | + | ||
| 178 | +int64_t re_create() | ||
| 179 | +{ | ||
| 180 | + return 0; | ||
| 181 | +} | ||
| 182 | +int64_t re_update(int64_t re, u_int32_t time) | ||
| 183 | +{ | ||
| 184 | + if (time - re > 500) { | ||
| 185 | + usleep((time - re) * 1000); | ||
| 186 | + return time; | ||
| 187 | + } | ||
| 188 | + | ||
| 189 | + return re; | ||
| 190 | +} | ||
| 191 | + | ||
| 192 | +int open_flv_file(char* in_flv_file) | ||
| 193 | +{ | ||
| 194 | + return open(in_flv_file, O_RDONLY); | ||
| 195 | +} | ||
| 196 | + | ||
| 197 | +void close_flv_file(int fd) | ||
| 198 | +{ | ||
| 199 | + if (fd > 0) { | ||
| 200 | + close(fd); | ||
| 201 | + } | ||
| 202 | +} | ||
| 203 | + | ||
| 204 | +int flv_open_ic(int flv_fd) | ||
| 205 | +{ | ||
| 206 | + int ret = 0; | ||
| 207 | + | ||
| 208 | + char h[13]; // 9+4 | ||
| 209 | + | ||
| 210 | + if (read(flv_fd, h, sizeof(h)) != sizeof(h)) { | ||
| 211 | + ret = -1; | ||
| 212 | + trace("read flv header failed. ret=%d", ret); | ||
| 213 | + return ret; | ||
| 214 | + } | ||
| 215 | + | ||
| 216 | + if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') { | ||
| 217 | + ret = -1; | ||
| 218 | + trace("input is not a flv file. ret=%d", ret); | ||
| 219 | + return ret; | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + return ret; | ||
| 223 | +} | ||
| 224 | + | ||
| 225 | +int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size) | ||
| 226 | +{ | ||
| 227 | + int ret = 0; | ||
| 228 | + | ||
| 229 | + char th[11]; // tag header | ||
| 230 | + char ts[4]; // tag size | ||
| 231 | + | ||
| 232 | + int32_t data_size = 0; | ||
| 233 | + u_int32_t time = 0; | ||
| 234 | + | ||
| 235 | + char* pp; | ||
| 236 | + | ||
| 237 | + // read tag header | ||
| 238 | + if (read(flv_fd, th, sizeof(th)) != sizeof(th)) { | ||
| 239 | + ret = -1; | ||
| 240 | + trace("read flv tag header failed. ret=%d", ret); | ||
| 241 | + return ret; | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + // Reserved UB [2] | ||
| 245 | + // Filter UB [1] | ||
| 246 | + // TagType UB [5] | ||
| 247 | + *type = (int)(th[0] & 0x1F); | ||
| 248 | + | ||
| 249 | + // DataSize UI24 | ||
| 250 | + pp = (char*)&data_size; | ||
| 251 | + pp[2] = th[1]; | ||
| 252 | + pp[1] = th[2]; | ||
| 253 | + pp[0] = th[3]; | ||
| 254 | + | ||
| 255 | + // Timestamp UI24 | ||
| 256 | + pp = (char*)&time; | ||
| 257 | + pp[2] = th[4]; | ||
| 258 | + pp[1] = th[5]; | ||
| 259 | + pp[0] = th[6]; | ||
| 260 | + | ||
| 261 | + // TimestampExtended UI8 | ||
| 262 | + pp[3] = th[7]; | ||
| 263 | + | ||
| 264 | + *timestamp = time; | ||
| 265 | + | ||
| 266 | + // check data size. | ||
| 267 | + if (data_size <= 0) { | ||
| 268 | + ret = -1; | ||
| 269 | + trace("invalid data size. size=%d, ret=%d", data_size, ret); | ||
| 270 | + return ret; | ||
| 271 | + } | ||
| 272 | + | ||
| 273 | + // read tag data. | ||
| 274 | + *size = data_size; | ||
| 275 | + *data = (char*)malloc(data_size); | ||
| 276 | + if (read(flv_fd, *data, data_size) != data_size) { | ||
| 277 | + ret = -1; | ||
| 278 | + trace("read flv tag data failed. size=%d, ret=%d", data_size, ret); | ||
| 279 | + return ret; | ||
| 280 | + } | ||
| 281 | + | ||
| 282 | + // ignore 4bytes tag size. | ||
| 283 | + if (read(flv_fd, ts, sizeof(ts)) != sizeof(ts)) { | ||
| 284 | + ret = -1; | ||
| 285 | + trace("read flv tag size failed. ret=%d", ret); | ||
| 286 | + return ret; | ||
| 287 | + } | ||
| 288 | + | ||
| 289 | + return ret; | ||
| 290 | +} |
-
请 注册 或 登录 后发表评论