Blame view

trunk/research/librtmp/srs_ingest_rtmp.c 5.4 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
/*
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_ingest_rtmp.c ../../objs/lib/srs_librtmp.a -g -O0 -lstdc++ -o srs_ingest_rtmp
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "../../objs/include/srs_librtmp.h"
32 33 34 35 36

int connect_ic(srs_rtmp_t irtmp);
int connect_oc(srs_rtmp_t ortmp);
int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp);
37 38
int main(int argc, char** argv)
{
39
    int ret = 0;
40
    
41 42 43 44 45 46 47
    // user option parse index.
    int opt = 0;
    // user options.
    char* in_rtmp_url; char* out_rtmp_url;
    // rtmp handler
    srs_rtmp_t irtmp, ortmp;
    
48 49 50 51
    printf("ingest RTMP and publish to RTMP server like edge.\n");
    printf("srs(simple-rtmp-server) client librtmp library.\n");
    printf("version: %d.%d.%d\n", srs_version_major(), srs_version_minor(), srs_version_revision());
    
52 53
    if (argc <= 2) {
        printf("ingest RTMP and publish to RTMP server\n"
54
            "Usage: %s <-i in_rtmp_url> <-y out_rtmp_url>\n"
55
            "   in_rtmp_url     input rtmp url, ingest from this url.\n"
56 57 58
            "   out_rtmp_url    output rtmp url, publish to this url.\n"
            "For example:\n"
            "   %s -i rtmp://127.0.0.1/live/livestream -y rtmp://127.0.0.1/live/demo\n",
winlin authored
59
            argv[0], argv[0]);
60
        exit(-1);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    }
    
    // parse options in FFMPEG format.
    while ((opt = getopt(argc, argv, "i:y:")) != -1) {
        switch (opt) {
            case 'i':
                in_rtmp_url = optarg;
                break;
            case 'y':
                out_rtmp_url = optarg;
                break;
            default:
                break;
        }
    }
    
77 78
    srs_human_trace("input:  %s", in_rtmp_url);
    srs_human_trace("output: %s", out_rtmp_url);
79 80 81 82 83
    
    irtmp = srs_rtmp_create(in_rtmp_url);
    ortmp = srs_rtmp_create(out_rtmp_url);

    ret = proxy(irtmp, ortmp);
84
    srs_human_trace("proxy completed");
85 86 87 88 89 90 91 92 93 94 95 96
    
    srs_rtmp_destroy(irtmp);
    srs_rtmp_destroy(ortmp);
    
    return ret;
}

int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp)
{
    int ret = 0;
    
    // packet data
97 98 99 100
    int size;
    char type;
    char* data;
    u_int32_t timestamp;
101 102 103 104 105 106 107 108

    if ((ret = connect_ic(irtmp)) != 0) {
        return ret;
    }
    if ((ret = connect_oc(ortmp)) != 0) {
        return ret;
    }
    
109
    srs_human_trace("start proxy RTMP stream");
110
    for (;;) {
111
        if ((ret = srs_rtmp_read_packet(irtmp, &type, &timestamp, &data, &size)) != 0) {
112
            srs_human_trace("irtmp get packet failed. ret=%d", ret);
113 114
            return ret;
        }
115
        
116 117
        if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) != 0) {
            srs_human_trace("print packet failed. ret=%d", ret);
118 119
            return ret;
        }
120
        
121
        if ((ret = srs_rtmp_write_packet(ortmp, type, timestamp, data, size)) != 0) {
122
            srs_human_trace("irtmp get packet failed. ret=%d", ret);
123 124
            return ret;
        }
125 126
        srs_human_verbose("ortmp sent packet: type=%s, time=%d, size=%d", 
            srs_human_flv_tag_type2string(type), timestamp, size);
127 128
    }
    
129 130 131 132 133 134 135
    return ret;
}

int connect_ic(srs_rtmp_t irtmp)
{
    int ret = 0;
    
136
    if ((ret = srs_rtmp_handshake(irtmp)) != 0) {
137
        srs_human_trace("irtmp simple handshake failed. ret=%d", ret);
138 139
        return ret;
    }
140
    srs_human_trace("irtmp simple handshake success");
141
    
142
    if ((ret = srs_rtmp_connect_app(irtmp)) != 0) {
143
        srs_human_trace("irtmp connect vhost/app failed. ret=%d", ret);
144 145
        return ret;
    }
146
    srs_human_trace("irtmp connect vhost/app success");
147
    
148
    if ((ret = srs_rtmp_play_stream(irtmp)) != 0) {
149
        srs_human_trace("irtmp play stream failed. ret=%d", ret);
150 151
        return ret;
    }
152
    srs_human_trace("irtmp play stream success");
153 154 155 156 157 158 159 160
    
    return ret;
}

int connect_oc(srs_rtmp_t ortmp)
{
    int ret = 0;
    
161
    if ((ret = srs_rtmp_handshake(ortmp)) != 0) {
162
        srs_human_trace("ortmp simple handshake failed. ret=%d", ret);
163 164
        return ret;
    }
165
    srs_human_trace("ortmp simple handshake success");
166
    
167
    if ((ret = srs_rtmp_connect_app(ortmp)) != 0) {
168
        srs_human_trace("ortmp connect vhost/app failed. ret=%d", ret);
169 170
        return ret;
    }
171
    srs_human_trace("ortmp connect vhost/app success");
172
    
173
    if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) {
174
        srs_human_trace("ortmp publish stream failed. ret=%d", ret);
175 176
        return ret;
    }
177
    srs_human_trace("ortmp publish stream success");
178
    
179
    return ret;
180
}