Esempio n. 1
0
int connect_oc(srs_rtmp_t ortmp) {
    int ret = 0;

    if ((ret = srs_rtmp_handshake(ortmp)) != 0) {
        srs_human_trace("ortmp simple handshake failed. ret=%d", ret);
        return ret;
    }
    srs_human_trace("ortmp simple handshake success");

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

    if ((ret = srs_rtmp_publish_stream(ortmp)) != 0) {
        srs_human_trace("ortmp publish stream failed. ret=%d", ret);
        return ret;
    }
    srs_human_trace("ortmp publish stream success");

    return ret;
}
Esempio n. 2
0
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/simple-rtmp-server/srs/issues/66\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 h264 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("h264 raw file %s empty.", raw_file);
        goto rtmp_destroy;
    }
    srs_human_trace("read entirely h264 raw file, size=%dKB", (int)(file_size / 1024));
    
    char* h264_raw = (char*)malloc(file_size);
    if (!h264_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, h264_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");
    
    int dts = 0;
    int 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.
    int 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, (int)file_size, &p, &nb_start_code, fps, &data, &size, &dts, &pts) < 0) {
            srs_human_trace("read a frame from file buffer failed.");
            goto rtmp_destroy;
        }
        
        // send out the h264 packet over RTMP
        int ret = srs_h264_write_raw_frames(rtmp, data, size, dts, pts);
        if (ret != 0) {
            if (srs_h264_is_dvbsp_error(ret)) {
                srs_human_trace("ignore drop video error, code=%d", ret);
            } else if (srs_h264_is_duplicated_sps_error(ret)) {
                srs_human_trace("ignore duplicated sps, code=%d", ret);
            } else if (srs_h264_is_duplicated_pps_error(ret)) {
                srs_human_trace("ignore duplicated pps, code=%d", ret);
            } else {
                srs_human_trace("send h264 raw data failed. ret=%d", ret);
                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_human_trace("sent packet: type=%s, time=%d, size=%d, fps=%d, b[%d]=%#x(%s)", 
            srs_human_flv_tag_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_human_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)
{
    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;
}
Esempio n. 5
0
void PushEngine::run(){
    while(1){
        int64_t currentTime = srs_utils_time_ms();
        
        if(currentTime - m_timeTick > 1000){
            m_timeTick = currentTime;
#if 0
            for(std::list<OneStream*>::iterator it = m_streamList.begin(); it != m_streamList.end(); it++){
                m_logger.information("==> current publish [rtmp://%s/live/%s%d] size[%d]", (*it)->ipPort, \
                            (*it)->prefixName, (*it)->randNum, (*it)->receiveSize/1000);
                (*it)->receiveSize = 0;
            }
#endif
        }
        
        if(m_jobQueue.size() > 0){
            OneStreamSharePtr node = m_jobQueue.get();
            if(node.get() != NULL){
                node->setExpiredTime(currentTime);
                node->m_sendIndex = 0;
                m_streamList.push_back(node);
            }
            
            std::string url = node->getURL();
            node->rtmp = srs_rtmp_create(url.c_str());
            m_logger.information("begin publish to [%s].", url);

            if (srs_rtmp_handshake(node->rtmp) != 0) {
                srs_rtmp_destroy(node->rtmp);       // 析构rtmp
                m_streamEventQueue.put(StreamEventSharePtr(new StreamEvent(node, currentTime, HANDSHAKE_FAIL)));    // 发送握手失败事件
                m_logger.error("simple handshake to [%s] failed.", url);
            }
                
            if (srs_rtmp_connect_app(node->rtmp) != 0) {
                srs_rtmp_destroy(node->rtmp);       // 析构rtmp
                m_streamEventQueue.put(StreamEventSharePtr(new StreamEvent(node, currentTime, CONNECT_FAIL)));      // 发送连接失败事件
                m_logger.error("connect [%s] vhost/app failed.", url);
                continue;
            }

            if (srs_rtmp_publish_stream(node->rtmp) != 0) {
                srs_rtmp_destroy(node->rtmp);       // 析构rtmp
                m_streamEventQueue.put(StreamEventSharePtr(new StreamEvent(node, currentTime, PUBLIC_FAIL)));       // 发送public失败事件
                m_logger.error("publish stream [%s] failed.", url);
                continue;
            }

            m_streamEventQueue.put(StreamEventSharePtr(new StreamEvent(node, currentTime, START_STREAM)));          // 发送开始推流事件
        }
        
        //  发送当前时间片的帧到所有链接
        for(std::list<OneStreamSharePtr>::iterator it = m_streamList.begin(); it != m_streamList.end(); ){
            bool endThisStream = false;
            if((*it)->m_sendIndex == 0 || (*it)->m_sendIndex + 1 > m_flvFrame.size()){
                (*it)->m_baseTimestamp = currentTime;
                (*it)->m_sendIndex = 0;
            }
            
            if(((*it)->m_baseTimestamp + m_flvFrame[(*it)->m_sendIndex].timestamp) <= currentTime){
                Frame f = m_flvFrame[((*it)->m_sendIndex)++];
                char* data = (char*)malloc(f.size);
                memcpy(data, f.data, f.size);
                if (srs_rtmp_write_packet((*it)->rtmp, f.type, ((*it)->m_baseTimestamp + f.timestamp) % 10000000, data, f.size) != 0) {
                    endThisStream = true;
                    m_streamEventQueue.put(StreamEventSharePtr(new StreamEvent((*it), currentTime, PUSH_STREAM_FAIL)));       // 发送推流失败事件
                    m_logger.error("error to publish [%s].", (*it)->getURL());                    
                }
                (*it)->addReceiveSize(f.size);
            }
            
            // 推流时间到
            if(currentTime > (*it)->getExpiredTime()){
                endThisStream = true;
                m_streamEventQueue.put(StreamEventSharePtr(new StreamEvent((*it), currentTime, END_STREAM)));            // 发送推流结束事件
                m_logger.information("stop to publish [%s].", (*it)->getURL());
            }
            
            if(endThisStream){
                srs_rtmp_destroy((*it)->rtmp);      // 析构rtmp
                it = m_streamList.erase(it);        // 从链表中删除
                continue;
            }
            else{
                it++;
            }
        }

        usleep(10 * 1000);
    }
}
Esempio n. 6
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;
}