mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
fix bug of ingest for android, init the variable to 0
This commit is contained in:
parent
c530a8e5fc
commit
5c8aad4fe4
1 changed files with 290 additions and 286 deletions
576
trunk/research/librtmp/srs_ingest_flv.c
Normal file → Executable file
576
trunk/research/librtmp/srs_ingest_flv.c
Normal file → Executable file
|
@ -1,286 +1,290 @@
|
||||||
/*
|
/*
|
||||||
The MIT License (MIT)
|
The MIT License (MIT)
|
||||||
|
|
||||||
Copyright (c) 2013-2014 winlin
|
Copyright (c) 2013-2014 winlin
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
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
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
the Software without restriction, including without limitation the rights to
|
the Software without restriction, including without limitation the rights to
|
||||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
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,
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
subject to the following conditions:
|
subject to the following conditions:
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
The above copyright notice and this permission notice shall be included in all
|
||||||
copies or substantial portions of the Software.
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
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
|
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
|
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.
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
/**
|
/**
|
||||||
gcc srs_ingest_flv.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_ingest_flv
|
gcc srs_ingest_flv.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_ingest_flv
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "../../objs/include/srs_librtmp.h"
|
#include "../../objs/include/srs_librtmp.h"
|
||||||
#include "srs_research_public.h"
|
#include "srs_research_public.h"
|
||||||
|
|
||||||
int proxy(int flv_fd, srs_rtmp_t ortmp);
|
int proxy(int flv_fd, srs_rtmp_t ortmp);
|
||||||
int connect_oc(srs_rtmp_t ortmp);
|
int connect_oc(srs_rtmp_t ortmp);
|
||||||
|
|
||||||
int open_flv_file(char* in_flv_file);
|
int open_flv_file(char* in_flv_file);
|
||||||
void close_flv_file(int flv_fd);
|
void close_flv_file(int flv_fd);
|
||||||
int flv_open_ic(int flv_fd);
|
int flv_open_ic(int flv_fd);
|
||||||
int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size);
|
int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size);
|
||||||
|
|
||||||
int64_t re_create();
|
int64_t re_create();
|
||||||
int64_t re_update(int64_t re, u_int32_t time);
|
int64_t re_update(int64_t re, u_int32_t time);
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
// user option parse index.
|
// user option parse index.
|
||||||
int opt = 0;
|
int opt = 0;
|
||||||
// user options.
|
// user options.
|
||||||
char* in_flv_file; char* out_rtmp_url;
|
char* in_flv_file; char* out_rtmp_url;
|
||||||
// rtmp handler
|
// rtmp handler
|
||||||
srs_rtmp_t ortmp;
|
srs_rtmp_t ortmp;
|
||||||
// flv handler
|
// flv handler
|
||||||
int flv_fd;
|
int flv_fd;
|
||||||
|
|
||||||
if (argc <= 2) {
|
if (argc <= 2) {
|
||||||
printf("ingest flv file and publish to RTMP server\n"
|
printf("ingest flv file and publish to RTMP server\n"
|
||||||
"Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n"
|
"Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n"
|
||||||
" in_flv_file input flv file, ingest from this file.\n"
|
" in_flv_file input flv file, ingest from this file.\n"
|
||||||
" out_rtmp_url output rtmp url, publish to this url.\n"
|
" out_rtmp_url output rtmp url, publish to this url.\n"
|
||||||
"For example:\n"
|
"For example:\n"
|
||||||
" %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/demo\n",
|
" %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/demo\n",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
exit(ret);
|
exit(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse options in FFMPEG format.
|
// parse options in FFMPEG format.
|
||||||
while ((opt = getopt(argc, argv, "i:y:")) != -1) {
|
while ((opt = getopt(argc, argv, "i:y:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'i':
|
case 'i':
|
||||||
in_flv_file = optarg;
|
in_flv_file = optarg;
|
||||||
break;
|
break;
|
||||||
case 'y':
|
case 'y':
|
||||||
out_rtmp_url = optarg;
|
out_rtmp_url = optarg;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
trace("ingest flv file and publish to RTMP server like FFMPEG.");
|
trace("ingest flv file and publish to RTMP server like FFMPEG.");
|
||||||
trace("srs(simple-rtmp-server) client librtmp library.");
|
trace("srs(simple-rtmp-server) client librtmp library.");
|
||||||
trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision());
|
trace("version: %d.%d.%d", srs_version_major(), srs_version_minor(), srs_version_revision());
|
||||||
trace("input: %s", in_flv_file);
|
trace("input: %s", in_flv_file);
|
||||||
trace("output: %s", out_rtmp_url);
|
trace("output: %s", out_rtmp_url);
|
||||||
|
|
||||||
flv_fd = open_flv_file(in_flv_file);
|
flv_fd = open_flv_file(in_flv_file);
|
||||||
if (flv_fd <= 0) {
|
if (flv_fd <= 0) {
|
||||||
ret = 2;
|
ret = 2;
|
||||||
trace("open flv file failed. ret=%d", ret);
|
trace("open flv file failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ortmp = srs_rtmp_create(out_rtmp_url);
|
ortmp = srs_rtmp_create(out_rtmp_url);
|
||||||
|
|
||||||
ret = proxy(flv_fd, ortmp);
|
ret = proxy(flv_fd, ortmp);
|
||||||
trace("ingest flv to RTMP completed");
|
trace("ingest flv to RTMP completed");
|
||||||
|
|
||||||
srs_rtmp_destroy(ortmp);
|
srs_rtmp_destroy(ortmp);
|
||||||
close_flv_file(flv_fd);
|
close_flv_file(flv_fd);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int proxy(int flv_fd, srs_rtmp_t ortmp)
|
int proxy(int flv_fd, srs_rtmp_t ortmp)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
// packet data
|
// packet data
|
||||||
int type, size;
|
int type, size;
|
||||||
u_int32_t timestamp = 0;
|
u_int32_t timestamp = 0;
|
||||||
char* data = NULL;
|
char* data = NULL;
|
||||||
// re
|
// re
|
||||||
int64_t re = re_create();
|
int64_t re = re_create();
|
||||||
|
|
||||||
if ((ret = flv_open_ic(flv_fd)) != 0) {
|
if ((ret = flv_open_ic(flv_fd)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
if ((ret = connect_oc(ortmp)) != 0) {
|
if ((ret = connect_oc(ortmp)) != 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
trace("start ingest flv to RTMP stream");
|
trace("start ingest flv to RTMP stream");
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if ((ret = flv_read_packet(flv_fd, &type, ×tamp, &data, &size)) != 0) {
|
if ((ret = flv_read_packet(flv_fd, &type, ×tamp, &data, &size)) != 0) {
|
||||||
trace("irtmp get packet failed. ret=%d", ret);
|
trace("irtmp get packet failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
verbose("irtmp got packet: type=%s, time=%d, size=%d",
|
verbose("irtmp got packet: type=%s, time=%d, size=%d",
|
||||||
srs_type2string(type), timestamp, size);
|
srs_type2string(type), timestamp, size);
|
||||||
|
|
||||||
if ((ret = srs_write_packet(ortmp, type, timestamp, data, size)) != 0) {
|
if ((ret = srs_write_packet(ortmp, type, timestamp, data, size)) != 0) {
|
||||||
trace("irtmp get packet failed. ret=%d", ret);
|
trace("irtmp get packet failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
verbose("ortmp sent packet: type=%s, time=%d, size=%d",
|
verbose("ortmp sent packet: type=%s, time=%d, size=%d",
|
||||||
srs_type2string(type), timestamp, size);
|
srs_type2string(type), timestamp, size);
|
||||||
|
|
||||||
re = re_update(re, timestamp);
|
re = re_update(re, timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int connect_oc(srs_rtmp_t ortmp)
|
int connect_oc(srs_rtmp_t ortmp)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if ((ret = srs_simple_handshake(ortmp)) != 0) {
|
if ((ret = srs_simple_handshake(ortmp)) != 0) {
|
||||||
trace("ortmp simple handshake failed. ret=%d", ret);
|
trace("ortmp simple handshake failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
trace("ortmp simple handshake success");
|
trace("ortmp simple handshake success");
|
||||||
|
|
||||||
if ((ret = srs_connect_app(ortmp)) != 0) {
|
if ((ret = srs_connect_app(ortmp)) != 0) {
|
||||||
trace("ortmp connect vhost/app failed. ret=%d", ret);
|
trace("ortmp connect vhost/app failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
trace("ortmp connect vhost/app success");
|
trace("ortmp connect vhost/app success");
|
||||||
|
|
||||||
if ((ret = srs_publish_stream(ortmp)) != 0) {
|
if ((ret = srs_publish_stream(ortmp)) != 0) {
|
||||||
trace("ortmp publish stream failed. ret=%d", ret);
|
trace("ortmp publish stream failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
trace("ortmp publish stream success");
|
trace("ortmp publish stream success");
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t re_create()
|
int64_t re_create()
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int64_t re_update(int64_t re, u_int32_t time)
|
int64_t re_update(int64_t re, u_int32_t time)
|
||||||
{
|
{
|
||||||
if (time - re > 500) {
|
if (time - re > 500) {
|
||||||
usleep((time - re) * 1000);
|
usleep((time - re) * 1000);
|
||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
return re;
|
return re;
|
||||||
}
|
}
|
||||||
|
|
||||||
int open_flv_file(char* in_flv_file)
|
int open_flv_file(char* in_flv_file)
|
||||||
{
|
{
|
||||||
return open(in_flv_file, O_RDONLY);
|
return open(in_flv_file, O_RDONLY);
|
||||||
}
|
}
|
||||||
|
|
||||||
void close_flv_file(int fd)
|
void close_flv_file(int fd)
|
||||||
{
|
{
|
||||||
if (fd > 0) {
|
if (fd > 0) {
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int flv_open_ic(int flv_fd)
|
int flv_open_ic(int flv_fd)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
char h[13]; // 9+4
|
char h[13]; // 9+4
|
||||||
|
|
||||||
if (read(flv_fd, h, sizeof(h)) != sizeof(h)) {
|
if (read(flv_fd, h, sizeof(h)) != sizeof(h)) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
trace("read flv header failed. ret=%d", ret);
|
trace("read flv header failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') {
|
if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
trace("input is not a flv file. ret=%d", ret);
|
trace("input is not a flv file. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size)
|
int flv_read_packet(int flv_fd, int* type, u_int32_t* timestamp, char** data, int* size)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
char th[11]; // tag header
|
char th[11]; // tag header
|
||||||
char ts[4]; // tag size
|
char ts[4]; // tag size
|
||||||
|
|
||||||
u_int32_t data_size = 0;
|
int32_t data_size = 0;
|
||||||
u_int32_t time = 0;
|
u_int32_t time = 0;
|
||||||
|
|
||||||
char* pp;
|
char* pp;
|
||||||
|
|
||||||
// read tag header
|
// read tag header
|
||||||
if (read(flv_fd, th, sizeof(th)) != sizeof(th)) {
|
if (read(flv_fd, th, sizeof(th)) != sizeof(th)) {
|
||||||
ret = -1;
|
ret = -1;
|
||||||
trace("read flv tag header failed. ret=%d", ret);
|
trace("read flv tag header failed. ret=%d", ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reserved UB [2]
|
// Reserved UB [2]
|
||||||
// Filter UB [1]
|
// Filter UB [1]
|
||||||
// TagType UB [5]
|
// TagType UB [5]
|
||||||
*type = (int)(th[0] & 0x1F);
|
*type = (int)(th[0] & 0x1F);
|
||||||
|
|
||||||
// DataSize UI24
|
// DataSize UI24
|
||||||
pp = (char*)&data_size;
|
pp = (char*)&data_size;
|
||||||
pp[2] = th[1];
|
pp[2] = th[1];
|
||||||
pp[1] = th[2];
|
pp[1] = th[2];
|
||||||
pp[0] = th[3];
|
pp[0] = th[3];
|
||||||
|
|
||||||
// Timestamp UI24
|
// Timestamp UI24
|
||||||
pp = (char*)&time;
|
pp = (char*)&time;
|
||||||
pp[2] = th[4];
|
pp[2] = th[4];
|
||||||
pp[1] = th[5];
|
pp[1] = th[5];
|
||||||
pp[0] = th[6];
|
pp[0] = th[6];
|
||||||
|
|
||||||
// TimestampExtended UI8
|
// TimestampExtended UI8
|
||||||
pp[3] = th[7];
|
pp[3] = th[7];
|
||||||
|
|
||||||
*timestamp = time;
|
*timestamp = time;
|
||||||
|
|
||||||
if (data_size > 0) {
|
// check data size.
|
||||||
*size = data_size;
|
if (data_size <= 0) {
|
||||||
*data = (char*)malloc(data_size);
|
ret = -1;
|
||||||
|
trace("invalid data size. size=%d, ret=%d", data_size, ret);
|
||||||
// read tag data
|
return ret;
|
||||||
if (read(flv_fd, *data, data_size) != data_size) {
|
}
|
||||||
ret = -1;
|
|
||||||
trace("read flv tag data failed. size=%d, ret=%d", data_size, ret);
|
// read tag data.
|
||||||
return ret;
|
*size = data_size;
|
||||||
}
|
*data = (char*)malloc(data_size);
|
||||||
}
|
if (read(flv_fd, *data, data_size) != data_size) {
|
||||||
|
ret = -1;
|
||||||
// ignore 4bytes tag size.
|
trace("read flv tag data failed. size=%d, ret=%d", data_size, ret);
|
||||||
if (read(flv_fd, ts, sizeof(ts)) != sizeof(ts)) {
|
return ret;
|
||||||
ret = -1;
|
}
|
||||||
trace("read flv tag size failed. ret=%d", ret);
|
|
||||||
return ret;
|
// ignore 4bytes tag size.
|
||||||
}
|
if (read(flv_fd, ts, sizeof(ts)) != sizeof(ts)) {
|
||||||
|
ret = -1;
|
||||||
return ret;
|
trace("read flv tag size failed. ret=%d", ret);
|
||||||
}
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue