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; }
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 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; }
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); } }
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; }