Skip to content

Commit

Permalink
For #913: TS/HLS/MPEGTS support complex error
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Sep 22, 2017
1 parent 20a4259 commit abcaba3
Show file tree
Hide file tree
Showing 24 changed files with 699 additions and 823 deletions.
6 changes: 3 additions & 3 deletions trunk/src/app/srs_app_async_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ SrsAsyncCallWorker::~SrsAsyncCallWorker()
srs_cond_destroy(wait);
}

int SrsAsyncCallWorker::execute(ISrsAsyncCallTask* t)
srs_error_t SrsAsyncCallWorker::execute(ISrsAsyncCallTask* t)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

tasks.push_back(t);
srs_cond_signal(wait);

return ret;
return err;
}

int SrsAsyncCallWorker::count()
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_async_call.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class SrsAsyncCallWorker : public ISrsCoroutineHandler
SrsAsyncCallWorker();
virtual ~SrsAsyncCallWorker();
public:
virtual int execute(ISrsAsyncCallTask* t);
virtual srs_error_t execute(ISrsAsyncCallTask* t);
virtual int count();
public:
virtual srs_error_t start();
Expand Down
29 changes: 24 additions & 5 deletions trunk/src/app/srs_app_dash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ SrsFragmentedMp4::~SrsFragmentedMp4()
int SrsFragmentedMp4::initialize(SrsRequest* r, bool video, SrsMpdWriter* mpd, uint32_t tid)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

