From 9802dc326e4a7d952d9087f38eab8f681d9f7433 Mon Sep 17 00:00:00 2001 From: winlin Date: Sat, 23 Sep 2017 22:12:33 +0800 Subject: [PATCH] For #913, source support complex error --- trunk/src/app/srs_app_conn.cpp | 7 +- trunk/src/app/srs_app_dvr.cpp | 383 +++++++------ trunk/src/app/srs_app_dvr.hpp | 76 +-- trunk/src/app/srs_app_edge.cpp | 139 ++--- trunk/src/app/srs_app_edge.hpp | 10 +- trunk/src/app/srs_app_forward.cpp | 109 ++-- trunk/src/app/srs_app_forward.hpp | 12 +- trunk/src/app/srs_app_http_stream.cpp | 27 +- trunk/src/app/srs_app_recv_thread.cpp | 6 +- trunk/src/app/srs_app_rtmp_conn.cpp | 479 ++++++---------- trunk/src/app/srs_app_rtmp_conn.hpp | 26 +- trunk/src/app/srs_app_rtsp.cpp | 6 +- trunk/src/app/srs_app_server.cpp | 7 +- trunk/src/app/srs_app_server.hpp | 2 +- trunk/src/app/srs_app_source.cpp | 621 +++++++++------------ trunk/src/app/srs_app_source.hpp | 68 +-- trunk/src/kernel/srs_kernel_codec.cpp | 386 +++++-------- trunk/src/kernel/srs_kernel_codec.hpp | 32 +- trunk/src/kernel/srs_kernel_error.cpp | 12 + trunk/src/kernel/srs_kernel_error.hpp | 5 +- trunk/src/kernel/srs_kernel_ts.cpp | 10 +- trunk/src/protocol/srs_protocol_format.cpp | 15 +- trunk/src/protocol/srs_protocol_format.hpp | 10 +- trunk/src/protocol/srs_rtsp_stack.cpp | 6 +- 24 files changed, 1023 insertions(+), 1431 deletions(-) diff --git a/trunk/src/app/srs_app_conn.cpp b/trunk/src/app/srs_app_conn.cpp index 37e9fdded..30199c3d4 100644 --- a/trunk/src/app/srs_app_conn.cpp +++ b/trunk/src/app/srs_app_conn.cpp @@ -112,11 +112,12 @@ srs_error_t SrsConnection::cycle() // TODO: FIXME: Only reset the error when client closed it. if (srs_is_client_gracefully_close(srs_error_code(err))) { srs_warn("client disconnect peer. ret=%d", srs_error_code(err)); - srs_freep(err); - return srs_success; + } else { + srs_error("connect error %s", srs_error_desc(err).c_str()); } - return srs_error_wrap(err, "cycle"); + srs_freep(err); + return srs_success; } int SrsConnection::srs_id() diff --git a/trunk/src/app/srs_app_dvr.cpp b/trunk/src/app/srs_app_dvr.cpp index 86211008e..b544d912c 100644 --- a/trunk/src/app/srs_app_dvr.cpp +++ b/trunk/src/app/srs_app_dvr.cpp @@ -81,30 +81,25 @@ SrsFragment* SrsDvrSegmenter::current() return fragment; } -int SrsDvrSegmenter::open() +srs_error_t SrsDvrSegmenter::open() { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // ignore when already open. if (fs->is_open()) { - return ret; + return err; } string path = generate_path(); if (srs_path_exists(path)) { - ret = ERROR_DVR_CANNOT_APPEND; - srs_error("DVR can't append to exists path=%s. ret=%d", path.c_str(), ret); - return ret; + return srs_error_new(ERROR_DVR_CANNOT_APPEND, "DVR can't append to exists path=%s", path.c_str()); } fragment->set_path(path); // create dir first. if ((err = fragment->create_dir()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "create dir"); } // create jitter. @@ -114,106 +109,95 @@ int SrsDvrSegmenter::open() // open file writer, in append or create mode. string tmp_dvr_file = fragment->tmppath(); if ((ret = fs->open(tmp_dvr_file)) != ERROR_SUCCESS) { - srs_error("open file stream for file %s failed. ret=%d", path.c_str(), ret); - return ret; + return srs_error_new(ret, "open file %s", path.c_str()); } // initialize the encoder. - if ((ret = open_encoder()) != ERROR_SUCCESS) { - srs_error("initialize enc by fs for file %s failed. ret=%d", path.c_str(), ret); - return ret; + if ((err = open_encoder()) != srs_success) { + return srs_error_wrap(err, "open encoder"); } srs_trace("dvr stream %s to file %s", req->stream.c_str(), path.c_str()); - - return ret; + return err; } -int SrsDvrSegmenter::write_metadata(SrsSharedPtrMessage* metadata) +srs_error_t SrsDvrSegmenter::write_metadata(SrsSharedPtrMessage* metadata) { return encode_metadata(metadata); } -int SrsDvrSegmenter::write_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) +srs_error_t SrsDvrSegmenter::write_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsSharedPtrMessage* audio = shared_audio->copy(); SrsAutoFree(SrsSharedPtrMessage, audio); - if ((jitter->correct(audio, jitter_algorithm)) != ERROR_SUCCESS) { - return ret; + if ((err = jitter->correct(audio, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "jitter"); } - if ((ret = on_update_duration(audio)) != ERROR_SUCCESS) { - return ret; + if ((err = on_update_duration(audio)) != srs_success) { + return srs_error_wrap(err, "update duration"); } - if ((ret = encode_audio(audio, format)) != ERROR_SUCCESS) { - return ret; + if ((err = encode_audio(audio, format)) != srs_success) { + return srs_error_wrap(err, "encode audio"); } - return ret; + return err; } -int SrsDvrSegmenter::write_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) +srs_error_t SrsDvrSegmenter::write_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsSharedPtrMessage* video = shared_video->copy(); SrsAutoFree(SrsSharedPtrMessage, video); - if ((jitter->correct(video, jitter_algorithm)) != ERROR_SUCCESS) { - return ret; + if ((err = jitter->correct(video, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "jitter"); } - if ((ret = encode_video(video, format)) != ERROR_SUCCESS) { - return ret; + if ((err = encode_video(video, format)) != srs_success) { + return srs_error_wrap(err, "encode video"); } - if ((ret = on_update_duration(video)) != ERROR_SUCCESS) { - return ret; + if ((err = on_update_duration(video)) != srs_success) { + return srs_error_wrap(err, "update duration"); } - return ret; + return err; } -int SrsDvrSegmenter::close() +srs_error_t SrsDvrSegmenter::close() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // ignore when already closed. if (!fs->is_open()) { - return ret; + return err; } // Close the encoder, then close the fs object. - if ((ret = close_encoder()) != ERROR_SUCCESS) { - return ret; + if ((err = close_encoder()) != srs_success) { + return srs_error_wrap(err, "close encoder"); } fs->close(); // when tmp flv file exists, reap it. if ((err = fragment->rename()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "rename fragment"); } // TODO: FIXME: the http callback is async, which will trigger thread switch, // so the on_video maybe invoked during the http callback, and error. if ((err = plan->on_reap_segment()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - srs_error("dvr: notify plan to reap segment failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "reap segment"); } - return ret; + return err; } string SrsDvrSegmenter::generate_path() @@ -235,13 +219,10 @@ string SrsDvrSegmenter::generate_path() return flv_path; } -int SrsDvrSegmenter::on_update_duration(SrsSharedPtrMessage* msg) +srs_error_t SrsDvrSegmenter::on_update_duration(SrsSharedPtrMessage* msg) { - int ret = ERROR_SUCCESS; - fragment->append(msg->timestamp); - - return ret; + return srs_success; } srs_error_t SrsDvrSegmenter::on_reload_vhost_dvr(std::string vhost) @@ -273,13 +254,14 @@ SrsDvrFlvSegmenter::~SrsDvrFlvSegmenter() srs_freep(enc); } -int SrsDvrFlvSegmenter::refresh_metadata() +srs_error_t SrsDvrFlvSegmenter::refresh_metadata() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // no duration or filesize specified. if (!duration_offset || !filesize_offset) { - return ret; + return err; } int64_t cur = fs->tellg(); @@ -290,7 +272,7 @@ int SrsDvrFlvSegmenter::refresh_metadata() SrsBuffer stream; if ((ret = stream.initialize(buf, SrsAmf0Size::number())) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init stream"); } // filesize to buf. @@ -299,13 +281,13 @@ int SrsDvrFlvSegmenter::refresh_metadata() stream.skip(-1 * stream.pos()); if ((ret = size->write(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write filesize"); } // update the flesize. fs->seek2(filesize_offset); if ((ret = fs->write(buf, SrsAmf0Size::number(), NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "update filesize"); } // duration to buf @@ -314,24 +296,25 @@ int SrsDvrFlvSegmenter::refresh_metadata() stream.skip(-1 * stream.pos()); if ((ret = dur->write(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write duration"); } // update the duration fs->seek2(duration_offset); if ((ret = fs->write(buf, SrsAmf0Size::number(), NULL)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "update duration"); } // reset the offset. fs->seek2(cur); - return ret; + return err; } -int SrsDvrFlvSegmenter::open_encoder() +srs_error_t SrsDvrFlvSegmenter::open_encoder() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; has_keyframe = false; @@ -343,42 +326,42 @@ int SrsDvrFlvSegmenter::open_encoder() enc = new SrsFlvTransmuxer(); if ((ret = enc->initialize(fs)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init encoder"); } // write the flv header to writer. if ((ret = enc->write_header()) != ERROR_SUCCESS) { - srs_error("write flv header failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "write flv header"); } - return ret; + return err; } -int SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) +srs_error_t SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // Ignore when metadata already written. if (duration_offset || filesize_offset) { - return ret; + return err; } SrsBuffer stream; if ((ret = stream.initialize(metadata->payload, metadata->size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init stream"); } SrsAmf0Any* name = SrsAmf0Any::str(); SrsAutoFree(SrsAmf0Any, name); if ((ret = name->read(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read name"); } SrsAmf0Object* obj = SrsAmf0Any::object(); SrsAutoFree(SrsAmf0Object, obj); if ((ret = obj->read(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read object"); } // remove duration and filesize. @@ -401,39 +384,41 @@ int SrsDvrFlvSegmenter::encode_metadata(SrsSharedPtrMessage* metadata) // convert metadata to bytes. if ((ret = stream.initialize(payload, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init stream"); } if ((ret = name->write(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write name"); } if ((ret = obj->write(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write object"); } // to flv file. if ((ret = enc->write_metadata(18, payload, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write metadata"); } - return ret; + return err; } -int SrsDvrFlvSegmenter::encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) +srs_error_t SrsDvrFlvSegmenter::encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; char* payload = audio->payload; int size = audio->size; if ((ret = enc->write_audio(audio->timestamp, payload, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write audio"); } - return ret; + return err; } -int SrsDvrFlvSegmenter::encode_video(SrsSharedPtrMessage* video, SrsFormat* format) +srs_error_t SrsDvrFlvSegmenter::encode_video(SrsSharedPtrMessage* video, SrsFormat* format) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; char* payload = video->payload; int size = video->size; @@ -446,21 +431,18 @@ int SrsDvrFlvSegmenter::encode_video(SrsSharedPtrMessage* video, SrsFormat* form // accept the sequence header here. // when got no keyframe, ignore when should wait keyframe. - if (!has_keyframe && !sh) { - if (wait_keyframe) { - srs_info("dvr: ignore when wait keyframe."); - return ret; - } + if (!has_keyframe && !sh && wait_keyframe) { + return err; } if ((ret = enc->write_video(video->timestamp, payload, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "write video"); } - return ret; + return err; } -int SrsDvrFlvSegmenter::close_encoder() +srs_error_t SrsDvrFlvSegmenter::close_encoder() { return refresh_metadata(); } @@ -475,32 +457,36 @@ SrsDvrMp4Segmenter::~SrsDvrMp4Segmenter() srs_freep(enc); } -int SrsDvrMp4Segmenter::refresh_metadata() +srs_error_t SrsDvrMp4Segmenter::refresh_metadata() { - return ERROR_SUCCESS; + return srs_success; } -int SrsDvrMp4Segmenter::open_encoder() +srs_error_t SrsDvrMp4Segmenter::open_encoder() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; srs_freep(enc); enc = new SrsMp4Encoder(); if ((ret = enc->initialize(fs)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init encoder"); } - return ret; + return err; } -int SrsDvrMp4Segmenter::encode_metadata(SrsSharedPtrMessage* /*metadata*/) +srs_error_t SrsDvrMp4Segmenter::encode_metadata(SrsSharedPtrMessage* /*metadata*/) { - return ERROR_SUCCESS; + return srs_success; } -int SrsDvrMp4Segmenter::encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) +srs_error_t SrsDvrMp4Segmenter::encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) { + int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; + SrsAudioCodecId sound_format = format->acodec->id; SrsAudioSampleRate sound_rate = format->acodec->sound_rate; SrsAudioSampleBits sound_size = format->acodec->sound_size; @@ -518,11 +504,18 @@ int SrsDvrMp4Segmenter::encode_audio(SrsSharedPtrMessage* audio, SrsFormat* form uint32_t nb_sample = (uint32_t)format->nb_raw; uint32_t dts = (uint32_t)audio->timestamp; - return enc->write_sample(SrsMp4HandlerTypeSOUN, 0x00, ct, dts, dts, sample, nb_sample); + if ((ret = enc->write_sample(SrsMp4HandlerTypeSOUN, 0x00, ct, dts, dts, sample, nb_sample)) != ERROR_SUCCESS) { + return srs_error_new(ret, "write sample"); + } + + return err; } -int SrsDvrMp4Segmenter::encode_video(SrsSharedPtrMessage* video, SrsFormat* format) +srs_error_t SrsDvrMp4Segmenter::encode_video(SrsSharedPtrMessage* video, SrsFormat* format) { + int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; + SrsVideoAvcFrameType frame_type = format->video->frame_type; SrsVideoCodecId codec_id = format->vcodec->id; @@ -538,18 +531,23 @@ int SrsDvrMp4Segmenter::encode_video(SrsSharedPtrMessage* video, SrsFormat* form uint8_t* sample = (uint8_t*)format->raw; uint32_t nb_sample = (uint32_t)format->nb_raw; - return enc->write_sample(SrsMp4HandlerTypeVIDE, frame_type, ct, dts, pts, sample, nb_sample); + if ((ret = enc->write_sample(SrsMp4HandlerTypeVIDE, frame_type, ct, dts, pts, sample, nb_sample)) != ERROR_SUCCESS) { + return srs_error_new(ret, "write sample"); + } + + return err; } -int SrsDvrMp4Segmenter::close_encoder() +srs_error_t SrsDvrMp4Segmenter::close_encoder() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if ((ret = enc->flush()) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "flush encoder"); } - return ret; + return err; } SrsDvrAsyncCallOnDvr::SrsDvrAsyncCallOnDvr(int c, SrsRequest* r, string p) @@ -579,13 +577,9 @@ srs_error_t SrsDvrAsyncCallOnDvr::call() if (true) { SrsConfDirective* conf = _srs_config->get_vhost_on_dvr(req->vhost); - - if (!conf) { - srs_info("ignore the empty http callback: on_dvr"); - return err; + if (conf) { + hooks = conf->args; } - - hooks = conf->args; } for (int i = 0; i < (int)hooks.size(); i++) { @@ -639,45 +633,45 @@ srs_error_t SrsDvrPlan::initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsReque return err; } -int SrsDvrPlan::on_meta_data(SrsSharedPtrMessage* shared_metadata) +srs_error_t SrsDvrPlan::on_meta_data(SrsSharedPtrMessage* shared_metadata) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!dvr_enabled) { - return ret; + return err; } return segment->write_metadata(shared_metadata); } -int SrsDvrPlan::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) +srs_error_t SrsDvrPlan::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!dvr_enabled) { - return ret; + return err; } - if ((ret = segment->write_audio(shared_audio, format)) != ERROR_SUCCESS) { - return ret; + if ((err = segment->write_audio(shared_audio, format)) != srs_success) { + return srs_error_wrap(err, "write audio"); } - return ret; + return err; } -int SrsDvrPlan::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) +srs_error_t SrsDvrPlan::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!dvr_enabled) { - return ret; + return err; } - if ((ret = segment->write_video(shared_video, format)) != ERROR_SUCCESS) { - return ret; + if ((err = segment->write_video(shared_video, format)) != srs_success) { + return srs_error_wrap(err, "write video"); } - return ret; + return err; } srs_error_t SrsDvrPlan::on_reap_segment() @@ -719,30 +713,30 @@ SrsDvrSessionPlan::~SrsDvrSessionPlan() { } -int SrsDvrSessionPlan::on_publish() +srs_error_t SrsDvrSessionPlan::on_publish() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // support multiple publish. if (dvr_enabled) { - return ret; + return err; } if (!_srs_config->get_dvr_enabled(req->vhost)) { - return ret; + return err; } - if ((ret = segment->close()) != ERROR_SUCCESS) { - return ret; + if ((err = segment->close()) != srs_success) { + return srs_error_wrap(err, "close segment"); } - if ((ret = segment->open()) != ERROR_SUCCESS) { - return ret; + if ((err = segment->open()) != srs_success) { + return srs_error_wrap(err, "open segment"); } dvr_enabled = true; - return ret; + return err; } void SrsDvrSessionPlan::on_unpublish() @@ -753,9 +747,9 @@ void SrsDvrSessionPlan::on_unpublish() } // ignore error. - int ret = segment->close(); - if (ret != ERROR_SUCCESS) { - srs_warn("ignore flv close error. ret=%d", ret); + srs_error_t err = segment->close(); + if (err != srs_success) { + srs_warn("ignore flv close error %s", srs_error_desc(err).c_str()); } dvr_enabled = false; @@ -788,111 +782,109 @@ srs_error_t SrsDvrSegmentPlan::initialize(SrsOriginHub* h, SrsDvrSegmenter* s, S return srs_success; } -int SrsDvrSegmentPlan::on_publish() +srs_error_t SrsDvrSegmentPlan::on_publish() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // support multiple publish. if (dvr_enabled) { - return ret; + return err; } if (!_srs_config->get_dvr_enabled(req->vhost)) { - return ret; + return err; } - if ((ret = segment->close()) != ERROR_SUCCESS) { - return ret; + if ((err = segment->close()) != srs_success) { + return srs_error_wrap(err, "segment close"); } - if ((ret = segment->open()) != ERROR_SUCCESS) { - return ret; + if ((err = segment->open()) != srs_success) { + return srs_error_wrap(err, "segment open"); } dvr_enabled = true; - return ret; + return err; } void SrsDvrSegmentPlan::on_unpublish() { } -int SrsDvrSegmentPlan::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) +srs_error_t SrsDvrSegmentPlan::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = update_duration(shared_audio)) != ERROR_SUCCESS) { - return ret; + if ((err = update_duration(shared_audio)) != srs_success) { + return srs_error_wrap(err, "update duration"); } - if ((ret = SrsDvrPlan::on_audio(shared_audio, format)) != ERROR_SUCCESS) { - return ret; + if ((err = SrsDvrPlan::on_audio(shared_audio, format)) != srs_success) { + return srs_error_wrap(err, "consume audio"); } - return ret; + return err; } -int SrsDvrSegmentPlan::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) +srs_error_t SrsDvrSegmentPlan::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = update_duration(shared_video)) != ERROR_SUCCESS) { - return ret; + if ((err = update_duration(shared_video)) != srs_success) { + return srs_error_wrap(err, "update duration"); } - if ((ret = SrsDvrPlan::on_video(shared_video, format)) != ERROR_SUCCESS) { - return ret; + if ((err = SrsDvrPlan::on_video(shared_video, format)) != srs_success) { + return srs_error_wrap(err, "consume video"); } - return ret; + return err; } -int SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg) +srs_error_t SrsDvrSegmentPlan::update_duration(SrsSharedPtrMessage* msg) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; srs_assert(segment); // ignore if duration ok. SrsFragment* fragment = segment->current(); if (cduration <= 0 || fragment->duration() < cduration) { - return ret; + return err; } // when wait keyframe, ignore if no frame arrived. // @see https://github.com/ossrs/srs/issues/177 if (wait_keyframe) { if (!msg->is_video()) { - return ret; + return err; } char* payload = msg->payload; int size = msg->size; - bool is_key_frame = SrsFlvVideo::h264(payload, size) - && SrsFlvVideo::keyframe(payload, size) - && !SrsFlvVideo::sh(payload, size); + bool is_key_frame = SrsFlvVideo::h264(payload, size) && SrsFlvVideo::keyframe(payload, size) && !SrsFlvVideo::sh(payload, size); if (!is_key_frame) { - return ret; + return err; } } // reap segment - if ((ret = segment->close()) != ERROR_SUCCESS) { - return ret; + if ((err = segment->close()) != srs_success) { + return srs_error_wrap(err, "segment close"); } // open new flv file - if ((ret = segment->open()) != ERROR_SUCCESS) { - return ret; + if ((err = segment->open()) != srs_success) { + return srs_error_wrap(err, "segment open"); } // update sequence header - if ((ret = hub->on_dvr_request_sh()) != ERROR_SUCCESS) { - return ret; + if ((err = hub->on_dvr_request_sh()) != srs_success) { + return srs_error_wrap(err, "request sh"); } - return ret; + return err; } srs_error_t SrsDvrSegmentPlan::on_reload_vhost_dvr(string vhost) @@ -959,20 +951,20 @@ srs_error_t SrsDvr::initialize(SrsOriginHub* h, SrsRequest* r) return err; } -int SrsDvr::on_publish() +srs_error_t SrsDvr::on_publish() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // the dvr for this stream is not actived. if (!actived) { - return ret; + return err; } - if ((ret = plan->on_publish()) != ERROR_SUCCESS) { - return ret; + if ((err = plan->on_publish()) != srs_success) { + return srs_error_wrap(err, "publish"); } - return ret; + return err; } void SrsDvr::on_unpublish() @@ -980,37 +972,37 @@ void SrsDvr::on_unpublish() plan->on_unpublish(); } -int SrsDvr::on_meta_data(SrsSharedPtrMessage* metadata) +srs_error_t SrsDvr::on_meta_data(SrsSharedPtrMessage* metadata) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // the dvr for this stream is not actived. if (!actived) { - return ret; + return err; } - if ((ret = plan->on_meta_data(metadata)) != ERROR_SUCCESS) { - return ret; + if ((err = plan->on_meta_data(metadata)) != srs_success) { + return srs_error_wrap(err, "metadata"); } - return ret; + return err; } -int SrsDvr::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) +srs_error_t SrsDvr::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) { // the dvr for this stream is not actived. if (!actived) { - return ERROR_SUCCESS; + return srs_success; } return plan->on_audio(shared_audio, format); } -int SrsDvr::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) +srs_error_t SrsDvr::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) { // the dvr for this stream is not actived. if (!actived) { - return ERROR_SUCCESS; + return srs_success; } return plan->on_video(shared_video, format); @@ -1018,7 +1010,6 @@ int SrsDvr::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format) srs_error_t SrsDvr::on_reload_vhost_dvr_apply(string vhost) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsConfDirective* conf = _srs_config->get_dvr_apply(req->vhost); @@ -1035,11 +1026,11 @@ srs_error_t SrsDvr::on_reload_vhost_dvr_apply(string vhost) return err; } - if ((ret = on_publish()) != ERROR_SUCCESS) { - return srs_error_new(ret, "on publish"); + if ((err = on_publish()) != srs_success) { + return srs_error_wrap(err, "on publish"); } - if ((ret = hub->on_dvr_request_sh()) != ERROR_SUCCESS) { - return srs_error_new(ret, "request sh"); + if ((err = hub->on_dvr_request_sh()) != srs_success) { + return srs_error_wrap(err, "request sh"); } return err; diff --git a/trunk/src/app/srs_app_dvr.hpp b/trunk/src/app/srs_app_dvr.hpp index 953a2e23a..b36a8407d 100644 --- a/trunk/src/app/srs_app_dvr.hpp +++ b/trunk/src/app/srs_app_dvr.hpp @@ -78,33 +78,33 @@ public: // Open new segment file. // @param use_tmp_file Whether use tmp file for DVR, and rename when close. // @remark Ignore when file is already open. - virtual int open(); + virtual srs_error_t open(); // Write the metadata. - virtual int write_metadata(SrsSharedPtrMessage* metadata); + virtual srs_error_t write_metadata(SrsSharedPtrMessage* metadata); // Write audio packet. // @param shared_audio, directly ptr, copy it if need to save it. - virtual int write_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format); + virtual srs_error_t write_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format); // Write video packet. // @param shared_video, directly ptr, copy it if need to save it. - virtual int write_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); + virtual srs_error_t write_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); // Refresh the metadata. For example, there is duration in flv metadata, // when DVR in append mode, the duration must be update every some seconds. // @remark Maybe ignored by concreate segmenter. - virtual int refresh_metadata() = 0; + virtual srs_error_t refresh_metadata() = 0; // Close current segment. // @remark ignore when already closed. - virtual int close(); + virtual srs_error_t close(); protected: - virtual int open_encoder() = 0; - virtual int encode_metadata(SrsSharedPtrMessage* metadata) = 0; - virtual int encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) = 0; - virtual int encode_video(SrsSharedPtrMessage* video, SrsFormat* format) = 0; - virtual int close_encoder() = 0; + virtual srs_error_t open_encoder() = 0; + virtual srs_error_t encode_metadata(SrsSharedPtrMessage* metadata) = 0; + virtual srs_error_t encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format) = 0; + virtual srs_error_t encode_video(SrsSharedPtrMessage* video, SrsFormat* format) = 0; + virtual srs_error_t close_encoder() = 0; private: // Generate the flv segment path. virtual std::string generate_path(); // When update the duration of segment by rtmp msg. - virtual int on_update_duration(SrsSharedPtrMessage* msg); + virtual srs_error_t on_update_duration(SrsSharedPtrMessage* msg); // interface ISrsReloadHandler public: virtual srs_error_t on_reload_vhost_dvr(std::string vhost); @@ -131,13 +131,13 @@ public: SrsDvrFlvSegmenter(); virtual ~SrsDvrFlvSegmenter(); public: - virtual int refresh_metadata(); + virtual srs_error_t refresh_metadata(); protected: - virtual int open_encoder(); - virtual int encode_metadata(SrsSharedPtrMessage* metadata); - virtual int encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format); - virtual int encode_video(SrsSharedPtrMessage* video, SrsFormat* format); - virtual int close_encoder(); + virtual srs_error_t open_encoder(); + virtual srs_error_t encode_metadata(SrsSharedPtrMessage* metadata); + virtual srs_error_t encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format); + virtual srs_error_t encode_video(SrsSharedPtrMessage* video, SrsFormat* format); + virtual srs_error_t close_encoder(); }; /** @@ -152,13 +152,13 @@ public: SrsDvrMp4Segmenter(); virtual ~SrsDvrMp4Segmenter(); public: - virtual int refresh_metadata(); + virtual srs_error_t refresh_metadata(); protected: - virtual int open_encoder(); - virtual int encode_metadata(SrsSharedPtrMessage* metadata); - virtual int encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format); - virtual int encode_video(SrsSharedPtrMessage* video, SrsFormat* format); - virtual int close_encoder(); + virtual srs_error_t open_encoder(); + virtual srs_error_t encode_metadata(SrsSharedPtrMessage* metadata); + virtual srs_error_t encode_audio(SrsSharedPtrMessage* audio, SrsFormat* format); + virtual srs_error_t encode_video(SrsSharedPtrMessage* video, SrsFormat* format); + virtual srs_error_t close_encoder(); }; /** @@ -195,12 +195,12 @@ public: virtual ~SrsDvrPlan(); public: virtual srs_error_t initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r); - virtual int on_publish() = 0; + virtual srs_error_t on_publish() = 0; virtual void on_unpublish() = 0; - virtual int on_meta_data(SrsSharedPtrMessage* shared_metadata); - virtual int on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format); - virtual int on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); - // Internal interface for segmenter. + virtual srs_error_t on_meta_data(SrsSharedPtrMessage* shared_metadata); + virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format); + virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); +// Internal interface for segmenter. public: // When segmenter close a segment. virtual srs_error_t on_reap_segment(); @@ -217,7 +217,7 @@ public: SrsDvrSessionPlan(); virtual ~SrsDvrSessionPlan(); public: - virtual int on_publish(); + virtual srs_error_t on_publish(); virtual void on_unpublish(); }; @@ -235,12 +235,12 @@ public: virtual ~SrsDvrSegmentPlan(); public: virtual srs_error_t initialize(SrsOriginHub* h, SrsDvrSegmenter* s, SrsRequest* r); - virtual int on_publish(); + virtual srs_error_t on_publish(); virtual void on_unpublish(); - virtual int on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format); - virtual int on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); + virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format); + virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); private: - virtual int update_duration(SrsSharedPtrMessage* msg); + virtual srs_error_t update_duration(SrsSharedPtrMessage* msg); // interface ISrsReloadHandler public: virtual srs_error_t on_reload_vhost_dvr(std::string vhost); @@ -275,7 +275,7 @@ public: * when encoder start to publish RTMP stream. * @param fetch_sequence_header whether fetch sequence from source. */ - virtual int on_publish(); + virtual srs_error_t on_publish(); /** * the unpublish event., * when encoder stop(unpublish) to publish RTMP stream. @@ -284,17 +284,17 @@ public: /** * get some information from metadata, it's optinal. */ - virtual int on_meta_data(SrsSharedPtrMessage* metadata); + virtual srs_error_t on_meta_data(SrsSharedPtrMessage* metadata); /** * mux the audio packets to dvr. * @param shared_audio, directly ptr, copy it if need to save it. */ - virtual int on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* foramt); + virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* foramt); /** * mux the video packets to dvr. * @param shared_video, directly ptr, copy it if need to save it. */ - virtual int on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); + virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format); // interface ISrsReloadHandler public: virtual srs_error_t on_reload_vhost_dvr_apply(std::string vhost); diff --git a/trunk/src/app/srs_app_edge.cpp b/trunk/src/app/srs_app_edge.cpp index b0f04c844..541c10a3f 100644 --- a/trunk/src/app/srs_app_edge.cpp +++ b/trunk/src/app/srs_app_edge.cpp @@ -189,11 +189,10 @@ srs_error_t SrsEdgeIngester::initialize(SrsSource* s, SrsPlayEdge* e, SrsRequest srs_error_t SrsEdgeIngester::start() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; - if ((ret = source->on_publish()) != ERROR_SUCCESS) { - return srs_error_new(ret, "notify source"); + if ((err = source->on_publish()) != srs_success) { + return srs_error_wrap(err, "notify source"); } srs_freep(trd); @@ -262,8 +261,8 @@ srs_error_t SrsEdgeIngester::do_cycle() // reset the redirect to empty, for maybe the origin changed. redirect = ""; - if ((ret = source->on_source_id_changed(_srs_context->get_id())) != ERROR_SUCCESS) { - return srs_error_new(ret, "on source id changed"); + if ((err = source->on_source_id_changed(_srs_context->get_id())) != srs_success) { + return srs_error_wrap(err, "on source id changed"); } if ((ret = upstream->connect(req, lb)) != ERROR_SUCCESS) { @@ -274,17 +273,17 @@ srs_error_t SrsEdgeIngester::do_cycle() return srs_error_new(ret, "notify edge play"); } - ret = ingest(); + err = ingest(); // retry for rtmp 302 immediately. - if (ret == ERROR_CONTROL_REDIRECT) { - ret = ERROR_SUCCESS; + if (srs_error_code(err) == ERROR_CONTROL_REDIRECT) { + srs_error_reset(err); continue; } - if (srs_is_client_gracefully_close(ret)) { - srs_warn("origin disconnected, retry. ret=%d", ret); - ret = ERROR_SUCCESS; + if (srs_is_client_gracefully_close(err)) { + srs_warn("origin disconnected, retry, error %s", srs_error_desc(err).c_str()); + srs_error_reset(err); } break; } @@ -292,9 +291,10 @@ srs_error_t SrsEdgeIngester::do_cycle() return srs_error_new(ret, "cycle"); } -int SrsEdgeIngester::ingest() +srs_error_t SrsEdgeIngester::ingest() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsPithyPrint* pprint = SrsPithyPrint::create_edge(); SrsAutoFree(SrsPithyPrint, pprint); @@ -305,10 +305,7 @@ int SrsEdgeIngester::ingest() while (true) { srs_error_t err = srs_success; if ((err = trd->pull()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "thread quit"); } pprint->elapse(); @@ -321,82 +318,71 @@ int SrsEdgeIngester::ingest() // read from client. SrsCommonMessage* msg = NULL; if ((ret = upstream->recv_message(&msg)) != ERROR_SUCCESS) { - if (!srs_is_client_gracefully_close(ret)) { - srs_error("pull origin server message failed. ret=%d", ret); - } - return ret; + return srs_error_new(ret, "recv message"); } - srs_verbose("edge loop recv message. ret=%d", ret); srs_assert(msg); SrsAutoFree(SrsCommonMessage, msg); - if ((ret = process_publish_message(msg)) != ERROR_SUCCESS) { - return ret; + if ((err = process_publish_message(msg)) != srs_success) { + return srs_error_wrap(err, "process message"); } } - return ret; + return err; } -int SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg) +srs_error_t SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // process audio packet if (msg->header.is_audio()) { - if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) { - srs_error("source process audio message failed. ret=%d", ret); - return ret; + if ((err = source->on_audio(msg)) != srs_success) { + return srs_error_wrap(err, "source consume audio"); } } // process video packet if (msg->header.is_video()) { - if ((ret = source->on_video(msg)) != ERROR_SUCCESS) { - srs_error("source process video message failed. ret=%d", ret); - return ret; + if ((err = source->on_video(msg)) != srs_success) { + return srs_error_wrap(err, "source consume video"); } } // process aggregate packet if (msg->header.is_aggregate()) { - if ((ret = source->on_aggregate(msg)) != ERROR_SUCCESS) { - srs_error("source process aggregate message failed. ret=%d", ret); - return ret; + if ((err = source->on_aggregate(msg)) != srs_success) { + return srs_error_wrap(err, "source consume aggregate"); } - return ret; + return err; } // process onMetaData if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) { SrsPacket* pkt = NULL; if ((ret = upstream->decode_message(msg, &pkt)) != ERROR_SUCCESS) { - srs_error("decode onMetaData message failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "decode message"); } SrsAutoFree(SrsPacket, pkt); if (dynamic_cast(pkt)) { SrsOnMetaDataPacket* metadata = dynamic_cast(pkt); - if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) { - srs_error("source process onMetaData message failed. ret=%d", ret); - return ret; + if ((err = source->on_meta_data(msg, metadata)) != srs_success) { + return srs_error_wrap(err, "source consume metadata"); } - srs_info("process onMetaData message success."); - return ret; + return err; } - srs_info("ignore AMF0/AMF3 data message."); - return ret; + return err; } // call messages, for example, reject, redirect. if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) { SrsPacket* pkt = NULL; if ((ret = upstream->decode_message(msg, &pkt)) != ERROR_SUCCESS) { - srs_error("decode call message failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "decode message"); } SrsAutoFree(SrsPacket, pkt); @@ -404,35 +390,33 @@ int SrsEdgeIngester::process_publish_message(SrsCommonMessage* msg) if (dynamic_cast(pkt)) { SrsCallPacket* call = dynamic_cast(pkt); if (!call->arguments->is_object()) { - return ret; + return err; } SrsAmf0Any* prop = NULL; SrsAmf0Object* evt = call->arguments->to_object(); if ((prop = evt->ensure_property_string("level")) == NULL) { - return ret; + return err; } else if (prop->to_str() != StatusLevelError) { - return ret; + return err; } if ((prop = evt->get_property("ex")) == NULL || !prop->is_object()) { - return ret; + return err; } SrsAmf0Object* ex = prop->to_object(); if ((prop = ex->ensure_property_string("redirect")) == NULL) { - return ret; + return err; } redirect = prop->to_str(); - ret = ERROR_CONTROL_REDIRECT; - srs_info("RTMP 302 redirect to %s, ret=%d", redirect.c_str(), ret); - return ret; + return srs_error_new(ERROR_CONTROL_REDIRECT, "RTMP 302 redirect to %s", redirect.c_str()); } } - return ret; + return err; } SrsEdgeForwarder::SrsEdgeForwarder() @@ -591,8 +575,8 @@ srs_error_t SrsEdgeForwarder::do_cycle() // forward all messages. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. int count = 0; - if ((ret = queue->dump_packets(msgs.max, msgs.msgs, count)) != ERROR_SUCCESS) { - return srs_error_new(ret, "queue dumps packets"); + if ((err = queue->dump_packets(msgs.max, msgs.msgs, count)) != srs_success) { + return srs_error_wrap(err, "queue dumps packets"); } pprint->elapse(); @@ -617,13 +601,13 @@ srs_error_t SrsEdgeForwarder::do_cycle() return err; } -int SrsEdgeForwarder::proxy(SrsCommonMessage* msg) +srs_error_t SrsEdgeForwarder::proxy(SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if ((ret = send_error_code) != ERROR_SUCCESS) { - srs_error("publish edge proxy thread send error, ret=%d", ret); - return ret; + return srs_error_new(ret, "edge forwarder"); } // the msg is auto free by source, @@ -631,24 +615,21 @@ int SrsEdgeForwarder::proxy(SrsCommonMessage* msg) if (msg->size <= 0 || msg->header.is_set_chunk_size() || msg->header.is_window_ackledgement_size() - || msg->header.is_ackledgement() - ) { - return ret; + || msg->header.is_ackledgement()) { + return err; } SrsSharedPtrMessage copy; if ((ret = copy.create(msg)) != ERROR_SUCCESS) { - srs_error("initialize the msg failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "create message"); } - srs_verbose("initialize shared ptr msg success."); copy.stream_id = sdk->sid(); - if ((ret = queue->enqueue(copy.copy())) != ERROR_SUCCESS) { - srs_error("enqueue edge publish msg failed. ret=%d", ret); + if ((err = queue->enqueue(copy.copy())) != srs_success) { + return srs_error_wrap(err, "enqueue message"); } - return ret; + return err; } SrsPlayEdge::SrsPlayEdge() @@ -761,17 +742,13 @@ bool SrsPublishEdge::can_publish() return state != SrsEdgeStatePublish; } -int SrsPublishEdge::on_client_publish() +srs_error_t SrsPublishEdge::on_client_publish() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // error when not init state. if (state != SrsEdgeStateInit) { - ret = ERROR_RTMP_EDGE_PUBLISH_STATE; - srs_error("invalid state for client to publish stream on edge. " - "state=%d, ret=%d", state, ret); - return ret; + return srs_error_new(ERROR_RTMP_EDGE_PUBLISH_STATE, "invalid state"); } // @see https://github.com/ossrs/srs/issues/180 @@ -786,22 +763,18 @@ int SrsPublishEdge::on_client_publish() // start to forward stream to origin. err = forwarder->start(); - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - // @see https://github.com/ossrs/srs/issues/180 // when failed, revert to init - if (ret != ERROR_SUCCESS) { + if (err != srs_success) { SrsEdgeState pstate = state; state = SrsEdgeStateInit; - srs_trace("edge revert from %d to state %d (push). ret=%d", pstate, state, ret); + srs_trace("edge revert from %d to state %d (push), error %s", pstate, state, srs_error_desc(err).c_str()); } - return ret; + return err; } -int SrsPublishEdge::on_proxy_publish(SrsCommonMessage* msg) +srs_error_t SrsPublishEdge::on_proxy_publish(SrsCommonMessage* msg) { return forwarder->proxy(msg); } diff --git a/trunk/src/app/srs_app_edge.hpp b/trunk/src/app/srs_app_edge.hpp index 9ab486b14..99c6b55f8 100644 --- a/trunk/src/app/srs_app_edge.hpp +++ b/trunk/src/app/srs_app_edge.hpp @@ -139,8 +139,8 @@ public: private: virtual srs_error_t do_cycle(); private: - virtual int ingest(); - virtual int process_publish_message(SrsCommonMessage* msg); + virtual srs_error_t ingest(); + virtual srs_error_t process_publish_message(SrsCommonMessage* msg); }; /** @@ -181,7 +181,7 @@ public: private: virtual srs_error_t do_cycle(); public: - virtual int proxy(SrsCommonMessage* msg); + virtual srs_error_t proxy(SrsCommonMessage* msg); }; /** @@ -239,11 +239,11 @@ public: /** * when client publish stream on edge. */ - virtual int on_client_publish(); + virtual srs_error_t on_client_publish(); /** * proxy publish stream to edge */ - virtual int on_proxy_publish(SrsCommonMessage* msg); + virtual srs_error_t on_proxy_publish(SrsCommonMessage* msg); /** * proxy unpublish stream to edge. */ diff --git a/trunk/src/app/srs_app_forward.cpp b/trunk/src/app/srs_app_forward.cpp index 5c32de7e5..c932af712 100755 --- a/trunk/src/app/srs_app_forward.cpp +++ b/trunk/src/app/srs_app_forward.cpp @@ -71,9 +71,9 @@ SrsForwarder::~SrsForwarder() srs_freep(sh_audio); } -int SrsForwarder::initialize(SrsRequest* r, string ep) +srs_error_t SrsForwarder::initialize(SrsRequest* r, string ep) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // it's ok to use the request object, // SrsSource already copy it and never delete it. @@ -82,7 +82,7 @@ int SrsForwarder::initialize(SrsRequest* r, string ep) // the ep(endpoint) to forward to ep_forward = ep; - return ret; + return err; } void SrsForwarder::set_queue_size(double queue_size) @@ -90,9 +90,8 @@ void SrsForwarder::set_queue_size(double queue_size) queue->set_queue_size(queue_size); } -int SrsForwarder::on_publish() +srs_error_t SrsForwarder::on_publish() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // discovery the server port and tcUrl from req and ep_forward. @@ -127,26 +126,17 @@ int SrsForwarder::on_publish() dest_ep += req->vhost; if (source_ep == dest_ep) { - ret = ERROR_SYSTEM_FORWARD_LOOP; - srs_warn("forward loop detected. src=%s, dest=%s, ret=%d", - source_ep.c_str(), dest_ep.c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_FORWARD_LOOP, "forward loop detected. src=%s, dest=%s", source_ep.c_str(), dest_ep.c_str()); } - srs_trace("start forward %s to %s, tcUrl=%s, stream=%s", - source_ep.c_str(), dest_ep.c_str(), tcUrl.c_str(), - req->stream.c_str()); + srs_trace("start forward %s to %s, tcUrl=%s, stream=%s", source_ep.c_str(), dest_ep.c_str(), tcUrl.c_str(), req->stream.c_str()); srs_freep(trd); trd = new SrsSTCoroutine("forward", this); if ((err = trd->start()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - - return ret; + return srs_error_wrap(err, "start thread"); } - return ret; + return err; } void SrsForwarder::on_unpublish() @@ -155,43 +145,33 @@ void SrsForwarder::on_unpublish() sdk->close(); } -int SrsForwarder::on_meta_data(SrsSharedPtrMessage* shared_metadata) +srs_error_t SrsForwarder::on_meta_data(SrsSharedPtrMessage* shared_metadata) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsSharedPtrMessage* metadata = shared_metadata->copy(); // TODO: FIXME: config the jitter of Forwarder. if ((err = jitter->correct(metadata, SrsRtmpJitterAlgorithmOFF)) != srs_success) { - srs_freep(metadata); - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "jitter"); } - if ((ret = queue->enqueue(metadata)) != ERROR_SUCCESS) { - return ret; + if ((err = queue->enqueue(metadata)) != srs_success) { + return srs_error_wrap(err, "enqueue metadata"); } - return ret; + return err; } -int SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio) +srs_error_t SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsSharedPtrMessage* msg = shared_audio->copy(); // TODO: FIXME: config the jitter of Forwarder. if ((err = jitter->correct(msg, SrsRtmpJitterAlgorithmOFF)) != srs_success) { - srs_freep(msg); - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "jitter"); } if (SrsFlvAudio::sh(msg->payload, msg->size)) { @@ -199,27 +179,22 @@ int SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio) sh_audio = msg->copy(); } - if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) { - return ret; + if ((err = queue->enqueue(msg)) != srs_success) { + return srs_error_wrap(err, "enqueue audio"); } - return ret; + return err; } -int SrsForwarder::on_video(SrsSharedPtrMessage* shared_video) +srs_error_t SrsForwarder::on_video(SrsSharedPtrMessage* shared_video) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsSharedPtrMessage* msg = shared_video->copy(); // TODO: FIXME: config the jitter of Forwarder. if ((err = jitter->correct(msg, SrsRtmpJitterAlgorithmOFF)) != srs_success) { - srs_freep(msg); - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "jitter"); } if (SrsFlvVideo::sh(msg->payload, msg->size)) { @@ -227,11 +202,11 @@ int SrsForwarder::on_video(SrsSharedPtrMessage* shared_video) sh_video = msg->copy(); } - if ((ret = queue->enqueue(msg)) != ERROR_SUCCESS) { - return ret; + if ((err = queue->enqueue(msg)) != srs_success) { + return srs_error_wrap(err, "enqueue video"); } - return ret; + return err; } // when error, forwarder sleep for a while and retry. @@ -287,21 +262,22 @@ srs_error_t SrsForwarder::do_cycle() return srs_error_new(ret, "sdk publish"); } - if ((ret = hub->on_forwarder_start(this)) != ERROR_SUCCESS) { - return srs_error_new(ret, "notify hub start"); + if ((err = hub->on_forwarder_start(this)) != srs_success) { + return srs_error_wrap(err, "notify hub start"); } - if ((ret = forward()) != ERROR_SUCCESS) { - return srs_error_new(ret, "forward"); + if ((err = forward()) != srs_success) { + return srs_error_wrap(err, "forward"); } return err; } #define SYS_MAX_FORWARD_SEND_MSGS 128 -int SrsForwarder::forward() +srs_error_t SrsForwarder::forward() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE_TMMS); @@ -314,24 +290,18 @@ int SrsForwarder::forward() // TODO: FIXME: maybe need to zero the sequence header timestamp. if (sh_video) { if ((ret = sdk->send_and_free_message(sh_video->copy())) != ERROR_SUCCESS) { - srs_error("forwarder send sh_video to server failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "send video sh"); } } if (sh_audio) { if ((ret = sdk->send_and_free_message(sh_audio->copy())) != ERROR_SUCCESS) { - srs_error("forwarder send sh_audio to server failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "send audio sh"); } } while (true) { - srs_error_t err = srs_success; if ((err = trd->pull()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "thread quit"); } pprint->elapse(); @@ -341,10 +311,8 @@ int SrsForwarder::forward() SrsCommonMessage* msg = NULL; ret = sdk->recv_message(&msg); - srs_verbose("play loop recv message. ret=%d", ret); if (ret != ERROR_SUCCESS && ret != ERROR_SOCKET_TIMEOUT) { - srs_error("recv server control message failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "receive control message"); } srs_freep(msg); @@ -353,9 +321,8 @@ int SrsForwarder::forward() // forward all messages. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. int count = 0; - if ((ret = queue->dump_packets(msgs.max, msgs.msgs, count)) != ERROR_SUCCESS) { - srs_error("get message to forward failed. ret=%d", ret); - return ret; + if ((err = queue->dump_packets(msgs.max, msgs.msgs, count)) != srs_success) { + return srs_error_wrap(err, "dump packets"); } // pithy print @@ -365,18 +332,16 @@ int SrsForwarder::forward() // ignore when no messages. if (count <= 0) { - srs_verbose("no packets to forward."); continue; } // sendout messages, all messages are freed by send_and_free_messages(). if ((ret = sdk->send_and_free_messages(msgs.msgs, count)) != ERROR_SUCCESS) { - srs_error("forwarder messages to server failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "send messages"); } } - return ret; + return err; } diff --git a/trunk/src/app/srs_app_forward.hpp b/trunk/src/app/srs_app_forward.hpp index fdc67404c..f5312734f 100644 --- a/trunk/src/app/srs_app_forward.hpp +++ b/trunk/src/app/srs_app_forward.hpp @@ -70,33 +70,33 @@ public: SrsForwarder(SrsOriginHub* h); virtual ~SrsForwarder(); public: - virtual int initialize(SrsRequest* r, std::string ep); + virtual srs_error_t initialize(SrsRequest* r, std::string ep); virtual void set_queue_size(double queue_size); public: - virtual int on_publish(); + virtual srs_error_t on_publish(); virtual void on_unpublish(); /** * forward the audio packet. * @param shared_metadata, directly ptr, copy it if need to save it. */ - virtual int on_meta_data(SrsSharedPtrMessage* shared_metadata); + virtual srs_error_t on_meta_data(SrsSharedPtrMessage* shared_metadata); /** * forward the audio packet. * @param shared_audio, directly ptr, copy it if need to save it. */ - virtual int on_audio(SrsSharedPtrMessage* shared_audio); + virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio); /** * forward the video packet. * @param shared_video, directly ptr, copy it if need to save it. */ - virtual int on_video(SrsSharedPtrMessage* shared_video); + virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video); // interface ISrsReusableThread2Handler. public: virtual srs_error_t cycle(); private: virtual srs_error_t do_cycle(); private: - virtual int forward(); + virtual srs_error_t forward(); }; #endif diff --git a/trunk/src/app/srs_app_http_stream.cpp b/trunk/src/app/srs_app_http_stream.cpp index d2b8d419c..5d115bba0 100755 --- a/trunk/src/app/srs_app_http_stream.cpp +++ b/trunk/src/app/srs_app_http_stream.cpp @@ -99,6 +99,7 @@ srs_error_t SrsBufferCache::start() int SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (fast_cache <= 0) { srs_info("http: ignore dump fast cache."); @@ -106,7 +107,10 @@ int SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jit } // the jitter is get from SrsSource, which means the time_jitter of vhost. - if ((ret = queue->dump_packets(consumer, false, jitter)) != ERROR_SUCCESS) { + if ((err = queue->dump_packets(consumer, false, jitter)) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); return ret; } @@ -118,7 +122,6 @@ int SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jit srs_error_t SrsBufferCache::cycle() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // TODO: FIXME: support reload. @@ -130,8 +133,8 @@ srs_error_t SrsBufferCache::cycle() // the stream cache will create consumer to cache stream, // which will trigger to fetch stream from origin for edge. SrsConsumer* consumer = NULL; - if ((ret = source->create_consumer(NULL, consumer, false, false, true)) != ERROR_SUCCESS) { - return srs_error_new(ret, "create consumer"); + if ((err = source->create_consumer(NULL, consumer, false, false, true)) != srs_success) { + return srs_error_wrap(err, "create consumer"); } SrsAutoFree(SrsConsumer, consumer); @@ -154,8 +157,8 @@ srs_error_t SrsBufferCache::cycle() // get messages from consumer. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. int count = 0; - if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) { - return srs_error_new(ret, "consumer dump packets"); + if ((err = consumer->dump_packets(&msgs, count)) != srs_success) { + return srs_error_wrap(err, "consumer dump packets"); } if (count <= 0) { @@ -521,8 +524,8 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage // create consumer of souce, ignore gop cache, use the audio gop cache. SrsConsumer* consumer = NULL; - if ((ret = source->create_consumer(NULL, consumer, true, true, !enc->has_cache())) != ERROR_SUCCESS) { - return srs_error_new(ret, "create consumer"); + if ((err = source->create_consumer(NULL, consumer, true, true, !enc->has_cache())) != srs_success) { + return srs_error_wrap(err, "create consumer"); } SrsAutoFree(SrsConsumer, consumer); srs_verbose("http: consumer created success."); @@ -579,8 +582,8 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage // get messages from consumer. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. int count = 0; - if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) { - return srs_error_new(ret, "consumer dump packets"); + if ((err = consumer->dump_packets(&msgs, count)) != srs_success) { + return srs_error_wrap(err, "consumer dump packets"); } if (count <= 0) { @@ -977,8 +980,8 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle } SrsSource* s = NULL; - if ((ret = SrsSource::fetch_or_create(r, server, &s)) != ERROR_SUCCESS) { - return srs_error_new(ret, "source create"); + if ((err = SrsSource::fetch_or_create(r, server, &s)) != srs_success) { + return srs_error_wrap(err, "source create"); } srs_assert(s != NULL); diff --git a/trunk/src/app/srs_app_recv_thread.cpp b/trunk/src/app/srs_app_recv_thread.cpp index e840379d8..3592be47a 100644 --- a/trunk/src/app/srs_app_recv_thread.cpp +++ b/trunk/src/app/srs_app_recv_thread.cpp @@ -368,6 +368,7 @@ void SrsPublishRecvThread::stop() int SrsPublishRecvThread::consume(SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // when cid changed, change it. if (ncid != cid) { @@ -386,7 +387,10 @@ int SrsPublishRecvThread::consume(SrsCommonMessage* msg) srs_update_system_time_ms(), msg->header.timestamp, msg->size); // the rtmp connection will handle this message - ret = _conn->handle_publish_message(_source, msg); + err = _conn->handle_publish_message(_source, msg); + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); // must always free it, // the source will copy it if need to use. diff --git a/trunk/src/app/srs_app_rtmp_conn.cpp b/trunk/src/app/srs_app_rtmp_conn.cpp index 0e8915185..e66a4ee6f 100644 --- a/trunk/src/app/srs_app_rtmp_conn.cpp +++ b/trunk/src/app/srs_app_rtmp_conn.cpp @@ -201,8 +201,8 @@ srs_error_t SrsRtmpConn::do_cycle() } // check vhost, allow default vhost. - if ((ret = check_vhost(true)) != ERROR_SUCCESS) { - return srs_error_new(ret, "check vhost"); + if ((err = check_vhost(true)) != srs_success) { + return srs_error_wrap(err, "check vhost"); } srs_trace("connect app, tcUrl=%s, pageUrl=%s, swfUrl=%s, schema=%s, vhost=%s, port=%d, app=%s, args=%s", @@ -239,8 +239,8 @@ srs_error_t SrsRtmpConn::do_cycle() } } - if ((ret = service_cycle()) != ERROR_SUCCESS) { - err = srs_error_new(ret, "service cycle"); + if ((err = service_cycle()) != srs_success) { + err = srs_error_wrap(err, "service cycle"); } srs_error_t r0 = srs_success; @@ -375,36 +375,36 @@ void SrsRtmpConn::cleanup() kbps->cleanup(); } -int SrsRtmpConn::service_cycle() +srs_error_t SrsRtmpConn::service_cycle() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; int out_ack_size = _srs_config->get_out_ack_size(req->vhost); if (out_ack_size && (ret = rtmp->set_window_ack_size(out_ack_size)) != ERROR_SUCCESS) { - srs_error("set output window acknowledgement size failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: set out window ack size"); } int in_ack_size = _srs_config->get_in_ack_size(req->vhost); if (in_ack_size && (ret = rtmp->set_in_window_ack_size(in_ack_size)) != ERROR_SUCCESS) { - srs_error("set input window acknowledgement size failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: set in window ack size"); } if ((ret = rtmp->set_peer_bandwidth((int)(2.5 * 1000 * 1000), 2)) != ERROR_SUCCESS) { - srs_error("set peer bandwidth failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: set peer bandwidth"); } - srs_verbose("set peer bandwidth success"); // get the ip which client connected. std::string local_ip = srs_get_local_ip(srs_netfd_fileno(stfd)); // do bandwidth test if connect to the vhost which is for bandwidth check. if (_srs_config->get_bw_check_enabled(req->vhost)) { - return bandwidth->bandwidth_check(rtmp, skt, req, local_ip); + if ((ret = bandwidth->bandwidth_check(rtmp, skt, req, local_ip)) != ERROR_SUCCESS) { + return srs_error_new(ret, "rtmp: bandwidth check"); + } + return err; } // do token traverse before serve it. @@ -413,9 +413,8 @@ int SrsRtmpConn::service_cycle() info->edge = _srs_config->get_vhost_is_edge(req->vhost); bool edge_traverse = _srs_config->get_vhost_edge_token_traverse(req->vhost); if (info->edge && edge_traverse) { - if ((ret = check_edge_token_traverse_auth()) != ERROR_SUCCESS) { - srs_warn("token auth failed, ret=%d", ret); - return ret; + if ((err = check_edge_token_traverse_auth()) != srs_success) { + return srs_error_wrap(err, "rtmp: check token traverse"); } } } @@ -425,81 +424,71 @@ int SrsRtmpConn::service_cycle() // to make OBS happy, @see https://github.com/ossrs/srs/issues/454 int chunk_size = _srs_config->get_chunk_size(req->vhost); if ((ret = rtmp->set_chunk_size(chunk_size)) != ERROR_SUCCESS) { - srs_error("set chunk_size=%d failed. ret=%d", chunk_size, ret); - return ret; + return srs_error_new(ret, "rtmp: set chunk size %d", chunk_size); } - srs_info("set chunk_size=%d success", chunk_size); // response the client connect ok. if ((ret = rtmp->response_connect_app(req, local_ip.c_str())) != ERROR_SUCCESS) { - srs_error("response connect app failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: response connect app"); } - srs_verbose("response connect app success"); if ((ret = rtmp->on_bw_done()) != ERROR_SUCCESS) { - srs_error("on_bw_done failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: on bw down"); } - srs_verbose("on_bw_done success"); while (true) { srs_error_t err = srs_success; if ((err = trd->pull()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "rtmp: thread quit"); } - ret = stream_service_cycle(); + err = stream_service_cycle(); // stream service must terminated with error, never success. // when terminated with success, it's user required to stop. - if (ret == ERROR_SUCCESS) { + if (srs_error_code(err) == ERROR_SUCCESS) { + srs_freep(err); continue; } // when not system control error, fatal error, return. - if (!srs_is_system_control_error(ret)) { - if (ret != ERROR_SOCKET_TIMEOUT && !srs_is_client_gracefully_close(ret)) { - srs_error("stream service cycle failed. ret=%d", ret); - } - return ret; + if (!srs_is_system_control_error(err)) { + return srs_error_wrap(err, "rtmp: stream service"); } // for republish, continue service - if (ret == ERROR_CONTROL_REPUBLISH) { + if (srs_error_code(err) == ERROR_CONTROL_REPUBLISH) { // set timeout to a larger value, wait for encoder to republish. rtmp->set_send_timeout(SRS_REPUBLISH_RECV_TMMS); rtmp->set_recv_timeout(SRS_REPUBLISH_SEND_TMMS); - srs_trace("control message(unpublish) accept, retry stream service."); + srs_trace("rtmp: retry for republish"); + srs_freep(err); continue; } // for "some" system control error, // logical accept and retry stream service. - if (ret == ERROR_CONTROL_RTMP_CLOSE) { + if (srs_error_code(err) == ERROR_CONTROL_RTMP_CLOSE) { // TODO: FIXME: use ping message to anti-death of socket. // @see: https://github.com/ossrs/srs/issues/39 // set timeout to a larger value, for user paused. rtmp->set_recv_timeout(SRS_PAUSED_RECV_TMMS); rtmp->set_send_timeout(SRS_PAUSED_SEND_TMMS); - srs_trace("control message(close) accept, retry stream service."); + srs_trace("rtmp: retry for close"); + srs_freep(err); continue; } // for other system control message, fatal error. - srs_error("control message(%d) reject as error. ret=%d", ret, ret); - return ret; + return srs_error_wrap(err, "rtmp: reject"); } - return ret; + return err; } -int SrsRtmpConn::stream_service_cycle() +srs_error_t SrsRtmpConn::stream_service_cycle() { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; @@ -507,28 +496,21 @@ int SrsRtmpConn::stream_service_cycle() SrsRequest* req = info->req; if ((ret = rtmp->identify_client(info->res->stream_id, info->type, req->stream, req->duration)) != ERROR_SUCCESS) { - if (!srs_is_client_gracefully_close(ret)) { - srs_error("identify client failed. ret=%d", ret); - } - return ret; + return srs_error_new(ret, "rtmp: identify client"); } req->strip(); srs_trace("client identified, type=%s, stream_name=%s, duration=%.2f", - srs_client_type_string(info->type).c_str(), req->stream.c_str(), req->duration); + srs_client_type_string(info->type).c_str(), req->stream.c_str(), req->duration); // security check if ((ret = security->check(info->type, ip, req)) != ERROR_SUCCESS) { - srs_error("security check failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: security check"); } - srs_info("security check ok"); // Never allow the empty stream name, for HLS may write to a file with empty name. // @see https://github.com/ossrs/srs/issues/834 if (req->stream.empty()) { - ret = ERROR_RTMP_STREAM_NAME_EMPTY; - srs_error("RTMP: Empty stream name not allowed, ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_STREAM_NAME_EMPTY, "rtmp: empty stream"); } // client is identified, set the timeout to service timeout. @@ -537,88 +519,67 @@ int SrsRtmpConn::stream_service_cycle() // find a source to serve. SrsSource* source = NULL; - if ((ret = SrsSource::fetch_or_create(req, server, &source)) != ERROR_SUCCESS) { - return ret; + if ((err = SrsSource::fetch_or_create(req, server, &source)) != srs_success) { + return srs_error_wrap(err, "rtmp: fetch source"); } srs_assert(source != NULL); // update the statistic when source disconveried. SrsStatistic* stat = SrsStatistic::instance(); if ((ret = stat->on_client(_srs_context->get_id(), req, this, info->type)) != ERROR_SUCCESS) { - srs_error("stat client failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: stat client"); } bool enabled_cache = _srs_config->get_gop_cache(req->vhost); srs_trace("source url=%s, ip=%s, cache=%d, is_edge=%d, source_id=%d[%d]", - req->get_stream_url().c_str(), ip.c_str(), enabled_cache, info->edge, - source->source_id(), source->source_id()); + req->get_stream_url().c_str(), ip.c_str(), enabled_cache, info->edge, source->source_id(), source->source_id()); source->set_cache(enabled_cache); switch (info->type) { case SrsRtmpConnPlay: { - srs_verbose("start to play stream %s.", req->stream.c_str()); - // response connection start play if ((ret = rtmp->start_play(info->res->stream_id)) != ERROR_SUCCESS) { - srs_error("start to play stream failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: start play"); } if ((err = http_hooks_on_play()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - srs_error("http hook on_play failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "rtmp: callback on play"); } - srs_info("start to play stream %s success", req->stream.c_str()); - ret = playing(source); + err = playing(source); http_hooks_on_stop(); - return ret; + return err; } case SrsRtmpConnFMLEPublish: { - srs_verbose("FMLE start to publish stream %s.", req->stream.c_str()); - if ((ret = rtmp->start_fmle_publish(info->res->stream_id)) != ERROR_SUCCESS) { - srs_error("start to publish stream failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: start FMLE publish"); } return publishing(source); } case SrsRtmpConnHaivisionPublish: { - srs_verbose("Haivision start to publish stream %s.", req->stream.c_str()); - if ((ret = rtmp->start_haivision_publish(info->res->stream_id)) != ERROR_SUCCESS) { - srs_error("start to publish stream failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: start HAIVISION publish"); } return publishing(source); } case SrsRtmpConnFlashPublish: { - srs_verbose("flash start to publish stream %s.", req->stream.c_str()); - if ((ret = rtmp->start_flash_publish(info->res->stream_id)) != ERROR_SUCCESS) { - srs_error("flash start to publish stream failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: start FLASH publish"); } return publishing(source); } default: { - ret = ERROR_SYSTEM_CLIENT_INVALID; - srs_info("invalid client type=%d. ret=%d", info->type, ret); - return ret; + return srs_error_new(ERROR_SYSTEM_CLIENT_INVALID, "rtmp: unknown client type=%d", info->type); } } - return ret; + return err; } -int SrsRtmpConn::check_vhost(bool try_default_vhost) +srs_error_t SrsRtmpConn::check_vhost(bool try_default_vhost) { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; @@ -628,15 +589,11 @@ int SrsRtmpConn::check_vhost(bool try_default_vhost) SrsConfDirective* vhost = _srs_config->get_vhost(req->vhost, try_default_vhost); if (vhost == NULL) { - ret = ERROR_RTMP_VHOST_NOT_FOUND; - srs_error("vhost %s not found. ret=%d", req->vhost.c_str(), ret); - return ret; + return srs_error_new(ERROR_RTMP_VHOST_NOT_FOUND, "rtmp: no vhost %s", req->vhost.c_str()); } if (!_srs_config->get_vhost_enabled(req->vhost)) { - ret = ERROR_RTMP_VHOST_NOT_FOUND; - srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret); - return ret; + return srs_error_new(ERROR_RTMP_VHOST_NOT_FOUND, "rtmp: vhost %s disabled", req->vhost.c_str()); } if (req->vhost != vhost->arg0()) { @@ -646,36 +603,27 @@ int SrsRtmpConn::check_vhost(bool try_default_vhost) if (_srs_config->get_refer_enabled(req->vhost)) { if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_all(req->vhost))) != ERROR_SUCCESS) { - srs_error("check refer failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: referer check"); } - srs_verbose("check refer success."); } if ((err = http_hooks_on_connect()) != srs_success) { - srs_error("check vhost failed %s", srs_error_desc(err).c_str()); - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "rtmp: callback on connect"); } - return ret; + return err; } -int SrsRtmpConn::playing(SrsSource* source) +srs_error_t SrsRtmpConn::playing(SrsSource* source) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // create consumer of souce. SrsConsumer* consumer = NULL; - if ((ret = source->create_consumer(this, consumer)) != ERROR_SUCCESS) { - srs_error("create consumer failed. ret=%d", ret); - return ret; + if ((err = source->create_consumer(this, consumer)) != srs_success) { + return srs_error_wrap(err, "rtmp: create consumer"); } SrsAutoFree(SrsConsumer, consumer); - srs_verbose("consumer created success."); // use isolate thread to recv, // @see: https://github.com/ossrs/srs/issues/217 @@ -683,17 +631,12 @@ int SrsRtmpConn::playing(SrsSource* source) // start isolate recv thread. if ((err = trd.start()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - - srs_error("start isolate recv thread failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "rtmp: start receive thread"); } // delivery messages for clients playing stream. wakable = consumer; - ret = do_playing(source, consumer, &trd); + err = do_playing(source, consumer, &trd); wakable = NULL; // stop isolate recv thread @@ -704,22 +647,21 @@ int SrsRtmpConn::playing(SrsSource* source) srs_warn("drop the received %d messages", trd.size()); } - return ret; + return err; } -int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRecvThread* rtrd) +srs_error_t SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRecvThread* rtrd) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; srs_assert(consumer != NULL); SrsRequest* req = info->req; if (_srs_config->get_refer_enabled(req->vhost)) { if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_play(req->vhost))) != ERROR_SUCCESS) { - srs_error("check play_refer failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: referer check"); } - srs_verbose("check play_refer success."); } // initialize other components @@ -743,17 +685,15 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe set_sock_options(); srs_trace("start play smi=%.2f, mw_sleep=%d, mw_enabled=%d, realtime=%d, tcp_nodelay=%d", - send_min_interval, mw_sleep, mw_enabled, realtime, tcp_nodelay); + send_min_interval, mw_sleep, mw_enabled, realtime, tcp_nodelay); while (true) { // collect elapse for pithy print. pprint->elapse(); // when source is set to expired, disconnect it. - if (trd->pull()) { - ret = ERROR_USER_DISCONNECT; - srs_error("connection expired. ret=%d", ret); - return ret; + if ((err = trd->pull()) != srs_success) { + return srs_error_wrap(err, "rtmp: thread quit"); } // to use isolate thread to recv, can improve about 33% performance. @@ -761,28 +701,17 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe // @see: https://github.com/ossrs/srs/issues/217 while (!rtrd->empty()) { SrsCommonMessage* msg = rtrd->pump(); - srs_verbose("pump client message to process."); - - if ((ret = process_play_control_msg(consumer, msg)) != ERROR_SUCCESS) { - if (!srs_is_system_control_error(ret) && !srs_is_client_gracefully_close(ret)) { - srs_error("process play control message failed. ret=%d", ret); - } - return ret; + if ((err = process_play_control_msg(consumer, msg)) != srs_success) { + return srs_error_wrap(err, "rtmp: play control message"); } } // quit when recv thread error. if ((ret = rtrd->error_code()) != ERROR_SUCCESS) { - if (!srs_is_client_gracefully_close(ret) && !srs_is_system_control_error(ret)) { - srs_error("recv thread failed. ret=%d", ret); - } - return ret; + return srs_error_new(ret, "rtmp: recv thread"); } #ifdef SRS_PERF_QUEUE_COND_WAIT - // for send wait time debug - srs_verbose("send thread now=%" PRId64 "us, wait %dms", srs_update_system_time_ms(), mw_sleep); - // wait for message to incoming. // @see https://github.com/ossrs/srs/issues/251 // @see https://github.com/ossrs/srs/issues/257 @@ -793,54 +722,31 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe // for no-realtime, got some msgs then send. consumer->wait(SRS_PERF_MW_MIN_MSGS, mw_sleep); } - - // for send wait time debug - srs_verbose("send thread now=%" PRId64 "us wakeup", srs_update_system_time_ms()); #endif // get messages from consumer. // each msg in msgs.msgs must be free, for the SrsMessageArray never free them. // @remark when enable send_min_interval, only fetch one message a time. int count = (send_min_interval > 0)? 1 : 0; - if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) { - srs_error("get messages from consumer failed. ret=%d", ret); - return ret; + if ((err = consumer->dump_packets(&msgs, count)) != srs_success) { + return srs_error_wrap(err, "rtmp: consumer dump packets"); } // reportable if (pprint->can_print()) { kbps->sample(); - srs_trace("-> " SRS_CONSTS_LOG_PLAY - " time=%" PRId64 ", msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d, mw=%d", - pprint->age(), count, - kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(), - kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m(), - mw_sleep - ); - } - - // we use wait timeout to get messages, - // for min latency event no message incoming, - // so the count maybe zero. - if (count > 0) { - srs_verbose("mw wait %dms and got %d msgs %d(%" PRId64 "-%" PRId64 ")ms", - mw_sleep, count, - (count > 0? msgs.msgs[count - 1]->timestamp - msgs.msgs[0]->timestamp : 0), - (count > 0? msgs.msgs[0]->timestamp : 0), - (count > 0? msgs.msgs[count - 1]->timestamp : 0)); + srs_trace("-> " SRS_CONSTS_LOG_PLAY " time=%d, msgs=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d, mw=%d", + (int)pprint->age(), count, kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(), + kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m(), mw_sleep); } if (count <= 0) { #ifndef SRS_PERF_QUEUE_COND_WAIT - srs_info("mw sleep %dms for no msg", mw_sleep); srs_usleep(mw_sleep * 1000); -#else - srs_verbose("mw wait %dms and got nothing.", mw_sleep); #endif // ignore when nothing got. continue; } - srs_info("got %d msgs, min=%d, mw=%d", count, SRS_PERF_MW_MIN_MSGS, mw_sleep); // only when user specifies the duration, // we start to collect the durations for each message. @@ -861,19 +767,14 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe // sendout messages, all messages are freed by send_and_free_messages(). // no need to assert msg, for the rtmp will assert it. if (count > 0 && (ret = rtmp->send_and_free_messages(msgs.msgs, count, info->res->stream_id)) != ERROR_SUCCESS) { - if (!srs_is_client_gracefully_close(ret)) { - srs_error("send messages to client failed. ret=%d", ret); - } - return ret; + return srs_error_new(ret, "rtmp: send %d messages", count); } // if duration specified, and exceed it, stop play live. // @see: https://github.com/ossrs/srs/issues/45 if (user_specified_duration_to_stop) { if (duration >= (int64_t)req->duration) { - ret = ERROR_RTMP_DURATION_EXCEED; - srs_trace("stop live for duration exceed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_DURATION_EXCEED, "rtmp: time %d up %d", (int)duration, (int)req->duration); } } @@ -883,10 +784,10 @@ int SrsRtmpConn::do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRe } } - return ret; + return err; } -int SrsRtmpConn::publishing(SrsSource* source) +srs_error_t SrsRtmpConn::publishing(SrsSource* source) { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; @@ -895,29 +796,19 @@ int SrsRtmpConn::publishing(SrsSource* source) if (_srs_config->get_refer_enabled(req->vhost)) { if ((ret = refer->check(req->pageUrl, _srs_config->get_refer_publish(req->vhost))) != ERROR_SUCCESS) { - srs_error("check publish_refer failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: referer check"); } - srs_verbose("check publish_refer success."); } if ((err = http_hooks_on_publish()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - srs_error("http hook on_publish failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "rtmp: callback on publish"); } - if ((ret = acquire_publish(source)) == ERROR_SUCCESS) { + if ((err = acquire_publish(source)) == srs_success) { // use isolate thread to recv, // @see: https://github.com/ossrs/srs/issues/237 SrsPublishRecvThread rtrd(rtmp, req, srs_netfd_fileno(stfd), 0, this, source); - - srs_info("start to publish stream %s success", req->stream.c_str()); - ret = do_publishing(source, &rtrd); - - // stop isolate recv thread + err = do_publishing(source, &rtrd); rtrd.stop(); } @@ -926,16 +817,16 @@ int SrsRtmpConn::publishing(SrsSource* source) // but failed, so we must cleanup it. // @see https://github.com/ossrs/srs/issues/474 // @remark when stream is busy, should never release it. - if (ret != ERROR_SYSTEM_STREAM_BUSY) { + if (srs_error_code(err) != ERROR_SYSTEM_STREAM_BUSY) { release_publish(source); } http_hooks_on_unpublish(); - return ret; + return err; } -int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) +srs_error_t SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; @@ -946,12 +837,7 @@ int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) // start isolate recv thread. if ((err = rtrd->start()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - - srs_error("start isolate recv thread failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "rtmp: receive thread"); } // change the isolate recv thread context id, @@ -970,7 +856,7 @@ int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) bool mr = _srs_config->get_mr_enabled(req->vhost); int mr_sleep = _srs_config->get_mr_sleep_ms(req->vhost); srs_trace("start publish mr=%d/%d, p1stpt=%d, pnt=%d, tcp_nodelay=%d, rtcid=%d", - mr, mr_sleep, publish_1stpkt_timeout, publish_normal_timeout, tcp_nodelay, receive_thread_cid); + mr, mr_sleep, publish_1stpkt_timeout, publish_normal_timeout, tcp_nodelay, receive_thread_cid); } int64_t nb_msgs = 0; @@ -978,11 +864,8 @@ int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) while (true) { pprint->elapse(); - // when source is set to expired, disconnect it. - if (trd->pull()) { - ret = ERROR_USER_DISCONNECT; - srs_error("connection expired. ret=%d", ret); - return ret; + if ((err = trd->pull()) != srs_success) { + return srs_error_wrap(err, "rtmp: thread quit"); } // cond wait for timeout. @@ -996,18 +879,13 @@ int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) // check the thread error code. if ((ret = rtrd->error_code()) != ERROR_SUCCESS) { - if (!srs_is_system_control_error(ret) && !srs_is_client_gracefully_close(ret)) { - srs_error("recv thread failed. ret=%d", ret); - } - return ret; + return srs_error_new(ret, "rtmp: receive thread"); } // when not got any messages, timeout. if (rtrd->nb_msgs() <= nb_msgs) { - ret = ERROR_SOCKET_TIMEOUT; - srs_warn("publish timeout %dms, nb_msgs=%" PRId64 ", ret=%d", - nb_msgs? publish_normal_timeout : publish_1stpkt_timeout, nb_msgs, ret); - break; + return srs_error_new(ERROR_SOCKET_TIMEOUT, "rtmp: publish timeout %dms, nb_msgs=%d", + nb_msgs? publish_normal_timeout : publish_1stpkt_timeout, (int)nb_msgs); } nb_msgs = rtrd->nb_msgs(); @@ -1015,7 +893,7 @@ int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) // @remark https://github.com/ossrs/srs/issues/851 SrsStatistic* stat = SrsStatistic::instance(); if ((ret = stat->on_video_frames(req, (int)(rtrd->nb_video_frames() - nb_frames))) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "rtmp: stat video frames"); } nb_frames = rtrd->nb_video_frames(); @@ -1024,45 +902,37 @@ int SrsRtmpConn::do_publishing(SrsSource* source, SrsPublishRecvThread* rtrd) kbps->sample(); bool mr = _srs_config->get_mr_enabled(req->vhost); int mr_sleep = _srs_config->get_mr_sleep_ms(req->vhost); - srs_trace("<- " SRS_CONSTS_LOG_CLIENT_PUBLISH - " time=%" PRId64 ", okbps=%d,%d,%d, ikbps=%d,%d,%d, mr=%d/%d, p1stpt=%d, pnt=%d", pprint->age(), - kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(), - kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m(), - mr, mr_sleep, publish_1stpkt_timeout, publish_normal_timeout - ); + srs_trace("<- " SRS_CONSTS_LOG_CLIENT_PUBLISH " time=%d, okbps=%d,%d,%d, ikbps=%d,%d,%d, mr=%d/%d, p1stpt=%d, pnt=%d", + (int)pprint->age(), kbps->get_send_kbps(), kbps->get_send_kbps_30s(), kbps->get_send_kbps_5m(), + kbps->get_recv_kbps(), kbps->get_recv_kbps_30s(), kbps->get_recv_kbps_5m(), mr, mr_sleep, publish_1stpkt_timeout, publish_normal_timeout); } } - return ret; + return err; } -int SrsRtmpConn::acquire_publish(SrsSource* source) +srs_error_t SrsRtmpConn::acquire_publish(SrsSource* source) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; if (!source->can_publish(info->edge)) { - ret = ERROR_SYSTEM_STREAM_BUSY; - srs_warn("stream %s is already publishing. ret=%d", - req->get_stream_url().c_str(), ret); - return ret; + return srs_error_new(ERROR_SYSTEM_STREAM_BUSY, "rtmp: stream %s is busy", req->get_stream_url().c_str()); } // when edge, ignore the publish event, directly proxy it. if (info->edge) { - if ((ret = source->on_edge_start_publish()) != ERROR_SUCCESS) { - srs_error("notice edge start publish stream failed. ret=%d", ret); - return ret; + if ((err = source->on_edge_start_publish()) != srs_success) { + return srs_error_wrap(err, "rtmp: edge start publish"); } } else { - if ((ret = source->on_publish()) != ERROR_SUCCESS) { - srs_error("notify publish failed. ret=%d", ret); - return ret; + if ((err = source->on_publish()) != srs_success) { + return srs_error_wrap(err, "rtmp: source publish"); } } - return ret; + return err; } void SrsRtmpConn::release_publish(SrsSource* source) @@ -1076,16 +946,16 @@ void SrsRtmpConn::release_publish(SrsSource* source) } } -int SrsRtmpConn::handle_publish_message(SrsSource* source, SrsCommonMessage* msg) +srs_error_t SrsRtmpConn::handle_publish_message(SrsSource* source, SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // process publish event. if (msg->header.is_amf0_command() || msg->header.is_amf3_command()) { SrsPacket* pkt = NULL; if ((ret = rtmp->decode_message(msg, &pkt)) != ERROR_SUCCESS) { - srs_error("fmle decode unpublish message failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: decode message"); } SrsAutoFree(SrsPacket, pkt); @@ -1094,127 +964,112 @@ int SrsRtmpConn::handle_publish_message(SrsSource* source, SrsCommonMessage* msg // flash unpublish. // TODO: maybe need to support republish. srs_trace("flash flash publish finished."); - return ERROR_CONTROL_REPUBLISH; + return srs_error_new(ERROR_CONTROL_REPUBLISH, "rtmp: republish"); } // for fmle, drop others except the fmle start packet. if (dynamic_cast(pkt)) { SrsFMLEStartPacket* unpublish = dynamic_cast(pkt); if ((ret = rtmp->fmle_unpublish(info->res->stream_id, unpublish->transaction_id)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "rtmp: republish"); } - return ERROR_CONTROL_REPUBLISH; + return srs_error_new(ERROR_CONTROL_REPUBLISH, "rtmp: republish"); } srs_trace("fmle ignore AMF0/AMF3 command message."); - return ret; + return err; } // video, audio, data message - if ((ret = process_publish_message(source, msg)) != ERROR_SUCCESS) { - srs_error("fmle process publish message failed. ret=%d", ret); - return ret; + if ((err = process_publish_message(source, msg)) != srs_success) { + return srs_error_wrap(err, "rtmp: consume message"); } - return ret; + return err; } -int SrsRtmpConn::process_publish_message(SrsSource* source, SrsCommonMessage* msg) +srs_error_t SrsRtmpConn::process_publish_message(SrsSource* source, SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // for edge, directly proxy message to origin. if (info->edge) { - if ((ret = source->on_edge_proxy_publish(msg)) != ERROR_SUCCESS) { - srs_error("edge publish proxy msg failed. ret=%d", ret); - return ret; + if ((err = source->on_edge_proxy_publish(msg)) != srs_success) { + return srs_error_wrap(err, "rtmp: proxy publish"); } - return ret; + return err; } // process audio packet if (msg->header.is_audio()) { - if ((ret = source->on_audio(msg)) != ERROR_SUCCESS) { - srs_error("source process audio message failed. ret=%d", ret); - return ret; + if ((err = source->on_audio(msg)) != srs_success) { + return srs_error_wrap(err, "rtmp: consume audio"); } - return ret; + return err; } // process video packet if (msg->header.is_video()) { - if ((ret = source->on_video(msg)) != ERROR_SUCCESS) { - srs_error("source process video message failed. ret=%d", ret); - return ret; + if ((err = source->on_video(msg)) != srs_success) { + return srs_error_wrap(err, "rtmp: consume video"); } - return ret; + return err; } // process aggregate packet if (msg->header.is_aggregate()) { - if ((ret = source->on_aggregate(msg)) != ERROR_SUCCESS) { - srs_error("source process aggregate message failed. ret=%d", ret); - return ret; + if ((err = source->on_aggregate(msg)) != srs_success) { + return srs_error_wrap(err, "rtmp: consume aggregate"); } - return ret; + return err; } // process onMetaData if (msg->header.is_amf0_data() || msg->header.is_amf3_data()) { SrsPacket* pkt = NULL; if ((ret = rtmp->decode_message(msg, &pkt)) != ERROR_SUCCESS) { - srs_error("decode onMetaData message failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: decode message"); } SrsAutoFree(SrsPacket, pkt); if (dynamic_cast(pkt)) { SrsOnMetaDataPacket* metadata = dynamic_cast(pkt); - if ((ret = source->on_meta_data(msg, metadata)) != ERROR_SUCCESS) { - srs_error("source process onMetaData message failed. ret=%d", ret); - return ret; + if ((err = source->on_meta_data(msg, metadata)) != srs_success) { + return srs_error_wrap(err, "rtmp: consume metadata"); } - srs_info("process onMetaData message success."); - return ret; + return err; } - - srs_info("ignore AMF0/AMF3 data message."); - return ret; + return err; } - return ret; + return err; } -int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg) +srs_error_t SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!msg) { - srs_verbose("ignore all empty message."); - return ret; + return err; } SrsAutoFree(SrsCommonMessage, msg); if (!msg->header.is_amf0_command() && !msg->header.is_amf3_command()) { - srs_info("ignore all message except amf0/amf3 command."); - return ret; + return err; } SrsPacket* pkt = NULL; if ((ret = rtmp->decode_message(msg, &pkt)) != ERROR_SUCCESS) { - srs_error("decode the amf0/amf3 command packet failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: decode message"); } - srs_info("decode the amf0/amf3 command packet success."); - SrsAutoFree(SrsPacket, pkt); // for jwplayer/flowplayer, which send close as pause message. // @see https://github.com/ossrs/srs/issues/6 SrsCloseStreamPacket* close = dynamic_cast(pkt); if (close) { - ret = ERROR_CONTROL_RTMP_CLOSE; - srs_trace("system control message: rtmp close stream. ret=%d", ret); - return ret; + return srs_error_new(ERROR_CONTROL_RTMP_CLOSE, "rtmp: close stream"); } // call msg, @@ -1230,34 +1085,26 @@ int SrsRtmpConn::process_play_control_msg(SrsConsumer* consumer, SrsCommonMessag res->command_object = SrsAmf0Any::null(); res->response = SrsAmf0Any::null(); if ((ret = rtmp->send_and_free_packet(res, 0)) != ERROR_SUCCESS) { - if (!srs_is_system_control_error(ret) && !srs_is_client_gracefully_close(ret)) { - srs_warn("response call failed. ret=%d", ret); - } - return ret; + return srs_error_new(ret, "rtmp: send packets"); } } - return ret; + return err; } // pause SrsPausePacket* pause = dynamic_cast(pkt); if (pause) { if ((ret = rtmp->on_play_client_pause(info->res->stream_id, pause->is_pause)) != ERROR_SUCCESS) { - srs_error("rtmp process play client pause failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: pause"); } - - if ((ret = consumer->on_play_client_pause(pause->is_pause)) != ERROR_SUCCESS) { - srs_error("consumer process play client pause failed. ret=%d", ret); - return ret; + if ((err = consumer->on_play_client_pause(pause->is_pause)) != srs_success) { + return srs_error_wrap(err, "rtmp: pause"); } - srs_info("process pause success, is_pause=%d, time=%d.", pause->is_pause, pause->time_ms); - return ret; + return err; } // other msg. - srs_info("ignore all amf0/amf3 command except pause and video control."); - return ret; + return err; } void SrsRtmpConn::change_mw_sleep(int sleep_ms) @@ -1340,9 +1187,8 @@ void SrsRtmpConn::set_sock_options() } } -int SrsRtmpConn::check_edge_token_traverse_auth() +srs_error_t SrsRtmpConn::check_edge_token_traverse_auth() { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsRequest* req = info->req; @@ -1350,7 +1196,7 @@ int SrsRtmpConn::check_edge_token_traverse_auth() vector args = _srs_config->get_vhost_edge_origin(req->vhost)->args; if (args.empty()) { - return ret; + return err; } for (int i = 0; i < (int)args.size(); i++) { @@ -1365,27 +1211,23 @@ int SrsRtmpConn::check_edge_token_traverse_auth() SrsAutoFree(SrsTcpClient, transport); if ((err = transport->connect()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); + srs_warn("Illegal edge token, tcUrl=%s, %s", req->tcUrl.c_str(), srs_error_desc(err).c_str()); srs_freep(err); - srs_warn("Illegal edge token, tcUrl=%s to server=%s, port=%d. ret=%d", req->tcUrl.c_str(), server.c_str(), port, ret); continue; } SrsRtmpClient* client = new SrsRtmpClient(transport); SrsAutoFree(SrsRtmpClient, client); - return do_token_traverse_auth(client); } - ret = ERROR_EDGE_PORT_INVALID; - srs_error("Illegal edge token, server=%d. ret=%d", (int)args.size(), ret); - return ret; + return srs_error_new(ERROR_EDGE_PORT_INVALID, "rtmp: Illegal edge token, server=%d", (int)args.size()); } -int SrsRtmpConn::do_token_traverse_auth(SrsRtmpClient* client) +srs_error_t SrsRtmpConn::do_token_traverse_auth(SrsRtmpClient* client) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsRequest* req = info->req; srs_assert(client); @@ -1394,19 +1236,16 @@ int SrsRtmpConn::do_token_traverse_auth(SrsRtmpClient* client) client->set_send_timeout(SRS_CONSTS_RTMP_TMMS); if ((ret = client->handshake()) != ERROR_SUCCESS) { - srs_error("handshake with server failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "rtmp: handshake"); } // for token tranverse, always take the debug info(which carries token). if ((ret = client->connect_app(req->app, req->tcUrl, req, true, NULL)) != ERROR_SUCCESS) { - srs_error("connect with server failed, tcUrl=%s. ret=%d", req->tcUrl.c_str(), ret); - return ret; + return srs_error_new(ret, "rtmp: connect tcUrl"); } srs_trace("edge token auth ok, tcUrl=%s", req->tcUrl.c_str()); - - return ret; + return err; } srs_error_t SrsRtmpConn::on_disconnect() diff --git a/trunk/src/app/srs_app_rtmp_conn.hpp b/trunk/src/app/srs_app_rtmp_conn.hpp index 97f24dd37..686448a05 100644 --- a/trunk/src/app/srs_app_rtmp_conn.hpp +++ b/trunk/src/app/srs_app_rtmp_conn.hpp @@ -148,24 +148,24 @@ public: virtual void cleanup(); private: // when valid and connected to vhost/app, service the client. - virtual int service_cycle(); + virtual srs_error_t service_cycle(); // stream(play/publish) service cycle, identify client first. - virtual int stream_service_cycle(); - virtual int check_vhost(bool try_default_vhost); - virtual int playing(SrsSource* source); - virtual int do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRecvThread* trd); - virtual int publishing(SrsSource* source); - virtual int do_publishing(SrsSource* source, SrsPublishRecvThread* trd); - virtual int acquire_publish(SrsSource* source); + virtual srs_error_t stream_service_cycle(); + virtual srs_error_t check_vhost(bool try_default_vhost); + virtual srs_error_t playing(SrsSource* source); + virtual srs_error_t do_playing(SrsSource* source, SrsConsumer* consumer, SrsQueueRecvThread* trd); + virtual srs_error_t publishing(SrsSource* source); + virtual srs_error_t do_publishing(SrsSource* source, SrsPublishRecvThread* trd); + virtual srs_error_t acquire_publish(SrsSource* source); virtual void release_publish(SrsSource* source); - virtual int handle_publish_message(SrsSource* source, SrsCommonMessage* msg); - virtual int process_publish_message(SrsSource* source, SrsCommonMessage* msg); - virtual int process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg); + virtual srs_error_t handle_publish_message(SrsSource* source, SrsCommonMessage* msg); + virtual srs_error_t process_publish_message(SrsSource* source, SrsCommonMessage* msg); + virtual srs_error_t process_play_control_msg(SrsConsumer* consumer, SrsCommonMessage* msg); virtual void change_mw_sleep(int sleep_ms); virtual void set_sock_options(); private: - virtual int check_edge_token_traverse_auth(); - virtual int do_token_traverse_auth(SrsRtmpClient* client); + virtual srs_error_t check_edge_token_traverse_auth(); + virtual srs_error_t do_token_traverse_auth(SrsRtmpClient* client); private: /** * when the connection disconnect, call this method. diff --git a/trunk/src/app/srs_app_rtsp.cpp b/trunk/src/app/srs_app_rtsp.cpp index 7790ab7cd..1d45d902c 100644 --- a/trunk/src/app/srs_app_rtsp.cpp +++ b/trunk/src/app/srs_app_rtsp.cpp @@ -484,6 +484,7 @@ int SrsRtspConn::kickoff_audio_cache(SrsRtpPacket* pkt, int64_t dts) int SrsRtspConn::write_sequence_header() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // use the current dts. int64_t dts = vjitter->timestamp() / 90; @@ -500,7 +501,10 @@ int SrsRtspConn::write_sequence_header() SrsFormat* format = new SrsFormat(); SrsAutoFree(SrsFormat, format); - if ((ret = format->on_aac_sequence_header((char*)sh.c_str(), (int)sh.length())) != ERROR_SUCCESS) { + if ((err = format->on_aac_sequence_header((char*)sh.c_str(), (int)sh.length())) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); return ret; } diff --git a/trunk/src/app/srs_app_server.cpp b/trunk/src/app/srs_app_server.cpp index 8c14d9c82..a5716573b 100644 --- a/trunk/src/app/srs_app_server.cpp +++ b/trunk/src/app/srs_app_server.cpp @@ -1389,15 +1389,16 @@ srs_error_t SrsServer::on_reload_http_stream_updated() return err; } -int SrsServer::on_publish(SrsSource* s, SrsRequest* r) +srs_error_t SrsServer::on_publish(SrsSource* s, SrsRequest* r) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if ((ret = http_server->http_mount(s, r)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "http mount"); } - return ret; + return err; } void SrsServer::on_unpublish(SrsSource* s, SrsRequest* r) diff --git a/trunk/src/app/srs_app_server.hpp b/trunk/src/app/srs_app_server.hpp index 7d9637aad..b9d97c61f 100644 --- a/trunk/src/app/srs_app_server.hpp +++ b/trunk/src/app/srs_app_server.hpp @@ -382,7 +382,7 @@ public: virtual srs_error_t on_reload_http_stream_updated(); // interface ISrsSourceHandler public: - virtual int on_publish(SrsSource* s, SrsRequest* r); + virtual srs_error_t on_publish(SrsSource* s, SrsRequest* r); virtual void on_unpublish(SrsSource* s, SrsRequest* r); }; diff --git a/trunk/src/app/srs_app_source.cpp b/trunk/src/app/srs_app_source.cpp index cadfbf667..0458df59a 100755 --- a/trunk/src/app/srs_app_source.cpp +++ b/trunk/src/app/srs_app_source.cpp @@ -151,9 +151,9 @@ srs_error_t SrsRtmpJitter::correct(SrsSharedPtrMessage* msg, SrsRtmpJitterAlgori return err; } -int SrsRtmpJitter::get_time() +int64_t SrsRtmpJitter::get_time() { - return (int)last_pkt_correct_time; + return last_pkt_correct_time; } #ifdef SRS_PERF_QUEUE_FAST_VECTOR @@ -271,9 +271,9 @@ void SrsMessageQueue::set_queue_size(double queue_size) queue_size_ms = (int)(queue_size * 1000); } -int SrsMessageQueue::enqueue(SrsSharedPtrMessage* msg, bool* is_overflow) +srs_error_t SrsMessageQueue::enqueue(SrsSharedPtrMessage* msg, bool* is_overflow) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (msg->is_av()) { if (av_start_time == -1) { @@ -294,16 +294,16 @@ int SrsMessageQueue::enqueue(SrsSharedPtrMessage* msg, bool* is_overflow) shrink(); } - return ret; + return err; } -int SrsMessageQueue::dump_packets(int max_count, SrsSharedPtrMessage** pmsgs, int& count) +srs_error_t SrsMessageQueue::dump_packets(int max_count, SrsSharedPtrMessage** pmsgs, int& count) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int nb_msgs = (int)msgs.size(); if (nb_msgs <= 0) { - return ret; + return err; } srs_assert(max_count > 0); @@ -328,27 +328,27 @@ int SrsMessageQueue::dump_packets(int max_count, SrsSharedPtrMessage** pmsgs, in msgs.erase(msgs.begin(), msgs.begin() + count); } - return ret; + return err; } -int SrsMessageQueue::dump_packets(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag) +srs_error_t SrsMessageQueue::dump_packets(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int nb_msgs = (int)msgs.size(); if (nb_msgs <= 0) { - return ret; + return err; } SrsSharedPtrMessage** omsgs = msgs.data(); for (int i = 0; i < nb_msgs; i++) { SrsSharedPtrMessage* msg = omsgs[i]; - if ((ret = consumer->enqueue(msg, atc, ag)) != ERROR_SUCCESS) { - return ret; + if ((err = consumer->enqueue(msg, atc, ag)) != srs_success) { + return srs_error_wrap(err, "consume message"); } } - return ret; + return err; } void SrsMessageQueue::shrink() @@ -462,30 +462,25 @@ void SrsConsumer::update_source_id() should_update_source_id = true; } -int SrsConsumer::get_time() +int64_t SrsConsumer::get_time() { return jitter->get_time(); } -int SrsConsumer::enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitterAlgorithm ag) +srs_error_t SrsConsumer::enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitterAlgorithm ag) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsSharedPtrMessage* msg = shared_msg->copy(); if (!atc) { if ((err = jitter->correct(msg, ag)) != srs_success) { - srs_freep(msg); - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - return ret; + return srs_error_wrap(err, "consume message"); } } - if ((ret = queue->enqueue(msg, NULL)) != ERROR_SUCCESS) { - return ret; + if ((err = queue->enqueue(msg, NULL)) != srs_success) { + return srs_error_wrap(err, "enqueue message"); } #ifdef SRS_PERF_QUEUE_COND_WAIT @@ -503,24 +498,24 @@ int SrsConsumer::enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitte if (atc && duration_ms < 0) { srs_cond_signal(mw_wait); mw_waiting = false; - return ret; + return err; } // when duration ok, signal to flush. if (match_min_msgs && duration_ms > mw_duration) { srs_cond_signal(mw_wait); mw_waiting = false; - return ret; + return err; } } #endif - return ret; + return err; } -int SrsConsumer::dump_packets(SrsMessageArray* msgs, int& count) +srs_error_t SrsConsumer::dump_packets(SrsMessageArray* msgs, int& count) { - int ret =ERROR_SUCCESS; + srs_error_t err = srs_success; srs_assert(count >= 0); srs_assert(msgs->max > 0); @@ -539,15 +534,15 @@ int SrsConsumer::dump_packets(SrsMessageArray* msgs, int& count) // paused, return nothing. if (paused) { - return ret; + return err; } // pump msgs from queue. - if ((ret = queue->dump_packets(max, msgs->msgs, count)) != ERROR_SUCCESS) { - return ret; + if ((err = queue->dump_packets(max, msgs->msgs, count)) != srs_success) { + return srs_error_wrap(err, "dump packets"); } - return ret; + return err; } #ifdef SRS_PERF_QUEUE_COND_WAIT @@ -577,14 +572,14 @@ void SrsConsumer::wait(int nb_msgs, int duration) } #endif -int SrsConsumer::on_play_client_pause(bool is_pause) +srs_error_t SrsConsumer::on_play_client_pause(bool is_pause) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; srs_trace("stream consumer change pause state %d=>%d", paused, is_pause); paused = is_pause; - return ret; + return err; } void SrsConsumer::wakeup() @@ -632,13 +627,12 @@ bool SrsGopCache::enabled() return enable_gop_cache; } -int SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) +srs_error_t SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!enable_gop_cache) { - srs_verbose("gop cache is disabled."); - return ret; + return err; } // the gop cache know when to gop it. @@ -648,8 +642,7 @@ int SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) if (msg->is_video()) { // drop video when not h.264 if (!SrsFlvVideo::h264(msg->payload, msg->size)) { - srs_info("gop cache drop video for none h.264"); - return ret; + return err; } cached_video_count++; @@ -658,8 +651,7 @@ int SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) // no acceptable video or pure audio, disable the cache. if (pure_audio()) { - srs_verbose("ignore any frame util got a h264 video frame."); - return ret; + return err; } // ok, gop cache enabled, and got an audio. @@ -671,14 +663,11 @@ int SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) if (audio_after_last_video_count > SRS_PURE_AUDIO_GUESS_COUNT) { srs_warn("clear gop cache for guess pure audio overflow"); clear(); - return ret; + return err; } // clear gop cache when got key frame if (msg->is_video() && SrsFlvVideo::keyframe(msg->payload, msg->size)) { - srs_info("clear gop cache when got keyframe. vcount=%d, count=%d", - cached_video_count, (int)gop_cache.size()); - clear(); // curent msg is video frame, so we set to 1. @@ -688,7 +677,7 @@ int SrsGopCache::cache(SrsSharedPtrMessage* shared_msg) // cache the frame. gop_cache.push_back(msg->copy()); - return ret; + return err; } void SrsGopCache::clear() @@ -704,21 +693,20 @@ void SrsGopCache::clear() audio_after_last_video_count = 0; } -int SrsGopCache::dump(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm jitter_algorithm) +srs_error_t SrsGopCache::dump(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm jitter_algorithm) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; std::vector::iterator it; for (it = gop_cache.begin(); it != gop_cache.end(); ++it) { SrsSharedPtrMessage* msg = *it; - if ((ret = consumer->enqueue(msg, atc, jitter_algorithm)) != ERROR_SUCCESS) { - srs_error("dispatch cached gop failed. ret=%d", ret); - return ret; + if ((err = consumer->enqueue(msg, atc, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "enqueue message"); } } srs_trace("dispatch cached gop success. count=%d, duration=%d", (int)gop_cache.size(), consumer->get_time()); - return ret; + return err; } bool SrsGopCache::empty() @@ -935,13 +923,12 @@ srs_error_t SrsOriginHub::cycle() return err; } -int SrsOriginHub::on_meta_data(SrsSharedPtrMessage* shared_metadata, SrsOnMetaDataPacket* packet) +srs_error_t SrsOriginHub::on_meta_data(SrsSharedPtrMessage* shared_metadata, SrsOnMetaDataPacket* packet) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = format->on_metadata(packet)) != ERROR_SUCCESS) { - srs_error("Codec parse metadata failed, ret=%d", ret); - return ret; + if ((err = format->on_metadata(packet)) != srs_success) { + return srs_error_wrap(err, "Format parse metadata"); } // copy to all forwarders @@ -949,31 +936,28 @@ int SrsOriginHub::on_meta_data(SrsSharedPtrMessage* shared_metadata, SrsOnMetaDa std::vector::iterator it; for (it = forwarders.begin(); it != forwarders.end(); ++it) { SrsForwarder* forwarder = *it; - if ((ret = forwarder->on_meta_data(shared_metadata)) != ERROR_SUCCESS) { - srs_error("forwarder process onMetaData message failed. ret=%d", ret); - return ret; + if ((err = forwarder->on_meta_data(shared_metadata)) != srs_success) { + return srs_error_wrap(err, "Forwarder consume metadata"); } } } - if ((ret = dvr->on_meta_data(shared_metadata)) != ERROR_SUCCESS) { - srs_error("dvr process onMetaData message failed. ret=%d", ret); - return ret; + if ((err = dvr->on_meta_data(shared_metadata)) != srs_success) { + return srs_error_wrap(err, "DVR consume metadata"); } - return ret; + return err; } -int SrsOriginHub::on_audio(SrsSharedPtrMessage* shared_audio) +srs_error_t SrsOriginHub::on_audio(SrsSharedPtrMessage* shared_audio) { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsSharedPtrMessage* msg = shared_audio; - if ((ret = format->on_audio(msg)) != ERROR_SUCCESS) { - srs_error("Codec parse audio failed, ret=%d", ret); - return ret; + if ((err = format->on_audio(msg)) != srs_success) { + return srs_error_wrap(err, "format consume audio"); } // cache the sequence header if aac @@ -988,7 +972,7 @@ int SrsOriginHub::on_audio(SrsSharedPtrMessage* shared_audio) // when got audio stream info. SrsStatistic* stat = SrsStatistic::instance(); if ((ret = stat->on_audio_info(req, SrsAudioCodecIdAAC, c->sound_rate, c->sound_type, c->aac_object)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "stat audio"); } srs_trace("%dB audio sh, codec(%d, profile=%s, %dchannels, %dkbps, %dHZ), flv(%dbits, %dchannels, %dHZ)", @@ -999,58 +983,41 @@ int SrsOriginHub::on_audio(SrsSharedPtrMessage* shared_audio) } if ((err = hls->on_audio(msg, format)) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - // apply the error strategy for hls. // @see https://github.com/ossrs/srs/issues/264 std::string hls_error_strategy = _srs_config->get_hls_on_error(req->vhost); if (srs_config_hls_is_on_error_ignore(hls_error_strategy)) { - srs_warn("hls process audio message failed, ignore and disable hls. ret=%d", ret); - - // unpublish, ignore ret. + srs_warn("hls: ignore audio error %s", srs_error_desc(err).c_str()); hls->on_unpublish(); - - // ignore. - ret = ERROR_SUCCESS; + srs_error_reset(err); } else if (srs_config_hls_is_on_error_continue(hls_error_strategy)) { - if (srs_hls_can_continue(ret, source->meta->ash(), msg)) { - ret = ERROR_SUCCESS; + if (srs_hls_can_continue(srs_error_code(err), source->meta->ash(), msg)) { + srs_error_reset(err); } else { - srs_warn("hls continue audio failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "hls: audio"); } } else { - srs_warn("hls disconnect publisher for audio error. ret=%d", ret); - return ret; + return srs_error_wrap(err, "hls: audio"); } } if ((ret = dash->on_audio(msg, format)) != ERROR_SUCCESS) { - srs_warn("DASH failed, ignore and disable it. ret=%d", ret); + srs_warn("dash: ignore audio error ret=%d", ret); dash->on_unpublish(); - ret = ERROR_SUCCESS; + srs_error_reset(err); } - if ((ret = dvr->on_audio(msg, format)) != ERROR_SUCCESS) { - srs_warn("dvr process audio message failed, ignore and disable dvr. ret=%d", ret); - - // unpublish, ignore ret. + if ((err = dvr->on_audio(msg, format)) != srs_success) { + srs_warn("dvr: ignore audio error %s", srs_error_desc(err).c_str()); dvr->on_unpublish(); - - // ignore. - ret = ERROR_SUCCESS; + srs_error_reset(err); } #ifdef SRS_AUTO_HDS if ((ret = hds->on_audio(msg)) != ERROR_SUCCESS) { - srs_warn("hds process audio message failed, ignore and disable dvr. ret=%d", ret); - - // unpublish, ignore ret. + srs_warn("hds: ignore audio error ret=%d", ret); hds->on_unpublish(); - // ignore. - ret = ERROR_SUCCESS; + srs_error_reset(err); } #endif @@ -1059,17 +1026,16 @@ int SrsOriginHub::on_audio(SrsSharedPtrMessage* shared_audio) std::vector::iterator it; for (it = forwarders.begin(); it != forwarders.end(); ++it) { SrsForwarder* forwarder = *it; - if ((ret = forwarder->on_audio(msg)) != ERROR_SUCCESS) { - srs_error("forwarder process audio message failed. ret=%d", ret); - return ret; + if ((err = forwarder->on_audio(msg)) != srs_success) { + return srs_error_wrap(err, "forward: audio"); } } } - return ret; + return err; } -int SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_header) +srs_error_t SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_header) { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; @@ -1082,9 +1048,8 @@ int SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_h format->avc_parse_sps = _srs_config->get_parse_sps(req->vhost); } - if ((ret = format->on_video(msg)) != ERROR_SUCCESS) { - srs_error("Codec parse video failed, ret=%d", ret); - return ret; + if ((err = format->on_video(msg)) != srs_success) { + return srs_error_wrap(err, "format consume video"); } // cache the sequence header if h264 @@ -1096,7 +1061,7 @@ int SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_h // when got video stream info. SrsStatistic* stat = SrsStatistic::instance(); if ((ret = stat->on_video_info(req, SrsVideoCodecIdAVC, c->avc_profile, c->avc_level, c->width, c->height)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "stat video"); } srs_trace("%dB video sh, codec(%d, profile=%s, level=%s, %dx%d, %dkbps, %.1ffps, %.1fs)", @@ -1106,58 +1071,41 @@ int SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_h } if ((err = hls->on_video(msg, format)) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - // apply the error strategy for hls. // @see https://github.com/ossrs/srs/issues/264 std::string hls_error_strategy = _srs_config->get_hls_on_error(req->vhost); if (srs_config_hls_is_on_error_ignore(hls_error_strategy)) { - srs_warn("hls process video message failed, ignore and disable hls. ret=%d", ret); - - // unpublish, ignore ret. + srs_warn("hls: ignore video error %s", srs_error_desc(err).c_str()); hls->on_unpublish(); - - // ignore. - ret = ERROR_SUCCESS; + srs_error_reset(err); } else if (srs_config_hls_is_on_error_continue(hls_error_strategy)) { if (srs_hls_can_continue(ret, source->meta->vsh(), msg)) { - ret = ERROR_SUCCESS; + srs_error_reset(err); } else { - srs_warn("hls continue video failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "hls: video"); } } else { - srs_warn("hls disconnect publisher for video error. ret=%d", ret); - return ret; + return srs_error_wrap(err, "hls: video"); } } if ((ret = dash->on_video(msg, format)) != ERROR_SUCCESS) { - srs_warn("DASH failed, ignore and disable it. ret=%d", ret); + srs_warn("dash: ignore video error ret=%d", ret); dash->on_unpublish(); - ret = ERROR_SUCCESS; + srs_error_reset(err); } - if ((ret = dvr->on_video(msg, format)) != ERROR_SUCCESS) { - srs_warn("dvr process video message failed, ignore and disable dvr. ret=%d", ret); - - // unpublish, ignore ret. + if ((err = dvr->on_video(msg, format)) != srs_success) { + srs_warn("dvr: ignore video error %s", srs_error_desc(err).c_str()); dvr->on_unpublish(); - - // ignore. - ret = ERROR_SUCCESS; + srs_error_reset(err); } #ifdef SRS_AUTO_HDS if ((ret = hds->on_video(msg)) != ERROR_SUCCESS) { - srs_warn("hds process video message failed, ignore and disable dvr. ret=%d", ret); - - // unpublish, ignore ret. + srs_warn("hds: ignore video error ret=%d", ret); hds->on_unpublish(); - // ignore. - ret = ERROR_SUCCESS; + srs_error_reset(err); } #endif @@ -1166,70 +1114,59 @@ int SrsOriginHub::on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_h std::vector::iterator it; for (it = forwarders.begin(); it != forwarders.end(); ++it) { SrsForwarder* forwarder = *it; - if ((ret = forwarder->on_video(msg)) != ERROR_SUCCESS) { - srs_error("forwarder process video message failed. ret=%d", ret); - return ret; + if ((err = forwarder->on_video(msg)) != srs_success) { + return srs_error_wrap(err, "forward video"); } } } - return ret; + return err; } -int SrsOriginHub::on_publish() +srs_error_t SrsOriginHub::on_publish() { int ret = ERROR_SUCCESS; srs_error_t err = srs_success; // create forwarders - if ((ret = create_forwarders()) != ERROR_SUCCESS) { - srs_error("create forwarders failed. ret=%d", ret); - return ret; + if ((err = create_forwarders()) != srs_success) { + return srs_error_wrap(err, "create forwarders"); } // TODO: FIXME: use initialize to set req. #ifdef SRS_AUTO_TRANSCODE if ((ret = encoder->on_publish(req)) != ERROR_SUCCESS) { - srs_error("start encoder failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "encoder publish"); } #endif if ((err = hls->on_publish()) != srs_success) { - // TODO: FIXME: Use error - ret = srs_error_code(err); - srs_freep(err); - srs_error("start hls failed. ret=%d", ret); - return ret; + return srs_error_wrap(err, "hls publish"); } if ((ret = dash->on_publish()) != ERROR_SUCCESS) { - srs_error("Start DASH failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "dash publish"); } - if ((ret = dvr->on_publish()) != ERROR_SUCCESS) { - srs_error("start dvr failed. ret=%d", ret); - return ret; + if ((err = dvr->on_publish()) != srs_success) { + return srs_error_wrap(err, "dvr publish"); } // TODO: FIXME: use initialize to set req. #ifdef SRS_AUTO_HDS if ((ret = hds->on_publish(req)) != ERROR_SUCCESS) { - srs_error("start hds failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "hds publish"); } #endif // TODO: FIXME: use initialize to set req. if ((ret = ng_exec->on_publish(req)) != ERROR_SUCCESS) { - srs_error("start exec failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "exec publish"); } is_active = true; - return ret; + return err; } void SrsOriginHub::on_unpublish() @@ -1254,9 +1191,9 @@ void SrsOriginHub::on_unpublish() ng_exec->on_unpublish(); } -int SrsOriginHub::on_forwarder_start(SrsForwarder* forwarder) +srs_error_t SrsOriginHub::on_forwarder_start(SrsForwarder* forwarder) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsSharedPtrMessage* cache_metadata = source->meta->data(); SrsSharedPtrMessage* cache_sh_video = source->meta->vsh(); @@ -1264,25 +1201,22 @@ int SrsOriginHub::on_forwarder_start(SrsForwarder* forwarder) // feed the forwarder the metadata/sequence header, // when reload to enable the forwarder. - if (cache_metadata && (ret = forwarder->on_meta_data(cache_metadata)) != ERROR_SUCCESS) { - srs_error("forwarder process onMetaData message failed. ret=%d", ret); - return ret; + if (cache_metadata && (err = forwarder->on_meta_data(cache_metadata)) != srs_success) { + return srs_error_wrap(err, "forward metadata"); } - if (cache_sh_video && (ret = forwarder->on_video(cache_sh_video)) != ERROR_SUCCESS) { - srs_error("forwarder process video sequence header message failed. ret=%d", ret); - return ret; + if (cache_sh_video && (err = forwarder->on_video(cache_sh_video)) != srs_success) { + return srs_error_wrap(err, "forward video sh"); } - if (cache_sh_audio && (ret = forwarder->on_audio(cache_sh_audio)) != ERROR_SUCCESS) { - srs_error("forwarder process audio sequence header message failed. ret=%d", ret); - return ret; + if (cache_sh_audio && (err = forwarder->on_audio(cache_sh_audio)) != srs_success) { + return srs_error_wrap(err, "forward audio sh"); } - return ret; + return err; } -int SrsOriginHub::on_dvr_request_sh() +srs_error_t SrsOriginHub::on_dvr_request_sh() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsSharedPtrMessage* cache_metadata = source->meta->data(); SrsSharedPtrMessage* cache_sh_video = source->meta->vsh(); @@ -1291,31 +1225,27 @@ int SrsOriginHub::on_dvr_request_sh() // feed the dvr the metadata/sequence header, // when reload to start dvr, dvr will never get the sequence header in stream, // use the SrsSource.on_dvr_request_sh to push the sequence header to DVR. - if (cache_metadata && (ret = dvr->on_meta_data(cache_metadata)) != ERROR_SUCCESS) { - srs_error("dvr process onMetaData message failed. ret=%d", ret); - return ret; + if (cache_metadata && (err = dvr->on_meta_data(cache_metadata)) != srs_success) { + return srs_error_wrap(err, "dvr metadata"); } if (cache_sh_video) { - if ((ret = dvr->on_video(cache_sh_video, source->meta->vsh_format())) != ERROR_SUCCESS) { - srs_error("dvr process video sequence header message failed. ret=%d", ret); - return ret; + if ((err = dvr->on_video(cache_sh_video, source->meta->vsh_format())) != srs_success) { + return srs_error_wrap(err, "dvr video"); } } if (cache_sh_audio) { - if ((ret = dvr->on_audio(cache_sh_audio, source->meta->ash_format())) != ERROR_SUCCESS) { - srs_error("dvr process audio sequence header message failed. ret=%d", ret); - return ret; + if ((err = dvr->on_audio(cache_sh_audio, source->meta->ash_format())) != srs_success) { + return srs_error_wrap(err, "dvr audio"); } } - return ret; + return err; } srs_error_t SrsOriginHub::on_reload_vhost_forward(string vhost) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; if (req->vhost != vhost) { @@ -1332,8 +1262,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_forward(string vhost) return err; } - if ((ret = create_forwarders()) != ERROR_SUCCESS) { - return srs_error_new(ret, "create forwarders"); + if ((err = create_forwarders()) != srs_success) { + return srs_error_wrap(err, "create forwarders"); } srs_trace("vhost %s forwarders reload success", vhost.c_str()); @@ -1363,8 +1293,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_dash(string vhost) SrsSharedPtrMessage* cache_sh_video = source->meta->vsh(); if (cache_sh_video) { - if ((ret = format->on_video(cache_sh_video)) != ERROR_SUCCESS) { - return srs_error_new(ret, "format on_video"); + if ((err = format->on_video(cache_sh_video)) != srs_success) { + return srs_error_wrap(err, "format on_video"); } if ((ret = dash->on_video(cache_sh_video, format)) != ERROR_SUCCESS) { return srs_error_new(ret, "dash on_video"); @@ -1373,8 +1303,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_dash(string vhost) SrsSharedPtrMessage* cache_sh_audio = source->meta->ash(); if (cache_sh_audio) { - if ((ret = format->on_audio(cache_sh_audio)) != ERROR_SUCCESS) { - return srs_error_new(ret, "format on_audio"); + if ((err = format->on_audio(cache_sh_audio)) != srs_success) { + return srs_error_wrap(err, "format on_audio"); } if ((ret = dash->on_audio(cache_sh_audio, format)) != ERROR_SUCCESS) { return srs_error_new(ret, "dash on_audio"); @@ -1386,7 +1316,6 @@ srs_error_t SrsOriginHub::on_reload_vhost_dash(string vhost) srs_error_t SrsOriginHub::on_reload_vhost_hls(string vhost) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; if (req->vhost != vhost) { @@ -1414,8 +1343,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_hls(string vhost) // use the SrsSource.on_hls_start to push the sequence header to HLS. SrsSharedPtrMessage* cache_sh_video = source->meta->vsh(); if (cache_sh_video) { - if ((ret = format->on_video(cache_sh_video)) != ERROR_SUCCESS) { - return srs_error_new(ret, "format on_video"); + if ((err = format->on_video(cache_sh_video)) != srs_success) { + return srs_error_wrap(err, "format on_video"); } if ((err = hls->on_video(cache_sh_video, format)) != srs_success) { return srs_error_wrap(err, "hls on_video"); @@ -1424,8 +1353,8 @@ srs_error_t SrsOriginHub::on_reload_vhost_hls(string vhost) SrsSharedPtrMessage* cache_sh_audio = source->meta->ash(); if (cache_sh_audio) { - if ((ret = format->on_audio(cache_sh_audio)) != ERROR_SUCCESS) { - return srs_error_new(ret, "format on_audio"); + if ((err = format->on_audio(cache_sh_audio)) != srs_success) { + return srs_error_wrap(err, "format on_audio"); } if ((err = hls->on_audio(cache_sh_audio, format)) != srs_success) { return srs_error_wrap(err, "hls on_audio"); @@ -1465,7 +1394,6 @@ srs_error_t SrsOriginHub::on_reload_vhost_hds(string vhost) srs_error_t SrsOriginHub::on_reload_vhost_dvr(string vhost) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; if (req->vhost != vhost) { @@ -1488,12 +1416,12 @@ srs_error_t SrsOriginHub::on_reload_vhost_dvr(string vhost) } // start to publish by new plan. - if ((ret = dvr->on_publish()) != ERROR_SUCCESS) { - return srs_error_new(ret, "dvr publish failed"); + if ((err = dvr->on_publish()) != srs_success) { + return srs_error_wrap(err, "dvr publish failed"); } - if ((ret = on_dvr_request_sh()) != ERROR_SUCCESS) { - return srs_error_new(ret, "request sh"); + if ((err = on_dvr_request_sh()) != srs_success) { + return srs_error_wrap(err, "request sh"); } srs_trace("vhost %s dvr reload success", vhost.c_str()); @@ -1555,12 +1483,12 @@ srs_error_t SrsOriginHub::on_reload_vhost_exec(string vhost) return err; } -int SrsOriginHub::create_forwarders() +srs_error_t SrsOriginHub::create_forwarders() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!_srs_config->get_forward_enabled(req->vhost)) { - return ret; + return err; } SrsConfDirective* conf = _srs_config->get_forwards(req->vhost); @@ -1571,24 +1499,21 @@ int SrsOriginHub::create_forwarders() forwarders.push_back(forwarder); // initialize the forwarder with request. - if ((ret = forwarder->initialize(req, forward_server)) != ERROR_SUCCESS) { - return ret; + if ((err = forwarder->initialize(req, forward_server)) != srs_success) { + return srs_error_wrap(err, "init forwarder"); } // TODO: FIXME: support queue size. //double queue_size = _srs_config->get_queue_length(req->vhost); //forwarder->set_queue_size(queue_size); - if ((ret = forwarder->on_publish()) != ERROR_SUCCESS) { - srs_error("start forwarder failed. " - "vhost=%s, app=%s, stream=%s, forward-to=%s", - req->vhost.c_str(), req->app.c_str(), req->stream.c_str(), - forward_server.c_str()); - return ret; + if ((err = forwarder->on_publish()) != srs_success) { + return srs_error_wrap(err, "start forwarder failed, vhost=%s, app=%s, stream=%s, forward-to=%s", + req->vhost.c_str(), req->app.c_str(), req->stream.c_str(), forward_server.c_str()); } } - return ret; + return err; } void SrsOriginHub::destroy_forwarders() @@ -1646,40 +1571,35 @@ SrsFormat* SrsMetaCache::ash_format() return aformat; } -int SrsMetaCache::dumps(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag, bool dm, bool ds) +srs_error_t SrsMetaCache::dumps(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag, bool dm, bool ds) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // copy metadata. - if (dm && meta && (ret = consumer->enqueue(meta, atc, ag)) != ERROR_SUCCESS) { - srs_error("dispatch metadata failed. ret=%d", ret); - return ret; + if (dm && meta && (err = consumer->enqueue(meta, atc, ag)) != srs_success) { + return srs_error_wrap(err, "enqueue metadata"); } - srs_info("dispatch metadata success"); // copy sequence header // copy audio sequence first, for hls to fast parse the "right" audio codec. // @see https://github.com/ossrs/srs/issues/301 - if (ds && audio && (ret = consumer->enqueue(audio, atc, ag)) != ERROR_SUCCESS) { - srs_error("dispatch audio sequence header failed. ret=%d", ret); - return ret; + if (ds && audio && (err = consumer->enqueue(audio, atc, ag)) != srs_success) { + return srs_error_wrap(err, "enqueue audio sh"); } - srs_info("dispatch audio sequence header success"); - if (ds && video && (ret = consumer->enqueue(video, atc, ag)) != ERROR_SUCCESS) { - srs_error("dispatch video sequence header failed. ret=%d", ret); - return ret; + if (ds && video && (err = consumer->enqueue(video, atc, ag)) != srs_success) { + return srs_error_wrap(err, "enqueue video sh"); } - srs_info("dispatch video sequence header success"); - return ret; + return err; } -int SrsMetaCache::update_data(SrsMessageHeader* header, SrsOnMetaDataPacket* metadata, bool& updated) +srs_error_t SrsMetaCache::update_data(SrsMessageHeader* header, SrsOnMetaDataPacket* metadata, bool& updated) { updated = false; int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsAmf0Any* prop = NULL; @@ -1717,15 +1637,12 @@ int SrsMetaCache::update_data(SrsMessageHeader* header, SrsOnMetaDataPacket* met int size = 0; char* payload = NULL; if ((ret = metadata->encode(size, payload)) != ERROR_SUCCESS) { - srs_error("encode metadata error. ret=%d", ret); - srs_freep(payload); - return ret; + return srs_error_new(ret, "encode metadata"); } - srs_verbose("encode metadata success."); if (size <= 0) { srs_warn("ignore the invalid metadata. size=%d", size); - return ret; + return err; } // create a shared ptr message. @@ -1736,22 +1653,20 @@ int SrsMetaCache::update_data(SrsMessageHeader* header, SrsOnMetaDataPacket* met // dump message to shared ptr message. // the payload/size managed by cache_metadata, user should not free it. if ((ret = meta->create(header, payload, size)) != ERROR_SUCCESS) { - srs_error("initialize the cache metadata failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "create metadata"); } - srs_verbose("initialize shared ptr metadata success."); - return ret; + return err; } -int SrsMetaCache::update_ash(SrsSharedPtrMessage* msg) +srs_error_t SrsMetaCache::update_ash(SrsSharedPtrMessage* msg) { srs_freep(audio); audio = msg->copy(); return aformat->on_audio(msg); } -int SrsMetaCache::update_vsh(SrsSharedPtrMessage* msg) +srs_error_t SrsMetaCache::update_vsh(SrsSharedPtrMessage* msg) { srs_freep(video); video = msg->copy(); @@ -1760,15 +1675,14 @@ int SrsMetaCache::update_vsh(SrsSharedPtrMessage* msg) std::map SrsSource::pool; -int SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps) +srs_error_t SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; SrsSource* source = NULL; if ((source = fetch(r)) != NULL) { *pps = source; - return ret; + return err; } string stream_url = r->get_stream_url(); @@ -1779,12 +1693,7 @@ int SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** source = new SrsSource(); if ((err = source->initialize(r, h)) != srs_success) { - // TODO: FIXME: Use error. - ret = srs_error_code(err); - srs_freep(err); - - srs_freep(source); - return ret; + return srs_error_wrap(err, "init source %s", r->get_stream_url().c_str()); } pool[stream_url] = source; @@ -1792,7 +1701,7 @@ int SrsSource::fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** *pps = source; - return ret; + return err; } SrsSource* SrsSource::fetch(SrsRequest* r) @@ -2085,12 +1994,12 @@ srs_error_t SrsSource::on_reload_vhost_play(string vhost) return err; } -int SrsSource::on_source_id_changed(int id) +srs_error_t SrsSource::on_source_id_changed(int id) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (_source_id == id) { - return ret; + return err; } if (_pre_source_id == -1) { @@ -2108,7 +2017,7 @@ int SrsSource::on_source_id_changed(int id) consumer->update_source_id(); } - return ret; + return err; } int SrsSource::source_id() @@ -2130,9 +2039,9 @@ bool SrsSource::can_publish(bool is_edge) return _can_publish; } -int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata) +srs_error_t SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // if allow atc_auto and bravo-atc detected, open atc for vhost. SrsAmf0Any* prop = NULL; @@ -2147,11 +2056,11 @@ int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata // Update the meta cache. bool updated = false; - if ((ret = meta->update_data(&msg->header, metadata, updated)) != ERROR_SUCCESS) { - return ret; + if ((err = meta->update_data(&msg->header, metadata, updated)) != srs_success) { + return srs_error_wrap(err, "update metadata"); } if (!updated) { - return ret; + return err; } // when already got metadata, drop when reduce sequence header. @@ -2166,9 +2075,8 @@ int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata std::vector::iterator it; for (it = consumers.begin(); it != consumers.end(); ++it) { SrsConsumer* consumer = *it; - if ((ret = consumer->enqueue(meta->data(), atc, jitter_algorithm)) != ERROR_SUCCESS) { - srs_error("dispatch the metadata failed. ret=%d", ret); - return ret; + if ((err = consumer->enqueue(meta->data(), atc, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "consume metadata"); } } } @@ -2177,9 +2085,10 @@ int SrsSource::on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata return hub->on_meta_data(meta->data(), metadata); } -int SrsSource::on_audio(SrsCommonMessage* shared_audio) +srs_error_t SrsSource::on_audio(SrsCommonMessage* shared_audio) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // monotically increase detect. if (!mix_correct && is_monotonically_increase) { @@ -2194,10 +2103,8 @@ int SrsSource::on_audio(SrsCommonMessage* shared_audio) // the payload is transfer to msg, and set to NULL in shared_audio. SrsSharedPtrMessage msg; if ((ret = msg.create(shared_audio)) != ERROR_SUCCESS) { - srs_error("initialize the audio failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "create message"); } - srs_info("Audio dts=%" PRId64 ", size=%d", msg.timestamp, msg.size); // directly process the audio message. if (!mix_correct) { @@ -2210,25 +2117,24 @@ int SrsSource::on_audio(SrsCommonMessage* shared_audio) // fetch someone from mix queue. SrsSharedPtrMessage* m = mix_queue->pop(); if (!m) { - return ret; + return err; } // consume the monotonically increase message. if (m->is_audio()) { - ret = on_audio_imp(m); + err = on_audio_imp(m); } else { - ret = on_video_imp(m); + err = on_video_imp(m); } srs_freep(m); - return ret; + return err; } -int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) +srs_error_t SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - srs_info("Audio dts=%" PRId64 ", size=%d", msg->timestamp, msg->size); bool is_aac_sequence_header = SrsFlvAudio::sh(msg->payload, msg->size); bool is_sequence_header = is_aac_sequence_header; @@ -2245,39 +2151,35 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) if (!drop_for_reduce) { for (int i = 0; i < (int)consumers.size(); i++) { SrsConsumer* consumer = consumers.at(i); - if ((ret = consumer->enqueue(msg, atc, jitter_algorithm)) != ERROR_SUCCESS) { - srs_error("dispatch the audio failed. ret=%d", ret); - return ret; + if ((err = consumer->enqueue(msg, atc, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "consume message"); } } - srs_info("dispatch audio success."); } // Copy to hub to all utilities. - if ((ret = hub->on_audio(msg)) != ERROR_SUCCESS) { - return ret; + if ((err = hub->on_audio(msg)) != srs_success) { + return srs_error_wrap(err, "consume audio"); } // cache the sequence header of aac, or first packet of mp3. // for example, the mp3 is used for hls to write the "right" audio codec. // TODO: FIXME: to refine the stream info system. if (is_aac_sequence_header || !meta->ash()) { - if ((ret = meta->update_ash(msg)) != ERROR_SUCCESS) { - return ret; + if ((err = meta->update_ash(msg)) != srs_success) { + return srs_error_wrap(err, "meta consume audio"); } } // when sequence header, donot push to gop cache and adjust the timestamp. if (is_sequence_header) { - return ret; + return err; } // cache the last gop packets - if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) { - srs_error("shrink gop cache failed. ret=%d", ret); - return ret; + if ((err = gop_cache->cache(msg)) != srs_success) { + return srs_error_wrap(err, "gop cache consume audio"); } - srs_verbose("cache gop success."); // if atc, update the sequence header to abs time. if (atc) { @@ -2289,12 +2191,13 @@ int SrsSource::on_audio_imp(SrsSharedPtrMessage* msg) } } - return ret; + return err; } -int SrsSource::on_video(SrsCommonMessage* shared_video) +srs_error_t SrsSource::on_video(SrsCommonMessage* shared_video) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // monotically increase detect. if (!mix_correct && is_monotonically_increase) { @@ -2314,17 +2217,15 @@ int SrsSource::on_video(SrsCommonMessage* shared_video) } srs_warn("drop unknown header video, size=%d, bytes[0]=%#x", shared_video->size, b0); - return ret; + return err; } // convert shared_video to msg, user should not use shared_video again. // the payload is transfer to msg, and set to NULL in shared_video. SrsSharedPtrMessage msg; if ((ret = msg.create(shared_video)) != ERROR_SUCCESS) { - srs_error("initialize the video failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "create message"); } - srs_info("Video dts=%" PRId64 ", size=%d", msg.timestamp, msg.size); // directly process the audio message. if (!mix_correct) { @@ -2337,25 +2238,23 @@ int SrsSource::on_video(SrsCommonMessage* shared_video) // fetch someone from mix queue. SrsSharedPtrMessage* m = mix_queue->pop(); if (!m) { - return ret; + return err; } // consume the monotonically increase message. if (m->is_audio()) { - ret = on_audio_imp(m); + err = on_audio_imp(m); } else { - ret = on_video_imp(m); + err = on_video_imp(m); } srs_freep(m); - return ret; + return err; } -int SrsSource::on_video_imp(SrsSharedPtrMessage* msg) +srs_error_t SrsSource::on_video_imp(SrsSharedPtrMessage* msg) { - int ret = ERROR_SUCCESS; - - srs_info("Video dts=%" PRId64 ", size=%d", msg->timestamp, msg->size); + srs_error_t err = srs_success; bool is_sequence_header = SrsFlvVideo::sh(msg->payload, msg->size); @@ -2370,38 +2269,34 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg) // cache the sequence header if h264 // donot cache the sequence header to gop_cache, return here. - if (is_sequence_header && (ret = meta->update_vsh(msg)) != ERROR_SUCCESS) { - return ret; + if (is_sequence_header && (err = meta->update_vsh(msg)) != srs_success) { + return srs_error_wrap(err, "meta update video"); } // Copy to hub to all utilities. - if ((ret = hub->on_video(msg, is_sequence_header)) != ERROR_SUCCESS) { - return ret; + if ((err = hub->on_video(msg, is_sequence_header)) != srs_success) { + return srs_error_wrap(err, "hub consume video"); } // copy to all consumer if (!drop_for_reduce) { for (int i = 0; i < (int)consumers.size(); i++) { SrsConsumer* consumer = consumers.at(i); - if ((ret = consumer->enqueue(msg, atc, jitter_algorithm)) != ERROR_SUCCESS) { - srs_error("dispatch the video failed. ret=%d", ret); - return ret; + if ((err = consumer->enqueue(msg, atc, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "consume video"); } } - srs_info("dispatch video success."); } // when sequence header, donot push to gop cache and adjust the timestamp. if (is_sequence_header) { - return ret; + return err; } // cache the last gop packets - if ((ret = gop_cache->cache(msg)) != ERROR_SUCCESS) { - srs_error("gop cache msg failed. ret=%d", ret); - return ret; + if ((err = gop_cache->cache(msg)) != srs_success) { + return srs_error_wrap(err, "gop cache consume vdieo"); } - srs_verbose("cache gop success."); // if atc, update the sequence header to abs time. if (atc) { @@ -2413,16 +2308,17 @@ int SrsSource::on_video_imp(SrsSharedPtrMessage* msg) } } - return ret; + return err; } -int SrsSource::on_aggregate(SrsCommonMessage* msg) +srs_error_t SrsSource::on_aggregate(SrsCommonMessage* msg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; SrsBuffer* stream = aggregate_stream; if ((ret = stream->initialize(msg->payload, msg->size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init stream"); } // the aggregate message always use abs time. @@ -2430,36 +2326,26 @@ int SrsSource::on_aggregate(SrsCommonMessage* msg) while (!stream->empty()) { if (!stream->require(1)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message type. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate"); } int8_t type = stream->read_1bytes(); if (!stream->require(3)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message size. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate"); } int32_t data_size = stream->read_3bytes(); if (data_size < 0) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message size(negative). ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate size"); } if (!stream->require(3)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message time. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate time"); } int32_t timestamp = stream->read_3bytes(); if (!stream->require(1)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message time(high). ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate time(high bits)"); } int32_t time_h = stream->read_1bytes(); @@ -2474,16 +2360,12 @@ int SrsSource::on_aggregate(SrsCommonMessage* msg) timestamp += delta; if (!stream->require(3)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message stream_id. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate stream id"); } int32_t stream_id = stream->read_3bytes(); if (data_size > 0 && !stream->require(data_size)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message data. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate data"); } // to common message. @@ -2503,30 +2385,28 @@ int SrsSource::on_aggregate(SrsCommonMessage* msg) } if (!stream->require(4)) { - ret = ERROR_RTMP_AGGREGATE; - srs_error("invalid aggregate message previous tag size. ret=%d", ret); - return ret; + return srs_error_new(ERROR_RTMP_AGGREGATE, "aggregate previous tag size"); } stream->read_4bytes(); // process parsed message if (o.header.is_audio()) { - if ((ret = on_audio(&o)) != ERROR_SUCCESS) { - return ret; + if ((err = on_audio(&o)) != srs_success) { + return srs_error_wrap(err, "consume audio"); } } else if (o.header.is_video()) { - if ((ret = on_video(&o)) != ERROR_SUCCESS) { - return ret; + if ((err = on_video(&o)) != srs_success) { + return srs_error_wrap(err, "consume video"); } } } - return ret; + return err; } -int SrsSource::on_publish() +srs_error_t SrsSource::on_publish() { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // update the request object. srs_assert(req); @@ -2535,8 +2415,8 @@ int SrsSource::on_publish() // whatever, the publish thread is the source or edge source, // save its id to srouce id. - if ((ret = on_source_id_changed(_srs_context->get_id())) != ERROR_SUCCESS) { - return ret; + if ((err = on_source_id_changed(_srs_context->get_id())) != srs_success) { + return srs_error_wrap(err, "source id change"); } // reset the mix queue. @@ -2547,20 +2427,19 @@ int SrsSource::on_publish() last_packet_time = 0; // Notify the hub about the publish event. - if ((ret = hub->on_publish()) != ERROR_SUCCESS) { - return ret; + if ((err = hub->on_publish()) != srs_success) { + return srs_error_wrap(err, "hub publish"); } // notify the handler. srs_assert(handler); - if ((ret = handler->on_publish(this, req)) != ERROR_SUCCESS) { - srs_error("handle on publish failed. ret=%d", ret); - return ret; + if ((err = handler->on_publish(this, req)) != srs_success) { + return srs_error_wrap(err, "handle publish"); } SrsStatistic* stat = SrsStatistic::instance(); stat->on_stream_publish(req, _source_id); - return ret; + return err; } void SrsSource::on_unpublish() @@ -2596,9 +2475,10 @@ void SrsSource::on_unpublish() } } -int SrsSource::create_consumer(SrsConnection* conn, SrsConsumer*& consumer, bool ds, bool dm, bool dg) +srs_error_t SrsSource::create_consumer(SrsConnection* conn, SrsConsumer*& consumer, bool ds, bool dm, bool dg) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; consumer = new SrsConsumer(this, conn); consumers.push_back(consumer); @@ -2620,13 +2500,13 @@ int SrsSource::create_consumer(SrsConnection* conn, SrsConsumer*& consumer, bool } // Copy metadata and sequence header to consumer. - if ((ret = meta->dumps(consumer, atc, jitter_algorithm, dm, ds)) != ERROR_SUCCESS) { - return ret; + if ((err = meta->dumps(consumer, atc, jitter_algorithm, dm, ds)) != srs_success) { + return srs_error_wrap(err, "meta dumps"); } // copy gop cache to client. - if (dg && (ret = gop_cache->dump(consumer, atc, jitter_algorithm)) != ERROR_SUCCESS) { - return ret; + if (dg && (err = gop_cache->dump(consumer, atc, jitter_algorithm)) != srs_success) { + return srs_error_wrap(err, "gop cache dumps"); } // print status. @@ -2640,12 +2520,11 @@ int SrsSource::create_consumer(SrsConnection* conn, SrsConsumer*& consumer, bool if (_srs_config->get_vhost_is_edge(req->vhost)) { // notice edge to start for the first client. if ((ret = play_edge->on_client_play()) != ERROR_SUCCESS) { - srs_error("notice edge start play stream failed. ret=%d", ret); - return ret; + return srs_error_new(ret, "play edge"); } } - return ret; + return err; } void SrsSource::on_consumer_destroy(SrsConsumer* consumer) @@ -2673,12 +2552,12 @@ SrsRtmpJitterAlgorithm SrsSource::jitter() return jitter_algorithm; } -int SrsSource::on_edge_start_publish() +srs_error_t SrsSource::on_edge_start_publish() { return publish_edge->on_client_publish(); } -int SrsSource::on_edge_proxy_publish(SrsCommonMessage* msg) +srs_error_t SrsSource::on_edge_proxy_publish(SrsCommonMessage* msg) { return publish_edge->on_proxy_publish(msg); } diff --git a/trunk/src/app/srs_app_source.hpp b/trunk/src/app/srs_app_source.hpp index 63110f789..3f1c18d3a 100644 --- a/trunk/src/app/srs_app_source.hpp +++ b/trunk/src/app/srs_app_source.hpp @@ -98,7 +98,7 @@ public: /** * get current client time, the last packet time. */ - virtual int get_time(); + virtual int64_t get_time(); }; #ifdef SRS_PERF_QUEUE_FAST_VECTOR @@ -168,19 +168,19 @@ public: * @param msg, the msg to enqueue, user never free it whatever the return code. * @param is_overflow, whether overflow and shrinked. NULL to ignore. */ - virtual int enqueue(SrsSharedPtrMessage* msg, bool* is_overflow = NULL); + virtual srs_error_t enqueue(SrsSharedPtrMessage* msg, bool* is_overflow = NULL); /** * get packets in consumer queue. * @pmsgs SrsSharedPtrMessage*[], used to store the msgs, user must alloc it. * @count the count in array, output param. * @max_count the max count to dequeue, must be positive. */ - virtual int dump_packets(int max_count, SrsSharedPtrMessage** pmsgs, int& count); + virtual srs_error_t dump_packets(int max_count, SrsSharedPtrMessage** pmsgs, int& count); /** * dumps packets to consumer, use specified args. * @remark the atc/tba/tbv/ag are same to SrsConsumer.enqueue(). */ - virtual int dump_packets(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag); + virtual srs_error_t dump_packets(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag); private: /** * remove a gop from the front. @@ -250,21 +250,21 @@ public: /** * get current client time, the last packet time. */ - virtual int get_time(); + virtual int64_t get_time(); /** * enqueue an shared ptr message. * @param shared_msg, directly ptr, copy it if need to save it. * @param whether atc, donot use jitter correct if true. * @param ag the algorithm of time jitter. */ - virtual int enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitterAlgorithm ag); + virtual srs_error_t enqueue(SrsSharedPtrMessage* shared_msg, bool atc, SrsRtmpJitterAlgorithm ag); /** * get packets in consumer queue. * @param msgs the msgs array to dump packets to send. * @param count the count in array, intput and output param. * @remark user can specifies the count to get specified msgs; 0 to get all if possible. */ - virtual int dump_packets(SrsMessageArray* msgs, int& count); + virtual srs_error_t dump_packets(SrsMessageArray* msgs, int& count); #ifdef SRS_PERF_QUEUE_COND_WAIT /** * wait for messages incomming, atleast nb_msgs and in duration. @@ -276,7 +276,7 @@ public: /** * when client send the pause message. */ - virtual int on_play_client_pause(bool is_pause); + virtual srs_error_t on_play_client_pause(bool is_pause); // ISrsWakable public: /** @@ -341,7 +341,7 @@ public: * 2. clear gop when got keyframe. * @param shared_msg, directly ptr, copy it if need to save it. */ - virtual int cache(SrsSharedPtrMessage* shared_msg); + virtual srs_error_t cache(SrsSharedPtrMessage* shared_msg); /** * clear the gop cache. */ @@ -349,7 +349,7 @@ public: /** * dump the cached gop to consumer. */ - virtual int dump(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm jitter_algorithm); + virtual srs_error_t dump(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm jitter_algorithm); /** * used for atc to get the time of gop cache, * the atc will adjust the sequence header timestamp to gop cache. @@ -381,7 +381,7 @@ public: /** * when stream start publish, mount stream. */ - virtual int on_publish(SrsSource* s, SrsRequest* r) = 0; + virtual srs_error_t on_publish(SrsSource* s, SrsRequest* r) = 0; /** * when stream stop publish, unmount stream. */ @@ -454,22 +454,22 @@ public: virtual srs_error_t cycle(); public: // When got a parsed metadata. - virtual int on_meta_data(SrsSharedPtrMessage* shared_metadata, SrsOnMetaDataPacket* packet); + virtual srs_error_t on_meta_data(SrsSharedPtrMessage* shared_metadata, SrsOnMetaDataPacket* packet); // When got a parsed audio packet. - virtual int on_audio(SrsSharedPtrMessage* shared_audio); + virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio); // When got a parsed video packet. - virtual int on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_header); + virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video, bool is_sequence_header); public: // When start publish stream. - virtual int on_publish(); + virtual srs_error_t on_publish(); // When stop publish stream. virtual void on_unpublish(); // Internal callback. public: // for the SrsForwarder to callback to request the sequence headers. - virtual int on_forwarder_start(SrsForwarder* forwarder); + virtual srs_error_t on_forwarder_start(SrsForwarder* forwarder); // for the SrsDvr to callback to request the sequence headers. - virtual int on_dvr_request_sh(); + virtual srs_error_t on_dvr_request_sh(); // interface ISrsReloadHandler public: virtual srs_error_t on_reload_vhost_forward(std::string vhost); @@ -480,7 +480,7 @@ public: virtual srs_error_t on_reload_vhost_transcode(std::string vhost); virtual srs_error_t on_reload_vhost_exec(std::string vhost); private: - virtual int create_forwarders(); + virtual srs_error_t create_forwarders(); virtual void destroy_forwarders(); }; @@ -518,14 +518,14 @@ public: // Dumps cached metadata to consumer. // @param dm Whether dumps the metadata. // @param ds Whether dumps the sequence header. - virtual int dumps(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag, bool dm, bool ds); + virtual srs_error_t dumps(SrsConsumer* consumer, bool atc, SrsRtmpJitterAlgorithm ag, bool dm, bool ds); public: // Update the cached metadata by packet. - virtual int update_data(SrsMessageHeader* header, SrsOnMetaDataPacket* metadata, bool& updated); + virtual srs_error_t update_data(SrsMessageHeader* header, SrsOnMetaDataPacket* metadata, bool& updated); // Update the cached audio sequence header. - virtual int update_ash(SrsSharedPtrMessage* msg); + virtual srs_error_t update_ash(SrsSharedPtrMessage* msg); // Update the cached video sequence header. - virtual int update_vsh(SrsSharedPtrMessage* msg); + virtual srs_error_t update_vsh(SrsSharedPtrMessage* msg); }; /** @@ -543,7 +543,7 @@ public: * @param h the event handler for source. * @param pps the matched source, if success never be NULL. */ - static int fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps); + static srs_error_t fetch_or_create(SrsRequest* r, ISrsSourceHandler* h, SrsSource** pps); private: /** * get the exists source, NULL when not exists. @@ -635,30 +635,30 @@ public: // for the tools callback public: // source id changed. - virtual int on_source_id_changed(int id); + virtual srs_error_t on_source_id_changed(int id); // get current source id. virtual int source_id(); virtual int pre_source_id(); // logic data methods public: virtual bool can_publish(bool is_edge); - virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata); + virtual srs_error_t on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata); public: - virtual int on_audio(SrsCommonMessage* audio); + virtual srs_error_t on_audio(SrsCommonMessage* audio); private: - virtual int on_audio_imp(SrsSharedPtrMessage* audio); + virtual srs_error_t on_audio_imp(SrsSharedPtrMessage* audio); public: - virtual int on_video(SrsCommonMessage* video); + virtual srs_error_t on_video(SrsCommonMessage* video); private: - virtual int on_video_imp(SrsSharedPtrMessage* video); + virtual srs_error_t on_video_imp(SrsSharedPtrMessage* video); public: - virtual int on_aggregate(SrsCommonMessage* msg); + virtual srs_error_t on_aggregate(SrsCommonMessage* msg); /** * publish stream event notify. * @param _req the request from client, the source will deep copy it, * for when reload the request of client maybe invalid. */ - virtual int on_publish(); + virtual srs_error_t on_publish(); virtual void on_unpublish(); // consumer methods public: @@ -669,16 +669,16 @@ public: * @param dm, whether dumps the metadata. * @param dg, whether dumps the gop cache. */ - virtual int create_consumer(SrsConnection* conn, SrsConsumer*& consumer, bool ds = true, bool dm = true, bool dg = true); + virtual srs_error_t create_consumer(SrsConnection* conn, SrsConsumer*& consumer, bool ds = true, bool dm = true, bool dg = true); virtual void on_consumer_destroy(SrsConsumer* consumer); virtual void set_cache(bool enabled); virtual SrsRtmpJitterAlgorithm jitter(); // internal public: // for edge, when publish edge stream, check the state - virtual int on_edge_start_publish(); + virtual srs_error_t on_edge_start_publish(); // for edge, proxy the publish - virtual int on_edge_proxy_publish(SrsCommonMessage* msg); + virtual srs_error_t on_edge_proxy_publish(SrsCommonMessage* msg); // for edge, proxy stop publish virtual void on_edge_proxy_unpublish(); public: diff --git a/trunk/src/kernel/srs_kernel_codec.cpp b/trunk/src/kernel/srs_kernel_codec.cpp index b151f302d..fe43cd63b 100644 --- a/trunk/src/kernel/srs_kernel_codec.cpp +++ b/trunk/src/kernel/srs_kernel_codec.cpp @@ -420,30 +420,28 @@ SrsFrame::~SrsFrame() srs_freep(codec); } -int SrsFrame::initialize(SrsCodecConfig* c) +srs_error_t SrsFrame::initialize(SrsCodecConfig* c) { codec = c; nb_samples = 0; dts = 0; cts = 0; - return ERROR_SUCCESS; + return srs_success; } -int SrsFrame::add_sample(char* bytes, int size) +srs_error_t SrsFrame::add_sample(char* bytes, int size) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (nb_samples >= SrsMaxNbSamples) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("Frame samples overflow, max=%d. ret=%d", SrsMaxNbSamples, ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "Frame samples overflow"); } SrsSample* sample = &samples[nb_samples++]; sample->bytes = bytes; sample->size = size; - return ret; + return err; } SrsAudioFrame::SrsAudioFrame() @@ -472,12 +470,12 @@ SrsVideoFrame::~SrsVideoFrame() { } -int SrsVideoFrame::add_sample(char* bytes, int size) +srs_error_t SrsVideoFrame::add_sample(char* bytes, int size) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; - if ((ret = SrsFrame::add_sample(bytes, size)) != ERROR_SUCCESS) { - return ret; + if ((err = SrsFrame::add_sample(bytes, size)) != srs_success) { + return srs_error_wrap(err, "add frame"); } // for video, parse the nalu type, set the IDR flag. @@ -495,7 +493,7 @@ int SrsVideoFrame::add_sample(char* bytes, int size) first_nalu_type = nal_unit_type; } - return ret; + return err; } SrsVideoCodecConfig* SrsVideoFrame::vcodec() @@ -529,24 +527,23 @@ srs_error_t SrsFormat::initialize() return srs_success; } -int SrsFormat::on_audio(int64_t timestamp, char* data, int size) +srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!data || size <= 0) { srs_trace("no audio present, ignore it."); - return ret; + return err; } if ((ret = buffer->initialize(data, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init buffer"); } // audio decode if (!buffer->require(1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("aac decode sound_format failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "aac decode sound_format"); } // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76 @@ -554,7 +551,7 @@ int SrsFormat::on_audio(int64_t timestamp, char* data, int size) SrsAudioCodecId codec = (SrsAudioCodecId)((v >> 4) & 0x0f); if (codec != SrsAudioCodecIdMP3 && codec != SrsAudioCodecIdAAC) { - return ret; + return err; } if (!acodec) { @@ -564,8 +561,8 @@ int SrsFormat::on_audio(int64_t timestamp, char* data, int size) audio = new SrsAudioFrame(); } - if ((ret = audio->initialize(acodec)) != ERROR_SUCCESS) { - return ret; + if ((err = audio->initialize(acodec)) != srs_success) { + return srs_error_wrap(err, "init audio"); } // Parse by specified codec. @@ -578,24 +575,23 @@ int SrsFormat::on_audio(int64_t timestamp, char* data, int size) return audio_aac_demux(buffer, timestamp); } -int SrsFormat::on_video(int64_t timestamp, char* data, int size) +srs_error_t SrsFormat::on_video(int64_t timestamp, char* data, int size) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!data || size <= 0) { srs_trace("no video present, ignore it."); - return ret; + return err; } if ((ret = buffer->initialize(data, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init buffer"); } // video decode if (!buffer->require(1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode frame_type failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode frame_type"); } // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78 @@ -604,7 +600,7 @@ int SrsFormat::on_video(int64_t timestamp, char* data, int size) // TODO: Support other codecs. if (codec_id != SrsVideoCodecIdAVC) { - return ret; + return err; } if (!vcodec) { @@ -614,17 +610,17 @@ int SrsFormat::on_video(int64_t timestamp, char* data, int size) video = new SrsVideoFrame(); } - if ((ret = video->initialize(vcodec)) != ERROR_SUCCESS) { - return ret; + if ((err = video->initialize(vcodec)) != srs_success) { + return srs_error_wrap(err, "init video"); } buffer->skip(-1 * buffer->pos()); return video_avc_demux(buffer, timestamp); } -int SrsFormat::on_aac_sequence_header(char* data, int size) +srs_error_t SrsFormat::on_aac_sequence_header(char* data, int size) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (!acodec) { acodec = new SrsAudioCodecConfig(); @@ -633,8 +629,8 @@ int SrsFormat::on_aac_sequence_header(char* data, int size) audio = new SrsAudioFrame(); } - if ((ret = audio->initialize(acodec)) != ERROR_SUCCESS) { - return ret; + if ((err = audio->initialize(acodec)) != srs_success) { + return srs_error_wrap(err, "init audio"); } return audio_aac_sequence_header_demux(data, size); @@ -652,9 +648,9 @@ bool SrsFormat::is_avc_sequence_header() && video && video->avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader; } -int SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp) +srs_error_t SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78 int8_t frame_type = stream->read_1bytes(); @@ -666,22 +662,18 @@ int SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp) // ignore info frame without error, // @see https://github.com/ossrs/srs/issues/288#issuecomment-69863909 if (video->frame_type == SrsVideoAvcFrameTypeVideoInfoFrame) { - srs_warn("avc igone the info frame, ret=%d", ret); - return ret; + srs_warn("avc igone the info frame"); + return err; } // only support h.264/avc if (codec_id != SrsVideoCodecIdAVC) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc only support video h.264/avc codec. actual=%d, ret=%d", codec_id, ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc only support video h.264/avc, actual=%d", codec_id); } vcodec->id = codec_id; if (!stream->require(4)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode avc_packet_type failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc decode avc_packet_type"); } int8_t avc_packet_type = stream->read_1bytes(); int32_t composition_time = stream->read_3bytes(); @@ -696,27 +688,22 @@ int SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp) nb_raw = stream->size() - stream->pos(); if (avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader) { - if ((ret = avc_demux_sps_pps(stream)) != ERROR_SUCCESS) { - return ret; + if ((err = avc_demux_sps_pps(stream)) != srs_success) { + return srs_error_wrap(err, "demux SPS/PPS"); } } else if (avc_packet_type == SrsVideoAvcFrameTraitNALU){ - if ((ret = video_nalu_demux(stream)) != ERROR_SUCCESS) { - return ret; + if ((err = video_nalu_demux(stream)) != srs_success) { + return srs_error_wrap(err, "demux NALU"); } } else { // ignored. } - srs_info("avc decoded, type=%d, codec=%d, avc=%d, cts=%d, size=%d", frame_type, codec_id, avc_packet_type, - composition_time, stream->size() - stream->pos()); - - return ret; + return err; } -int SrsFormat::avc_demux_sps_pps(SrsBuffer* stream) +srs_error_t SrsFormat::avc_demux_sps_pps(SrsBuffer* stream) { - int ret = ERROR_SUCCESS; - // AVCDecoderConfigurationRecord // 5.2.4.1.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16 int avc_extra_size = stream->size() - stream->pos(); @@ -726,9 +713,7 @@ int SrsFormat::avc_demux_sps_pps(SrsBuffer* stream) } if (!stream->require(6)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc decode sequence header"); } //int8_t configurationVersion = stream->read_1bytes(); stream->read_1bytes(); @@ -750,35 +735,25 @@ int SrsFormat::avc_demux_sps_pps(SrsBuffer* stream) // The value of this field shall be one of 0, 1, or 3 corresponding to a // length encoded with 1, 2, or 4 bytes, respectively. if (vcodec->NAL_unit_length == 2) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps lengthSizeMinusOne should never be 2. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps lengthSizeMinusOne should never be 2"); } // 1 sps, 7.3.2.1 Sequence parameter set RBSP syntax // ISO_IEC_14496-10-AVC-2003.pdf, page 45. if (!stream->require(1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header sps failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS"); } int8_t numOfSequenceParameterSets = stream->read_1bytes(); numOfSequenceParameterSets &= 0x1f; if (numOfSequenceParameterSets != 1) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header sps failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS"); } if (!stream->require(2)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header sps size failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS size"); } uint16_t sequenceParameterSetLength = stream->read_2bytes(); if (!stream->require(sequenceParameterSetLength)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header sps data failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS data"); } if (sequenceParameterSetLength > 0) { vcodec->sequenceParameterSetNALUnit.resize(sequenceParameterSetLength); @@ -786,27 +761,19 @@ int SrsFormat::avc_demux_sps_pps(SrsBuffer* stream) } // 1 pps if (!stream->require(1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header pps failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS"); } int8_t numOfPictureParameterSets = stream->read_1bytes(); numOfPictureParameterSets &= 0x1f; if (numOfPictureParameterSets != 1) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header pps failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS"); } if (!stream->require(2)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header pps size failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS size"); } uint16_t pictureParameterSetLength = stream->read_2bytes(); if (!stream->require(pictureParameterSetLength)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sequenc header pps data failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS data"); } if (pictureParameterSetLength > 0) { vcodec->pictureParameterSetNALUnit.resize(pictureParameterSetLength); @@ -816,45 +783,40 @@ int SrsFormat::avc_demux_sps_pps(SrsBuffer* stream) return avc_demux_sps(); } -int SrsFormat::avc_demux_sps() +srs_error_t SrsFormat::avc_demux_sps() { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if (vcodec->sequenceParameterSetNALUnit.empty()) { - return ret; + return err; } SrsBuffer stream; char* sps = &vcodec->sequenceParameterSetNALUnit[0]; int nbsps = (int)vcodec->sequenceParameterSetNALUnit.size(); if ((ret = stream.initialize(sps, nbsps)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init stream"); } // for NALU, 7.3.1 NAL unit syntax // ISO_IEC_14496-10-AVC-2012.pdf, page 61. if (!stream.require(1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode sps failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS"); } int8_t nutv = stream.read_1bytes(); // forbidden_zero_bit shall be equal to 0. int8_t forbidden_zero_bit = (nutv >> 7) & 0x01; if (forbidden_zero_bit) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("forbidden_zero_bit shall be equal to 0. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "forbidden_zero_bit shall be equal to 0"); } // nal_ref_idc not equal to 0 specifies that the content of the NAL unit contains a sequence parameter set or a picture // parameter set or a slice of a reference picture or a slice data partition of a reference picture. int8_t nal_ref_idc = (nutv >> 5) & 0x03; if (!nal_ref_idc) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("for sps, nal_ref_idc shall be not be equal to 0. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "for sps, nal_ref_idc shall be not be equal to 0"); } // 7.4.1 NAL unit semantics @@ -862,9 +824,7 @@ int SrsFormat::avc_demux_sps() // nal_unit_type specifies the type of RBSP data structure contained in the NAL unit as specified in Table 7-1. SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(nutv & 0x1f); if (nal_unit_type != 7) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("for sps, nal_unit_type shall be equal to 7. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "for sps, nal_unit_type shall be equal to 7"); } // decode the rbsp from sps. @@ -894,106 +854,95 @@ int SrsFormat::avc_demux_sps() } -int SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) +srs_error_t SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // we donot parse the detail of sps. // @see https://github.com/ossrs/srs/issues/474 if (!avc_parse_sps) { - return ret; + return err; } // reparse the rbsp. SrsBuffer stream; if ((ret = stream.initialize(rbsp, nb_rbsp)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init stream"); } // for SPS, 7.3.2.1.1 Sequence parameter set data syntax // ISO_IEC_14496-10-AVC-2012.pdf, page 62. if (!stream.require(3)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps shall atleast 3bytes. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps shall atleast 3bytes"); } uint8_t profile_idc = stream.read_1bytes(); if (!profile_idc) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps the profile_idc invalid. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the profile_idc invalid"); } int8_t flags = stream.read_1bytes(); if (flags & 0x03) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps the flags invalid. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the flags invalid"); } uint8_t level_idc = stream.read_1bytes(); if (!level_idc) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps the level_idc invalid. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the level_idc invalid"); } SrsBitBuffer bs; if ((ret = bs.initialize(&stream)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init bit buffer"); } int32_t seq_parameter_set_id = -1; if ((ret = srs_avc_nalu_read_uev(&bs, seq_parameter_set_id)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read seq_parameter_set_id"); } if (seq_parameter_set_id < 0) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps the seq_parameter_set_id invalid. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the seq_parameter_set_id invalid"); } - srs_info("sps parse profile=%d, level=%d, sps_id=%d", profile_idc, level_idc, seq_parameter_set_id); int32_t chroma_format_idc = -1; if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 || profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc == 118 - || profile_idc == 128 - ) { + || profile_idc == 128) { if ((ret = srs_avc_nalu_read_uev(&bs, chroma_format_idc)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read chroma_format_idc"); } if (chroma_format_idc == 3) { int8_t separate_colour_plane_flag = -1; if ((ret = srs_avc_nalu_read_bit(&bs, separate_colour_plane_flag)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read separate_colour_plane_flag"); } } int32_t bit_depth_luma_minus8 = -1; if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_luma_minus8)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read bit_depth_luma_minus8");; } int32_t bit_depth_chroma_minus8 = -1; if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_chroma_minus8)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read bit_depth_chroma_minus8");; } int8_t qpprime_y_zero_transform_bypass_flag = -1; if ((ret = srs_avc_nalu_read_bit(&bs, qpprime_y_zero_transform_bypass_flag)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read qpprime_y_zero_transform_bypass_flag");; } int8_t seq_scaling_matrix_present_flag = -1; if ((ret = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read seq_scaling_matrix_present_flag");; } if (seq_scaling_matrix_present_flag) { int nb_scmpfs = ((chroma_format_idc != 3)? 8:12); for (int i = 0; i < nb_scmpfs; i++) { int8_t seq_scaling_matrix_present_flag_i = -1; if ((ret = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag_i)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read seq_scaling_matrix_present_flag_i");; } } } @@ -1001,147 +950,140 @@ int SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp) int32_t log2_max_frame_num_minus4 = -1; if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_frame_num_minus4)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read log2_max_frame_num_minus4");; } int32_t pic_order_cnt_type = -1; if ((ret = srs_avc_nalu_read_uev(&bs, pic_order_cnt_type)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read pic_order_cnt_type");; } if (pic_order_cnt_type == 0) { int32_t log2_max_pic_order_cnt_lsb_minus4 = -1; if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_pic_order_cnt_lsb_minus4)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read log2_max_pic_order_cnt_lsb_minus4");; } } else if (pic_order_cnt_type == 1) { int8_t delta_pic_order_always_zero_flag = -1; if ((ret = srs_avc_nalu_read_bit(&bs, delta_pic_order_always_zero_flag)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read delta_pic_order_always_zero_flag");; } int32_t offset_for_non_ref_pic = -1; if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_non_ref_pic)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read offset_for_non_ref_pic");; } int32_t offset_for_top_to_bottom_field = -1; if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_top_to_bottom_field)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read offset_for_top_to_bottom_field");; } int32_t num_ref_frames_in_pic_order_cnt_cycle = -1; if ((ret = srs_avc_nalu_read_uev(&bs, num_ref_frames_in_pic_order_cnt_cycle)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read num_ref_frames_in_pic_order_cnt_cycle");; } if (num_ref_frames_in_pic_order_cnt_cycle < 0) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("sps the num_ref_frames_in_pic_order_cnt_cycle invalid. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the num_ref_frames_in_pic_order_cnt_cycle"); } for (int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) { int32_t offset_for_ref_frame_i = -1; if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_ref_frame_i)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read offset_for_ref_frame_i");; } } } int32_t max_num_ref_frames = -1; if ((ret = srs_avc_nalu_read_uev(&bs, max_num_ref_frames)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read max_num_ref_frames");; } int8_t gaps_in_frame_num_value_allowed_flag = -1; if ((ret = srs_avc_nalu_read_bit(&bs, gaps_in_frame_num_value_allowed_flag)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read gaps_in_frame_num_value_allowed_flag");; } int32_t pic_width_in_mbs_minus1 = -1; if ((ret = srs_avc_nalu_read_uev(&bs, pic_width_in_mbs_minus1)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read pic_width_in_mbs_minus1");; } int32_t pic_height_in_map_units_minus1 = -1; if ((ret = srs_avc_nalu_read_uev(&bs, pic_height_in_map_units_minus1)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "read pic_height_in_map_units_minus1");; } vcodec->width = (int)(pic_width_in_mbs_minus1 + 1) * 16; vcodec->height = (int)(pic_height_in_map_units_minus1 + 1) * 16; - return ret; + return err; } -int SrsFormat::video_nalu_demux(SrsBuffer* stream) +srs_error_t SrsFormat::video_nalu_demux(SrsBuffer* stream) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // ensure the sequence header demuxed if (!vcodec->is_avc_codec_ok()) { - srs_warn("avc ignore type=%d for no sequence header. ret=%d", SrsVideoAvcFrameTraitNALU, ret); - return ret; + srs_warn("avc ignore type=%d for no sequence header", SrsVideoAvcFrameTraitNALU); + return err; } // guess for the first time. if (vcodec->payload_format == SrsAvcPayloadFormatGuess) { // One or more NALUs (Full frames are required) // try "AnnexB" from ISO_IEC_14496-10-AVC-2003.pdf, page 211. - if ((ret = avc_demux_annexb_format(stream)) != ERROR_SUCCESS) { + if ((err = avc_demux_annexb_format(stream)) != srs_success) { // stop try when system error. - if (ret != ERROR_HLS_AVC_TRY_OTHERS) { - srs_error("avc demux for annexb failed. ret=%d", ret); - return ret; + if (srs_error_code(err) != ERROR_HLS_AVC_TRY_OTHERS) { + return srs_error_wrap(err, "avc demux for annexb"); } + srs_freep(err); // try "ISO Base Media File Format" from ISO_IEC_14496-15-AVC-format-2012.pdf, page 20 - if ((ret = avc_demux_ibmf_format(stream)) != ERROR_SUCCESS) { - return ret; + if ((err = avc_demux_ibmf_format(stream)) != srs_success) { + return srs_error_wrap(err, "avc demux ibmf"); } else { vcodec->payload_format = SrsAvcPayloadFormatIbmf; - srs_info("hls guess avc payload is ibmf format."); } } else { vcodec->payload_format = SrsAvcPayloadFormatAnnexb; - srs_info("hls guess avc payload is annexb format."); } } else if (vcodec->payload_format == SrsAvcPayloadFormatIbmf) { // try "ISO Base Media File Format" from ISO_IEC_14496-15-AVC-format-2012.pdf, page 20 - if ((ret = avc_demux_ibmf_format(stream)) != ERROR_SUCCESS) { - return ret; + if ((err = avc_demux_ibmf_format(stream)) != srs_success) { + return srs_error_wrap(err, "avc demux ibmf"); } - srs_info("hls decode avc payload in ibmf format."); } else { // One or more NALUs (Full frames are required) // try "AnnexB" from ISO_IEC_14496-10-AVC-2003.pdf, page 211. - if ((ret = avc_demux_annexb_format(stream)) != ERROR_SUCCESS) { + if ((err = avc_demux_annexb_format(stream)) != srs_success) { // ok, we guess out the payload is annexb, but maybe changed to ibmf. - if (ret != ERROR_HLS_AVC_TRY_OTHERS) { - srs_error("avc demux for annexb failed. ret=%d", ret); - return ret; + if (srs_error_code(err) != ERROR_HLS_AVC_TRY_OTHERS) { + return srs_error_wrap(err, "avc demux annexb"); } + srs_freep(err); // try "ISO Base Media File Format" from ISO_IEC_14496-15-AVC-format-2012.pdf, page 20 - if ((ret = avc_demux_ibmf_format(stream)) != ERROR_SUCCESS) { - return ret; + if ((err = avc_demux_ibmf_format(stream)) != srs_success) { + return srs_error_wrap(err, "avc demux ibmf"); } else { vcodec->payload_format = SrsAvcPayloadFormatIbmf; - srs_warn("hls avc payload change from annexb to ibmf format."); } } - srs_info("hls decode avc payload in annexb format."); } - return ret; + return err; } -int SrsFormat::avc_demux_annexb_format(SrsBuffer* stream) +srs_error_t SrsFormat::avc_demux_annexb_format(SrsBuffer* stream) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // not annexb, try others if (!srs_avc_startswith_annexb(stream, NULL)) { - return ERROR_HLS_AVC_TRY_OTHERS; + return srs_error_new(ERROR_HLS_AVC_TRY_OTHERS, "try others"); } // AnnexB @@ -1151,7 +1093,7 @@ int SrsFormat::avc_demux_annexb_format(SrsBuffer* stream) // find start code int nb_start_code = 0; if (!srs_avc_startswith_annexb(stream, &nb_start_code)) { - return ret; + return err; } // skip the start code. @@ -1179,18 +1121,17 @@ int SrsFormat::avc_demux_annexb_format(SrsBuffer* stream) } // got the NALU. - if ((ret = video->add_sample(p, (int)(pp - p))) != ERROR_SUCCESS) { - srs_error("annexb add video sample failed. ret=%d", ret); - return ret; + if ((err = video->add_sample(p, (int)(pp - p))) != srs_success) { + return srs_error_wrap(err, "add video frame"); } } - return ret; + return err; } -int SrsFormat::avc_demux_ibmf_format(SrsBuffer* stream) +srs_error_t SrsFormat::avc_demux_ibmf_format(SrsBuffer* stream) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; int PictureLength = stream->size() - stream->pos(); @@ -1205,9 +1146,7 @@ int SrsFormat::avc_demux_ibmf_format(SrsBuffer* stream) for (int i = 0; i < PictureLength;) { // unsigned int((NAL_unit_length+1)*8) NALUnitLength; if (!stream->require(vcodec->NAL_unit_length + 1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode NALU size failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc decode NALU size"); } int32_t NALUnitLength = 0; if (vcodec->NAL_unit_length == 3) { @@ -1221,33 +1160,28 @@ int SrsFormat::avc_demux_ibmf_format(SrsBuffer* stream) // maybe stream is invalid format. // see: https://github.com/ossrs/srs/issues/183 if (NALUnitLength < 0) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("maybe stream is AnnexB format. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "maybe stream is AnnexB format"); } // NALUnit if (!stream->require(NALUnitLength)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("avc decode NALU data failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc decode NALU data"); } // 7.3.1 NAL unit syntax, ISO_IEC_14496-10-AVC-2003.pdf, page 44. - if ((ret = video->add_sample(stream->data() + stream->pos(), NALUnitLength)) != ERROR_SUCCESS) { - srs_error("avc add video sample failed. ret=%d", ret); - return ret; + if ((err = video->add_sample(stream->data() + stream->pos(), NALUnitLength)) != srs_success) { + return srs_error_wrap(err, "avc add video frame"); } stream->skip(NALUnitLength); i += vcodec->NAL_unit_length + 1 + NALUnitLength; } - return ret; + return err; } -int SrsFormat::audio_aac_demux(SrsBuffer* stream, int64_t timestamp) +srs_error_t SrsFormat::audio_aac_demux(SrsBuffer* stream, int64_t timestamp) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; audio->cts = 0; audio->dts = timestamp; @@ -1269,20 +1203,16 @@ int SrsFormat::audio_aac_demux(SrsBuffer* stream, int64_t timestamp) // we support h.264+mp3 for hls. if (codec_id == SrsAudioCodecIdMP3) { - return ERROR_HLS_TRY_MP3; + return srs_error_new(ERROR_HLS_TRY_MP3, "try mp3"); } // only support aac if (codec_id != SrsAudioCodecIdAAC) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("aac only support mp3/aac codec. actual=%d, ret=%d", codec_id, ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "not supported codec %d", codec_id); } if (!stream->require(1)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("aac decode aac_packet_type failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "aac decode aac_packet_type"); } SrsAudioAacFrameTrait aac_packet_type = (SrsAudioAacFrameTrait)stream->read_1bytes(); @@ -1300,22 +1230,21 @@ int SrsFormat::audio_aac_demux(SrsBuffer* stream, int64_t timestamp) char *copy_stream_from = stream->data() + stream->pos(); acodec->aac_extra_data = std::vector(copy_stream_from, copy_stream_from + aac_extra_size); - if ((ret = audio_aac_sequence_header_demux(&acodec->aac_extra_data[0], aac_extra_size)) != ERROR_SUCCESS) { - return ret; + if ((err = audio_aac_sequence_header_demux(&acodec->aac_extra_data[0], aac_extra_size)) != srs_success) { + return srs_error_wrap(err, "demux aac sh"); } } } else if (aac_packet_type == SrsAudioAacFrameTraitRawData) { // ensure the sequence header demuxed if (!acodec->is_aac_codec_ok()) { - srs_warn("aac ignore type=%d for no sequence header. ret=%d", aac_packet_type, ret); - return ret; + srs_warn("aac ignore type=%d for no sequence header", aac_packet_type); + return err; } // Raw AAC frame data in UI8 [] // 6.3 Raw Data, ISO_IEC_13818-7-AAC-2004.pdf, page 28 - if ((ret = audio->add_sample(stream->data() + stream->pos(), stream->size() - stream->pos())) != ERROR_SUCCESS) { - srs_error("aac add sample failed. ret=%d", ret); - return ret; + if ((err = audio->add_sample(stream->data() + stream->pos(), stream->size() - stream->pos())) != srs_success) { + return srs_error_wrap(err, "add audio frame"); } } else { // ignored. @@ -1344,15 +1273,12 @@ int SrsFormat::audio_aac_demux(SrsBuffer* stream, int64_t timestamp) }; } - srs_info("aac decoded, type=%d, codec=%d, asize=%d, rate=%d, format=%d, size=%d", sound_type, codec_id, sound_size, - sound_rate, sound_format, stream->size() - stream->pos()); - - return ret; + return err; } -int SrsFormat::audio_mp3_demux(SrsBuffer* stream, int64_t timestamp) +srs_error_t SrsFormat::audio_mp3_demux(SrsBuffer* stream, int64_t timestamp) { - int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; audio->cts = 0; audio->dts = timestamp; @@ -1381,30 +1307,27 @@ int SrsFormat::audio_mp3_demux(SrsBuffer* stream, int64_t timestamp) stream->skip(1); if (stream->empty()) { - return ret; + return err; } char* data = stream->data() + stream->pos(); int size = stream->size() - stream->pos(); // mp3 payload. - if ((ret = audio->add_sample(data, size)) != ERROR_SUCCESS) { - srs_error("audio codec add mp3 sample failed. ret=%d", ret); - return ret; + if ((err = audio->add_sample(data, size)) != srs_success) { + return srs_error_wrap(err, "add audio frame"); } - srs_info("audio decoded, codec=%d, ssize=%d, srate=%d, channels=%d, size=%d", - acodec->id, acodec->sound_size, acodec->sound_rate, acodec->sound_type, size); - - return ret; + return err; } -int SrsFormat::audio_aac_sequence_header_demux(char* data, int size) +srs_error_t SrsFormat::audio_aac_sequence_header_demux(char* data, int size) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; if ((ret = buffer->initialize(data, size)) != ERROR_SUCCESS) { - return ret; + return srs_error_new(ret, "init buffer"); } // only need to decode the first 2bytes: @@ -1412,9 +1335,7 @@ int SrsFormat::audio_aac_sequence_header_demux(char* data, int size) // samplingFrequencyIndex, aac_sample_rate, 4bits. // channelConfiguration, aac_channels, 4bits if (!buffer->require(2)) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("audio codec decode aac sequence header failed. ret=%d", ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "audio codec decode aac sh"); } uint8_t profile_ObjectType = buffer->read_1bytes(); uint8_t samplingFrequencyIndex = buffer->read_1bytes(); @@ -1429,10 +1350,7 @@ int SrsFormat::audio_aac_sequence_header_demux(char* data, int size) // convert the object type in sequence header to aac profile of ADTS. acodec->aac_object = (SrsAacObjectType)profile_ObjectType; if (acodec->aac_object == SrsAacObjectTypeReserved) { - ret = ERROR_HLS_DECODE_ERROR; - srs_error("audio codec decode aac sequence header failed, " - "adts object=%d invalid. ret=%d", profile_ObjectType, ret); - return ret; + return srs_error_new(ERROR_HLS_DECODE_ERROR, "aac decode sh object %d", profile_ObjectType); } // TODO: FIXME: to support aac he/he-v2, see: ngx_rtmp_codec_parse_aac_header @@ -1447,6 +1365,6 @@ int SrsFormat::audio_aac_sequence_header_demux(char* data, int size) //aac_profile = 1; //} - return ret; + return err; } diff --git a/trunk/src/kernel/srs_kernel_codec.hpp b/trunk/src/kernel/srs_kernel_codec.hpp index 8299c6349..50c2a469f 100644 --- a/trunk/src/kernel/srs_kernel_codec.hpp +++ b/trunk/src/kernel/srs_kernel_codec.hpp @@ -621,9 +621,9 @@ public: virtual ~SrsFrame(); public: // Initialize the frame, to parse sampels. - virtual int initialize(SrsCodecConfig* c); + virtual srs_error_t initialize(SrsCodecConfig* c); // Add a sample to frame. - virtual int add_sample(char* bytes, int size); + virtual srs_error_t add_sample(char* bytes, int size); }; /** @@ -662,7 +662,7 @@ public: virtual ~SrsVideoFrame(); public: // Add the sample without ANNEXB or IBMF header, or RAW AAC or MP3 data. - virtual int add_sample(char* bytes, int size); + virtual srs_error_t add_sample(char* bytes, int size); public: virtual SrsVideoCodecConfig* vcodec(); }; @@ -696,12 +696,12 @@ public: virtual srs_error_t initialize(); // When got a parsed audio packet. // @param data The data in FLV format. - virtual int on_audio(int64_t timestamp, char* data, int size); + virtual srs_error_t on_audio(int64_t timestamp, char* data, int size); // When got a parsed video packet. // @param data The data in FLV format. - virtual int on_video(int64_t timestamp, char* data, int size); + virtual srs_error_t on_video(int64_t timestamp, char* data, int size); // When got a audio aac sequence header. - virtual int on_aac_sequence_header(char* data, int size); + virtual srs_error_t on_aac_sequence_header(char* data, int size); public: virtual bool is_aac_sequence_header(); virtual bool is_avc_sequence_header(); @@ -710,28 +710,28 @@ private: // The packet is muxed in FLV format, defined in flv specification. // Demux the sps/pps from sequence header. // Demux the samples from NALUs. - virtual int video_avc_demux(SrsBuffer* stream, int64_t timestamp); + virtual srs_error_t video_avc_demux(SrsBuffer* stream, int64_t timestamp); private: // Parse the H.264 SPS/PPS. - virtual int avc_demux_sps_pps(SrsBuffer* stream); - virtual int avc_demux_sps(); - virtual int avc_demux_sps_rbsp(char* rbsp, int nb_rbsp); + virtual srs_error_t avc_demux_sps_pps(SrsBuffer* stream); + virtual srs_error_t avc_demux_sps(); + virtual srs_error_t avc_demux_sps_rbsp(char* rbsp, int nb_rbsp); private: // Parse the H.264 NALUs. - virtual int video_nalu_demux(SrsBuffer* stream); + virtual srs_error_t video_nalu_demux(SrsBuffer* stream); // Demux the avc NALU in "AnnexB" from ISO_IEC_14496-10-AVC-2003.pdf, page 211. - virtual int avc_demux_annexb_format(SrsBuffer* stream); + virtual srs_error_t avc_demux_annexb_format(SrsBuffer* stream); // Demux the avc NALU in "ISO Base Media File Format" from ISO_IEC_14496-15-AVC-format-2012.pdf, page 20 - virtual int avc_demux_ibmf_format(SrsBuffer* stream); + virtual srs_error_t avc_demux_ibmf_format(SrsBuffer* stream); private: // Demux the audio packet in AAC codec. // Demux the asc from sequence header. // Demux the sampels from RAW data. - virtual int audio_aac_demux(SrsBuffer* stream, int64_t timestamp); - virtual int audio_mp3_demux(SrsBuffer* stream, int64_t timestamp); + virtual srs_error_t audio_aac_demux(SrsBuffer* stream, int64_t timestamp); + virtual srs_error_t audio_mp3_demux(SrsBuffer* stream, int64_t timestamp); public: // Directly demux the sequence header, without RTMP packet header. - virtual int audio_aac_sequence_header_demux(char* data, int size); + virtual srs_error_t audio_aac_sequence_header_demux(char* data, int size); }; #endif diff --git a/trunk/src/kernel/srs_kernel_error.cpp b/trunk/src/kernel/srs_kernel_error.cpp index 9b857d24e..d0ebe60fb 100644 --- a/trunk/src/kernel/srs_kernel_error.cpp +++ b/trunk/src/kernel/srs_kernel_error.cpp @@ -37,6 +37,12 @@ bool srs_is_system_control_error(int error_code) || error_code == ERROR_CONTROL_REDIRECT; } +bool srs_is_system_control_error(srs_error_t err) +{ + int error_code = srs_error_code(err); + return srs_is_system_control_error(error_code); +} + bool srs_is_client_gracefully_close(int error_code) { return error_code == ERROR_SOCKET_READ @@ -44,6 +50,12 @@ bool srs_is_client_gracefully_close(int error_code) || error_code == ERROR_SOCKET_WRITE; } +bool srs_is_client_gracefully_close(srs_error_t err) +{ + int error_code = srs_error_code(err); + return srs_is_client_gracefully_close(error_code); +} + SrsCplxError::SrsCplxError() { code = ERROR_SUCCESS; diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index f43cc2a51..88eb11881 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -325,7 +325,7 @@ // user-define error. /////////////////////////////////////////////////////// #define ERROR_USER_START 9000 -#define ERROR_USER_DISCONNECT 9001 +//#define ERROR_USER_DISCONNECT 9001 #define ERROR_SOURCE_NOT_FOUND 9002 #define ERROR_USER_END 9999 @@ -334,7 +334,9 @@ */ // TODO: FIXME: Remove it from underlayer for confused with error and logger. extern bool srs_is_system_control_error(int error_code); +extern bool srs_is_system_control_error(srs_error_t err); extern bool srs_is_client_gracefully_close(int error_code); +extern bool srs_is_client_gracefully_close(srs_error_t err); // Use complex errors, @read https://github.com/ossrs/srs/issues/913 class SrsCplxError @@ -374,6 +376,7 @@ public: #define srs_error_copy(err) SrsCplxError::copy(err) #define srs_error_desc(err) SrsCplxError::description(err) #define srs_error_code(err) SrsCplxError::error_code(err) +#define srs_error_reset(err) srs_freep(err); err = srs_success #endif diff --git a/trunk/src/kernel/srs_kernel_ts.cpp b/trunk/src/kernel/srs_kernel_ts.cpp index 6c76569d1..0c0dab6f8 100644 --- a/trunk/src/kernel/srs_kernel_ts.cpp +++ b/trunk/src/kernel/srs_kernel_ts.cpp @@ -2957,11 +2957,10 @@ srs_error_t SrsTsTransmuxer::initialize(SrsFileWriter* fw) srs_error_t SrsTsTransmuxer::write_audio(int64_t timestamp, char* data, int size) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; - if ((ret = format->on_audio(timestamp, data, size)) != ERROR_SUCCESS) { - return srs_error_new(ret, "ts: format on audio"); + if ((err = format->on_audio(timestamp, data, size)) != srs_success) { + return srs_error_wrap(err, "ts: format on audio"); } // ts support audio codec: aac/mp3 @@ -2994,11 +2993,10 @@ srs_error_t SrsTsTransmuxer::write_audio(int64_t timestamp, char* data, int size srs_error_t SrsTsTransmuxer::write_video(int64_t timestamp, char* data, int size) { - int ret = ERROR_SUCCESS; srs_error_t err = srs_success; - if ((ret = format->on_video(timestamp, data, size)) != ERROR_SUCCESS) { - return srs_error_new(ret, "ts: on video"); + if ((err = format->on_video(timestamp, data, size)) != srs_success) { + return srs_error_wrap(err, "ts: on video"); } // ignore info frame, diff --git a/trunk/src/protocol/srs_protocol_format.cpp b/trunk/src/protocol/srs_protocol_format.cpp index a81d4e811..a23bed9d1 100644 --- a/trunk/src/protocol/srs_protocol_format.cpp +++ b/trunk/src/protocol/srs_protocol_format.cpp @@ -38,16 +38,13 @@ SrsRtmpFormat::~SrsRtmpFormat() { } -int SrsRtmpFormat::on_metadata(SrsOnMetaDataPacket* meta) +srs_error_t SrsRtmpFormat::on_metadata(SrsOnMetaDataPacket* meta) { - int ret = ERROR_SUCCESS; - // TODO: FIXME: Try to initialize format from metadata. - - return ret; + return srs_success; } -int SrsRtmpFormat::on_audio(SrsSharedPtrMessage* shared_audio) +srs_error_t SrsRtmpFormat::on_audio(SrsSharedPtrMessage* shared_audio) { SrsSharedPtrMessage* msg = shared_audio; char* data = msg->payload; @@ -56,12 +53,12 @@ int SrsRtmpFormat::on_audio(SrsSharedPtrMessage* shared_audio) return SrsFormat::on_audio(msg->timestamp, data, size); } -int SrsRtmpFormat::on_audio(int64_t timestamp, char* data, int size) +srs_error_t SrsRtmpFormat::on_audio(int64_t timestamp, char* data, int size) { return SrsFormat::on_audio(timestamp, data, size); } -int SrsRtmpFormat::on_video(SrsSharedPtrMessage* shared_video) +srs_error_t SrsRtmpFormat::on_video(SrsSharedPtrMessage* shared_video) { SrsSharedPtrMessage* msg = shared_video; char* data = msg->payload; @@ -70,7 +67,7 @@ int SrsRtmpFormat::on_video(SrsSharedPtrMessage* shared_video) return SrsFormat::on_video(msg->timestamp, data, size); } -int SrsRtmpFormat::on_video(int64_t timestamp, char* data, int size) +srs_error_t SrsRtmpFormat::on_video(int64_t timestamp, char* data, int size) { return SrsFormat::on_video(timestamp, data, size); } diff --git a/trunk/src/protocol/srs_protocol_format.hpp b/trunk/src/protocol/srs_protocol_format.hpp index c794c3207..0ed28cb74 100644 --- a/trunk/src/protocol/srs_protocol_format.hpp +++ b/trunk/src/protocol/srs_protocol_format.hpp @@ -41,13 +41,13 @@ public: virtual ~SrsRtmpFormat(); public: // Initialize the format from metadata, optional. - virtual int on_metadata(SrsOnMetaDataPacket* meta); + virtual srs_error_t on_metadata(SrsOnMetaDataPacket* meta); // When got a parsed audio packet. - virtual int on_audio(SrsSharedPtrMessage* shared_audio); - virtual int on_audio(int64_t timestamp, char* data, int size); + virtual srs_error_t on_audio(SrsSharedPtrMessage* shared_audio); + virtual srs_error_t on_audio(int64_t timestamp, char* data, int size); // When got a parsed video packet. - virtual int on_video(SrsSharedPtrMessage* shared_video); - virtual int on_video(int64_t timestamp, char* data, int size); + virtual srs_error_t on_video(SrsSharedPtrMessage* shared_video); + virtual srs_error_t on_video(int64_t timestamp, char* data, int size); }; #endif diff --git a/trunk/src/protocol/srs_rtsp_stack.cpp b/trunk/src/protocol/srs_rtsp_stack.cpp index 1bf1cd07e..0b1a0d722 100644 --- a/trunk/src/protocol/srs_rtsp_stack.cpp +++ b/trunk/src/protocol/srs_rtsp_stack.cpp @@ -219,6 +219,7 @@ int SrsRtpPacket::decode(SrsBuffer* stream) int SrsRtpPacket::decode_97(SrsBuffer* stream) { int ret = ERROR_SUCCESS; + srs_error_t err = srs_success; // atleast 2bytes content. if (!stream->require(2)) { @@ -265,7 +266,10 @@ int SrsRtpPacket::decode_97(SrsBuffer* stream) return ret; } - if ((ret = audio->add_sample(sample, sample_size)) != ERROR_SUCCESS) { + if ((err = audio->add_sample(sample, sample_size)) != srs_success) { + // TODO: FIXME: Use error + ret = srs_error_code(err); + srs_freep(err); srs_error("rtsp: rtp type97 add sample failed. ret=%d", ret); return ret; }