int process(const char* in_flv_file, const char* out_flv_file, srs_flv_t* pic, srs_flv_t* poc) { int ret = 0; srs_flv_t ic; srs_flv_t oc; // to adjust metadata. // the ic metadata end offset, the next tag start offset. // all oc metadata must adjust according to: // adjust = new_metadata_end_offset - metadata_end_offset int64_t metadata_end_offset = 0; // metadata srs_amf0_t amf0_name = NULL; srs_amf0_t amf0_data = NULL; srs_amf0_t filepositions = NULL; if ((ic = srs_flv_open_read(in_flv_file)) == NULL) { ret = 2; srs_human_trace("open input flv file failed. ret=%d", ret); return ret; } *pic = ic; if ((oc = srs_flv_open_write(out_flv_file)) == NULL) { ret = 2; srs_human_trace("open output flv file failed. ret=%d", ret); return ret; } *poc = oc; /** * we use two roundtrip to avoid the paddings of metadata, * to support large keyframes videos without padding fields. */ // build keyframes offset to metadata. if ((ret = build_keyframes(ic, &amf0_name, &amf0_data, &filepositions, &metadata_end_offset)) != 0) { return ret; } // inject the metadata to oc. if ((ret = do_inject_flv(ic, oc, amf0_name, amf0_data, filepositions, metadata_end_offset)) != 0) { return ret; } // TODO: FIXME: mem leak when error. srs_amf0_free(amf0_name); srs_amf0_free(amf0_data); return ret; }
int parse_metadata(char* data, int size, srs_amf0_t* pname, srs_amf0_t* pdata) { int ret = 0; int nparsed = 0; *pname = srs_amf0_parse(data, size, &nparsed); if (*pname == NULL || nparsed >= size) { srs_human_trace("invalid amf0 name data."); return -1; } *pdata = srs_amf0_parse(data + nparsed, size - nparsed, &nparsed); if (*pdata == NULL || nparsed > size) { srs_human_trace("invalid amf0 value data"); return -1; } return ret; }
void re_cleanup(int64_t re, int32_t starttime, u_int32_t time) { // for the last pulse, always sleep. // for the virtual live encoder long time publishing. int64_t now = srs_utils_time_ms(); int64_t diff = time - starttime - (now - re); if (diff > 0) { srs_human_trace("re_cleanup, diff=%d, start=%d, last=%d ms", (int) diff, starttime, time); usleep(diff * 1000); } }
int64_t re_create() { // if not very precise, we can directly use this as re. int64_t re = srs_utils_time_ms(); // use the starttime to get the deviation int64_t deviation = re - tools_main_entrance_startup_time; srs_human_trace("deviation is %d ms, pulse is %d ms", (int) (deviation), (int) (RE_PULSE_MS)); // so, we adjust time to max(0, deviation) // because the last pulse, we already sleeped int adjust = (int) (deviation); if (adjust > 0) { srs_human_trace("adjust re time for %d ms", adjust); re -= adjust; } else { srs_human_trace("no need to adjust re time"); } return re; }
int read_h264_frame(char* data, int size, char** pp, int* pnb_start_code, int fps, char** frame, int* frame_size, int* dts, int* pts) { char* p = *pp; // @remark, for this demo, to publish h264 raw file to SRS, // we search the h264 frame from the buffer which cached the h264 data. // please get h264 raw data from device, it always a encoded frame. if (!srs_h264_startswith_annexb(p, size - (p - data), pnb_start_code)) { srs_human_trace("h264 raw data invalid."); return -1; } // @see srs_write_h264_raw_frames // each frame prefixed h.264 annexb header, by N[00] 00 00 01, where N>=0, // for instance, frame = header(00 00 00 01) + payload(67 42 80 29 95 A0 14 01 6E 40) *frame = p; p += *pnb_start_code; for (;p < data + size; p++) { if (srs_h264_startswith_annexb(p, size - (p - data), NULL)) { break; } } *pp = p; *frame_size = p - *frame; if (*frame_size <= 0) { srs_human_trace("h264 raw data invalid."); return -1; } // @remark, please get the dts and pts from device, // we assume there is no B frame, and the fps can guess the fps and dts, // while the dts and pts must read from encode lib or device. *dts += 1000 / fps; *pts = *dts; return 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 do_proxy(srs_flv_t flv, srs_rtmp_t ortmp, int64_t re, int32_t *pstarttime, u_int32_t *ptimestamp) { int ret = 0; // packet data char type; int size; char *data = NULL; srs_human_trace("start ingest flv to RTMP stream"); for (; ;) { // tag header if ((ret = srs_flv_read_tag_header(flv, &type, &size, ptimestamp)) != 0) { if (srs_flv_is_eof(ret)) { srs_human_trace("parse completed."); return 0; } srs_human_trace("flv get packet failed. ret=%d", ret); return ret; } if (size <= 0) { srs_human_trace("invalid size=%d", size); break; } // TODO: FIXME: mem leak when error. data = (char *) malloc(size); if ((ret = srs_flv_read_tag_data(flv, data, size)) != 0) { return ret; } u_int32_t timestamp = *ptimestamp; if ((ret = srs_human_print_rtmp_packet(type, timestamp, data, size)) != 0) { srs_human_trace("print packet failed. ret=%d", ret); return ret; } if ((ret = srs_rtmp_write_packet(ortmp, type, *ptimestamp, data, size)) != 0) { srs_human_trace("irtmp get packet failed. ret=%d", ret); return ret; } if (*pstarttime < 0) { *pstarttime = *ptimestamp; } re_update(re, *pstarttime, *ptimestamp); } return ret; }
// https://github.com/winlinvip/simple-rtmp-server/issues/212#issuecomment-63648892 // allspace: // Take this file as an example: https://github.com/allspace/files/blob/master/srs.pcm // It's captured using SDK callback method. I have filtered out h264 video, so it's audio only now. // For every frame, it's a 8 bytes vendor specific header, following 160 bytes audio frame. // The header part can be ignored. int read_audio_frame(char* audio_raw, int file_size, char** pp, char** pdata, int* psize) { char* p = *pp; if (file_size - (p - audio_raw) < 168) { srs_human_trace("audio must be 160+8 bytes. left %d bytes.", (int)(file_size - (p - audio_raw))); return - 1; } // ignore 8bytes vendor specific header. p += 8; // 160 bytes audio frame *pdata = p; *psize = 160; // next frame. *pp = p + *psize; return 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; }
int build_keyframes(srs_flv_t ic, srs_amf0_t *pname, srs_amf0_t* pdata, srs_amf0_t* pfilepositions, int64_t* pmetadata_end_offset) { int ret = 0; // flv header char header[13]; // packet data char type; u_int32_t timestamp = 0; char* data = NULL; int32_t size; int64_t offset = 0; // metadata srs_amf0_t amf0_name = NULL; srs_amf0_t amf0_data = NULL; srs_amf0_t keyframes = NULL; srs_amf0_t filepositions = NULL; srs_amf0_t times = NULL; // reset to generate metadata srs_flv_lseek(ic, 0); if ((ret = srs_flv_read_header(ic, header)) != 0) { return ret; } srs_human_trace("build keyframe infos from flv"); for (;;) { offset = srs_flv_tellg(ic); // tag header if ((ret = srs_flv_read_tag_header(ic, &type, &size, ×tamp)) != 0) { if (srs_flv_is_eof(ret)) { srs_human_trace("parse completed."); return 0; } srs_human_trace("flv get packet failed. ret=%d", ret); return ret; } if (size <= 0) { srs_human_trace("invalid size=%d", size); return ret; } // TODO: FIXME: mem leak when error. data = (char*)malloc(size); if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) { return ret; } // data tag if (type == SRS_RTMP_TYPE_VIDEO) { if (!srs_flv_is_sequence_header(data, size) && srs_flv_is_keyframe(data, size)) { srs_amf0_strict_array_append(filepositions, srs_amf0_create_number(offset)); srs_amf0_strict_array_append(times, srs_amf0_create_number(((double)timestamp)/ 1000)); } } else if (type == SRS_RTMP_TYPE_SCRIPT) { *pmetadata_end_offset = srs_flv_tellg(ic); if ((ret = parse_metadata(data, size, &amf0_name, &amf0_data)) != 0) { return ret; } *pname = amf0_name; *pdata = amf0_data; if (srs_amf0_is_object(amf0_data)) { keyframes = srs_amf0_object_property(amf0_data, "keyframes"); if (keyframes == NULL) { keyframes = srs_amf0_create_object(); srs_amf0_object_property_set(amf0_data, "keyframes", keyframes); } // always clear the old keyframes. srs_amf0_object_clear(keyframes); *pfilepositions = filepositions = srs_amf0_create_strict_array(); srs_amf0_object_property_set(keyframes, "filepositions", filepositions); times = srs_amf0_create_strict_array(); srs_amf0_object_property_set(keyframes, "times", times); } else if (srs_amf0_is_ecma_array(amf0_data)) { keyframes = srs_amf0_ecma_array_property(amf0_data, "keyframes"); if (keyframes == NULL) { keyframes = srs_amf0_create_object(); srs_amf0_ecma_array_property_set(amf0_data, "keyframes", keyframes); } // always clear the old keyframes. srs_amf0_object_clear(keyframes); *pfilepositions = filepositions = srs_amf0_create_strict_array(); srs_amf0_object_property_set(keyframes, "filepositions", filepositions); times = srs_amf0_create_strict_array(); srs_amf0_object_property_set(keyframes, "times", times); } } free(data); } 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; 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, ×tamp, &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) { 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; }
int do_inject_flv(srs_flv_t ic, srs_flv_t oc, srs_amf0_t amf0_name, srs_amf0_t amf0_data, srs_amf0_t filepositions, int64_t metadata_end_offset) { int ret = 0; // flv header char header[13]; // packet data char type; u_int32_t timestamp = 0; char* data = NULL; int32_t size; // metadata srs_amf0_t fileposition = NULL; int amf0_name_size = 0; int i; // the metadata end offset, the next tag start offset. int64_t new_metadata_end_offset = 0; int offset_adjust = 0; // reset to write injected file srs_flv_lseek(ic, 0); if ((ret = srs_flv_read_header(ic, header)) != 0) { return ret; } if ((ret = srs_flv_write_header(oc, header)) != 0) { return ret; } // write metadata if (amf0_name != NULL && amf0_data != NULL) { amf0_name_size = srs_amf0_size(amf0_name); size = amf0_name_size + srs_amf0_size(amf0_data); // adjust all offset of keyframes. new_metadata_end_offset = srs_flv_tellg(oc) + srs_flv_size_tag(size); // the adjust is new offset sub the old offset of metadata end. offset_adjust = new_metadata_end_offset - metadata_end_offset; for (i = 0; i < srs_amf0_strict_array_property_count(filepositions); i++) { fileposition = srs_amf0_strict_array_property_at(filepositions, i); srs_amf0_set_number(fileposition, srs_amf0_to_number(fileposition) + offset_adjust); } data = (char*)malloc(size); memset(data, 0, size); if ((ret = srs_amf0_serialize(amf0_name, data, amf0_name_size)) != 0) { return ret; } if ((ret = srs_amf0_serialize(amf0_data, data + amf0_name_size, size - amf0_name_size)) != 0) { return ret; } if ((ret = srs_flv_write_tag(oc, SRS_RTMP_TYPE_SCRIPT, 0, data, size)) != 0) { return ret; } free(data); } srs_human_trace("build keyframe infos from flv"); for (;;) { // tag header if ((ret = srs_flv_read_tag_header(ic, &type, &size, ×tamp)) != 0) { if (srs_flv_is_eof(ret)) { srs_human_trace("parse completed."); return 0; } srs_human_trace("flv get packet failed. ret=%d", ret); return ret; } if (size <= 0) { srs_human_trace("invalid size=%d", size); break; } // TODO: FIXME: mem leak when error. data = (char*)malloc(size); if ((ret = srs_flv_read_tag_data(ic, data, size)) != 0) { return ret; } // data tag if (type == SRS_RTMP_TYPE_SCRIPT) { continue; } // copy if ((ret = srs_flv_write_tag(oc, type, timestamp, data, size)) != 0) { return ret; } free(data); } return ret; }
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) { 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; }