string file_home;
string file_name;
Expand All @@ -97,7 +98,10 @@ int SrsFragmentedMp4::initialize(SrsRequest* r, bool video, SrsMpdWriter* mpd, u
string home = _srs_config->get_dash_path(r->vhost);
set_path(home + "/" + file_home + "/" + file_name);

if ((ret = create_dir()) != ERROR_SUCCESS) {
if ((err = create_dir()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}

Expand Down Expand Up @@ -146,6 +150,7 @@ int SrsFragmentedMp4::write(SrsSharedPtrMessage* shared_msg, SrsFormat* format)
int SrsFragmentedMp4::reap(uint64_t& dts)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

if ((ret = enc->flush(dts)) != ERROR_SUCCESS) {
srs_error("DASH: Flush encoder failed, ret=%d", ret);
Expand All @@ -154,7 +159,10 @@ int SrsFragmentedMp4::reap(uint64_t& dts)

srs_freep(fw);

if ((ret = rename()) != ERROR_SUCCESS) {
if ((err = rename()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}

Expand Down Expand Up @@ -191,6 +199,7 @@ srs_error_t SrsMpdWriter::initialize(SrsRequest* r)
int SrsMpdWriter::write(SrsFormat* format)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

// MPD is not expired?
if (last_update_mpd != -1 && srs_get_system_time_ms() - last_update_mpd < update_period) {
Expand All @@ -204,7 +213,10 @@ int SrsMpdWriter::write(SrsFormat* format)

fragment_home = srs_path_dirname(mpd_path) + "/" + req->stream;

if ((ret = srs_create_dir_recursively(full_home)) != ERROR_SUCCESS) {
if ((err = srs_create_dir_recursively(full_home)) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
srs_error("DASH: Create MPD home failed, home=%s, ret=%d", full_home.c_str(), ret);
return ret;
}
Expand Down Expand Up @@ -427,6 +439,7 @@ int SrsDashController::refresh_mpd(SrsFormat* format)
int SrsDashController::refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFormat* format)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

if (msg->size <= 0 || (msg->is_video() && !format->vcodec->is_avc_codec_ok())
|| (msg->is_audio() && !format->acodec->is_aac_codec_ok())) {
Expand All @@ -435,7 +448,10 @@ int SrsDashController::refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFormat* for
}

string full_home = home + "/" + req->app + "/" + req->stream;
if ((ret = srs_create_dir_recursively(full_home)) != ERROR_SUCCESS) {
if ((err = srs_create_dir_recursively(full_home)) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
srs_error("DASH: Create media home failed, home=%s, ret=%d", full_home.c_str(), ret);
return ret;
}
Expand All @@ -457,7 +473,10 @@ int SrsDashController::refresh_init_mp4(SrsSharedPtrMessage* msg, SrsFormat* for
return ret;
}

if ((ret = init_mp4->rename()) != ERROR_SUCCESS) {
if ((err = init_mp4->rename()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}

Expand Down
27 changes: 19 additions & 8 deletions trunk/src/app/srs_app_dvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ SrsFragment* SrsDvrSegmenter::current()
int SrsDvrSegmenter::open()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

// ignore when already open.
if (fs->is_open()) {
Expand All @@ -99,7 +100,10 @@ int SrsDvrSegmenter::open()
fragment->set_path(path);

// create dir first.
if ((ret = fragment->create_dir()) != ERROR_SUCCESS) {
if ((err = fragment->create_dir()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}

Expand Down Expand Up @@ -177,6 +181,7 @@ int SrsDvrSegmenter::write_video(SrsSharedPtrMessage* shared_video, SrsFormat* f
int SrsDvrSegmenter::close()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

// ignore when already closed.
if (!fs->is_open()) {
Expand All @@ -191,13 +196,19 @@ int SrsDvrSegmenter::close()
fs->close();

// when tmp flv file exists, reap it.
if ((ret = fragment->rename()) != ERROR_SUCCESS) {
if ((err = fragment->rename()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}

// 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 ((ret = plan->on_reap_segment()) != ERROR_SUCCESS) {
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;
}
Expand Down Expand Up @@ -669,20 +680,20 @@ int SrsDvrPlan::on_video(SrsSharedPtrMessage* shared_video, SrsFormat* format)
return ret;
}

int SrsDvrPlan::on_reap_segment()
srs_error_t SrsDvrPlan::on_reap_segment()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

int cid = _srs_context->get_id();

SrsFragment* fragment = segment->current();
string fullpath = fragment->fullpath();

if ((ret = async->execute(new SrsDvrAsyncCallOnDvr(cid, req, fullpath))) != ERROR_SUCCESS) {
return ret;
if ((err = async->execute(new SrsDvrAsyncCallOnDvr(cid, req, fullpath))) != srs_success) {
return srs_error_wrap(err, "reap segment");
}

return ret;
return err;
}

srs_error_t SrsDvrPlan::create_plan(string vhost, SrsDvrPlan** pplan)
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_dvr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class SrsDvrPlan : public ISrsReloadHandler
// Internal interface for segmenter.
public:
// When segmenter close a segment.
virtual int on_reap_segment();
virtual srs_error_t on_reap_segment();
public:
static srs_error_t create_plan(std::string vhost, SrsDvrPlan** pplan);
};
Expand Down
18 changes: 15 additions & 3 deletions trunk/src/app/srs_app_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,16 @@ void SrsForwarder::on_unpublish()
int 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 ((ret = jitter->correct(metadata, SrsRtmpJitterAlgorithmOFF)) != ERROR_SUCCESS) {
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;
}

Expand All @@ -177,12 +181,16 @@ int SrsForwarder::on_meta_data(SrsSharedPtrMessage* shared_metadata)
int 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 ((ret = jitter->correct(msg, SrsRtmpJitterAlgorithmOFF)) != ERROR_SUCCESS) {
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;
}

Expand All @@ -201,12 +209,16 @@ int SrsForwarder::on_audio(SrsSharedPtrMessage* shared_audio)
int 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 ((ret = jitter->correct(msg, SrsRtmpJitterAlgorithmOFF)) != ERROR_SUCCESS) {
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;
}

Expand Down
60 changes: 28 additions & 32 deletions trunk/src/app/srs_app_fragment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,68 +77,61 @@ void SrsFragment::set_path(string v)
filepath = v;
}

int SrsFragment::unlink_file()
srs_error_t SrsFragment::unlink_file()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

if (::unlink(filepath.c_str()) < 0) {
ret = ERROR_SYSTEM_FRAGMENT_UNLINK;
srs_error("Unlink fragment failed, file=%s, ret=%d.", filepath.c_str(), ret);
return ret;
return srs_error_new(ERROR_SYSTEM_FRAGMENT_UNLINK, "unlink %s", filepath.c_str());
}

return ret;
return err;
}

int SrsFragment::create_dir()
srs_error_t SrsFragment::create_dir()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

std::string segment_dir = srs_path_dirname(filepath);

if ((ret = srs_create_dir_recursively(segment_dir)) != ERROR_SUCCESS) {
srs_error("Create dir %s failed. ret=%d", segment_dir.c_str(), ret);
return ret;
if ((err = srs_create_dir_recursively(segment_dir)) != srs_success) {
return srs_error_wrap(err, "create %s", segment_dir.c_str());
}

srs_info("Create dir %s ok", segment_dir.c_str());

return ret;
return err;
}

string SrsFragment::tmppath()
{
return filepath + ".tmp";
}

int SrsFragment::unlink_tmpfile()
srs_error_t SrsFragment::unlink_tmpfile()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

string filepath = tmppath();
if (::unlink(filepath.c_str()) < 0) {
ret = ERROR_SYSTEM_FRAGMENT_UNLINK;
srs_error("Unlink temporary fragment failed, file=%s, ret=%d.", filepath.c_str(), ret);
return ret;
return srs_error_new(ERROR_SYSTEM_FRAGMENT_UNLINK, "unlink tmp file %s", filepath.c_str());
}

return ret;
return err;
}

int SrsFragment::rename()
srs_error_t SrsFragment::rename()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

string full_path = fullpath();
string tmp_file = tmppath();

if (::rename(tmp_file.c_str(), full_path.c_str()) < 0) {
ret = ERROR_SYSTEM_FRAGMENT_RENAME;
srs_error("rename ts file failed, %s => %s. ret=%d", tmp_file.c_str(), full_path.c_str(), ret);
return ret;
return srs_error_new(ERROR_SYSTEM_FRAGMENT_RENAME, "rename %s to %s", tmp_file.c_str(), full_path.c_str());
}

return ret;
return err;
}

SrsFragmentWindow::SrsFragmentWindow()
Expand All @@ -164,23 +157,25 @@ SrsFragmentWindow::~SrsFragmentWindow()

void SrsFragmentWindow::dispose()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

std::vector<SrsFragment*>::iterator it;

for (it = fragments.begin(); it != fragments.end(); ++it) {
SrsFragment* fragment = *it;
if ((ret = fragment->unlink_file()) != ERROR_SUCCESS) {
srs_warn("Unlink ts failed, file=%s, ret=%d", fragment->fullpath().c_str(), ret);
if ((err = fragment->unlink_file()) != srs_success) {
srs_warn("Unlink ts failed %s", srs_error_desc(err).c_str());
srs_freep(err);
}
srs_freep(fragment);
}
fragments.clear();

for (it = expired_fragments.begin(); it != expired_fragments.end(); ++it) {
SrsFragment* fragment = *it;
if ((ret = fragment->unlink_file()) != ERROR_SUCCESS) {
srs_warn("Unlink ts failed, file=%s, ret=%d", fragment->fullpath().c_str(), ret);
if ((err = fragment->unlink_file()) != srs_success) {
srs_warn("Unlink ts failed %s", srs_error_desc(err).c_str());
srs_freep(err);
}
srs_freep(fragment);
}
Expand Down Expand Up @@ -217,14 +212,15 @@ void SrsFragmentWindow::shrink(int64_t window)

void SrsFragmentWindow::clear_expired(bool delete_files)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;

std::vector<SrsFragment*>::iterator it;

for (it = expired_fragments.begin(); it != expired_fragments.end(); ++it) {
SrsFragment* fragment = *it;
if (delete_files && (ret = fragment->unlink_file()) != ERROR_SUCCESS) {
srs_warn("Unlink ts failed, file=%s, ret=%d", fragment->fullpath().c_str(), ret);
if (delete_files && (err = fragment->unlink_file()) != srs_success) {
srs_warn("Unlink ts failed, %s", srs_error_desc(err).c_str());
srs_freep(err);
}
srs_freep(fragment);
}
Expand Down
Loading

0 comments on commit abcaba3

Please sign in to comment.