コード例 #1
0
ファイル: srs_ingest_rtmp.c プロジェクト: 1016135097/srs
int proxy(srs_rtmp_t irtmp, srs_rtmp_t ortmp)
{
    int ret = 0;
    
    // packet data
    int type, size;
    u_int32_t timestamp = 0;
    char* data = NULL;

    if ((ret = connect_ic(irtmp)) != 0) {
        return ret;
    }
    if ((ret = connect_oc(ortmp)) != 0) {
        return ret;
    }
    
    trace("start proxy RTMP stream");
    for (;;) {
        if ((ret = srs_read_packet(irtmp, &type, &timestamp, &data, &size)) != 0) {
            trace("irtmp get packet failed. ret=%d", ret);
            return ret;
        }
        verbose("irtmp got packet: type=%s, time=%d, size=%d", 
            srs_type2string(type), timestamp, size);
        
        if ((ret = srs_write_packet(ortmp, type, timestamp, data, size)) != 0) {
            trace("irtmp get packet failed. ret=%d", ret);
            return ret;
        }
        verbose("ortmp sent packet: type=%s, time=%d, size=%d", 
            srs_type2string(type), timestamp, size);
    }
    
    return ret;
}
コード例 #2
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;
}
コード例 #3
0
ファイル: srs_publish.c プロジェクト: CareWB/srs
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;
}
コード例 #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("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;
}
コード例 #5
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) {
    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;
}
コード例 #6
0
int proxy(int flv_fd, srs_rtmp_t ortmp)
{
    int ret = 0;
    
    // packet data
    int type, size;
    u_int32_t timestamp = 0;
    char* data = NULL;
    // re
    int64_t re = re_create();
    
    if ((ret = flv_open_ic(flv_fd)) != 0) {
        return ret;
    }
    if ((ret = connect_oc(ortmp)) != 0) {
        return ret;
    }
    
    trace("start ingest flv to RTMP stream");
    for (;;) {
        if ((ret = flv_read_packet(flv_fd, &type, &timestamp, &data, &size)) != 0) {
            trace("irtmp get packet failed. ret=%d", ret);
            return ret;
        }
        verbose("irtmp got packet: type=%s, time=%d, size=%d", 
            srs_type2string(type), timestamp, size);
        
        if ((ret = srs_write_packet(ortmp, type, timestamp, data, size)) != 0) {
            trace("irtmp get packet failed. ret=%d", ret);
            return ret;
        }
        verbose("ortmp sent packet: type=%s, time=%d, size=%d", 
            srs_type2string(type), timestamp, size);
        
        re = re_update(re, timestamp);
    }
    
    return ret;
}
コード例 #7
0
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;
}