Exemplo n.º 1
0
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;
}
Exemplo n.º 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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
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());
    
    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;
}
Exemplo n.º 5
0
int connect_oc(srs_rtmp_t ortmp)
{
    int ret = 0;
    
    if ((ret = srs_simple_handshake(ortmp)) != 0) {
        trace("ortmp simple handshake failed. ret=%d", ret);
        return ret;
    }
    trace("ortmp simple handshake success");
    
    if ((ret = srs_connect_app(ortmp)) != 0) {
        trace("ortmp connect vhost/app failed. ret=%d", ret);
        return ret;
    }
    trace("ortmp connect vhost/app success");
    
    if ((ret = srs_publish_stream(ortmp)) != 0) {
        trace("ortmp publish stream failed. ret=%d", ret);
        return ret;
    }
    trace("ortmp publish stream success");
    
    return ret;
}
Exemplo n.º 6
0
int connect_ic(srs_rtmp_t irtmp)
{
    int ret = 0;
    
    if ((ret = srs_simple_handshake(irtmp)) != 0) {
        trace("irtmp simple handshake failed. ret=%d", ret);
        return ret;
    }
    trace("irtmp simple handshake success");
    
    if ((ret = srs_connect_app(irtmp)) != 0) {
        trace("irtmp connect vhost/app failed. ret=%d", ret);
        return ret;
    }
    trace("irtmp connect vhost/app success");
    
    if ((ret = srs_play_stream(irtmp)) != 0) {
        trace("irtmp play stream failed. ret=%d", ret);
        return ret;
    }
    trace("irtmp play stream success");
    
    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 type, size;
    u_int32_t timestamp = 0;
    char* data;
    
    // 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;
    
    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]);
        ret = 1;
        exit(ret);
        return ret;
    }
    
    rtmp = srs_rtmp_create2(argv[1]);
    
    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());
    printf("bandwidth check/test url: %s\n", argv[1]);
    
    if ((ret = srs_simple_handshake(rtmp)) != 0) {
        printf("simple handshake failed.\n");
        goto rtmp_destroy;
    }
    printf("simple handshake success\n");
    
    if ((ret = srs_connect_app2(rtmp, 
        srs_server_ip, srs_server, srs_primary_authors, srs_version, &srs_id, &srs_pid)) != 0) {
        printf("connect vhost/app failed.\n");
        goto rtmp_destroy;
    }
    printf("connect vhost/app success\n");
    
    if ((ret = srs_bandwidth_check(rtmp, 
        &start_time, &end_time, &play_kbps, &publish_kbps,
        &play_bytes, &publish_bytes, &play_duration, &publish_duration)) != 0
    ) {
        printf("bandwidth check/test failed.\n");
        goto rtmp_destroy;
    }
    printf("bandwidth check/test success\n");
    
    printf("\n%s, %s\n"
        "%s, %s, srs_pid=%d, srs_id=%d\n"
        "duration: %dms(%d+%d)\n"
        "play: %dkbps\n"
        "publish: %dkbps\n\n", 
        (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);
    
    printf("terminate with ret=%d\n\n", ret);
    
    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);
    return ret;
}