int main(int argc, char** argv)
{
    srs_rtmp_t rtmp;
    
    // packet data
    int type, size;
    u_int32_t timestamp = 0;
    char* data;
    
    if (argc <= 1) {
        printf("play stream on RTMP server\n"
            "Usage: %s <rtmp_url>\n"
            "   rtmp_url     RTMP stream url to play\n"
            "For example:\n"
            "   %s rtmp://127.0.0.1:1935/live/livestream\n",
            argv[0]);
        int ret = 1;
        exit(ret);
        return ret;
    }
    
    rtmp = srs_rtmp_create(argv[1]);
    
    printf("suck rtmp stream like rtmpdump\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());
    printf("rtmp url: %s\n", argv[1]);
    
    if (srs_simple_handshake(rtmp) != 0) {
        printf("simple handshake failed.\n");
        goto rtmp_destroy;
    }
    printf("simple handshake success\n");
    
    if (srs_connect_app(rtmp) != 0) {
        printf("connect vhost/app failed.\n");
        goto rtmp_destroy;
    }
    printf("connect vhost/app success\n");
    
    if (srs_play_stream(rtmp) != 0) {
        printf("play stream failed.\n");
        goto rtmp_destroy;
    }
    printf("play stream success\n");
    
    for (;;) {
        if (srs_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
            goto rtmp_destroy;
        }
        printf("got packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);
        
        free(data);
    }
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    
    return 0;
}
Beispiel #2
0
int main(int argc, char** argv)
{
    srs_rtmp_t rtmp;

    // packet data
    int type, size;
    u_int32_t timestamp = 0;
    char* data;

    printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\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());
    // warn it .
    // @see: https://github.com/simple-rtmp-server/srs/issues/126
    printf("\033[33m%s\033[0m",
           "[warning] it's only a sample to use librtmp. "
           "please never use it to publish and test forward/transcode/edge/HLS whatever. "
           "you should refer to this tool to use the srs-librtmp to publish the real media stream.");
    printf("\n");

    rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream");

    if (srs_simple_handshake(rtmp) != 0) {
        printf("simple handshake failed.\n");
        goto rtmp_destroy;
    }
    printf("simple handshake success\n");

    if (srs_connect_app(rtmp) != 0) {
        printf("connect vhost/app failed.\n");
        goto rtmp_destroy;
    }
    printf("connect vhost/app success\n");

    if (srs_publish_stream(rtmp) != 0) {
        printf("publish stream failed.\n");
        goto rtmp_destroy;
    }
    printf("publish stream success\n");

    for (;;) {
        type = SRS_RTMP_TYPE_VIDEO;
        timestamp += 40;
        size = 4096;
        data = (char*)malloc(4096);

        if (srs_write_packet(rtmp, type, timestamp, data, size) != 0) {
            goto rtmp_destroy;
        }
        printf("sent packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);

        usleep(40 * 1000);
    }

rtmp_destroy:
    srs_rtmp_destroy(rtmp);

    return 0;
}
int main(int argc, char** argv)
{
    srs_rtmp_t rtmp;
    
    // packet data
    int type, size;
    u_int32_t timestamp = 0;
    char* data;
    
    printf("suck rtmp stream like rtmpdump\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());
    
    if (argc > 1) {
        rtmp = srs_rtmp_create(argv[1]);
    } else {
        rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream");
    }
    
    if (1) {
        if (srs_complex_handshake(rtmp) != 0) {
            printf("complex handshake failed.\n");
            goto rtmp_destroy;
        }
        printf("complex handshake success\n");
    } else {
        if (srs_simple_handshake(rtmp) != 0) {
            printf("simple handshake failed.\n");
            goto rtmp_destroy;
        }
        printf("simple handshake success\n");
    }
    
    if (srs_connect_app(rtmp) != 0) {
        printf("connect vhost/app failed.\n");
        goto rtmp_destroy;
    }
    printf("connect vhost/app success\n");
    
    if (srs_play_stream(rtmp) != 0) {
        printf("play stream failed.\n");
        goto rtmp_destroy;
    }
    printf("play stream success\n");
    
    for (;;) {
        if (srs_read_packet(rtmp, &type, &timestamp, &data, &size) != 0) {
            goto rtmp_destroy;
        }
        printf("got packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);
        
        free(data);
    }
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    
    return 0;
}
Beispiel #4
0
int main(int argc, char** argv)
{
    int ret = 0;
    
    // user option parse index.
    int opt = 0;
    // user options.
    char* in_rtmp_url; char* out_rtmp_url;
    // rtmp handler
    srs_rtmp_t irtmp, ortmp;
    
    if (argc <= 2) {
        printf("ingest RTMP and publish to RTMP server\n"
            "Usage: %s <-i in_rtmp_url> <-y out_rtmp_url>\n"
            "   in_rtmp_url     input rtmp url, ingest from this url.\n"
            "   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",
            argv[0], argv[0]);
        ret = 1;
        exit(ret);
        return ret;
    }
    
    // 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;
        }
    }
    
    trace("ingest RTMP and publish to RTMP server like edge.");
    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_rtmp_url);
    trace("output: %s", out_rtmp_url);
    
    irtmp = srs_rtmp_create(in_rtmp_url);
    ortmp = srs_rtmp_create(out_rtmp_url);

    ret = proxy(irtmp, ortmp);
    trace("proxy completed");
    
    srs_rtmp_destroy(irtmp);
    srs_rtmp_destroy(ortmp);
    
    return ret;
}
int main(int argc, char** argv)
{
    srs_rtmp_t rtmp;
    
    // packet data
    int type, size;
    u_int32_t timestamp = 0;
    char* data;
    
    printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\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());
    
    rtmp = srs_rtmp_create("rtmp://127.0.0.1:1935/live/livestream");
    
    //if (srs_simple_handshake(rtmp) != 0) {
    if (srs_complex_handshake(rtmp) != 0) {
        printf("simple handshake failed.\n");
        goto rtmp_destroy;
    }
    printf("simple handshake success\n");
    
    if (srs_connect_app(rtmp) != 0) {
        printf("connect vhost/app failed.\n");
        goto rtmp_destroy;
    }
    printf("connect vhost/app success\n");
    
    if (srs_publish_stream(rtmp) != 0) {
        printf("publish stream failed.\n");
        goto rtmp_destroy;
    }
    printf("publish stream success\n");
    
    for (;;) {
        type = SRS_RTMP_TYPE_VIDEO;
        timestamp += 40;
        size = 4096;
        data = (char*)malloc(4096);
        
        if (srs_write_packet(rtmp, type, timestamp, data, size) != 0) {
            goto rtmp_destroy;
        }
        printf("sent packet: type=%s, time=%d, size=%d\n", srs_type2string(type), timestamp, size);
        
        usleep(40 * 1000);
    }
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    
    return 0;
}
Beispiel #6
0
int main(int argc, char** argv) 
{
    srs_rtmp_t rtmp;
    
    printf("Example for srs-librtmp\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());
    
    rtmp = srs_rtmp_create("rtmp://ossrs.net/live/livestream");
    srs_human_trace("create rtmp success");
    srs_rtmp_destroy(rtmp);
    
    return 0;
}
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;

    printf("inject flv file keyframes to metadata.\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());
    
    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"
            "   %s ../../doc/source.200kbps.768x320.flv injected.flv\n",
            argv[0], argv[0], argv[0]);
        exit(-1);
    }
    
    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);
    
    srs_human_trace("input:  %s", in_flv_file);
    srs_human_trace("output:  %s", out_flv_file);
    srs_human_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;
            srs_human_trace("file already injected.");
        } else {
            srs_human_trace("error, remove tmp file.");
        }
    } else {
        rename(tmp_file, out_flv_file);
        srs_human_trace("completed, rename to %s", out_flv_file);
    }
    
    free(tmp_file);
    
    return ret;
}
int main(int argc, char **argv) {
    int ret = 0;

    // main function
    tools_main_entrance_startup_time = srs_utils_time_ms();

    // user option parse index.
    int opt = 0;
    // user options.
    char *in_flv_file = NULL;
    char *out_rtmp_url = NULL;
    // rtmp handler
    srs_rtmp_t ortmp;
    // flv handler
    srs_flv_t flv;

    printf("ingest flv file and publish to RTMP server like FFMPEG.\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());

    if (argc <= 2) {
        printf("ingest flv file and publish to RTMP server\n"
                       "Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n"
                       "   in_flv_file     input flv file, ingest from this file.\n"
                       "   out_rtmp_url    output rtmp url, publish to this url.\n"
                       "For example:\n"
                       "   %s -i doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/livestream\n"
                       "   %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/livestream\n",
               argv[0], argv[0], argv[0]);
        exit(-1);
    }

    for (opt = 0; opt < argc; opt++) {
        srs_human_trace("argv[%d]=%s", opt, argv[opt]);
    }

    // fill the options for mac
    for (opt = 0; opt < argc - 1; opt++) {
        // ignore all options except -i and -y.
        char *p = argv[opt];

        // only accept -x
        if (p[0] != '-' || p[1] == 0 || p[2] != 0) {
            continue;
        }

        // parse according the option name.
        switch (p[1]) {
            case 'i':
                in_flv_file = argv[opt + 1];
                break;
            case 'y':
                out_rtmp_url = argv[opt + 1];
                break;
            default:
                break;
        }
    }

    if (!in_flv_file) {
        srs_human_trace("input invalid, use -i <input>");
        return -1;
    }
    if (!out_rtmp_url) {
        srs_human_trace("output invalid, use -y <output>");
        return -1;
    }

    srs_human_trace("input:  %s", in_flv_file);
    srs_human_trace("output: %s", out_rtmp_url);

    if ((flv = srs_flv_open_read(in_flv_file)) == NULL) {
        ret = 2;
        srs_human_trace("open flv file failed. ret=%d", ret);
        return ret;
    }

    ortmp = srs_rtmp_create(out_rtmp_url);

    ret = proxy(flv, ortmp);
    srs_human_trace("ingest flv to RTMP completed");

    srs_rtmp_destroy(ortmp);
    srs_flv_close(flv);

    return ret;
}
int main(int argc, char** argv)
{
    int ret = 0;
    
    // user option parse index.
    int opt = 0;
    // user options.
    char* in_flv_file; char* out_rtmp_url;
    // rtmp handler
    srs_rtmp_t ortmp;
    // flv handler
    int flv_fd;
    
    if (argc <= 2) {
        printf("ingest flv file and publish to RTMP server\n"
            "Usage: %s <-i in_flv_file> <-y out_rtmp_url>\n"
            "   in_flv_file     input flv file, ingest from this file.\n"
            "   out_rtmp_url    output rtmp url, publish to this url.\n"
            "For example:\n"
            "   %s -i ../../doc/source.200kbps.768x320.flv -y rtmp://127.0.0.1/live/demo\n",
            argv[0]);
        ret = 1;
        exit(ret);
        return ret;
    }
    
    // parse options in FFMPEG format.
    while ((opt = getopt(argc, argv, "i:y:")) != -1) {
        switch (opt) {
            case 'i':
                in_flv_file = optarg;
                break;
            case 'y':
                out_rtmp_url = optarg;
                break;
            default:
                break;
        }
    }
    
    trace("ingest flv file and publish to RTMP server like FFMPEG.");
    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_rtmp_url);

    flv_fd = open_flv_file(in_flv_file);
    if (flv_fd <= 0) {
        ret = 2;
        trace("open flv file failed. ret=%d", ret);
        return ret;
    }
    
    ortmp = srs_rtmp_create(out_rtmp_url);

    ret = proxy(flv_fd, ortmp);
    trace("ingest flv to RTMP completed");
    
    srs_rtmp_destroy(ortmp);
    close_flv_file(flv_fd);
    
    return ret;
}
int main(int argc, char **argv) {
    int ret = 0;
    srs_rtmp_t rtmp;

    // time
    int64_t time_startup = srs_utils_time_ms();
    int64_t time_dns_resolve = 0;
    int64_t time_socket_connect = 0;
    int64_t time_play_stream = 0;
    int64_t time_first_packet = 0;
    int64_t time_cleanup = 0;
    // delay = actual - expect time when quit.
    int delay = 0;
    // bytes
    int64_t bytes_nsend = 0;
    int time_duration = 0;
    int64_t bytes_nrecv = 0;

    // packet data
    int size;
    char type;
    char *data;
    u_int32_t timestamp;
    u_int32_t basetime = 0;

    // user options
    const char *rtmp_url = NULL;
    int duration = 0;
    int timeout = 0;

    printf("detect rtmp stream\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());

    if (argc <= 3) {
        printf("detect stream on RTMP server, print result to stderr.\n"
                       "Usage: %s <rtmp_url> <duration> <timeout>\n"
                       "   rtmp_url     RTMP stream url to play\n"
                       "   duration     how long to play, in seconds, stream time.\n"
                       "   timeout      how long to timeout, in seconds, system time.\n"
                       "For example:\n"
                       "   %s rtmp://127.0.0.1:1935/live/livestream 3 10\n",
               argv[0], argv[0]);
        exit(-1);
    }

    rtmp_url = argv[1];
    duration = atoi(argv[2]);
    timeout = atoi(argv[3]);

    srs_human_trace("rtmp url: %s", rtmp_url);
    srs_human_trace("duration: %ds, timeout:%ds", duration, timeout);

    if (duration <= 0 || timeout <= 0) {
        srs_human_trace("duration and timeout must be positive.");
        exit(-2);
    }

    rtmp = srs_rtmp_create(rtmp_url);

    if ((ret = __srs_rtmp_dns_resolve(rtmp)) != 0) {
        srs_human_trace("dns resolve failed. ret=%d", ret);
        goto rtmp_destroy;
    }
    srs_human_trace("dns resolve success");
    time_dns_resolve = srs_utils_time_ms();

    if ((ret = __srs_rtmp_connect_server(rtmp)) != 0) {
        srs_human_trace("socket connect failed. ret=%d", ret);
        goto rtmp_destroy;
    }
    srs_human_trace("socket connect success");
    time_socket_connect = srs_utils_time_ms();

    if ((ret = __srs_rtmp_do_simple_handshake(rtmp)) != 0) {
        srs_human_trace("do simple handshake failed. ret=%d", ret);
        goto rtmp_destroy;
    }
    srs_human_trace("do simple handshake success");

    if ((ret = srs_rtmp_connect_app(rtmp)) != 0) {
        srs_human_trace("connect vhost/app failed. ret=%d", ret);
        goto rtmp_destroy;
    }
    srs_human_trace("connect vhost/app success");

    if ((ret = srs_rtmp_play_stream(rtmp)) != 0) {
        srs_human_trace("play stream failed. ret=%d", ret);
        goto rtmp_destroy;
    }
    srs_human_trace("play stream success");
    time_play_stream = srs_utils_time_ms();

    for (; ;) {
        if ((ret = srs_rtmp_read_packet(rtmp, &type, &timestamp, &data, &size)) != 0) {
            srs_human_trace("read packet failed. ret=%d", ret);
            goto rtmp_destroy;
        }
        srs_human_trace("got packet: type=%s, time=%d, size=%d",
                        srs_human_flv_tag_type2string(type), timestamp, size);

        if (SRS_RTMP_TYPE_VIDEO == type || SRS_RTMP_TYPE_AUDIO == type) {
            if (time_first_packet <= 0) {
                time_first_packet = srs_utils_time_ms();
            }
            if (basetime <= 0) {
                basetime = timestamp;
            }
        }

        free(data);

        if (srs_utils_time_ms() - time_startup > timeout * 1000) {
            srs_human_trace("timeout, terminate.");
            goto rtmp_destroy;
        }

        if ((timestamp - basetime) > duration * 1000) {
            srs_human_trace("duration exceed, terminate.");
            goto rtmp_destroy;
        }
    }

    rtmp_destroy:
    bytes_nsend = srs_utils_send_bytes(rtmp);
    bytes_nrecv = srs_utils_recv_bytes(rtmp);

    srs_rtmp_destroy(rtmp);
    time_cleanup = srs_utils_time_ms();
    time_duration = (int) (time_cleanup - time_startup);

    // print result to stderr.
    fprintf(stderr, "{"
                    "\"%s\":%d, " //#0
                    "\"%s\":%d, " //#1
                    "\"%s\":%d, " // #2
                    "\"%s\":%d, " // #3
                    "\"%s\":%d, " // #4
                    "\"%s\":%d, " // #5
                    "\"%s\":%d, " // #6
                    "\"%s\":%d, " // #7
                    "\"%s\":%d, " // #8
                    "\"%s\":%d, " // #9
                    "\"%s\":%d, " // #10
                    "%s,%s,%s,%s}",
            "code", ret, //#0
            // total = dns + tcp_connect + start_play + first_packet + last_packet
            "total", time_duration, //#1
            "dns", (int) (time_dns_resolve - time_startup), //#2
            "tcp_connect", (int) (time_socket_connect - time_dns_resolve), //#3
            "start_play", (int) (time_play_stream - time_socket_connect), //#4
            "first_packet", (int) (time_first_packet - time_play_stream), //#5
            "last_packet", (int) (time_cleanup - time_first_packet), //#6
            "stream", (int) (timestamp - basetime), //#7
            // expect = time_cleanup - time_first_packet
            // actual = stream
            // delay = actual - expect
            "delay", (int) (timestamp - basetime - (time_cleanup - time_first_packet)), //#8
            "publish_kbps", (int) ((time_duration <= 0) ? 0 : (bytes_nsend * 8 / time_duration)), //#9
            "play_kbps", (int) ((time_duration <= 0) ? 0 : (bytes_nrecv * 8 / time_duration)), //#10
            // unit in ms.
            "\"unit\": \"ms\"",
            "\"remark0\": \"total = dns + tcp_connect + start_play + first_packet + last_packet\"",
            "\"remark1\": \"delay = stream - (time_cleanup - time_first_packet)\"",
            "\"remark2\": \"if code is not 0, user must ignore all data\""
    );

    srs_human_trace(" ");
    srs_human_trace("completed");

    return ret;
}
int main(int argc, char** argv)
{
    printf("publish raw h.264 as rtmp stream to server like FMLE/FFMPEG/Encoder\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());
    
    if (argc <= 2) {
        printf("Usage: %s <h264_raw_file> <rtmp_publish_url>\n", argv[0]);
        printf("     h264_raw_file: the h264 raw steam file.\n");
        printf("     rtmp_publish_url: the rtmp publish url.\n");
        printf("For example:\n");
        printf("     %s ./720p.h264.raw rtmp://127.0.0.1:1935/live/livestream\n", argv[0]);
        printf("Where the file: http://winlinvip.github.io/srs.release/3rdparty/720p.h264.raw\n");
        printf("See: https://github.com/winlinvip/simple-rtmp-server/issues/66\n");
        exit(-1);
    }
    
    const char* raw_file = argv[1];
    const char* rtmp_url = argv[2];
    srs_lib_trace("raw_file=%s, rtmp_url=%s", raw_file, rtmp_url);
    
    // open file
    int raw_fd = open(raw_file, O_RDONLY);
    if (raw_fd < 0) {
        srs_lib_trace("open h264 raw file %s failed.", raw_fd);
        goto rtmp_destroy;
    }
    
    off_t file_size = lseek(raw_fd, 0, SEEK_END);
    if (file_size <= 0) {
        srs_lib_trace("h264 raw file %s empty.", raw_file);
        goto rtmp_destroy;
    }
    srs_lib_trace("read entirely h264 raw file, size=%dKB", (int)(file_size / 1024));
    
    char* h264_raw = (char*)malloc(file_size);
    if (!h264_raw) {
        srs_lib_trace("alloc raw buffer failed for file %s.", raw_file);
        goto rtmp_destroy;
    }
    
    lseek(raw_fd, 0, SEEK_SET);
    ssize_t nb_read = 0;
    if ((nb_read = read(raw_fd, h264_raw, file_size)) != file_size) {
        srs_lib_trace("buffer %s failed, expect=%dKB, actual=%dKB.", 
            raw_file, (int)(file_size / 1024), (int)(nb_read / 1024));
        goto rtmp_destroy;
    }
    
    // connect rtmp context
    srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url);
    
    if (srs_simple_handshake(rtmp) != 0) {
        srs_lib_trace("simple handshake failed.");
        goto rtmp_destroy;
    }
    srs_lib_trace("simple handshake success");
    
    if (srs_connect_app(rtmp) != 0) {
        srs_lib_trace("connect vhost/app failed.");
        goto rtmp_destroy;
    }
    srs_lib_trace("connect vhost/app success");
    
    if (srs_publish_stream(rtmp) != 0) {
        srs_lib_trace("publish stream failed.");
        goto rtmp_destroy;
    }
    srs_lib_trace("publish stream success");
    
    u_int32_t dts = 0;
    u_int32_t pts = 0;
    // @remark, the dts and pts if read from device, for instance, the encode lib,
    // so we assume the fps is 25, and each h264 frame is 1000ms/25fps=40ms/f.
    u_int32_t fps = 25;
    // @remark, to decode the file.
    char* p = h264_raw;
    for (;p < h264_raw + file_size;) {
        // @remark, read a frame from file buffer.
        char* data = NULL;
        int size = 0;
        int nb_start_code = 0;
        if (read_h264_frame(h264_raw, file_size, &p, &nb_start_code, fps, 
            &data, &size, &dts, &pts) < 0
        ) {
            srs_lib_trace("read a frame from file buffer failed.");
            goto rtmp_destroy;
        }
        
        // send out the h264 packet over RTMP
        if (srs_write_h264_raw_frames(rtmp, data, size, dts, pts) != 0) {
            srs_lib_trace("send h264 raw data failed.");
            goto rtmp_destroy;
        }
        
        // 5bits, 7.3.1 NAL unit syntax, 
        // H.264-AVC-ISO_IEC_14496-10.pdf, page 44.
        u_int8_t nut = (char)data[nb_start_code] & 0x1f;
        srs_lib_trace("sent packet: type=%s, time=%d, size=%d, fps=%d, b[%d]=%#x(%s)", 
            srs_type2string(SRS_RTMP_TYPE_VIDEO), dts, size, fps, nb_start_code, (char)data[nb_start_code],
            (nut == 7? "SPS":(nut == 8? "PPS":(nut == 5? "I":(nut == 1? "P":"Unknown")))));
        
        // @remark, when use encode device, it not need to sleep.
        usleep(1000 / fps * 1000);
    }
    srs_lib_trace("h264 raw data completed");
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    close(raw_fd);
    free(h264_raw);
    
    return 0;
}
int main(int argc, char** argv)
{
    int ret = 0;
    srs_rtmp_t rtmp;
    
    // packet data
    int size;
    char type;
    char* data;
    u_int32_t timestamp;
    
    // srs debug info.
    char srs_server_ip[128];
    char srs_server[128];
    char srs_primary_authors[128];
    char srs_version[32];
    int srs_id = 0;
    int srs_pid = 0;
    // bandwidth test data.
    int64_t start_time = 0;
    int64_t end_time = 0;
    int play_kbps = 0;
    int publish_kbps = 0;
    int play_bytes = 0;
    int publish_bytes = 0;
    int play_duration = 0;
    int publish_duration = 0;
    
    // set to zero.
    srs_server_ip[0] = 0;
    srs_server[0] = 0;
    srs_primary_authors[0] = 0;
    srs_version[0] = 0;
    
    printf("RTMP bandwidth check/test with server.\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());
    
    if (argc <= 1) {
        printf("RTMP bandwidth check/test with server.\n"
            "Usage: %s <rtmp_url>\n"
            "   rtmp_url     RTMP bandwidth url to check. format: rtmp://server:port/app?key=xxx,vhost=xxx\n"
            "For example:\n"
            "   %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com\n"
            "   %s rtmp://127.0.0.1:1935/app?key=35c9b402c12a7246868752e2878f7e0e,vhost=bandcheck.srs.com>/dev/null\n"
            "@remark, output text to stdout, while json to stderr.\n",
            argv[0], argv[0], argv[0]);
        exit(-1);
    }
    
    rtmp = srs_rtmp_create2(argv[1]);
    
    srs_human_trace("bandwidth check/test url: %s", argv[1]);
    
    if ((ret = srs_rtmp_handshake(rtmp)) != 0) {
        srs_human_trace("simple handshake failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("simple handshake success");
    
    if ((ret = srs_rtmp_connect_app2(rtmp, 
        srs_server_ip, srs_server, srs_primary_authors, srs_version, &srs_id, &srs_pid)) != 0) {
        srs_human_trace("connect vhost/app failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("connect vhost/app success");
    
    if ((ret = srs_rtmp_bandwidth_check(rtmp, 
        &start_time, &end_time, &play_kbps, &publish_kbps,
        &play_bytes, &publish_bytes, &play_duration, &publish_duration)) != 0
    ) {
        srs_human_trace("bandwidth check/test failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("bandwidth check/test success");
    
    srs_human_trace("\n%s, %s\n"
        "%s, %s, srs_pid=%d, srs_id=%d\n"
        "duration: %dms(%d+%d)\n"
        "play: %dkbps\n"
        "publish: %dkbps", 
        (char*)srs_server, (char*)srs_primary_authors,
        (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id,
        (int)(end_time - start_time), play_duration, publish_duration,
        play_kbps, 
        publish_kbps);
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    
    fprintf(stderr, "{\"code\":%d,"
        "\"srs_server\":\"%s\", "
        "\"srs_primary_authors\":\"%s\", "
        "\"srs_server_ip\":\"%s\", "
        "\"srs_version\":\"%s\", "
        "\"srs_pid\":%d, "
        "\"srs_id\":%d, "
        "\"duration\":%d, "
        "\"play_duration\":%d, "
        "\"play_kbps\":%d, "
        "\"publish_kbps\":%d"
        "}",
        ret,
        (char*)srs_server, (char*)srs_primary_authors,
        (char*)srs_server_ip, (char*)srs_version, srs_pid, srs_id,
        (int)(end_time - start_time), play_duration, publish_duration,
        play_kbps, publish_kbps);
    
    srs_human_trace("");
    srs_human_trace("completed");
    
    return ret;
}
int main(int argc, char** argv)
{
    printf("publish raw audio as rtmp stream to server like FMLE/FFMPEG/Encoder\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());
    
    if (argc <= 2) {
        printf("Usage: %s <audio_raw_file> <rtmp_publish_url>\n", argv[0]);
        printf("     audio_raw_file: the audio raw steam file.\n");
        printf("     rtmp_publish_url: the rtmp publish url.\n");
        printf("For example:\n");
        printf("     %s ./audio.raw.pcm rtmp://127.0.0.1:1935/live/livestream\n", argv[0]);
        printf("Where the file: http://winlinvip.github.io/srs.release/3rdparty/audio.raw.pcm\n");
        printf("See: https://github.com/winlinvip/simple-rtmp-server/issues/212\n");
        exit(-1);
    }
    
    const char* raw_file = argv[1];
    const char* rtmp_url = argv[2];
    srs_human_trace("raw_file=%s, rtmp_url=%s", raw_file, rtmp_url);
    
    // open file
    int raw_fd = open(raw_file, O_RDONLY);
    if (raw_fd < 0) {
        srs_human_trace("open audio raw file %s failed.", raw_file);
        goto rtmp_destroy;
    }
    
    off_t file_size = lseek(raw_fd, 0, SEEK_END);
    if (file_size <= 0) {
        srs_human_trace("audio raw file %s empty.", raw_file);
        goto rtmp_destroy;
    }
    srs_human_trace("read entirely audio raw file, size=%dKB", (int)(file_size / 1024));
    
    char* audio_raw = (char*)malloc(file_size);
    if (!audio_raw) {
        srs_human_trace("alloc raw buffer failed for file %s.", raw_file);
        goto rtmp_destroy;
    }
    
    lseek(raw_fd, 0, SEEK_SET);
    ssize_t nb_read = 0;
    if ((nb_read = read(raw_fd, audio_raw, file_size)) != file_size) {
        srs_human_trace("buffer %s failed, expect=%dKB, actual=%dKB.", 
            raw_file, (int)(file_size / 1024), (int)(nb_read / 1024));
        goto rtmp_destroy;
    }
    
    // connect rtmp context
    srs_rtmp_t rtmp = srs_rtmp_create(rtmp_url);
    
    if (srs_rtmp_handshake(rtmp) != 0) {
        srs_human_trace("simple handshake failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("simple handshake success");
    
    if (srs_rtmp_connect_app(rtmp) != 0) {
        srs_human_trace("connect vhost/app failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("connect vhost/app success");
    
    if (srs_rtmp_publish_stream(rtmp) != 0) {
        srs_human_trace("publish stream failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("publish stream success");
    
    u_int32_t timestamp = 0;
    u_int32_t time_delta = 17;
    // @remark, to decode the file.
    char* p = audio_raw;
    for (;p < audio_raw + file_size;) {
        // @remark, read a frame from file buffer.
        char* data = NULL;
        int size = 0;
        if (read_audio_frame(audio_raw, file_size, &p, &data, &size) < 0) {
            srs_human_trace("read a frame from file buffer failed.");
            goto rtmp_destroy;
        }
        
        // 0 = Linear PCM, platform endian
        // 1 = ADPCM
        // 2 = MP3
        // 7 = G.711 A-law logarithmic PCM
        // 8 = G.711 mu-law logarithmic PCM
        // 10 = AAC
        // 11 = Speex
        char sound_format = 1;
        // 3 = 44 kHz
        char sound_rate = 3;
        // 1 = 16-bit samples
        char sound_size = 1;
        // 1 = Stereo sound
        char sound_type = 1;
        
        timestamp += time_delta;
        
        if (srs_audio_write_raw_frame(rtmp, 
            sound_format, sound_rate, sound_size, sound_type,
            data, size, timestamp) != 0
        ) {
            srs_human_trace("send audio raw data failed.");
            goto rtmp_destroy;
        }
        
        srs_human_trace("sent packet: type=%s, time=%d, size=%d, codec=%d, rate=%d, sample=%d, channel=%d", 
            srs_human_flv_tag_type2string(SRS_RTMP_TYPE_AUDIO), timestamp, size, sound_format, sound_rate, sound_size,
            sound_type);
        
        // @remark, when use encode device, it not need to sleep.
        usleep(1000 * time_delta);
    }
    
rtmp_destroy:
    srs_rtmp_destroy(rtmp);
    close(raw_fd);
    free(audio_raw);
    
    return 0;
}
Beispiel #14
0
int main(int argc, char **argv) {
    printf("publish rtmp stream to server like FMLE/FFMPEG/Encoder\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());

    if (argc <= 1) {
        printf("Usage: %s <rtmp_url>\n"
                       "   rtmp_url     RTMP stream url to publish\n"
                       "For example:\n"
                       "   %s rtmp://127.0.0.1:1935/live/livestream\n",
               argv[0], argv[0]);
        exit(-1);
    }

    // warn it .
    // @see: https://github.com/winlinvip/simple-rtmp-server/issues/126
    srs_human_trace("\033[33m%s\033[0m",
                    "[warning] it's only a sample to use librtmp. "
                            "please never use it to publish and test forward/transcode/edge/HLS whatever. "
                            "you should refer to this tool to use the srs-librtmp to publish the real media stream."
                            "read about: https://github.com/winlinvip/simple-rtmp-server/issues/126");
    srs_human_trace("rtmp url: %s", argv[1]);
    srs_rtmp_t rtmp = srs_rtmp_create(argv[1]);

    if (srs_rtmp_handshake(rtmp) != 0) {
        srs_human_trace("simple handshake failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("simple handshake success");

    if (srs_rtmp_connect_app(rtmp) != 0) {
        srs_human_trace("connect vhost/app failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("connect vhost/app success");

    if (srs_rtmp_publish_stream(rtmp) != 0) {
        srs_human_trace("publish stream failed.");
        goto rtmp_destroy;
    }
    srs_human_trace("publish stream success");

    u_int32_t timestamp = 0;
    for (; ;) {
        char type = SRS_RTMP_TYPE_VIDEO;
        int size = 4096;
        char *data = (char *) malloc(4096);

        timestamp += 40;

        if (srs_rtmp_write_packet(rtmp, type, timestamp, data, size) != 0) {
            goto rtmp_destroy;
        }
        srs_human_trace("sent packet: type=%s, time=%d, size=%d",
                        srs_human_flv_tag_type2string(type), timestamp, size);

        usleep(40 * 1000);
    }

    rtmp_destroy:
    srs_rtmp_destroy(rtmp);

    return 0;
}