1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #913, Kernel MP4 FLV HTTP support complex error.

This commit is contained in:
winlin 2017-12-31 12:11:48 +08:00
parent 9802dc326e
commit 204ef041da
23 changed files with 1413 additions and 1660 deletions

View file

@ -74,15 +74,13 @@ SrsBufferCache::~SrsBufferCache()
srs_freep(req); srs_freep(req);
} }
int SrsBufferCache::update(SrsSource* s, SrsRequest* r) srs_error_t SrsBufferCache::update(SrsSource* s, SrsRequest* r)
{ {
int ret = ERROR_SUCCESS;
srs_freep(req); srs_freep(req);
req = r->copy(); req = r->copy();
source = s; source = s;
return ret; return srs_success;
} }
srs_error_t SrsBufferCache::start() srs_error_t SrsBufferCache::start()
@ -96,28 +94,22 @@ srs_error_t SrsBufferCache::start()
return err; return err;
} }
int SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) srs_error_t SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (fast_cache <= 0) { if (fast_cache <= 0) {
srs_info("http: ignore dump fast cache."); return err;
return ret;
} }
// the jitter is get from SrsSource, which means the time_jitter of vhost. // the jitter is get from SrsSource, which means the time_jitter of vhost.
if ((err = queue->dump_packets(consumer, false, jitter)) != srs_success) { if ((err = queue->dump_packets(consumer, false, jitter)) != srs_success) {
// TODO: FIXME: Use error return srs_error_wrap(err, "dump packets");
ret = srs_error_code(err);
srs_freep(err);
return ret;
} }
srs_trace("http: dump cache %d msgs, duration=%dms, cache=%.2fs", srs_trace("http: dump cache %d msgs, duration=%dms, cache=%.2fs", queue->size(), queue->duration(), fast_cache);
queue->size(), queue->duration(), fast_cache);
return ret; return err;
} }
srs_error_t SrsBufferCache::cycle() srs_error_t SrsBufferCache::cycle()
@ -203,42 +195,42 @@ SrsTsStreamEncoder::~SrsTsStreamEncoder()
srs_freep(enc); srs_freep(enc);
} }
int SrsTsStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/) srs_error_t SrsTsStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
if ((err = enc->initialize(w)) != srs_success) { if ((err = enc->initialize(w)) != srs_success) {
// TODO: FIXME: Use error return srs_error_wrap(err, "init encoder");
ret = srs_error_code(err);
srs_freep(err);
return ret;
} }
return ret; return err;
} }
int SrsTsStreamEncoder::write_audio(int64_t timestamp, char* data, int size) srs_error_t SrsTsStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{ {
srs_error_t err = enc->write_audio(timestamp, data, size); srs_error_t err = srs_success;
// TODO: FIXME: Use error
int ret = srs_error_code(err); if ((err = enc->write_audio(timestamp, data, size)) != srs_success) {
srs_freep(err); return srs_error_wrap(err, "write audio");
return ret;
} }
int SrsTsStreamEncoder::write_video(int64_t timestamp, char* data, int size) return err;
{
srs_error_t err = enc->write_video(timestamp, data, size);
// TODO: FIXME: Use error
int ret = srs_error_code(err);
srs_freep(err);
return ret;
} }
int SrsTsStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/) srs_error_t SrsTsStreamEncoder::write_video(int64_t timestamp, char* data, int size)
{ {
return ERROR_SUCCESS; srs_error_t err = srs_success;
if ((err = enc->write_video(timestamp, data, size)) != srs_success) {
return srs_error_wrap(err, "write video");
}
return err;
}
srs_error_t SrsTsStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
return srs_success;
} }
bool SrsTsStreamEncoder::has_cache() bool SrsTsStreamEncoder::has_cache()
@ -247,10 +239,10 @@ bool SrsTsStreamEncoder::has_cache()
return false; return false;
} }
int SrsTsStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/) srs_error_t SrsTsStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
{ {
// for ts stream, ignore cache. // for ts stream, ignore cache.
return ERROR_SUCCESS; return srs_success;
} }
SrsFlvStreamEncoder::SrsFlvStreamEncoder() SrsFlvStreamEncoder::SrsFlvStreamEncoder()
@ -263,33 +255,33 @@ SrsFlvStreamEncoder::~SrsFlvStreamEncoder()
srs_freep(enc); srs_freep(enc);
} }
int SrsFlvStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/) srs_error_t SrsFlvStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if ((ret = enc->initialize(w)) != ERROR_SUCCESS) { if ((err = enc->initialize(w)) != srs_success) {
return ret; return srs_error_wrap(err, "init encoder");
} }
// write flv header. // write flv header.
if ((ret = enc->write_header()) != ERROR_SUCCESS) { if ((err = enc->write_header()) != srs_success) {
return ret; return srs_error_wrap(err, "write header");
} }
return ret; return err;
} }
int SrsFlvStreamEncoder::write_audio(int64_t timestamp, char* data, int size) srs_error_t SrsFlvStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{ {
return enc->write_audio(timestamp, data, size); return enc->write_audio(timestamp, data, size);
} }
int SrsFlvStreamEncoder::write_video(int64_t timestamp, char* data, int size) srs_error_t SrsFlvStreamEncoder::write_video(int64_t timestamp, char* data, int size)
{ {
return enc->write_video(timestamp, data, size); return enc->write_video(timestamp, data, size);
} }
int SrsFlvStreamEncoder::write_metadata(int64_t timestamp, char* data, int size) srs_error_t SrsFlvStreamEncoder::write_metadata(int64_t timestamp, char* data, int size)
{ {
return enc->write_metadata(SrsFrameTypeScript, data, size); return enc->write_metadata(SrsFrameTypeScript, data, size);
} }
@ -300,10 +292,10 @@ bool SrsFlvStreamEncoder::has_cache()
return false; return false;
} }
int SrsFlvStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/) srs_error_t SrsFlvStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
{ {
// for flv stream, ignore cache. // for flv stream, ignore cache.
return ERROR_SUCCESS; return srs_success;
} }
#ifdef SRS_PERF_FAST_FLV_ENCODER #ifdef SRS_PERF_FAST_FLV_ENCODER
@ -315,7 +307,7 @@ SrsFastFlvStreamEncoder::~SrsFastFlvStreamEncoder()
{ {
} }
int SrsFastFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count) srs_error_t SrsFastFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
{ {
return enc->write_tags(msgs, count); return enc->write_tags(msgs, count);
} }
@ -332,34 +324,34 @@ SrsAacStreamEncoder::~SrsAacStreamEncoder()
srs_freep(enc); srs_freep(enc);
} }
int SrsAacStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c) srs_error_t SrsAacStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
cache = c; cache = c;
if ((ret = enc->initialize(w)) != ERROR_SUCCESS) { if ((err = enc->initialize(w)) != srs_success) {
return ret; return srs_error_wrap(err, "init encoder");
} }
return ret; return err;
} }
int SrsAacStreamEncoder::write_audio(int64_t timestamp, char* data, int size) srs_error_t SrsAacStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{ {
return enc->write_audio(timestamp, data, size); return enc->write_audio(timestamp, data, size);
} }
int SrsAacStreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/) srs_error_t SrsAacStreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{ {
// aac ignore any flv video. // aac ignore any flv video.
return ERROR_SUCCESS; return srs_success;
} }
int SrsAacStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/) srs_error_t SrsAacStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{ {
// aac ignore any flv metadata. // aac ignore any flv metadata.
return ERROR_SUCCESS; return srs_success;
} }
bool SrsAacStreamEncoder::has_cache() bool SrsAacStreamEncoder::has_cache()
@ -367,7 +359,7 @@ bool SrsAacStreamEncoder::has_cache()
return true; return true;
} }
int SrsAacStreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) srs_error_t SrsAacStreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
{ {
srs_assert(cache); srs_assert(cache);
return cache->dump_cache(consumer, jitter); return cache->dump_cache(consumer, jitter);
@ -384,38 +376,38 @@ SrsMp3StreamEncoder::~SrsMp3StreamEncoder()
srs_freep(enc); srs_freep(enc);
} }
int SrsMp3StreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c) srs_error_t SrsMp3StreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
cache = c; cache = c;
if ((ret = enc->initialize(w)) != ERROR_SUCCESS) { if ((err = enc->initialize(w)) != srs_success) {
return ret; return srs_error_wrap(err, "init encoder");
} }
if ((ret = enc->write_header()) != ERROR_SUCCESS) { if ((err = enc->write_header()) != srs_success) {
return ret; return srs_error_wrap(err, "init encoder");
} }
return ret; return err;
} }
int SrsMp3StreamEncoder::write_audio(int64_t timestamp, char* data, int size) srs_error_t SrsMp3StreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{ {
return enc->write_audio(timestamp, data, size); return enc->write_audio(timestamp, data, size);
} }
int SrsMp3StreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/) srs_error_t SrsMp3StreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{ {
// mp3 ignore any flv video. // mp3 ignore any flv video.
return ERROR_SUCCESS; return srs_success;
} }
int SrsMp3StreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/) srs_error_t SrsMp3StreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{ {
// mp3 ignore any flv metadata. // mp3 ignore any flv metadata.
return ERROR_SUCCESS; return srs_success;
} }
bool SrsMp3StreamEncoder::has_cache() bool SrsMp3StreamEncoder::has_cache()
@ -423,7 +415,7 @@ bool SrsMp3StreamEncoder::has_cache()
return true; return true;
} }
int SrsMp3StreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) srs_error_t SrsMp3StreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
{ {
srs_assert(cache); srs_assert(cache);
return cache->dump_cache(consumer, jitter); return cache->dump_cache(consumer, jitter);
@ -438,9 +430,9 @@ SrsBufferWriter::~SrsBufferWriter()
{ {
} }
int SrsBufferWriter::open(std::string /*file*/) srs_error_t SrsBufferWriter::open(std::string /*file*/)
{ {
return ERROR_SUCCESS; return srs_success;
} }
void SrsBufferWriter::close() void SrsBufferWriter::close()
@ -457,7 +449,7 @@ int64_t SrsBufferWriter::tellg()
return 0; return 0;
} }
int SrsBufferWriter::write(void* buf, size_t count, ssize_t* pnwrite) srs_error_t SrsBufferWriter::write(void* buf, size_t count, ssize_t* pnwrite)
{ {
if (pnwrite) { if (pnwrite) {
*pnwrite = count; *pnwrite = count;
@ -465,7 +457,7 @@ int SrsBufferWriter::write(void* buf, size_t count, ssize_t* pnwrite)
return writer->write((char*)buf, (int)count); return writer->write((char*)buf, (int)count);
} }
int SrsBufferWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) srs_error_t SrsBufferWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
{ {
return writer->writev(iov, iovcnt, pnwrite); return writer->writev(iov, iovcnt, pnwrite);
} }
@ -482,20 +474,17 @@ SrsLiveStream::~SrsLiveStream()
srs_freep(req); srs_freep(req);
} }
int SrsLiveStream::update(SrsSource* s, SrsRequest* r) srs_error_t SrsLiveStream::update(SrsSource* s, SrsRequest* r)
{ {
int ret = ERROR_SUCCESS;
srs_freep(req); srs_freep(req);
source = s; source = s;
req = r->copy(); req = r->copy();
return ret; return srs_success;
} }
srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
ISrsBufferEncoder* enc = NULL; ISrsBufferEncoder* enc = NULL;
@ -537,20 +526,20 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
// update the statistic when source disconveried. // update the statistic when source disconveried.
SrsStatistic* stat = SrsStatistic::instance(); SrsStatistic* stat = SrsStatistic::instance();
if ((ret = stat->on_client(_srs_context->get_id(), req, NULL, SrsRtmpConnPlay)) != ERROR_SUCCESS) { if ((err = stat->on_client(_srs_context->get_id(), req, NULL, SrsRtmpConnPlay)) != srs_success) {
return srs_error_new(ret, "stat on client"); return srs_error_wrap(err, "stat on client");
} }
// the memory writer. // the memory writer.
SrsBufferWriter writer(w); SrsBufferWriter writer(w);
if ((ret = enc->initialize(&writer, cache)) != ERROR_SUCCESS) { if ((err = enc->initialize(&writer, cache)) != srs_success) {
return srs_error_new(ret, "init encoder"); return srs_error_wrap(err, "init encoder");
} }
// if gop cache enabled for encoder, dump to consumer. // if gop cache enabled for encoder, dump to consumer.
if (enc->has_cache()) { if (enc->has_cache()) {
if ((ret = enc->dump_cache(consumer, source->jitter())) != ERROR_SUCCESS) { if ((err = enc->dump_cache(consumer, source->jitter())) != srs_success) {
return srs_error_new(ret, "encoder dump cache"); return srs_error_wrap(err, "encoder dump cache");
} }
} }
@ -603,12 +592,12 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
// sendout all messages. // sendout all messages.
#ifdef SRS_PERF_FAST_FLV_ENCODER #ifdef SRS_PERF_FAST_FLV_ENCODER
if (ffe) { if (ffe) {
ret = ffe->write_tags(msgs.msgs, count); err = ffe->write_tags(msgs.msgs, count);
} else { } else {
ret = streaming_send_messages(enc, msgs.msgs, count); err = streaming_send_messages(enc, msgs.msgs, count);
} }
#else #else
ret = streaming_send_messages(enc, msgs.msgs, count); err = streaming_send_messages(enc, msgs.msgs, count);
#endif #endif
// free the messages. // free the messages.
@ -618,8 +607,8 @@ srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage
} }
// check send error code. // check send error code.
if (ret != ERROR_SUCCESS) { if (err != srs_success) {
return srs_error_new(ret, "send messages"); return srs_error_wrap(err, "send messages");
} }
} }
@ -720,21 +709,19 @@ SrsHttpStreamServer::~SrsHttpStreamServer()
srs_error_t SrsHttpStreamServer::initialize() srs_error_t SrsHttpStreamServer::initialize()
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// remux rtmp to flv live streaming // remux rtmp to flv live streaming
if ((ret = initialize_flv_streaming()) != ERROR_SUCCESS) { if ((err = initialize_flv_streaming()) != srs_success) {
return srs_error_new(ret, "http flv stream"); return srs_error_wrap(err, "http flv stream");
} }
return err; return err;
} }
// TODO: FIXME: rename for HTTP FLV mount. // TODO: FIXME: rename for HTTP FLV mount.
int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r) srs_error_t SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// the id to identify stream. // the id to identify stream.
@ -783,22 +770,12 @@ int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
// for the thread will cause thread switch context. // for the thread will cause thread switch context.
// @see https://github.com/ossrs/srs/issues/404 // @see https://github.com/ossrs/srs/issues/404
if ((err = mux.handle(mount, entry->stream)) != srs_success) { if ((err = mux.handle(mount, entry->stream)) != srs_success) {
// TODO: FIXME: Use error. return srs_error_wrap(err, "http: mount flv stream for vhost=%s failed", sid.c_str());
ret = srs_error_code(err);
srs_freep(err);
srs_error("http: mount flv stream for vhost=%s failed. ret=%d", sid.c_str(), ret);
return ret;
} }
// start http stream cache thread // start http stream cache thread
if ((err = entry->cache->start()) != srs_success) { if ((err = entry->cache->start()) != srs_success) {
// TODO: FIXME: Use error return srs_error_wrap(err, "http: start stream cache failed");
ret = srs_error_code(err);
srs_freep(err);
srs_error("http: start stream cache failed. ret=%d", ret);
return ret;
} }
srs_trace("http: mount flv stream for vhost=%s, mount=%s", sid.c_str(), mount.c_str()); srs_trace("http: mount flv stream for vhost=%s, mount=%s", sid.c_str(), mount.c_str());
} else { } else {
@ -809,10 +786,10 @@ int SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
if (entry->stream) { if (entry->stream) {
entry->stream->entry->enabled = true; entry->stream->entry->enabled = true;
return ret; return err;
} }
return ret; return err;
} }
void SrsHttpStreamServer::http_unmount(SrsSource* s, SrsRequest* r) void SrsHttpStreamServer::http_unmount(SrsSource* s, SrsRequest* r)
@ -841,12 +818,11 @@ srs_error_t SrsHttpStreamServer::on_reload_vhost_added(string vhost)
srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost) srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (tflvs.find(vhost) == tflvs.end()) { if (tflvs.find(vhost) == tflvs.end()) {
if ((ret = initialize_flv_entry(vhost)) != ERROR_SUCCESS) { if ((err = initialize_flv_entry(vhost)) != srs_success) {
return srs_error_new(ret, "init flv entry"); return srs_error_wrap(err, "init flv entry");
} }
// http mount need SrsRequest and SrsSource param, only create a mapping template entry // http mount need SrsRequest and SrsSource param, only create a mapping template entry
@ -882,8 +858,8 @@ srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost
std::string sid = req->get_stream_url(); std::string sid = req->get_stream_url();
// remount stream. // remount stream.
if ((ret = http_mount(source, req)) != ERROR_SUCCESS) { if ((err = http_mount(source, req)) != srs_success) {
return srs_error_new(ret, "vhost %s http_remux reload failed", vhost.c_str()); return srs_error_wrap(err, "vhost %s http_remux reload failed", vhost.c_str());
} }
} else { } else {
// for without SrsRequest and SrsSource if stream is not played yet, do http mount automatically // for without SrsRequest and SrsSource if stream is not played yet, do http mount automatically
@ -897,7 +873,6 @@ srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost
srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph) srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// when handler not the root, we think the handler is ok. // when handler not the root, we think the handler is ok.
@ -986,8 +961,8 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle
srs_assert(s != NULL); srs_assert(s != NULL);
// create http streaming handler. // create http streaming handler.
if ((ret = http_mount(s, r)) != ERROR_SUCCESS) { if ((err = http_mount(s, r)) != srs_success) {
return srs_error_new(ret, "http mount"); return srs_error_wrap(err, "http mount");
} }
// use the handler if exists. // use the handler if exists.
@ -1006,9 +981,9 @@ srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandle
return err; return err;
} }
int SrsHttpStreamServer::initialize_flv_streaming() srs_error_t SrsHttpStreamServer::initialize_flv_streaming()
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
// http flv live stream mount for each vhost. // http flv live stream mount for each vhost.
SrsConfDirective* root = _srs_config->get_root(); SrsConfDirective* root = _srs_config->get_root();
@ -1019,19 +994,20 @@ int SrsHttpStreamServer::initialize_flv_streaming()
continue; continue;
} }
if ((ret = initialize_flv_entry(conf->arg0())) != ERROR_SUCCESS) { if ((err = initialize_flv_entry(conf->arg0())) != srs_success) {
return ret; return srs_error_wrap(err, "init flv entries");
} }
} }
return ret;
}
int SrsHttpStreamServer::initialize_flv_entry(std::string vhost) return err;
}
srs_error_t SrsHttpStreamServer::initialize_flv_entry(std::string vhost)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (!_srs_config->get_vhost_http_remux_enabled(vhost)) { if (!_srs_config->get_vhost_http_remux_enabled(vhost)) {
return ret; return err;
} }
SrsLiveEntry* entry = new SrsLiveEntry(_srs_config->get_vhost_http_remux_mount(vhost)); SrsLiveEntry* entry = new SrsLiveEntry(_srs_config->get_vhost_http_remux_mount(vhost));
@ -1039,6 +1015,6 @@ int SrsHttpStreamServer::initialize_flv_entry(std::string vhost)
tflvs[vhost] = entry; tflvs[vhost] = entry;
srs_trace("http flv live stream, vhost=%s, mount=%s", vhost.c_str(), entry->mount.c_str()); srs_trace("http flv live stream, vhost=%s, mount=%s", vhost.c_str(), entry->mount.c_str());
return ret; return err;
} }

View file

@ -50,10 +50,10 @@ private:
public: public:
SrsBufferCache(SrsSource* s, SrsRequest* r); SrsBufferCache(SrsSource* s, SrsRequest* r);
virtual ~SrsBufferCache(); virtual ~SrsBufferCache();
virtual int update(SrsSource* s, SrsRequest* r); virtual srs_error_t update(SrsSource* s, SrsRequest* r);
public: public:
virtual srs_error_t start(); virtual srs_error_t start();
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter); virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
// interface ISrsEndlessThreadHandler. // interface ISrsEndlessThreadHandler.
public: public:
virtual srs_error_t cycle(); virtual srs_error_t cycle();
@ -73,13 +73,13 @@ public:
* @param w the writer to write to http response. * @param w the writer to write to http response.
* @param c the stream cache for audio stream fast startup. * @param c the stream cache for audio stream fast startup.
*/ */
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c) = 0; virtual srs_error_t initialize(SrsFileWriter* w, SrsBufferCache* c) = 0;
/** /**
* write rtmp video/audio/metadata. * write rtmp video/audio/metadata.
*/ */
virtual int write_audio(int64_t timestamp, char* data, int size) = 0; virtual srs_error_t write_audio(int64_t timestamp, char* data, int size) = 0;
virtual int write_video(int64_t timestamp, char* data, int size) = 0; virtual srs_error_t write_video(int64_t timestamp, char* data, int size) = 0;
virtual int write_metadata(int64_t timestamp, char* data, int size) = 0; virtual srs_error_t write_metadata(int64_t timestamp, char* data, int size) = 0;
public: public:
/** /**
* for some stream, for example, mp3 and aac, the audio stream, * for some stream, for example, mp3 and aac, the audio stream,
@ -90,7 +90,7 @@ public:
/** /**
* dumps the cache of encoder to consumer. * dumps the cache of encoder to consumer.
*/ */
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) = 0; virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) = 0;
}; };
/** /**
@ -104,13 +104,13 @@ public:
SrsFlvStreamEncoder(); SrsFlvStreamEncoder();
virtual ~SrsFlvStreamEncoder(); virtual ~SrsFlvStreamEncoder();
public: public:
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c); virtual srs_error_t initialize(SrsFileWriter* w, SrsBufferCache* c);
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
virtual int write_video(int64_t timestamp, char* data, int size); virtual srs_error_t write_video(int64_t timestamp, char* data, int size);
virtual int write_metadata(int64_t timestamp, char* data, int size); virtual srs_error_t write_metadata(int64_t timestamp, char* data, int size);
public: public:
virtual bool has_cache(); virtual bool has_cache();
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter); virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
}; };
#ifdef SRS_PERF_FAST_FLV_ENCODER #ifdef SRS_PERF_FAST_FLV_ENCODER
@ -127,7 +127,7 @@ public:
/** /**
* write the tags in a time. * write the tags in a time.
*/ */
virtual int write_tags(SrsSharedPtrMessage** msgs, int count); virtual srs_error_t write_tags(SrsSharedPtrMessage** msgs, int count);
}; };
#endif #endif
@ -142,13 +142,13 @@ public:
SrsTsStreamEncoder(); SrsTsStreamEncoder();
virtual ~SrsTsStreamEncoder(); virtual ~SrsTsStreamEncoder();
public: public:
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c); virtual srs_error_t initialize(SrsFileWriter* w, SrsBufferCache* c);
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
virtual int write_video(int64_t timestamp, char* data, int size); virtual srs_error_t write_video(int64_t timestamp, char* data, int size);
virtual int write_metadata(int64_t timestamp, char* data, int size); virtual srs_error_t write_metadata(int64_t timestamp, char* data, int size);
public: public:
virtual bool has_cache(); virtual bool has_cache();
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter); virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
}; };
/** /**
@ -163,13 +163,13 @@ public:
SrsAacStreamEncoder(); SrsAacStreamEncoder();
virtual ~SrsAacStreamEncoder(); virtual ~SrsAacStreamEncoder();
public: public:
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c); virtual srs_error_t initialize(SrsFileWriter* w, SrsBufferCache* c);
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
virtual int write_video(int64_t timestamp, char* data, int size); virtual srs_error_t write_video(int64_t timestamp, char* data, int size);
virtual int write_metadata(int64_t timestamp, char* data, int size); virtual srs_error_t write_metadata(int64_t timestamp, char* data, int size);
public: public:
virtual bool has_cache(); virtual bool has_cache();
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter); virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
}; };
/** /**
@ -184,13 +184,13 @@ public:
SrsMp3StreamEncoder(); SrsMp3StreamEncoder();
virtual ~SrsMp3StreamEncoder(); virtual ~SrsMp3StreamEncoder();
public: public:
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c); virtual srs_error_t initialize(SrsFileWriter* w, SrsBufferCache* c);
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
virtual int write_video(int64_t timestamp, char* data, int size); virtual srs_error_t write_video(int64_t timestamp, char* data, int size);
virtual int write_metadata(int64_t timestamp, char* data, int size); virtual srs_error_t write_metadata(int64_t timestamp, char* data, int size);
public: public:
virtual bool has_cache(); virtual bool has_cache();
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter); virtual srs_error_t dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
}; };
/** /**
@ -204,14 +204,14 @@ public:
SrsBufferWriter(ISrsHttpResponseWriter* w); SrsBufferWriter(ISrsHttpResponseWriter* w);
virtual ~SrsBufferWriter(); virtual ~SrsBufferWriter();
public: public:
virtual int open(std::string file); virtual srs_error_t open(std::string file);
virtual void close(); virtual void close();
public: public:
virtual bool is_open(); virtual bool is_open();
virtual int64_t tellg(); virtual int64_t tellg();
public: public:
virtual int write(void* buf, size_t count, ssize_t* pnwrite); virtual srs_error_t write(void* buf, size_t count, ssize_t* pnwrite);
virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite); virtual srs_error_t writev(const iovec* iov, int iovcnt, ssize_t* pnwrite);
}; };
/** /**
@ -227,11 +227,11 @@ private:
public: public:
SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c); SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c);
virtual ~SrsLiveStream(); virtual ~SrsLiveStream();
virtual int update(SrsSource* s, SrsRequest* r); virtual srs_error_t update(SrsSource* s, SrsRequest* r);
public: public:
virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r); virtual srs_error_t serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
private: private:
virtual int streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs); virtual srs_error_t streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs);
}; };
/** /**
@ -286,7 +286,7 @@ public:
virtual srs_error_t initialize(); virtual srs_error_t initialize();
// http flv/ts/mp3/aac stream // http flv/ts/mp3/aac stream
public: public:
virtual int http_mount(SrsSource* s, SrsRequest* r); virtual srs_error_t http_mount(SrsSource* s, SrsRequest* r);
virtual void http_unmount(SrsSource* s, SrsRequest* r); virtual void http_unmount(SrsSource* s, SrsRequest* r);
// interface ISrsReloadHandler. // interface ISrsReloadHandler.
public: public:
@ -296,8 +296,8 @@ public:
public: public:
virtual srs_error_t hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph); virtual srs_error_t hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph);
private: private:
virtual int initialize_flv_streaming(); virtual srs_error_t initialize_flv_streaming();
virtual int initialize_flv_entry(std::string vhost); virtual srs_error_t initialize_flv_entry(std::string vhost);
}; };
#endif #endif

View file

@ -39,55 +39,48 @@ using namespace std;
#include <srs_kernel_buffer.hpp> #include <srs_kernel_buffer.hpp>
#include <srs_kernel_file.hpp> #include <srs_kernel_file.hpp>
#include <srs_kernel_codec.hpp> #include <srs_kernel_codec.hpp>
#include <srs_core_autofree.hpp>
SrsAacTransmuxer::SrsAacTransmuxer() SrsAacTransmuxer::SrsAacTransmuxer()
{ {
_fs = NULL; _fs = NULL;
got_sequence_header = false; got_sequence_header = false;
tag_stream = new SrsBuffer();
aac_object = SrsAacObjectTypeReserved; aac_object = SrsAacObjectTypeReserved;
} }
SrsAacTransmuxer::~SrsAacTransmuxer() SrsAacTransmuxer::~SrsAacTransmuxer()
{ {
srs_freep(tag_stream);
} }
int SrsAacTransmuxer::initialize(SrsFileWriter* fs) srs_error_t SrsAacTransmuxer::initialize(SrsFileWriter* fs)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(fs); srs_assert(fs);
if (!fs->is_open()) { if (!fs->is_open()) {
ret = ERROR_KERNEL_AAC_STREAM_CLOSED; return srs_error_new(ERROR_KERNEL_AAC_STREAM_CLOSED, "stream is not open");
srs_warn("stream is not open for encoder. ret=%d", ret);
return ret;
} }
_fs = fs; _fs = fs;
return ret; return err;
} }
int SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size) srs_error_t SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
timestamp &= 0x7fffffff; timestamp &= 0x7fffffff;
SrsBuffer* stream = tag_stream; SrsBuffer* stream = new SrsBuffer(data, size);
if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) { SrsAutoFree(SrsBuffer, stream);
return ret;
}
// audio decode // audio decode
if (!stream->require(1)) { if (!stream->require(1)) {
ret = ERROR_AAC_DECODE_ERROR; return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode audio sound_format failed");
srs_error("aac decode audio sound_format failed. ret=%d", ret);
return ret;
} }
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76 // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
@ -99,15 +92,11 @@ int SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size)
sound_format = (sound_format >> 4) & 0x0f; sound_format = (sound_format >> 4) & 0x0f;
if ((SrsAudioCodecId)sound_format != SrsAudioCodecIdAAC) { if ((SrsAudioCodecId)sound_format != SrsAudioCodecIdAAC) {
ret = ERROR_AAC_DECODE_ERROR; return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac required, format=%d", sound_format);
srs_error("aac required, format=%d. ret=%d", sound_format, ret);
return ret;
} }
if (!stream->require(1)) { if (!stream->require(1)) {
ret = ERROR_AAC_DECODE_ERROR; return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode aac_packet_type failed");
srs_error("aac decode aac_packet_type failed. ret=%d", ret);
return ret;
} }
SrsAudioAacFrameTrait aac_packet_type = (SrsAudioAacFrameTrait)stream->read_1bytes(); SrsAudioAacFrameTrait aac_packet_type = (SrsAudioAacFrameTrait)stream->read_1bytes();
@ -120,9 +109,7 @@ int SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size)
// samplingFrequencyIndex, aac_sample_rate, 4bits. // samplingFrequencyIndex, aac_sample_rate, 4bits.
// channelConfiguration, aac_channels, 4bits // channelConfiguration, aac_channels, 4bits
if (!stream->require(2)) { if (!stream->require(2)) {
ret = ERROR_AAC_DECODE_ERROR; return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac decode sequence header failed");
srs_error("aac decode sequence header failed. ret=%d", ret);
return ret;
} }
int8_t audioObjectType = stream->read_1bytes(); int8_t audioObjectType = stream->read_1bytes();
@ -136,13 +123,11 @@ int SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size)
got_sequence_header = true; got_sequence_header = true;
return ret; return err;
} }
if (!got_sequence_header) { if (!got_sequence_header) {
ret = ERROR_AAC_DECODE_ERROR; return srs_error_new(ERROR_AAC_DECODE_ERROR, "aac no sequence header");
srs_error("aac no sequence header. ret=%d", ret);
return ret;
} }
// the left is the aac raw frame data. // the left is the aac raw frame data.
@ -207,16 +192,16 @@ int SrsAacTransmuxer::write_audio(int64_t timestamp, char* data, int size)
} }
// write 7bytes fixed header. // write 7bytes fixed header.
if ((ret = _fs->write(aac_fixed_header, 7, NULL)) != ERROR_SUCCESS) { if ((err = _fs->write(aac_fixed_header, 7, NULL)) != srs_success) {
return ret; return srs_error_wrap(err, "write aac header");
} }
// write aac frame body. // write aac frame body.
if ((ret = _fs->write(data + stream->pos(), aac_raw_length, NULL)) != ERROR_SUCCESS) { if ((err = _fs->write(data + stream->pos(), aac_raw_length, NULL)) != srs_success) {
return ret; return srs_error_wrap(err, "write aac frame");
} }
return ret; return err;
} }
#endif #endif

View file

@ -48,8 +48,6 @@ private:
int8_t aac_sample_rate; int8_t aac_sample_rate;
int8_t aac_channels; int8_t aac_channels;
bool got_sequence_header; bool got_sequence_header;
private:
SrsBuffer* tag_stream;
public: public:
SrsAacTransmuxer(); SrsAacTransmuxer();
virtual ~SrsAacTransmuxer(); virtual ~SrsAacTransmuxer();
@ -59,13 +57,13 @@ public:
* @remark user can initialize multiple times to encode multiple aac files. * @remark user can initialize multiple times to encode multiple aac files.
* @remark, user must free the fs, aac encoder never close/free it. * @remark, user must free the fs, aac encoder never close/free it.
*/ */
virtual int initialize(SrsFileWriter* fs); virtual srs_error_t initialize(SrsFileWriter* fs);
public: public:
/** /**
* write audio/video packet. * write audio/video packet.
* @remark assert data is not NULL. * @remark assert data is not NULL.
*/ */
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
}; };
#endif #endif

View file

@ -39,19 +39,14 @@ ISrsCodec::~ISrsCodec()
SrsBuffer::SrsBuffer() SrsBuffer::SrsBuffer()
{ {
set_value(NULL, 0); p = bytes = NULL;
nb_bytes = 0;
// TODO: support both little and big endian.
srs_assert(srs_is_little_endian());
} }
SrsBuffer::SrsBuffer(char* b, int nb_b) SrsBuffer::SrsBuffer(char* b, int nb_b)
{
set_value(b, nb_b);
}
SrsBuffer::~SrsBuffer()
{
}
void SrsBuffer::set_value(char* b, int nb_b)
{ {
p = bytes = b; p = bytes = b;
nb_bytes = nb_b; nb_bytes = nb_b;
@ -60,27 +55,8 @@ void SrsBuffer::set_value(char* b, int nb_b)
srs_assert(srs_is_little_endian()); srs_assert(srs_is_little_endian());
} }
int SrsBuffer::initialize(char* b, int nb) SrsBuffer::~SrsBuffer()
{ {
int ret = ERROR_SUCCESS;
if (!b) {
ret = ERROR_KERNEL_STREAM_INIT;
srs_error("stream param bytes must not be NULL. ret=%d", ret);
return ret;
}
if (nb <= 0) {
ret = ERROR_KERNEL_STREAM_INIT;
srs_error("stream param size must be positive. ret=%d", ret);
return ret;
}
nb_bytes = nb;
p = bytes = b;
srs_info("init stream ok, size=%d", size());
return ret;
} }
char* SrsBuffer::data() char* SrsBuffer::data()
@ -98,6 +74,11 @@ int SrsBuffer::pos()
return (int)(p - bytes); return (int)(p - bytes);
} }
int SrsBuffer::left()
{
return nb_bytes - (int)(p - bytes);
}
bool SrsBuffer::empty() bool SrsBuffer::empty()
{ {
return !bytes || (p >= bytes + nb_bytes); return !bytes || (p >= bytes + nb_bytes);
@ -281,11 +262,11 @@ SrsBitBuffer::~SrsBitBuffer()
{ {
} }
int SrsBitBuffer::initialize(SrsBuffer* s) { srs_error_t SrsBitBuffer::initialize(SrsBuffer* s) {
stream = s; stream = s;
cb = 0; cb = 0;
cb_left = 0; cb_left = 0;
return ERROR_SUCCESS; return srs_success;
} }
bool SrsBitBuffer::empty() { bool SrsBitBuffer::empty() {

View file

@ -70,12 +70,12 @@ public:
/** /**
* encode object to bytes in SrsBuffer. * encode object to bytes in SrsBuffer.
*/ */
virtual int encode(SrsBuffer* buf) = 0; virtual srs_error_t encode(SrsBuffer* buf) = 0;
public: public:
/** /**
* decode object from bytes in SrsBuffer. * decode object from bytes in SrsBuffer.
*/ */
virtual int decode(SrsBuffer* buf) = 0; virtual srs_error_t decode(SrsBuffer* buf) = 0;
}; };
/** /**
@ -97,18 +97,6 @@ public:
SrsBuffer(); SrsBuffer();
SrsBuffer(char* b, int nb_b); SrsBuffer(char* b, int nb_b);
virtual ~SrsBuffer(); virtual ~SrsBuffer();
private:
virtual void set_value(char* b, int nb_b);
public:
/**
* initialize the stream from bytes.
* @b, the bytes to convert from/to basic types.
* @nb, the size of bytes, total number of bytes for stream.
* @remark, stream never free the bytes, user must free it.
* @remark, return error when bytes NULL.
* @remark, return error when size is not positive.
*/
virtual int initialize(char* b, int nb);
// get the status of stream // get the status of stream
public: public:
/** /**
@ -125,6 +113,8 @@ public:
* tell the current pos. * tell the current pos.
*/ */
virtual int pos(); virtual int pos();
// Left bytes in buffer, total size() minus the current pos().
virtual int left();
/** /**
* whether stream is empty. * whether stream is empty.
* if empty, user should never read or write. * if empty, user should never read or write.
@ -219,7 +209,7 @@ public:
SrsBitBuffer(); SrsBitBuffer();
virtual ~SrsBitBuffer(); virtual ~SrsBitBuffer();
public: public:
virtual int initialize(SrsBuffer* s); virtual srs_error_t initialize(SrsBuffer* s);
virtual bool empty(); virtual bool empty();
virtual int8_t read_bit(); virtual int8_t read_bit();
}; };

View file

@ -507,7 +507,6 @@ SrsFormat::SrsFormat()
vcodec = NULL; vcodec = NULL;
audio = NULL; audio = NULL;
video = NULL; video = NULL;
buffer = new SrsBuffer();
avc_parse_sps = true; avc_parse_sps = true;
raw = NULL; raw = NULL;
nb_raw = 0; nb_raw = 0;
@ -519,7 +518,6 @@ SrsFormat::~SrsFormat()
srs_freep(video); srs_freep(video);
srs_freep(acodec); srs_freep(acodec);
srs_freep(vcodec); srs_freep(vcodec);
srs_freep(buffer);
} }
srs_error_t SrsFormat::initialize() srs_error_t SrsFormat::initialize()
@ -529,7 +527,6 @@ srs_error_t SrsFormat::initialize()
srs_error_t 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; srs_error_t err = srs_success;
if (!data || size <= 0) { if (!data || size <= 0) {
@ -537,9 +534,8 @@ srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size)
return err; return err;
} }
if ((ret = buffer->initialize(data, size)) != ERROR_SUCCESS) { SrsBuffer* buffer = new SrsBuffer(data, size);
return srs_error_new(ret, "init buffer"); SrsAutoFree(SrsBuffer, buffer);
}
// audio decode // audio decode
if (!buffer->require(1)) { if (!buffer->require(1)) {
@ -577,7 +573,6 @@ srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size)
srs_error_t 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; srs_error_t err = srs_success;
if (!data || size <= 0) { if (!data || size <= 0) {
@ -585,9 +580,8 @@ srs_error_t SrsFormat::on_video(int64_t timestamp, char* data, int size)
return err; return err;
} }
if ((ret = buffer->initialize(data, size)) != ERROR_SUCCESS) { SrsBuffer* buffer = new SrsBuffer(data, size);
return srs_error_new(ret, "init buffer"); SrsAutoFree(SrsBuffer, buffer);
}
// video decode // video decode
if (!buffer->require(1)) { if (!buffer->require(1)) {
@ -785,19 +779,16 @@ srs_error_t SrsFormat::avc_demux_sps_pps(SrsBuffer* stream)
srs_error_t SrsFormat::avc_demux_sps() srs_error_t SrsFormat::avc_demux_sps()
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (vcodec->sequenceParameterSetNALUnit.empty()) { if (vcodec->sequenceParameterSetNALUnit.empty()) {
return err; return err;
} }
SrsBuffer stream;
char* sps = &vcodec->sequenceParameterSetNALUnit[0]; char* sps = &vcodec->sequenceParameterSetNALUnit[0];
int nbsps = (int)vcodec->sequenceParameterSetNALUnit.size(); int nbsps = (int)vcodec->sequenceParameterSetNALUnit.size();
if ((ret = stream.initialize(sps, nbsps)) != ERROR_SUCCESS) {
return srs_error_new(ret, "init stream"); SrsBuffer stream(sps, nbsps);
}
// for NALU, 7.3.1 NAL unit syntax // for NALU, 7.3.1 NAL unit syntax
// ISO_IEC_14496-10-AVC-2012.pdf, page 61. // ISO_IEC_14496-10-AVC-2012.pdf, page 61.
@ -856,7 +847,6 @@ srs_error_t SrsFormat::avc_demux_sps()
srs_error_t 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; srs_error_t err = srs_success;
// we donot parse the detail of sps. // we donot parse the detail of sps.
@ -866,10 +856,7 @@ srs_error_t SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
} }
// reparse the rbsp. // reparse the rbsp.
SrsBuffer stream; SrsBuffer stream(rbsp, nb_rbsp);
if ((ret = stream.initialize(rbsp, nb_rbsp)) != ERROR_SUCCESS) {
return srs_error_new(ret, "init stream");
}
// for SPS, 7.3.2.1.1 Sequence parameter set data syntax // for SPS, 7.3.2.1.1 Sequence parameter set data syntax
// ISO_IEC_14496-10-AVC-2012.pdf, page 62. // ISO_IEC_14496-10-AVC-2012.pdf, page 62.
@ -892,13 +879,13 @@ srs_error_t SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
} }
SrsBitBuffer bs; SrsBitBuffer bs;
if ((ret = bs.initialize(&stream)) != ERROR_SUCCESS) { if ((err = bs.initialize(&stream)) != srs_success) {
return srs_error_new(ret, "init bit buffer"); return srs_error_wrap(err, "init bit buffer");
} }
int32_t seq_parameter_set_id = -1; int32_t seq_parameter_set_id = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, seq_parameter_set_id)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, seq_parameter_set_id)) != srs_success) {
return srs_error_new(ret, "read seq_parameter_set_id"); return srs_error_wrap(err, "read seq_parameter_set_id");
} }
if (seq_parameter_set_id < 0) { if (seq_parameter_set_id < 0) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the seq_parameter_set_id invalid"); return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the seq_parameter_set_id invalid");
@ -908,110 +895,110 @@ srs_error_t SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244 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 == 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) { if ((err = srs_avc_nalu_read_uev(&bs, chroma_format_idc)) != srs_success) {
return srs_error_new(ret, "read chroma_format_idc"); return srs_error_wrap(err, "read chroma_format_idc");
} }
if (chroma_format_idc == 3) { if (chroma_format_idc == 3) {
int8_t separate_colour_plane_flag = -1; int8_t separate_colour_plane_flag = -1;
if ((ret = srs_avc_nalu_read_bit(&bs, separate_colour_plane_flag)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_bit(&bs, separate_colour_plane_flag)) != srs_success) {
return srs_error_new(ret, "read separate_colour_plane_flag"); return srs_error_wrap(err, "read separate_colour_plane_flag");
} }
} }
int32_t bit_depth_luma_minus8 = -1; int32_t bit_depth_luma_minus8 = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_luma_minus8)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, bit_depth_luma_minus8)) != srs_success) {
return srs_error_new(ret, "read bit_depth_luma_minus8");; return srs_error_wrap(err, "read bit_depth_luma_minus8");;
} }
int32_t bit_depth_chroma_minus8 = -1; int32_t bit_depth_chroma_minus8 = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, bit_depth_chroma_minus8)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, bit_depth_chroma_minus8)) != srs_success) {
return srs_error_new(ret, "read bit_depth_chroma_minus8");; return srs_error_wrap(err, "read bit_depth_chroma_minus8");;
} }
int8_t qpprime_y_zero_transform_bypass_flag = -1; 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) { if ((err = srs_avc_nalu_read_bit(&bs, qpprime_y_zero_transform_bypass_flag)) != srs_success) {
return srs_error_new(ret, "read qpprime_y_zero_transform_bypass_flag");; return srs_error_wrap(err, "read qpprime_y_zero_transform_bypass_flag");;
} }
int8_t seq_scaling_matrix_present_flag = -1; int8_t seq_scaling_matrix_present_flag = -1;
if ((ret = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag)) != srs_success) {
return srs_error_new(ret, "read seq_scaling_matrix_present_flag");; return srs_error_wrap(err, "read seq_scaling_matrix_present_flag");;
} }
if (seq_scaling_matrix_present_flag) { if (seq_scaling_matrix_present_flag) {
int nb_scmpfs = ((chroma_format_idc != 3)? 8:12); int nb_scmpfs = ((chroma_format_idc != 3)? 8:12);
for (int i = 0; i < nb_scmpfs; i++) { for (int i = 0; i < nb_scmpfs; i++) {
int8_t seq_scaling_matrix_present_flag_i = -1; 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) { if ((err = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag_i)) != srs_success) {
return srs_error_new(ret, "read seq_scaling_matrix_present_flag_i");; return srs_error_wrap(err, "read seq_scaling_matrix_present_flag_i");;
} }
} }
} }
} }
int32_t log2_max_frame_num_minus4 = -1; int32_t log2_max_frame_num_minus4 = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, log2_max_frame_num_minus4)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, log2_max_frame_num_minus4)) != srs_success) {
return srs_error_new(ret, "read log2_max_frame_num_minus4");; return srs_error_wrap(err, "read log2_max_frame_num_minus4");;
} }
int32_t pic_order_cnt_type = -1; int32_t pic_order_cnt_type = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, pic_order_cnt_type)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, pic_order_cnt_type)) != srs_success) {
return srs_error_new(ret, "read pic_order_cnt_type");; return srs_error_wrap(err, "read pic_order_cnt_type");;
} }
if (pic_order_cnt_type == 0) { if (pic_order_cnt_type == 0) {
int32_t log2_max_pic_order_cnt_lsb_minus4 = -1; 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) { if ((err = srs_avc_nalu_read_uev(&bs, log2_max_pic_order_cnt_lsb_minus4)) != srs_success) {
return srs_error_new(ret, "read log2_max_pic_order_cnt_lsb_minus4");; return srs_error_wrap(err, "read log2_max_pic_order_cnt_lsb_minus4");;
} }
} else if (pic_order_cnt_type == 1) { } else if (pic_order_cnt_type == 1) {
int8_t delta_pic_order_always_zero_flag = -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) { if ((err = srs_avc_nalu_read_bit(&bs, delta_pic_order_always_zero_flag)) != srs_success) {
return srs_error_new(ret, "read delta_pic_order_always_zero_flag");; return srs_error_wrap(err, "read delta_pic_order_always_zero_flag");;
} }
int32_t offset_for_non_ref_pic = -1; int32_t offset_for_non_ref_pic = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_non_ref_pic)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, offset_for_non_ref_pic)) != srs_success) {
return srs_error_new(ret, "read offset_for_non_ref_pic");; return srs_error_wrap(err, "read offset_for_non_ref_pic");;
} }
int32_t offset_for_top_to_bottom_field = -1; 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) { if ((err = srs_avc_nalu_read_uev(&bs, offset_for_top_to_bottom_field)) != srs_success) {
return srs_error_new(ret, "read offset_for_top_to_bottom_field");; return srs_error_wrap(err, "read offset_for_top_to_bottom_field");;
} }
int32_t num_ref_frames_in_pic_order_cnt_cycle = -1; 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) { if ((err = srs_avc_nalu_read_uev(&bs, num_ref_frames_in_pic_order_cnt_cycle)) != srs_success) {
return srs_error_new(ret, "read num_ref_frames_in_pic_order_cnt_cycle");; return srs_error_wrap(err, "read num_ref_frames_in_pic_order_cnt_cycle");;
} }
if (num_ref_frames_in_pic_order_cnt_cycle < 0) { if (num_ref_frames_in_pic_order_cnt_cycle < 0) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the num_ref_frames_in_pic_order_cnt_cycle"); 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++) { for (int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
int32_t offset_for_ref_frame_i = -1; int32_t offset_for_ref_frame_i = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, offset_for_ref_frame_i)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, offset_for_ref_frame_i)) != srs_success) {
return srs_error_new(ret, "read offset_for_ref_frame_i");; return srs_error_wrap(err, "read offset_for_ref_frame_i");;
} }
} }
} }
int32_t max_num_ref_frames = -1; int32_t max_num_ref_frames = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, max_num_ref_frames)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, max_num_ref_frames)) != srs_success) {
return srs_error_new(ret, "read max_num_ref_frames");; return srs_error_wrap(err, "read max_num_ref_frames");;
} }
int8_t gaps_in_frame_num_value_allowed_flag = -1; 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) { if ((err = srs_avc_nalu_read_bit(&bs, gaps_in_frame_num_value_allowed_flag)) != srs_success) {
return srs_error_new(ret, "read gaps_in_frame_num_value_allowed_flag");; return srs_error_wrap(err, "read gaps_in_frame_num_value_allowed_flag");;
} }
int32_t pic_width_in_mbs_minus1 = -1; int32_t pic_width_in_mbs_minus1 = -1;
if ((ret = srs_avc_nalu_read_uev(&bs, pic_width_in_mbs_minus1)) != ERROR_SUCCESS) { if ((err = srs_avc_nalu_read_uev(&bs, pic_width_in_mbs_minus1)) != srs_success) {
return srs_error_new(ret, "read pic_width_in_mbs_minus1");; return srs_error_wrap(err, "read pic_width_in_mbs_minus1");;
} }
int32_t pic_height_in_map_units_minus1 = -1; 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) { if ((err = srs_avc_nalu_read_uev(&bs, pic_height_in_map_units_minus1)) != srs_success) {
return srs_error_new(ret, "read pic_height_in_map_units_minus1");; return srs_error_wrap(err, "read pic_height_in_map_units_minus1");;
} }
vcodec->width = (int)(pic_width_in_mbs_minus1 + 1) * 16; vcodec->width = (int)(pic_width_in_mbs_minus1 + 1) * 16;
@ -1323,12 +1310,10 @@ srs_error_t SrsFormat::audio_mp3_demux(SrsBuffer* stream, int64_t timestamp)
srs_error_t 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; srs_error_t err = srs_success;
if ((ret = buffer->initialize(data, size)) != ERROR_SUCCESS) { SrsBuffer* buffer = new SrsBuffer(data, size);
return srs_error_new(ret, "init buffer"); SrsAutoFree(SrsBuffer, buffer);
}
// only need to decode the first 2bytes: // only need to decode the first 2bytes:
// audioObjectType, aac_profile, 5bits. // audioObjectType, aac_profile, 5bits.

View file

@ -682,8 +682,6 @@ public:
public: public:
char* raw; char* raw;
int nb_raw; int nb_raw;
private:
SrsBuffer* buffer;
public: public:
// for sequence header, whether parse the h.264 sps. // for sequence header, whether parse the h.264 sps.
// TODO: FIXME: Refine it. // TODO: FIXME: Refine it.

View file

@ -46,66 +46,54 @@ SrsFileWriter::~SrsFileWriter()
close(); close();
} }
int SrsFileWriter::open(string p) srs_error_t SrsFileWriter::open(string p)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (fd > 0) { if (fd > 0) {
ret = ERROR_SYSTEM_FILE_ALREADY_OPENED; return srs_error_new(ERROR_SYSTEM_FILE_ALREADY_OPENED, "file %s already opened", p.c_str());
srs_error("file %s already opened. ret=%d", path.c_str(), ret);
return ret;
} }
int flags = O_CREAT|O_WRONLY|O_TRUNC; int flags = O_CREAT|O_WRONLY|O_TRUNC;
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
if ((fd = ::open(p.c_str(), flags, mode)) < 0) { if ((fd = ::open(p.c_str(), flags, mode)) < 0) {
ret = ERROR_SYSTEM_FILE_OPENE; return srs_error_new(ERROR_SYSTEM_FILE_OPENE, "open file %s failed", p.c_str());
srs_error("open file %s failed. ret=%d", p.c_str(), ret);
return ret;
} }
path = p; path = p;
return ret; return err;
} }
int SrsFileWriter::open_append(string p) srs_error_t SrsFileWriter::open_append(string p)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (fd > 0) { if (fd > 0) {
ret = ERROR_SYSTEM_FILE_ALREADY_OPENED; return srs_error_new(ERROR_SYSTEM_FILE_ALREADY_OPENED, "file %s already opened", path.c_str());
srs_error("file %s already opened. ret=%d", path.c_str(), ret);
return ret;
} }
int flags = O_APPEND|O_WRONLY; int flags = O_APPEND|O_WRONLY;
mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH; mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
if ((fd = ::open(p.c_str(), flags, mode)) < 0) { if ((fd = ::open(p.c_str(), flags, mode)) < 0) {
ret = ERROR_SYSTEM_FILE_OPENE; return srs_error_new(ERROR_SYSTEM_FILE_OPENE, "open file %s failed", p.c_str());
srs_error("open file %s failed. ret=%d", p.c_str(), ret);
return ret;
} }
path = p; path = p;
return ret; return err;
} }
void SrsFileWriter::close() void SrsFileWriter::close()
{ {
int ret = ERROR_SUCCESS;
if (fd < 0) { if (fd < 0) {
return; return;
} }
if (::close(fd) < 0) { if (::close(fd) < 0) {
ret = ERROR_SYSTEM_FILE_CLOSE; srs_warn("close file %s failed", path.c_str());
srs_error("close file %s failed. ret=%d", path.c_str(), ret);
return;
} }
fd = -1; fd = -1;
@ -128,35 +116,33 @@ int64_t SrsFileWriter::tellg()
return (int64_t)::lseek(fd, 0, SEEK_CUR); return (int64_t)::lseek(fd, 0, SEEK_CUR);
} }
int SrsFileWriter::write(void* buf, size_t count, ssize_t* pnwrite) srs_error_t SrsFileWriter::write(void* buf, size_t count, ssize_t* pnwrite)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
ssize_t nwrite; ssize_t nwrite;
// TODO: FIXME: use st_write. // TODO: FIXME: use st_write.
if ((nwrite = ::write(fd, buf, count)) < 0) { if ((nwrite = ::write(fd, buf, count)) < 0) {
ret = ERROR_SYSTEM_FILE_WRITE; return srs_error_new(ERROR_SYSTEM_FILE_WRITE, "write to file %s failed", path.c_str());
srs_error("write to file %s failed. ret=%d", path.c_str(), ret);
return ret;
} }
if (pnwrite != NULL) { if (pnwrite != NULL) {
*pnwrite = nwrite; *pnwrite = nwrite;
} }
return ret; return err;
} }
int SrsFileWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) srs_error_t SrsFileWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
ssize_t nwrite = 0; ssize_t nwrite = 0;
for (int i = 0; i < iovcnt; i++) { for (int i = 0; i < iovcnt; i++) {
const iovec* piov = iov + i; const iovec* piov = iov + i;
ssize_t this_nwrite = 0; ssize_t this_nwrite = 0;
if ((ret = write(piov->iov_base, piov->iov_len, &this_nwrite)) != ERROR_SUCCESS) { if ((err = write(piov->iov_base, piov->iov_len, &this_nwrite)) != srs_success) {
return ret; return srs_error_wrap(err, "write file");
} }
nwrite += this_nwrite; nwrite += this_nwrite;
} }
@ -165,20 +151,21 @@ int SrsFileWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
*pnwrite = nwrite; *pnwrite = nwrite;
} }
return ret; return err;
} }
int SrsFileWriter::lseek(off_t offset, int whence, off_t* seeked) srs_error_t SrsFileWriter::lseek(off_t offset, int whence, off_t* seeked)
{ {
off_t sk = ::lseek(fd, offset, whence); off_t sk = ::lseek(fd, offset, whence);
if (sk < 0) { if (sk < 0) {
return ERROR_SYSTEM_FILE_SEEK; return srs_error_new(ERROR_SYSTEM_FILE_SEEK, "seek file");
} }
if (seeked) { if (seeked) {
*seeked = sk; *seeked = sk;
} }
return ERROR_SUCCESS;
return srs_success;
} }
SrsFileReader::SrsFileReader() SrsFileReader::SrsFileReader()
@ -191,25 +178,21 @@ SrsFileReader::~SrsFileReader()
close(); close();
} }
int SrsFileReader::open(string p) srs_error_t SrsFileReader::open(string p)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (fd > 0) { if (fd > 0) {
ret = ERROR_SYSTEM_FILE_ALREADY_OPENED; return srs_error_new(ERROR_SYSTEM_FILE_ALREADY_OPENED, "file %s already opened", path.c_str());
srs_error("file %s already opened. ret=%d", path.c_str(), ret);
return ret;
} }
if ((fd = ::open(p.c_str(), O_RDONLY)) < 0) { if ((fd = ::open(p.c_str(), O_RDONLY)) < 0) {
ret = ERROR_SYSTEM_FILE_OPENE; return srs_error_new(ERROR_SYSTEM_FILE_OPENE, "open file %s failed", p.c_str());
srs_error("open file %s failed. ret=%d", p.c_str(), ret);
return ret;
} }
path = p; path = p;
return ret; return err;
} }
void SrsFileReader::close() void SrsFileReader::close()
@ -262,40 +245,38 @@ int64_t SrsFileReader::filesize()
return size; return size;
} }
int SrsFileReader::read(void* buf, size_t count, ssize_t* pnread) srs_error_t SrsFileReader::read(void* buf, size_t count, ssize_t* pnread)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
ssize_t nread; ssize_t nread;
// TODO: FIXME: use st_read. // TODO: FIXME: use st_read.
if ((nread = ::read(fd, buf, count)) < 0) { if ((nread = ::read(fd, buf, count)) < 0) {
ret = ERROR_SYSTEM_FILE_READ; return srs_error_new(ERROR_SYSTEM_FILE_READ, "read from file %s failed", path.c_str());
srs_error("read from file %s failed. ret=%d", path.c_str(), ret);
return ret;
} }
if (nread == 0) { if (nread == 0) {
ret = ERROR_SYSTEM_FILE_EOF; return srs_error_new(ERROR_SYSTEM_FILE_EOF, "file EOF");
return ret;
} }
if (pnread != NULL) { if (pnread != NULL) {
*pnread = nread; *pnread = nread;
} }
return ret; return err;
} }
int SrsFileReader::lseek(off_t offset, int whence, off_t* seeked) srs_error_t SrsFileReader::lseek(off_t offset, int whence, off_t* seeked)
{ {
off_t sk = ::lseek(fd, offset, whence); off_t sk = ::lseek(fd, offset, whence);
if (sk < 0) { if (sk < 0) {
return ERROR_SYSTEM_FILE_SEEK; return srs_error_new(ERROR_SYSTEM_FILE_SEEK, "seek %v failed", (int)sk);
} }
if (seeked) { if (seeked) {
*seeked = sk; *seeked = sk;
} }
return ERROR_SUCCESS;
return srs_success;
} }

View file

@ -51,12 +51,12 @@ public:
* open file writer, in truncate mode. * open file writer, in truncate mode.
* @param p a string indicates the path of file to open. * @param p a string indicates the path of file to open.
*/ */
virtual int open(std::string p); virtual srs_error_t open(std::string p);
/** /**
* open file writer, in append mode. * open file writer, in append mode.
* @param p a string indicates the path of file to open. * @param p a string indicates the path of file to open.
*/ */
virtual int open_append(std::string p); virtual srs_error_t open_append(std::string p);
/** /**
* close current writer. * close current writer.
* @remark user can reopen again. * @remark user can reopen again.
@ -68,9 +68,9 @@ public:
virtual int64_t tellg(); virtual int64_t tellg();
// Interface ISrsWriteSeeker // Interface ISrsWriteSeeker
public: public:
virtual int write(void* buf, size_t count, ssize_t* pnwrite); virtual srs_error_t write(void* buf, size_t count, ssize_t* pnwrite);
virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite); virtual srs_error_t writev(const iovec* iov, int iovcnt, ssize_t* pnwrite);
virtual int lseek(off_t offset, int whence, off_t* seeked); virtual srs_error_t lseek(off_t offset, int whence, off_t* seeked);
}; };
/** /**
@ -89,7 +89,7 @@ public:
* open file reader. * open file reader.
* @param p a string indicates the path of file to open. * @param p a string indicates the path of file to open.
*/ */
virtual int open(std::string p); virtual srs_error_t open(std::string p);
/** /**
* close current reader. * close current reader.
* @remark user can reopen again. * @remark user can reopen again.
@ -104,8 +104,8 @@ public:
virtual int64_t filesize(); virtual int64_t filesize();
// Interface ISrsReadSeeker // Interface ISrsReadSeeker
public: public:
virtual int read(void* buf, size_t count, ssize_t* pnread); virtual srs_error_t read(void* buf, size_t count, ssize_t* pnread);
virtual int lseek(off_t offset, int whence, off_t* seeked); virtual srs_error_t lseek(off_t offset, int whence, off_t* seeked);
}; };
#endif #endif

View file

@ -39,6 +39,7 @@ using namespace std;
#include <srs_kernel_codec.hpp> #include <srs_kernel_codec.hpp>
#include <srs_kernel_utility.hpp> #include <srs_kernel_utility.hpp>
#include <srs_core_mem_watch.hpp> #include <srs_core_mem_watch.hpp>
#include <srs_core_autofree.hpp>
SrsMessageHeader::SrsMessageHeader() SrsMessageHeader::SrsMessageHeader()
{ {
@ -178,10 +179,8 @@ void SrsCommonMessage::create_payload(int size)
#endif #endif
} }
int SrsCommonMessage::create(SrsMessageHeader* pheader, char* body, int size) srs_error_t SrsCommonMessage::create(SrsMessageHeader* pheader, char* body, int size)
{ {
int ret = ERROR_SUCCESS;
// drop previous payload. // drop previous payload.
srs_freepa(payload); srs_freepa(payload);
@ -189,7 +188,7 @@ int SrsCommonMessage::create(SrsMessageHeader* pheader, char* body, int size)
this->payload = body; this->payload = body;
this->size = size; this->size = size;
return ret; return srs_success;
} }
SrsSharedMessageHeader::SrsSharedMessageHeader() : payload_length(0), message_type(0), perfer_cid(0) SrsSharedMessageHeader::SrsSharedMessageHeader() : payload_length(0), message_type(0), perfer_cid(0)
@ -231,12 +230,12 @@ SrsSharedPtrMessage::~SrsSharedPtrMessage()
} }
} }
int SrsSharedPtrMessage::create(SrsCommonMessage* msg) srs_error_t SrsSharedPtrMessage::create(SrsCommonMessage* msg)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if ((ret = create(&msg->header, msg->payload, msg->size)) != ERROR_SUCCESS) { if ((err = create(&msg->header, msg->payload, msg->size)) != srs_success) {
return ret; return srs_error_wrap(err, "create message");
} }
// to prevent double free of payload: // to prevent double free of payload:
@ -245,19 +244,16 @@ int SrsSharedPtrMessage::create(SrsCommonMessage* msg)
msg->payload = NULL; msg->payload = NULL;
msg->size = 0; msg->size = 0;
return ret; return err;
} }
int SrsSharedPtrMessage::create(SrsMessageHeader* pheader, char* payload, int size) srs_error_t SrsSharedPtrMessage::create(SrsMessageHeader* pheader, char* payload, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (ptr) { if (ptr) {
ret = ERROR_SYSTEM_ASSERT_FAILED;
srs_error("should not set the payload twice. ret=%d", ret);
srs_assert(false); srs_assert(false);
return srs_error_new(ERROR_SYSTEM_ASSERT_FAILED, "should not set the payload twice");
return ret;
} }
ptr = new SrsSharedPtrPayload(); ptr = new SrsSharedPtrPayload();
@ -277,7 +273,7 @@ int SrsSharedPtrMessage::create(SrsMessageHeader* pheader, char* payload, int si
this->payload = ptr->payload; this->payload = ptr->payload;
this->size = ptr->size; this->size = ptr->size;
return ret; return err;
} }
int SrsSharedPtrMessage::count() int SrsSharedPtrMessage::count()
@ -291,8 +287,7 @@ bool SrsSharedPtrMessage::check(int stream_id)
// we donot use the complex basic header, // we donot use the complex basic header,
// ensure the basic header is 1bytes. // ensure the basic header is 1bytes.
if (ptr->header.perfer_cid < 2) { if (ptr->header.perfer_cid < 2) {
srs_info("change the chunk_id=%d to default=%d", srs_info("change the chunk_id=%d to default=%d", ptr->header.perfer_cid, RTMP_CID_ProtocolControl);
ptr->header.perfer_cid, RTMP_CID_ProtocolControl);
ptr->header.perfer_cid = RTMP_CID_ProtocolControl; ptr->header.perfer_cid = RTMP_CID_ProtocolControl;
} }
@ -324,9 +319,11 @@ bool SrsSharedPtrMessage::is_video()
int SrsSharedPtrMessage::chunk_header(char* cache, int nb_cache, bool c0) int SrsSharedPtrMessage::chunk_header(char* cache, int nb_cache, bool c0)
{ {
if (c0) { if (c0) {
return srs_chunk_header_c0(ptr->header.perfer_cid, timestamp, ptr->header.payload_length, ptr->header.message_type, stream_id, cache, nb_cache); return srs_chunk_header_c0(ptr->header.perfer_cid, (uint32_t)timestamp,
ptr->header.payload_length, ptr->header.message_type, stream_id, cache, nb_cache);
} else { } else {
return srs_chunk_header_c3(ptr->header.perfer_cid, timestamp, cache, nb_cache); return srs_chunk_header_c3(ptr->header.perfer_cid, (uint32_t)timestamp,
cache, nb_cache);
} }
} }
@ -350,7 +347,6 @@ SrsSharedPtrMessage* SrsSharedPtrMessage::copy()
SrsFlvTransmuxer::SrsFlvTransmuxer() SrsFlvTransmuxer::SrsFlvTransmuxer()
{ {
writer = NULL; writer = NULL;
tag_stream = new SrsBuffer();
#ifdef SRS_PERF_FAST_FLV_ENCODER #ifdef SRS_PERF_FAST_FLV_ENCODER
nb_tag_headers = 0; nb_tag_headers = 0;
@ -364,8 +360,6 @@ SrsFlvTransmuxer::SrsFlvTransmuxer()
SrsFlvTransmuxer::~SrsFlvTransmuxer() SrsFlvTransmuxer::~SrsFlvTransmuxer()
{ {
srs_freep(tag_stream);
#ifdef SRS_PERF_FAST_FLV_ENCODER #ifdef SRS_PERF_FAST_FLV_ENCODER
srs_freepa(tag_headers); srs_freepa(tag_headers);
srs_freepa(iovss_cache); srs_freepa(iovss_cache);
@ -373,16 +367,16 @@ SrsFlvTransmuxer::~SrsFlvTransmuxer()
#endif #endif
} }
int SrsFlvTransmuxer::initialize(ISrsWriter* fw) srs_error_t SrsFlvTransmuxer::initialize(ISrsWriter* fw)
{ {
srs_assert(fw); srs_assert(fw);
writer = fw; writer = fw;
return ERROR_SUCCESS; return srs_success;
} }
int SrsFlvTransmuxer::write_header() srs_error_t SrsFlvTransmuxer::write_header()
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
// 9bytes header and 4bytes first previous-tag-size // 9bytes header and 4bytes first previous-tag-size
char flv_header[] = { char flv_header[] = {
@ -397,88 +391,80 @@ int SrsFlvTransmuxer::write_header()
// so we generally set the audio/video to 0. // so we generally set the audio/video to 0.
// write 9bytes header. // write 9bytes header.
if ((ret = write_header(flv_header)) != ERROR_SUCCESS) { if ((err = write_header(flv_header)) != srs_success) {
return ret; return srs_error_wrap(err, "write header");
} }
return ret; return err;
} }
int SrsFlvTransmuxer::write_header(char flv_header[9]) srs_error_t SrsFlvTransmuxer::write_header(char flv_header[9])
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
// write data. // write data.
if ((ret = writer->write(flv_header, 9, NULL)) != ERROR_SUCCESS) { if ((err = writer->write(flv_header, 9, NULL)) != srs_success) {
srs_error("write flv header failed. ret=%d", ret); return srs_error_wrap(err, "write flv header failed");
return ret;
} }
// previous tag size. // previous tag size.
char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)0x00 }; char pts[] = { (char)0x00, (char)0x00, (char)0x00, (char)0x00 };
if ((ret = writer->write(pts, 4, NULL)) != ERROR_SUCCESS) { if ((err = writer->write(pts, 4, NULL)) != srs_success) {
return ret; return srs_error_wrap(err, "write pts");
} }
return ret; return err;
} }
int SrsFlvTransmuxer::write_metadata(char type, char* data, int size) srs_error_t SrsFlvTransmuxer::write_metadata(char type, char* data, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
if ((ret = write_metadata_to_cache(type, data, size, tag_header)) != ERROR_SUCCESS) { if ((err = write_metadata_to_cache(type, data, size, tag_header)) != srs_success) {
return ret; return srs_error_wrap(err, "cache metadata");
} }
if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) { if ((err = write_tag(tag_header, sizeof(tag_header), data, size)) != srs_success) {
if (!srs_is_client_gracefully_close(ret)) { return srs_error_wrap(err, "write tag");
srs_error("write flv data tag failed. ret=%d", ret);
}
return ret;
} }
return ret; return err;
} }
int SrsFlvTransmuxer::write_audio(int64_t timestamp, char* data, int size) srs_error_t SrsFlvTransmuxer::write_audio(int64_t timestamp, char* data, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
if ((ret = write_audio_to_cache(timestamp, data, size, tag_header)) != ERROR_SUCCESS) { if ((err = write_audio_to_cache(timestamp, data, size, tag_header)) != srs_success) {
return ret; return srs_error_wrap(err, "cache audio");
} }
if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) { if ((err = write_tag(tag_header, sizeof(tag_header), data, size)) != srs_success) {
if (!srs_is_client_gracefully_close(ret)) { return srs_error_wrap(err, "write tag");
srs_error("write flv audio tag failed. ret=%d", ret);
}
return ret;
} }
return ret; return err;
} }
int SrsFlvTransmuxer::write_video(int64_t timestamp, char* data, int size) srs_error_t SrsFlvTransmuxer::write_video(int64_t timestamp, char* data, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
if ((ret = write_video_to_cache(timestamp, data, size, tag_header)) != ERROR_SUCCESS) { if ((err = write_video_to_cache(timestamp, data, size, tag_header)) != srs_success) {
return ret; return srs_error_wrap(err, "cache video");
} }
if ((ret = write_tag(tag_header, sizeof(tag_header), data, size)) != ERROR_SUCCESS) { if ((err = write_tag(tag_header, sizeof(tag_header), data, size)) != srs_success) {
srs_error("write flv video tag failed. ret=%d", ret); return srs_error_wrap(err, "write flv video tag failed");
return ret;
} }
return ret; return err;
} }
int SrsFlvTransmuxer::size_tag(int data_size) int SrsFlvTransmuxer::size_tag(int data_size)
@ -488,9 +474,9 @@ int SrsFlvTransmuxer::size_tag(int data_size)
} }
#ifdef SRS_PERF_FAST_FLV_ENCODER #ifdef SRS_PERF_FAST_FLV_ENCODER
int SrsFlvTransmuxer::write_tags(SrsSharedPtrMessage** msgs, int count) srs_error_t SrsFlvTransmuxer::write_tags(SrsSharedPtrMessage** msgs, int count)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
// realloc the iovss. // realloc the iovss.
int nb_iovss = 3 * count; int nb_iovss = 3 * count;
@ -527,22 +513,22 @@ int SrsFlvTransmuxer::write_tags(SrsSharedPtrMessage** msgs, int count)
// cache all flv header. // cache all flv header.
if (msg->is_audio()) { if (msg->is_audio()) {
if ((ret = write_audio_to_cache(msg->timestamp, msg->payload, msg->size, cache)) != ERROR_SUCCESS) { if ((err = write_audio_to_cache(msg->timestamp, msg->payload, msg->size, cache)) != srs_success) {
return ret; return srs_error_wrap(err, "cache audio");
} }
} else if (msg->is_video()) { } else if (msg->is_video()) {
if ((ret = write_video_to_cache(msg->timestamp, msg->payload, msg->size, cache)) != ERROR_SUCCESS) { if ((err = write_video_to_cache(msg->timestamp, msg->payload, msg->size, cache)) != srs_success) {
return ret; return srs_error_wrap(err, "cache video");
} }
} else { } else {
if ((ret = write_metadata_to_cache(SrsFrameTypeScript, msg->payload, msg->size, cache)) != ERROR_SUCCESS) { if ((err = write_metadata_to_cache(SrsFrameTypeScript, msg->payload, msg->size, cache)) != srs_success) {
return ret; return srs_error_wrap(err, "cache metadata");
} }
} }
// cache all pts. // cache all pts.
if ((ret = write_pts_to_cache(SRS_FLV_TAG_HEADER_SIZE + msg->size, pts)) != ERROR_SUCCESS) { if ((err = write_pts_to_cache(SRS_FLV_TAG_HEADER_SIZE + msg->size, pts)) != srs_success) {
return ret; return srs_error_wrap(err, "cache pts");
} }
// all ioves. // all ioves.
@ -559,20 +545,17 @@ int SrsFlvTransmuxer::write_tags(SrsSharedPtrMessage** msgs, int count)
iovs += 3; iovs += 3;
} }
if ((ret = writer->writev(iovss, nb_iovss, NULL)) != ERROR_SUCCESS) { if ((err = writer->writev(iovss, nb_iovss, NULL)) != srs_success) {
if (!srs_is_client_gracefully_close(ret)) { return srs_error_wrap(err, "write flv tags failed");
srs_error("write flv tags failed. ret=%d", ret);
}
return ret;
} }
return ret; return err;
} }
#endif #endif
int SrsFlvTransmuxer::write_metadata_to_cache(char type, char* data, int size, char* cache) srs_error_t SrsFlvTransmuxer::write_metadata_to_cache(char type, char* data, int size, char* cache)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
@ -585,22 +568,22 @@ int SrsFlvTransmuxer::write_metadata_to_cache(char type, char* data, int size, c
(char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0. (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
};*/ };*/
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
SrsAutoFree(SrsBuffer, tag_stream);
// write data size. // write data size.
if ((ret = tag_stream->initialize(cache, 11)) != ERROR_SUCCESS) {
return ret;
}
tag_stream->write_1bytes(type); tag_stream->write_1bytes(type);
tag_stream->write_3bytes(size); tag_stream->write_3bytes(size);
tag_stream->write_3bytes(0x00); tag_stream->write_3bytes(0x00);
tag_stream->write_1bytes(0x00); tag_stream->write_1bytes(0x00);
tag_stream->write_3bytes(0x00); tag_stream->write_3bytes(0x00);
return ret; return err;
} }
int SrsFlvTransmuxer::write_audio_to_cache(int64_t timestamp, char* data, int size, char* cache) srs_error_t SrsFlvTransmuxer::write_audio_to_cache(int64_t timestamp, char* data, int size, char* cache)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
@ -615,10 +598,10 @@ int SrsFlvTransmuxer::write_audio_to_cache(int64_t timestamp, char* data, int si
(char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0. (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
};*/ };*/
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
SrsAutoFree(SrsBuffer, tag_stream);
// write data size. // write data size.
if ((ret = tag_stream->initialize(cache, 11)) != ERROR_SUCCESS) {
return ret;
}
tag_stream->write_1bytes(SrsFrameTypeAudio); tag_stream->write_1bytes(SrsFrameTypeAudio);
tag_stream->write_3bytes(size); tag_stream->write_3bytes(size);
tag_stream->write_3bytes((int32_t)timestamp); tag_stream->write_3bytes((int32_t)timestamp);
@ -626,12 +609,12 @@ int SrsFlvTransmuxer::write_audio_to_cache(int64_t timestamp, char* data, int si
tag_stream->write_1bytes((timestamp >> 24) & 0xFF); tag_stream->write_1bytes((timestamp >> 24) & 0xFF);
tag_stream->write_3bytes(0x00); tag_stream->write_3bytes(0x00);
return ret; return err;
} }
int SrsFlvTransmuxer::write_video_to_cache(int64_t timestamp, char* data, int size, char* cache) srs_error_t SrsFlvTransmuxer::write_video_to_cache(int64_t timestamp, char* data, int size, char* cache)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
@ -646,10 +629,10 @@ int SrsFlvTransmuxer::write_video_to_cache(int64_t timestamp, char* data, int si
(char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0. (char)0x00, (char)0x00, (char)0x00, // StreamID UI24 Always 0.
};*/ };*/
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
SrsAutoFree(SrsBuffer, tag_stream);
// write data size. // write data size.
if ((ret = tag_stream->initialize(cache, 11)) != ERROR_SUCCESS) {
return ret;
}
tag_stream->write_1bytes(SrsFrameTypeVideo); tag_stream->write_1bytes(SrsFrameTypeVideo);
tag_stream->write_3bytes(size); tag_stream->write_3bytes(size);
tag_stream->write_3bytes((int32_t)timestamp); tag_stream->write_3bytes((int32_t)timestamp);
@ -657,29 +640,29 @@ int SrsFlvTransmuxer::write_video_to_cache(int64_t timestamp, char* data, int si
tag_stream->write_1bytes((timestamp >> 24) & 0xFF); tag_stream->write_1bytes((timestamp >> 24) & 0xFF);
tag_stream->write_3bytes(0x00); tag_stream->write_3bytes(0x00);
return ret; return err;
} }
int SrsFlvTransmuxer::write_pts_to_cache(int size, char* cache) srs_error_t SrsFlvTransmuxer::write_pts_to_cache(int size, char* cache)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
SrsBuffer* tag_stream = new SrsBuffer(cache, 11);
SrsAutoFree(SrsBuffer, tag_stream);
if ((ret = tag_stream->initialize(cache, SRS_FLV_PREVIOUS_TAG_SIZE)) != ERROR_SUCCESS) {
return ret;
}
tag_stream->write_4bytes(size); tag_stream->write_4bytes(size);
return ret; return err;
} }
int SrsFlvTransmuxer::write_tag(char* header, int header_size, char* tag, int tag_size) srs_error_t SrsFlvTransmuxer::write_tag(char* header, int header_size, char* tag, int tag_size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
// PreviousTagSizeN UI32 Size of last tag, including its header, in bytes. // PreviousTagSizeN UI32 Size of last tag, including its header, in bytes.
char pre_size[SRS_FLV_PREVIOUS_TAG_SIZE]; char pre_size[SRS_FLV_PREVIOUS_TAG_SIZE];
if ((ret = write_pts_to_cache(tag_size + header_size, pre_size)) != ERROR_SUCCESS) { if ((err = write_pts_to_cache(tag_size + header_size, pre_size)) != srs_success) {
return ret; return srs_error_wrap(err, "cache pts");
} }
iovec iovs[3]; iovec iovs[3];
@ -690,58 +673,51 @@ int SrsFlvTransmuxer::write_tag(char* header, int header_size, char* tag, int ta
iovs[2].iov_base = pre_size; iovs[2].iov_base = pre_size;
iovs[2].iov_len = SRS_FLV_PREVIOUS_TAG_SIZE; iovs[2].iov_len = SRS_FLV_PREVIOUS_TAG_SIZE;
if ((ret = writer->writev(iovs, 3, NULL)) != ERROR_SUCCESS) { if ((err = writer->writev(iovs, 3, NULL)) != srs_success) {
if (!srs_is_client_gracefully_close(ret)) { return srs_error_wrap(err, "write flv tag failed");
srs_error("write flv tag failed. ret=%d", ret);
}
return ret;
} }
return ret; return err;
} }
SrsFlvDecoder::SrsFlvDecoder() SrsFlvDecoder::SrsFlvDecoder()
{ {
reader = NULL; reader = NULL;
tag_stream = new SrsBuffer();
} }
SrsFlvDecoder::~SrsFlvDecoder() SrsFlvDecoder::~SrsFlvDecoder()
{ {
srs_freep(tag_stream);
} }
int SrsFlvDecoder::initialize(ISrsReader* fr) srs_error_t SrsFlvDecoder::initialize(ISrsReader* fr)
{ {
srs_assert(fr); srs_assert(fr);
reader = fr; reader = fr;
return ERROR_SUCCESS; return srs_success;
} }
int SrsFlvDecoder::read_header(char header[9]) srs_error_t SrsFlvDecoder::read_header(char header[9])
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(header); srs_assert(header);
// TODO: FIXME: Should use readfully. // TODO: FIXME: Should use readfully.
if ((ret = reader->read(header, 9, NULL)) != ERROR_SUCCESS) { if ((err = reader->read(header, 9, NULL)) != srs_success) {
return ret; return srs_error_wrap(err, "read header");
} }
char* h = header; char* h = header;
if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') { if (h[0] != 'F' || h[1] != 'L' || h[2] != 'V') {
ret = ERROR_KERNEL_FLV_HEADER; return srs_error_new(ERROR_KERNEL_FLV_HEADER, "flv header must start with FLV");
srs_warn("flv header must start with FLV. ret=%d", ret);
return ret;
} }
return ret; return err;
} }
int SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, uint32_t* ptime) srs_error_t SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, uint32_t* ptime)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(ptype); srs_assert(ptype);
srs_assert(pdata_size); srs_assert(pdata_size);
@ -751,11 +727,8 @@ int SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, uint32_t* p
// read tag header // read tag header
// TODO: FIXME: Should use readfully. // TODO: FIXME: Should use readfully.
if ((ret = reader->read(th, 11, NULL)) != ERROR_SUCCESS) { if ((err = reader->read(th, 11, NULL)) != srs_success) {
if (ret != ERROR_SYSTEM_FILE_EOF) { return srs_error_wrap(err, "read flv tag header failed");
srs_error("read flv tag header failed. ret=%d", ret);
}
return ret;
} }
// Reserved UB [2] // Reserved UB [2]
@ -779,80 +752,68 @@ int SrsFlvDecoder::read_tag_header(char* ptype, int32_t* pdata_size, uint32_t* p
// TimestampExtended UI8 // TimestampExtended UI8
pp[3] = th[7]; pp[3] = th[7];
return ret; return err;
} }
int SrsFlvDecoder::read_tag_data(char* data, int32_t size) srs_error_t SrsFlvDecoder::read_tag_data(char* data, int32_t size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
// TODO: FIXME: Should use readfully. // TODO: FIXME: Should use readfully.
if ((ret = reader->read(data, size, NULL)) != ERROR_SUCCESS) { if ((err = reader->read(data, size, NULL)) != srs_success) {
if (ret != ERROR_SYSTEM_FILE_EOF) { return srs_error_wrap(err, "read flv tag header failed");
srs_error("read flv tag header failed. ret=%d", ret);
}
return ret;
} }
return ret; return err;
} }
int SrsFlvDecoder::read_previous_tag_size(char previous_tag_size[4]) srs_error_t SrsFlvDecoder::read_previous_tag_size(char previous_tag_size[4])
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(previous_tag_size); srs_assert(previous_tag_size);
// ignore 4bytes tag size. // ignore 4bytes tag size.
// TODO: FIXME: Should use readfully. // TODO: FIXME: Should use readfully.
if ((ret = reader->read(previous_tag_size, 4, NULL)) != ERROR_SUCCESS) { if ((err = reader->read(previous_tag_size, 4, NULL)) != srs_success) {
if (ret != ERROR_SYSTEM_FILE_EOF) { return srs_error_wrap(err, "read flv previous tag size failed");
srs_error("read flv previous tag size failed. ret=%d", ret);
}
return ret;
} }
return ret; return err;
} }
SrsFlvVodStreamDecoder::SrsFlvVodStreamDecoder() SrsFlvVodStreamDecoder::SrsFlvVodStreamDecoder()
{ {
reader = NULL; reader = NULL;
tag_stream = new SrsBuffer();
} }
SrsFlvVodStreamDecoder::~SrsFlvVodStreamDecoder() SrsFlvVodStreamDecoder::~SrsFlvVodStreamDecoder()
{ {
srs_freep(tag_stream);
} }
int SrsFlvVodStreamDecoder::initialize(ISrsReader* fr) srs_error_t SrsFlvVodStreamDecoder::initialize(ISrsReader* fr)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(fr); srs_assert(fr);
reader = dynamic_cast<SrsFileReader*>(fr); reader = dynamic_cast<SrsFileReader*>(fr);
if (!reader) { if (!reader) {
ret = ERROR_EXPECT_FILE_IO; return srs_error_new(ERROR_EXPECT_FILE_IO, "stream is not file io");
srs_error("stream is not file io. ret=%d", ret);
return ret;
} }
if (!reader->is_open()) { if (!reader->is_open()) {
ret = ERROR_KERNEL_FLV_STREAM_CLOSED; return srs_error_new(ERROR_KERNEL_FLV_STREAM_CLOSED, "stream is not open for decoder");
srs_warn("stream is not open for decoder. ret=%d", ret);
return ret;
} }
return ret; return err;
} }
int SrsFlvVodStreamDecoder::read_header_ext(char header[13]) srs_error_t SrsFlvVodStreamDecoder::read_header_ext(char header[13])
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(header); srs_assert(header);
@ -862,16 +823,16 @@ int SrsFlvVodStreamDecoder::read_header_ext(char header[13])
// 9bytes header and 4bytes first previous-tag-size // 9bytes header and 4bytes first previous-tag-size
int size = 13; int size = 13;
if ((ret = reader->read(header, size, NULL)) != ERROR_SUCCESS) { if ((err = reader->read(header, size, NULL)) != srs_success) {
return ret; return srs_error_wrap(err, "read header");
} }
return ret; return err;
} }
int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* psize) srs_error_t SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* psize)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(pstart); srs_assert(pstart);
srs_assert(psize); srs_assert(psize);
@ -896,13 +857,12 @@ int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* p
int64_t av_sequence_offset_start = -1; int64_t av_sequence_offset_start = -1;
int64_t av_sequence_offset_end = -1; int64_t av_sequence_offset_end = -1;
for (;;) { for (;;) {
if ((ret = reader->read(tag_header, SRS_FLV_TAG_HEADER_SIZE, NULL)) != ERROR_SUCCESS) { if ((err = reader->read(tag_header, SRS_FLV_TAG_HEADER_SIZE, NULL)) != srs_success) {
return ret; return srs_error_wrap(err, "read tag header");
} }
if ((ret = tag_stream->initialize(tag_header, SRS_FLV_TAG_HEADER_SIZE)) != ERROR_SUCCESS) { SrsBuffer* tag_stream = new SrsBuffer(tag_header, SRS_FLV_TAG_HEADER_SIZE);
return ret; SrsAutoFree(SrsBuffer, tag_stream);
}
int8_t tag_type = tag_stream->read_1bytes(); int8_t tag_type = tag_stream->read_1bytes();
int32_t data_size = tag_stream->read_3bytes(); int32_t data_size = tag_stream->read_3bytes();
@ -957,30 +917,22 @@ int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* p
*psize = (int)(av_sequence_offset_end - av_sequence_offset_start); *psize = (int)(av_sequence_offset_end - av_sequence_offset_start);
} }
return ret; return err;
} }
int SrsFlvVodStreamDecoder::seek2(int64_t offset) srs_error_t SrsFlvVodStreamDecoder::seek2(int64_t offset)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (offset >= reader->filesize()) { if (offset >= reader->filesize()) {
ret = ERROR_SYSTEM_FILE_EOF; return srs_error_new(ERROR_SYSTEM_FILE_EOF, "flv fast decoder seek overflow file, size=%d, offset=%d", (int)reader->filesize(), (int)offset);
srs_warn("flv fast decoder seek overflow file, "
"size=%" PRId64 ", offset=%" PRId64 ", ret=%d",
reader->filesize(), offset, ret);
return ret;
} }
if (reader->seek2(offset) < 0) { if (reader->seek2(offset) < 0) {
ret = ERROR_SYSTEM_FILE_SEEK; return srs_error_new(ERROR_SYSTEM_FILE_SEEK, "flv fast decoder seek error, size=%d, offset=%d", (int)reader->filesize(), (int)offset);
srs_warn("flv fast decoder seek error, "
"size=%" PRId64 ", offset=%" PRId64 ", ret=%d",
reader->filesize(), offset, ret);
return ret;
} }
return ret; return err;
} }

View file

@ -294,7 +294,7 @@ public:
* @remark user should never free the body. * @remark user should never free the body.
* @param pheader, the header to copy to the message. NULL to ignore. * @param pheader, the header to copy to the message. NULL to ignore.
*/ */
virtual int create(SrsMessageHeader* pheader, char* body, int size); virtual srs_error_t create(SrsMessageHeader* pheader, char* body, int size);
}; };
/** /**
@ -398,14 +398,14 @@ public:
* set the payload to NULL to prevent double free. * set the payload to NULL to prevent double free.
* @remark payload of msg set to NULL if success. * @remark payload of msg set to NULL if success.
*/ */
virtual int create(SrsCommonMessage* msg); virtual srs_error_t create(SrsCommonMessage* msg);
/** /**
* create shared ptr message, * create shared ptr message,
* from the header and payload. * from the header and payload.
* @remark user should never free the payload. * @remark user should never free the payload.
* @param pheader, the header to copy to the message. NULL to ignore. * @param pheader, the header to copy to the message. NULL to ignore.
*/ */
virtual int create(SrsMessageHeader* pheader, char* payload, int size); virtual srs_error_t create(SrsMessageHeader* pheader, char* payload, int size);
/** /**
* get current reference count. * get current reference count.
* when this object created, count set to 0. * when this object created, count set to 0.
@ -445,7 +445,6 @@ class SrsFlvTransmuxer
private: private:
ISrsWriter* writer; ISrsWriter* writer;
private: private:
SrsBuffer* tag_stream;
char tag_header[SRS_FLV_TAG_HEADER_SIZE]; char tag_header[SRS_FLV_TAG_HEADER_SIZE];
public: public:
SrsFlvTransmuxer(); SrsFlvTransmuxer();
@ -456,7 +455,7 @@ public:
* @remark user can initialize multiple times to encode multiple flv files. * @remark user can initialize multiple times to encode multiple flv files.
* @remark, user must free the @param fw, flv encoder never close/free it. * @remark, user must free the @param fw, flv encoder never close/free it.
*/ */
virtual int initialize(ISrsWriter* fw); virtual srs_error_t initialize(ISrsWriter* fw);
public: public:
/** /**
* write flv header. * write flv header.
@ -465,8 +464,8 @@ public:
* 2. PreviousTagSize0 UI32 Always 0 * 2. PreviousTagSize0 UI32 Always 0
* that is, 9+4=13bytes. * that is, 9+4=13bytes.
*/ */
virtual int write_header(); virtual srs_error_t write_header();
virtual int write_header(char flv_header[9]); virtual srs_error_t write_header(char flv_header[9]);
/** /**
* write flv metadata. * write flv metadata.
* @param type, the type of data, or other message type. * @param type, the type of data, or other message type.
@ -476,13 +475,13 @@ public:
* AMF0 object: the metadata object. * AMF0 object: the metadata object.
* @remark assert data is not NULL. * @remark assert data is not NULL.
*/ */
virtual int write_metadata(char type, char* data, int size); virtual srs_error_t write_metadata(char type, char* data, int size);
/** /**
* write audio/video packet. * write audio/video packet.
* @remark assert data is not NULL. * @remark assert data is not NULL.
*/ */
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
virtual int write_video(int64_t timestamp, char* data, int size); virtual srs_error_t write_video(int64_t timestamp, char* data, int size);
public: public:
/** /**
* get the tag size, * get the tag size,
@ -505,14 +504,14 @@ public:
/** /**
* write the tags in a time. * write the tags in a time.
*/ */
virtual int write_tags(SrsSharedPtrMessage** msgs, int count); virtual srs_error_t write_tags(SrsSharedPtrMessage** msgs, int count);
#endif #endif
private: private:
virtual int write_metadata_to_cache(char type, char* data, int size, char* cache); virtual srs_error_t write_metadata_to_cache(char type, char* data, int size, char* cache);
virtual int write_audio_to_cache(int64_t timestamp, char* data, int size, char* cache); virtual srs_error_t write_audio_to_cache(int64_t timestamp, char* data, int size, char* cache);
virtual int write_video_to_cache(int64_t timestamp, char* data, int size, char* cache); virtual srs_error_t write_video_to_cache(int64_t timestamp, char* data, int size, char* cache);
virtual int write_pts_to_cache(int size, char* cache); virtual srs_error_t write_pts_to_cache(int size, char* cache);
virtual int write_tag(char* header, int header_size, char* tag, int tag_size); virtual srs_error_t write_tag(char* header, int header_size, char* tag, int tag_size);
}; };
/** /**
@ -522,8 +521,6 @@ class SrsFlvDecoder
{ {
private: private:
ISrsReader* reader; ISrsReader* reader;
private:
SrsBuffer* tag_stream;
public: public:
SrsFlvDecoder(); SrsFlvDecoder();
virtual ~SrsFlvDecoder(); virtual ~SrsFlvDecoder();
@ -533,28 +530,28 @@ public:
* @remark user can initialize multiple times to decode multiple flv files. * @remark user can initialize multiple times to decode multiple flv files.
* @remark user must free the @param fr, flv decoder never close/free it * @remark user must free the @param fr, flv decoder never close/free it
*/ */
virtual int initialize(ISrsReader* fr); virtual srs_error_t initialize(ISrsReader* fr);
public: public:
/** /**
* read the flv header, donot including the 4bytes previous tag size. * read the flv header, donot including the 4bytes previous tag size.
* @remark assert header not NULL. * @remark assert header not NULL.
*/ */
virtual int read_header(char header[9]); virtual srs_error_t read_header(char header[9]);
/** /**
* read the tag header infos. * read the tag header infos.
* @remark assert ptype/pdata_size/ptime not NULL. * @remark assert ptype/pdata_size/ptime not NULL.
*/ */
virtual int read_tag_header(char* ptype, int32_t* pdata_size, uint32_t* ptime); virtual srs_error_t read_tag_header(char* ptype, int32_t* pdata_size, uint32_t* ptime);
/** /**
* read the tag data. * read the tag data.
* @remark assert data not NULL. * @remark assert data not NULL.
*/ */
virtual int read_tag_data(char* data, int32_t size); virtual srs_error_t read_tag_data(char* data, int32_t size);
/** /**
* read the 4bytes previous tag size. * read the 4bytes previous tag size.
* @remark assert previous_tag_size not NULL. * @remark assert previous_tag_size not NULL.
*/ */
virtual int read_previous_tag_size(char previous_tag_size[4]); virtual srs_error_t read_previous_tag_size(char previous_tag_size[4]);
}; };
/** /**
@ -566,8 +563,6 @@ class SrsFlvVodStreamDecoder
{ {
private: private:
SrsFileReader* reader; SrsFileReader* reader;
private:
SrsBuffer* tag_stream;
public: public:
SrsFlvVodStreamDecoder(); SrsFlvVodStreamDecoder();
virtual ~SrsFlvVodStreamDecoder(); virtual ~SrsFlvVodStreamDecoder();
@ -577,14 +572,14 @@ public:
* @remark user can initialize multiple times to decode multiple flv files. * @remark user can initialize multiple times to decode multiple flv files.
* @remark user must free the @param fr, flv decoder never close/free it. * @remark user must free the @param fr, flv decoder never close/free it.
*/ */
virtual int initialize(ISrsReader* fr); virtual srs_error_t initialize(ISrsReader* fr);
public: public:
/** /**
* read the flv header and its size. * read the flv header and its size.
* @param header, fill it 13bytes(9bytes header, 4bytes previous tag size). * @param header, fill it 13bytes(9bytes header, 4bytes previous tag size).
* @remark assert header not NULL. * @remark assert header not NULL.
*/ */
virtual int read_header_ext(char header[13]); virtual srs_error_t read_header_ext(char header[13]);
/** /**
* read the sequence header tags offset and its size. * read the sequence header tags offset and its size.
* @param pstart, the start offset of sequence header. * @param pstart, the start offset of sequence header.
@ -592,12 +587,12 @@ public:
* @remark we think the first audio/video is sequence header. * @remark we think the first audio/video is sequence header.
* @remark assert pstart/psize not NULL. * @remark assert pstart/psize not NULL.
*/ */
virtual int read_sequence_header_summary(int64_t* pstart, int* psize); virtual srs_error_t read_sequence_header_summary(int64_t* pstart, int* psize);
public: public:
/** /**
* for start offset, seed to this position and response flv stream. * for start offset, seed to this position and response flv stream.
*/ */
virtual int seek2(int64_t offset); virtual srs_error_t seek2(int64_t offset);
}; };
#endif #endif

View file

@ -44,7 +44,7 @@ public:
* Read bytes from reader. * Read bytes from reader.
* @param nread How many bytes read from channel. NULL to ignore. * @param nread How many bytes read from channel. NULL to ignore.
*/ */
virtual int read(void* buf, size_t size, ssize_t* nread) = 0; virtual srs_error_t read(void* buf, size_t size, ssize_t* nread) = 0;
}; };
/** /**
@ -65,7 +65,7 @@ public:
* @param seeked Upon successful completion, lseek() returns the resulting offset location as measured in bytes from * @param seeked Upon successful completion, lseek() returns the resulting offset location as measured in bytes from
* the beginning of the file. NULL to ignore. * the beginning of the file. NULL to ignore.
*/ */
virtual int lseek(off_t offset, int whence, off_t* seeked) = 0; virtual srs_error_t lseek(off_t offset, int whence, off_t* seeked) = 0;
}; };
/** /**
@ -91,7 +91,7 @@ public:
* write bytes over writer. * write bytes over writer.
* @nwrite the actual written bytes. NULL to ignore. * @nwrite the actual written bytes. NULL to ignore.
*/ */
virtual int write(void* buf, size_t size, ssize_t* nwrite) = 0; virtual srs_error_t write(void* buf, size_t size, ssize_t* nwrite) = 0;
}; };
/** /**
@ -109,7 +109,7 @@ public:
* @remark for the HTTP FLV, to writev to improve performance. * @remark for the HTTP FLV, to writev to improve performance.
* @see https://github.com/ossrs/srs/issues/405 * @see https://github.com/ossrs/srs/issues/405
*/ */
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0; virtual srs_error_t writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0;
}; };
/** /**

View file

@ -39,37 +39,36 @@ using namespace std;
#include <srs_kernel_buffer.hpp> #include <srs_kernel_buffer.hpp>
#include <srs_kernel_file.hpp> #include <srs_kernel_file.hpp>
#include <srs_kernel_codec.hpp> #include <srs_kernel_codec.hpp>
#include <srs_core_autofree.hpp>
SrsMp3Transmuxer::SrsMp3Transmuxer() SrsMp3Transmuxer::SrsMp3Transmuxer()
{ {
writer = NULL; writer = NULL;
tag_stream = new SrsBuffer();
} }
SrsMp3Transmuxer::~SrsMp3Transmuxer() SrsMp3Transmuxer::~SrsMp3Transmuxer()
{ {
srs_freep(tag_stream);
} }
int SrsMp3Transmuxer::initialize(SrsFileWriter* fw) srs_error_t SrsMp3Transmuxer::initialize(SrsFileWriter* fw)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(fw); srs_assert(fw);
if (!fw->is_open()) { if (!fw->is_open()) {
ret = ERROR_KERNEL_MP3_STREAM_CLOSED; return srs_error_new(ERROR_KERNEL_MP3_STREAM_CLOSED, "stream is not open");
srs_warn("stream is not open for encoder. ret=%d", ret);
return ret;
} }
writer = fw; writer = fw;
return ret; return err;
} }
int SrsMp3Transmuxer::write_header() srs_error_t SrsMp3Transmuxer::write_header()
{ {
srs_error_t err = srs_success;
char id3[] = { char id3[] = {
(char)0x49, (char)0x44, (char)0x33, // ID3 (char)0x49, (char)0x44, (char)0x33, // ID3
(char)0x03, (char)0x00, // version (char)0x03, (char)0x00, // version
@ -80,27 +79,28 @@ int SrsMp3Transmuxer::write_header()
(char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameSize (char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameSize
(char)0x00, (char)0x00 // Flags (char)0x00, (char)0x00 // Flags
}; };
return writer->write(id3, sizeof(id3), NULL);
if ((err = writer->write(id3, sizeof(id3), NULL)) != srs_success) {
return srs_error_wrap(err, "write id3");
} }
int SrsMp3Transmuxer::write_audio(int64_t timestamp, char* data, int size) return err;
}
srs_error_t SrsMp3Transmuxer::write_audio(int64_t timestamp, char* data, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
srs_assert(data); srs_assert(data);
timestamp &= 0x7fffffff; timestamp &= 0x7fffffff;
SrsBuffer* stream = tag_stream; SrsBuffer* stream = new SrsBuffer(data, size);
if ((ret = stream->initialize(data, size)) != ERROR_SUCCESS) { SrsAutoFree(SrsBuffer, stream);
return ret;
}
// audio decode // audio decode
if (!stream->require(1)) { if (!stream->require(1)) {
ret = ERROR_MP3_DECODE_ERROR; return srs_error_new(ERROR_MP3_DECODE_ERROR, "decode sound_format");
srs_error("mp3 decode audio sound_format failed. ret=%d", ret);
return ret;
} }
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76 // @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
@ -112,18 +112,18 @@ int SrsMp3Transmuxer::write_audio(int64_t timestamp, char* data, int size)
sound_format = (sound_format >> 4) & 0x0f; sound_format = (sound_format >> 4) & 0x0f;
if ((SrsAudioCodecId)sound_format != SrsAudioCodecIdMP3) { if ((SrsAudioCodecId)sound_format != SrsAudioCodecIdMP3) {
ret = ERROR_MP3_DECODE_ERROR; return srs_error_new(ERROR_MP3_DECODE_ERROR, "mp3 required, format=%d", sound_format);
srs_error("mp3 required, format=%d. ret=%d", sound_format, ret);
return ret;
} }
if (!stream->require(1)) { if (!stream->require(1)) {
ret = ERROR_MP3_DECODE_ERROR; return srs_error_new(ERROR_MP3_DECODE_ERROR, "mp3 decode aac_packet_type failed");
srs_error("mp3 decode aac_packet_type failed. ret=%d", ret);
return ret;
} }
return writer->write(data + stream->pos(), size - stream->pos(), NULL); if ((err = writer->write(data + stream->pos(), size - stream->pos(), NULL)) != srs_success) {
return srs_error_wrap(err, "write audio");
}
return err;
} }
#endif #endif

View file

@ -40,8 +40,6 @@ class SrsMp3Transmuxer
{ {
private: private:
SrsFileWriter* writer; SrsFileWriter* writer;
private:
SrsBuffer* tag_stream;
public: public:
SrsMp3Transmuxer(); SrsMp3Transmuxer();
virtual ~SrsMp3Transmuxer(); virtual ~SrsMp3Transmuxer();
@ -51,18 +49,18 @@ public:
* @remark user can initialize multiple times to encode multiple mp3 files. * @remark user can initialize multiple times to encode multiple mp3 files.
* @remark, user must free the @param fw, mp3 encoder never close/free it. * @remark, user must free the @param fw, mp3 encoder never close/free it.
*/ */
virtual int initialize(SrsFileWriter* fw); virtual srs_error_t initialize(SrsFileWriter* fw);
public: public:
/** /**
* write mp3 id3 v2.3 header. * write mp3 id3 v2.3 header.
* @see mp3.id3v2.3.0.pdf, http://id3.org/id3v2.3.0 * @see mp3.id3v2.3.0.pdf, http://id3.org/id3v2.3.0
*/ */
virtual int write_header(); virtual srs_error_t write_header();
/** /**
* write audio/video packet. * write audio/video packet.
* @remark assert data is not NULL. * @remark assert data is not NULL.
*/ */
virtual int write_audio(int64_t timestamp, char* data, int size); virtual srs_error_t write_audio(int64_t timestamp, char* data, int size);
}; };
#endif #endif

File diff suppressed because it is too large Load diff

View file

@ -222,15 +222,15 @@ public:
* Discovery the box from buffer. * Discovery the box from buffer.
* @param ppbox Output the discoveried box, which user must free it. * @param ppbox Output the discoveried box, which user must free it.
*/ */
static int discovery(SrsBuffer* buf, SrsMp4Box** ppbox); static srs_error_t discovery(SrsBuffer* buf, SrsMp4Box** ppbox);
// Interface ISrsCodec // Interface ISrsCodec
public: public:
virtual int nb_bytes(); virtual int nb_bytes();
virtual int encode(SrsBuffer* buf); virtual srs_error_t encode(SrsBuffer* buf);
virtual int decode(SrsBuffer* buf); virtual srs_error_t decode(SrsBuffer* buf);
protected: protected:
virtual int encode_boxes(SrsBuffer* buf); virtual srs_error_t encode_boxes(SrsBuffer* buf);
virtual int decode_boxes(SrsBuffer* buf); virtual srs_error_t decode_boxes(SrsBuffer* buf);
// Sub classes can override these functions for special codec. // Sub classes can override these functions for special codec.
// @remark For mdat box, we use completely different codec. // @remark For mdat box, we use completely different codec.
protected: protected:
@ -238,10 +238,10 @@ protected:
virtual int nb_header(); virtual int nb_header();
// It's not necessary to check the buffer, because we already know the size in parent function, // It's not necessary to check the buffer, because we already know the size in parent function,
// so we have checked the buffer is ok to write. // so we have checked the buffer is ok to write.
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
// It's not necessary to check the buffer, unless the box is not only determined by the verson. // It's not necessary to check the buffer, unless the box is not only determined by the verson.
// Generally, it's not necessary, that is, all boxes is determinated by version. // Generally, it's not necessary, that is, all boxes is determinated by version.
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
// Whether there contained boxes in header. // Whether there contained boxes in header.
virtual bool boxes_in_header(); virtual bool boxes_in_header();
// @remark internal for template methods. // @remark internal for template methods.
@ -266,8 +266,8 @@ public:
virtual ~SrsMp4FullBox(); virtual ~SrsMp4FullBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -298,8 +298,8 @@ public:
virtual void set_compatible_brands(SrsMp4BoxBrand b0, SrsMp4BoxBrand b1, SrsMp4BoxBrand b2, SrsMp4BoxBrand b3); virtual void set_compatible_brands(SrsMp4BoxBrand b0, SrsMp4BoxBrand b1, SrsMp4BoxBrand b2, SrsMp4BoxBrand b3);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -358,8 +358,8 @@ public:
virtual ~SrsMp4MovieFragmentHeaderBox(); virtual ~SrsMp4MovieFragmentHeaderBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -451,8 +451,8 @@ public:
virtual ~SrsMp4TrackFragmentHeaderBox(); virtual ~SrsMp4TrackFragmentHeaderBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -475,8 +475,8 @@ public:
virtual ~SrsMp4TrackFragmentDecodeTimeBox(); virtual ~SrsMp4TrackFragmentDecodeTimeBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -522,8 +522,8 @@ struct SrsMp4TrunEntry
virtual ~SrsMp4TrunEntry(); virtual ~SrsMp4TrunEntry();
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -553,8 +553,8 @@ public:
virtual ~SrsMp4TrackFragmentRunBox(); virtual ~SrsMp4TrackFragmentRunBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -622,13 +622,13 @@ public:
virtual int nb_bytes(); virtual int nb_bytes();
// To encode the mdat box, the buf should only contains the sz_header(), // To encode the mdat box, the buf should only contains the sz_header(),
// because the mdata only encode the header. // because the mdata only encode the header.
virtual int encode(SrsBuffer* buf); virtual srs_error_t encode(SrsBuffer* buf);
// To decode the mdat box, the buf should only contains the sz_header(), // To decode the mdat box, the buf should only contains the sz_header(),
// because the mdat only decode the header. // because the mdat only decode the header.
virtual int decode(SrsBuffer* buf); virtual srs_error_t decode(SrsBuffer* buf);
protected: protected:
virtual int encode_boxes(SrsBuffer* buf); virtual srs_error_t encode_boxes(SrsBuffer* buf);
virtual int decode_boxes(SrsBuffer* buf); virtual srs_error_t decode_boxes(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -646,8 +646,8 @@ public:
virtual ~SrsMp4FreeSpaceBox(); virtual ~SrsMp4FreeSpaceBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -682,8 +682,8 @@ public:
virtual int nb_soun_tracks(); virtual int nb_soun_tracks();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
}; };
/** /**
@ -732,8 +732,8 @@ public:
virtual uint64_t duration(); virtual uint64_t duration();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -783,8 +783,8 @@ public:
virtual ~SrsMp4TrackExtendsBox(); virtual ~SrsMp4TrackExtendsBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -904,8 +904,8 @@ public:
virtual ~SrsMp4TrackHeaderBox(); virtual ~SrsMp4TrackHeaderBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -968,8 +968,8 @@ public:
virtual ~SrsMp4EditListBox(); virtual ~SrsMp4EditListBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1048,8 +1048,8 @@ public:
virtual void set_language2(char v); virtual void set_language2(char v);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1080,8 +1080,8 @@ public:
virtual bool is_audio(); virtual bool is_audio();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1131,8 +1131,8 @@ public:
virtual ~SrsMp4VideoMeidaHeaderBox(); virtual ~SrsMp4VideoMeidaHeaderBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
}; };
/** /**
@ -1153,8 +1153,8 @@ public:
virtual ~SrsMp4SoundMeidaHeaderBox(); virtual ~SrsMp4SoundMeidaHeaderBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
}; };
/** /**
@ -1199,8 +1199,8 @@ public:
virtual ~SrsMp4DataEntryUrlBox(); virtual ~SrsMp4DataEntryUrlBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1218,8 +1218,8 @@ public:
virtual ~SrsMp4DataEntryUrnBox(); virtual ~SrsMp4DataEntryUrnBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1244,8 +1244,8 @@ public:
virtual SrsMp4DataReferenceBox* append(SrsMp4DataEntryBox* v); virtual SrsMp4DataReferenceBox* append(SrsMp4DataEntryBox* v);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1286,8 +1286,8 @@ public:
virtual void set_stss(SrsMp4SyncSampleBox* v); virtual void set_stss(SrsMp4SyncSampleBox* v);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
}; };
/** /**
@ -1307,8 +1307,8 @@ public:
virtual ~SrsMp4SampleEntry(); virtual ~SrsMp4SampleEntry();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1350,8 +1350,8 @@ public:
virtual void set_avcC(SrsMp4AvccBox* v); virtual void set_avcC(SrsMp4AvccBox* v);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1369,8 +1369,8 @@ public:
virtual ~SrsMp4AvccBox(); virtual ~SrsMp4AvccBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1399,8 +1399,8 @@ public:
virtual SrsMp4DecoderSpecificInfo* asc(); virtual SrsMp4DecoderSpecificInfo* asc();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1443,12 +1443,12 @@ public:
// Interface ISrsCodec // Interface ISrsCodec
public: public:
virtual int nb_bytes(); virtual int nb_bytes();
virtual int encode(SrsBuffer* buf); virtual srs_error_t encode(SrsBuffer* buf);
virtual int decode(SrsBuffer* buf); virtual srs_error_t decode(SrsBuffer* buf);
protected: protected:
virtual int32_t nb_payload() = 0; virtual int32_t nb_payload() = 0;
virtual int encode_payload(SrsBuffer* buf) = 0; virtual srs_error_t encode_payload(SrsBuffer* buf) = 0;
virtual int decode_payload(SrsBuffer* buf) = 0; virtual srs_error_t decode_payload(SrsBuffer* buf) = 0;
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1485,8 +1485,8 @@ public:
virtual ~SrsMp4DecoderSpecificInfo(); virtual ~SrsMp4DecoderSpecificInfo();
protected: protected:
virtual int32_t nb_payload(); virtual int32_t nb_payload();
virtual int encode_payload(SrsBuffer* buf); virtual srs_error_t encode_payload(SrsBuffer* buf);
virtual int decode_payload(SrsBuffer* buf); virtual srs_error_t decode_payload(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1513,8 +1513,8 @@ public:
virtual ~SrsMp4DecoderConfigDescriptor(); virtual ~SrsMp4DecoderConfigDescriptor();
protected: protected:
virtual int32_t nb_payload(); virtual int32_t nb_payload();
virtual int encode_payload(SrsBuffer* buf); virtual srs_error_t encode_payload(SrsBuffer* buf);
virtual int decode_payload(SrsBuffer* buf); virtual srs_error_t decode_payload(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1532,8 +1532,8 @@ public:
virtual ~SrsMp4SLConfigDescriptor(); virtual ~SrsMp4SLConfigDescriptor();
protected: protected:
virtual int32_t nb_payload(); virtual int32_t nb_payload();
virtual int encode_payload(SrsBuffer* buf); virtual srs_error_t encode_payload(SrsBuffer* buf);
virtual int decode_payload(SrsBuffer* buf); virtual srs_error_t decode_payload(SrsBuffer* buf);
}; };
/** /**
@ -1561,8 +1561,8 @@ public:
virtual ~SrsMp4ES_Descriptor(); virtual ~SrsMp4ES_Descriptor();
protected: protected:
virtual int32_t nb_payload(); virtual int32_t nb_payload();
virtual int encode_payload(SrsBuffer* buf); virtual srs_error_t encode_payload(SrsBuffer* buf);
virtual int decode_payload(SrsBuffer* buf); virtual srs_error_t decode_payload(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1585,8 +1585,8 @@ public:
virtual SrsMp4DecoderSpecificInfo* asc(); virtual SrsMp4DecoderSpecificInfo* asc();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1615,8 +1615,8 @@ public:
virtual SrsMp4SampleDescriptionBox* append(SrsMp4SampleEntry* v); virtual SrsMp4SampleDescriptionBox* append(SrsMp4SampleEntry* v);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
virtual bool boxes_in_header(); virtual bool boxes_in_header();
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
@ -1661,13 +1661,13 @@ public:
virtual ~SrsMp4DecodingTime2SampleBox(); virtual ~SrsMp4DecodingTime2SampleBox();
public: public:
// Initialize the counter. // Initialize the counter.
virtual int initialize_counter(); virtual srs_error_t initialize_counter();
// When got an sample, index starts from 0. // When got an sample, index starts from 0.
virtual int on_sample(uint32_t sample_index, SrsMp4SttsEntry** ppentry); virtual srs_error_t on_sample(uint32_t sample_index, SrsMp4SttsEntry** ppentry);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1715,13 +1715,13 @@ public:
virtual ~SrsMp4CompositionTime2SampleBox(); virtual ~SrsMp4CompositionTime2SampleBox();
public: public:
// Initialize the counter. // Initialize the counter.
virtual int initialize_counter(); virtual srs_error_t initialize_counter();
// When got an sample, index starts from 0. // When got an sample, index starts from 0.
virtual int on_sample(uint32_t sample_index, SrsMp4CttsEntry** ppentry); virtual srs_error_t on_sample(uint32_t sample_index, SrsMp4CttsEntry** ppentry);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1748,8 +1748,8 @@ public:
virtual bool is_sync(uint32_t sample_index); virtual bool is_sync(uint32_t sample_index);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1804,8 +1804,8 @@ public:
virtual SrsMp4StscEntry* on_chunk(uint32_t chunk_index); virtual SrsMp4StscEntry* on_chunk(uint32_t chunk_index);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1830,8 +1830,8 @@ public:
virtual ~SrsMp4ChunkOffsetBox(); virtual ~SrsMp4ChunkOffsetBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1856,8 +1856,8 @@ public:
virtual ~SrsMp4ChunkLargeOffsetBox(); virtual ~SrsMp4ChunkLargeOffsetBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1886,11 +1886,11 @@ public:
virtual ~SrsMp4SampleSizeBox(); virtual ~SrsMp4SampleSizeBox();
public: public:
// Get the size of sample. // Get the size of sample.
virtual int get_sample_size(uint32_t sample_index, uint32_t* psample_size); virtual srs_error_t get_sample_size(uint32_t sample_index, uint32_t* psample_size);
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1910,8 +1910,8 @@ public:
virtual ~SrsMp4UserDataBox(); virtual ~SrsMp4UserDataBox();
protected: protected:
virtual int nb_header(); virtual int nb_header();
virtual int encode_header(SrsBuffer* buf); virtual srs_error_t encode_header(SrsBuffer* buf);
virtual int decode_header(SrsBuffer* buf); virtual srs_error_t decode_header(SrsBuffer* buf);
public: public:
virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc); virtual std::stringstream& dumps_detail(std::stringstream& ss, SrsMp4DumpContext dc);
}; };
@ -1972,28 +1972,28 @@ public:
virtual ~SrsMp4SampleManager(); virtual ~SrsMp4SampleManager();
public: public:
// Load the samples from moov. There must be atleast one track. // Load the samples from moov. There must be atleast one track.
virtual int load(SrsMp4MovieBox* moov); virtual srs_error_t load(SrsMp4MovieBox* moov);
// Get the sample at index position. // Get the sample at index position.
// @remark NULL if exceed the max index. // @remark NULL if exceed the max index.
virtual SrsMp4Sample* at(uint32_t index); virtual SrsMp4Sample* at(uint32_t index);
// Append the sample to the tail of manager. // Append the sample to the tail of manager.
virtual void append(SrsMp4Sample* sample); virtual void append(SrsMp4Sample* sample);
// Write the samples info to moov. // Write the samples info to moov.
virtual int write(SrsMp4MovieBox* moov); virtual srs_error_t write(SrsMp4MovieBox* moov);
// Write the samples info to moof. // Write the samples info to moof.
// @param The dts is the dts of last segment. // @param The dts is the dts of last segment.
virtual int write(SrsMp4MovieFragmentBox* moof, uint64_t& dts); virtual srs_error_t write(SrsMp4MovieFragmentBox* moof, uint64_t& dts);
private: private:
virtual int write_track(SrsFrameType track, virtual srs_error_t write_track(SrsFrameType track,
SrsMp4DecodingTime2SampleBox* stts, SrsMp4SyncSampleBox* stss, SrsMp4CompositionTime2SampleBox* ctts, SrsMp4DecodingTime2SampleBox* stts, SrsMp4SyncSampleBox* stss, SrsMp4CompositionTime2SampleBox* ctts,
SrsMp4Sample2ChunkBox* stsc, SrsMp4SampleSizeBox* stsz, SrsMp4ChunkOffsetBox* stco); SrsMp4Sample2ChunkBox* stsc, SrsMp4SampleSizeBox* stsz, SrsMp4ChunkOffsetBox* stco);
virtual int do_load(std::map<uint64_t, SrsMp4Sample*>& tses, SrsMp4MovieBox* moov); virtual srs_error_t do_load(std::map<uint64_t, SrsMp4Sample*>& tses, SrsMp4MovieBox* moov);
private: private:
// Load the samples of track from stco, stsz and stsc. // Load the samples of track from stco, stsz and stsc.
// @param tses The temporary samples, key is offset, value is sample. // @param tses The temporary samples, key is offset, value is sample.
// @param tt The type of sample, convert to flv tag type. // @param tt The type of sample, convert to flv tag type.
// TODO: Support co64 for stco. // TODO: Support co64 for stco.
virtual int load_trak(std::map<uint64_t, SrsMp4Sample*>& tses, SrsFrameType tt, virtual srs_error_t load_trak(std::map<uint64_t, SrsMp4Sample*>& tses, SrsFrameType tt,
SrsMp4MediaHeaderBox* mdhd, SrsMp4ChunkOffsetBox* stco, SrsMp4SampleSizeBox* stsz, SrsMp4Sample2ChunkBox* stsc, SrsMp4MediaHeaderBox* mdhd, SrsMp4ChunkOffsetBox* stco, SrsMp4SampleSizeBox* stsz, SrsMp4Sample2ChunkBox* stsc,
SrsMp4DecodingTime2SampleBox* stts, SrsMp4CompositionTime2SampleBox* ctts, SrsMp4SyncSampleBox* stss); SrsMp4DecodingTime2SampleBox* stts, SrsMp4CompositionTime2SampleBox* ctts, SrsMp4SyncSampleBox* stss);
}; };
@ -2012,12 +2012,12 @@ public:
SrsMp4BoxReader(); SrsMp4BoxReader();
virtual ~SrsMp4BoxReader(); virtual ~SrsMp4BoxReader();
public: public:
virtual int initialize(ISrsReadSeeker* rs); virtual srs_error_t initialize(ISrsReadSeeker* rs);
public: public:
// Read a MP4 box to pbox, the stream is fill with the bytes of box to decode. // Read a MP4 box to pbox, the stream is fill with the bytes of box to decode.
virtual int read(SrsSimpleStream* stream, SrsMp4Box** ppbox); virtual srs_error_t read(SrsSimpleStream* stream, SrsMp4Box** ppbox);
// Skip the box from stream, and skip in file if need. // Skip the box from stream, and skip in file if need.
virtual int skip(SrsMp4Box* box, SrsSimpleStream* stream); virtual srs_error_t skip(SrsMp4Box* box, SrsSimpleStream* stream);
}; };
/** /**
@ -2075,7 +2075,7 @@ public:
* Initialize the decoder with a reader r. * Initialize the decoder with a reader r.
* @param r The underlayer io reader, user must manage it. * @param r The underlayer io reader, user must manage it.
*/ */
virtual int initialize(ISrsReadSeeker* rs); virtual srs_error_t initialize(ISrsReadSeeker* rs);
/** /**
* Read a sample from mp4. * Read a sample from mp4.
* @param pht The sample hanler type, audio/soun or video/vide. * @param pht The sample hanler type, audio/soun or video/vide.
@ -2087,17 +2087,17 @@ public:
* @param psample The output payload, user must free it. * @param psample The output payload, user must free it.
* @remark The decoder will generate the first two audio/video sequence header. * @remark The decoder will generate the first two audio/video sequence header.
*/ */
virtual int read_sample(SrsMp4HandlerType* pht, uint16_t* pft, uint16_t* pct, virtual srs_error_t read_sample(SrsMp4HandlerType* pht, uint16_t* pft, uint16_t* pct,
uint32_t* pdts, uint32_t* ppts, uint8_t** psample, uint32_t* pnb_sample); uint32_t* pdts, uint32_t* ppts, uint8_t** psample, uint32_t* pnb_sample);
private: private:
virtual int parse_ftyp(SrsMp4FileTypeBox* ftyp); virtual srs_error_t parse_ftyp(SrsMp4FileTypeBox* ftyp);
virtual int parse_moov(SrsMp4MovieBox* moov); virtual srs_error_t parse_moov(SrsMp4MovieBox* moov);
private: private:
// Load the next box from reader. // Load the next box from reader.
// @param required_box_type The box type required, 0 for any box. // @param required_box_type The box type required, 0 for any box.
virtual int load_next_box(SrsMp4Box** ppbox, uint32_t required_box_type); virtual srs_error_t load_next_box(SrsMp4Box** ppbox, uint32_t required_box_type);
// @remark Never load the mdat box content, for it's too large. // @remark Never load the mdat box content, for it's too large.
virtual int do_load_next_box(SrsMp4Box** ppbox, uint32_t required_box_type); virtual srs_error_t do_load_next_box(SrsMp4Box** ppbox, uint32_t required_box_type);
}; };
/** /**
@ -2107,7 +2107,6 @@ class SrsMp4Encoder
{ {
private: private:
ISrsWriteSeeker* wsio; ISrsWriteSeeker* wsio;
SrsBuffer* buffer;
// The mdat offset at file, we must update the header when flush. // The mdat offset at file, we must update the header when flush.
off_t mdat_offset; off_t mdat_offset;
// The mdat size in bytes, we must update it to the mdat box header. // The mdat size in bytes, we must update it to the mdat box header.
@ -2151,7 +2150,7 @@ public:
public: public:
// Initialize the encoder with a writer and seeker ws. // Initialize the encoder with a writer and seeker ws.
// @param ws The underlayer io writer and seeker, user must manage it. // @param ws The underlayer io writer and seeker, user must manage it.
virtual int initialize(ISrsWriteSeeker* ws); virtual srs_error_t initialize(ISrsWriteSeeker* ws);
// Write a sample to mp4. // Write a sample to mp4.
// @param ht, The sample handler type, audio/soun or video/vide. // @param ht, The sample handler type, audio/soun or video/vide.
// @param ft, The frame type. For video, it's SrsVideoAvcFrameType. // @param ft, The frame type. For video, it's SrsVideoAvcFrameType.
@ -2160,13 +2159,13 @@ public:
// @param pts The output pts in milliseconds. // @param pts The output pts in milliseconds.
// @param sample The output payload, user must free it. // @param sample The output payload, user must free it.
// @param nb_sample The output size of payload. // @param nb_sample The output size of payload.
virtual int write_sample(SrsMp4HandlerType ht, uint16_t ft, uint16_t ct, virtual srs_error_t write_sample(SrsMp4HandlerType ht, uint16_t ft, uint16_t ct,
uint32_t dts, uint32_t pts, uint8_t* sample, uint32_t nb_sample); uint32_t dts, uint32_t pts, uint8_t* sample, uint32_t nb_sample);
// Flush the encoder, to write the moov. // Flush the encoder, to write the moov.
virtual int flush(); virtual srs_error_t flush();
private: private:
virtual int copy_sequence_header(bool vsh, uint8_t* sample, uint32_t nb_sample); virtual srs_error_t copy_sequence_header(bool vsh, uint8_t* sample, uint32_t nb_sample);
virtual int do_write_sample(SrsMp4Sample* ps, uint8_t* sample, uint32_t nb_sample); virtual srs_error_t do_write_sample(SrsMp4Sample* ps, uint8_t* sample, uint32_t nb_sample);
}; };
/** /**
@ -2181,9 +2180,9 @@ public:
virtual ~SrsMp4M2tsInitEncoder(); virtual ~SrsMp4M2tsInitEncoder();
public: public:
// Initialize the encoder with a writer w. // Initialize the encoder with a writer w.
virtual int initialize(ISrsWriter* w); virtual srs_error_t initialize(ISrsWriter* w);
// Write the sequence header. // Write the sequence header.
virtual int write(SrsFormat* format, bool video, int tid); virtual srs_error_t write(SrsFormat* format, bool video, int tid);
}; };
/** /**
@ -2210,7 +2209,7 @@ public:
virtual ~SrsMp4M2tsSegmentEncoder(); virtual ~SrsMp4M2tsSegmentEncoder();
public: public:
// Initialize the encoder with a writer w. // Initialize the encoder with a writer w.
virtual int initialize(ISrsWriter* w, uint32_t sequence, uint64_t basetime, uint32_t tid); virtual srs_error_t initialize(ISrsWriter* w, uint32_t sequence, uint64_t basetime, uint32_t tid);
// Cache a sample. // Cache a sample.
// @param ht, The sample handler type, audio/soun or video/vide. // @param ht, The sample handler type, audio/soun or video/vide.
// @param ft, The frame type. For video, it's SrsVideoAvcFrameType. // @param ft, The frame type. For video, it's SrsVideoAvcFrameType.
@ -2219,10 +2218,10 @@ public:
// @param sample The output payload, user must free it. // @param sample The output payload, user must free it.
// @param nb_sample The output size of payload. // @param nb_sample The output size of payload.
// @remark All samples are RAW AAC/AVC data, because sequence header is writen to init.mp4. // @remark All samples are RAW AAC/AVC data, because sequence header is writen to init.mp4.
virtual int write_sample(SrsMp4HandlerType ht, uint16_t ft, virtual srs_error_t write_sample(SrsMp4HandlerType ht, uint16_t ft,
uint32_t dts, uint32_t pts, uint8_t* sample, uint32_t nb_sample); uint32_t dts, uint32_t pts, uint8_t* sample, uint32_t nb_sample);
// Flush the encoder, to write the moof and mdat. // Flush the encoder, to write the moof and mdat.
virtual int flush(uint64_t& dts); virtual srs_error_t flush(uint64_t& dts);
}; };
#endif #endif

View file

@ -374,7 +374,6 @@ void SrsTsContext::set_sync_byte(int8_t sb)
srs_error_t SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as) srs_error_t SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, SrsTsStream vs, int16_t apid, SrsTsStream as)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (vs != SrsTsStreamVideoH264 && as != SrsTsStreamAudioAAC && as != SrsTsStreamAudioMp3) { if (vs != SrsTsStreamVideoH264 && as != SrsTsStreamAudioAAC && as != SrsTsStreamAudioMp3) {
@ -397,15 +396,12 @@ srs_error_t SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, Sr
srs_assert(nb_buf < SRS_TS_PACKET_SIZE); srs_assert(nb_buf < SRS_TS_PACKET_SIZE);
memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf);
SrsBuffer stream; SrsBuffer stream(buf, nb_buf);
if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) {
return srs_error_new(ret, "ts: init stream");
}
if ((err = pkt->encode(&stream)) != srs_success) { if ((err = pkt->encode(&stream)) != srs_success) {
return srs_error_wrap(err, "ts: encode packet"); return srs_error_wrap(err, "ts: encode packet");
} }
if ((ret = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != ERROR_SUCCESS) { if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) {
return srs_error_new(ret, "ts: write packet"); return srs_error_wrap(err, "ts: write packet");
} }
} }
if (true) { if (true) {
@ -422,15 +418,12 @@ srs_error_t SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, Sr
srs_assert(nb_buf < SRS_TS_PACKET_SIZE); srs_assert(nb_buf < SRS_TS_PACKET_SIZE);
memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf); memset(buf + nb_buf, 0xFF, SRS_TS_PACKET_SIZE - nb_buf);
SrsBuffer stream; SrsBuffer stream(buf, nb_buf);
if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) {
return srs_error_new(ret, "ts: init stream");
}
if ((err = pkt->encode(&stream)) != srs_success) { if ((err = pkt->encode(&stream)) != srs_success) {
return srs_error_wrap(err, "ts: encode packet"); return srs_error_wrap(err, "ts: encode packet");
} }
if ((ret = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != ERROR_SUCCESS) { if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) {
return srs_error_new(ret, "ts: write packet"); return srs_error_wrap(err, "ts: write packet");
} }
} }
@ -442,7 +435,6 @@ srs_error_t SrsTsContext::encode_pat_pmt(SrsFileWriter* writer, int16_t vpid, Sr
srs_error_t SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t pid, SrsTsStream sid, bool pure_audio) srs_error_t SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, int16_t pid, SrsTsStream sid, bool pure_audio)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// Sometimes, the context is not ready(PAT/PMT write failed), error in this situation. // Sometimes, the context is not ready(PAT/PMT write failed), error in this situation.
@ -529,15 +521,12 @@ srs_error_t SrsTsContext::encode_pes(SrsFileWriter* writer, SrsTsMessage* msg, i
memcpy(buf + nb_buf, p, left); memcpy(buf + nb_buf, p, left);
p += left; p += left;
SrsBuffer stream; SrsBuffer stream(buf, nb_buf);
if ((ret = stream.initialize(buf, nb_buf)) != ERROR_SUCCESS) {
return srs_error_new(ret, "ts: init stream");
}
if ((err = pkt->encode(&stream)) != srs_success) { if ((err = pkt->encode(&stream)) != srs_success) {
return srs_error_wrap(err, "ts: encode packet"); return srs_error_wrap(err, "ts: encode packet");
} }
if ((ret = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != ERROR_SUCCESS) { if ((err = writer->write(buf, SRS_TS_PACKET_SIZE, NULL)) != srs_success) {
return srs_error_new(ret, "ts: write packet"); return srs_error_wrap(err, "ts: write packet");
} }
} }
@ -2562,7 +2551,6 @@ SrsTsContextWriter::~SrsTsContextWriter()
srs_error_t SrsTsContextWriter::open(string p) srs_error_t SrsTsContextWriter::open(string p)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
path = p; path = p;
@ -2572,8 +2560,8 @@ srs_error_t SrsTsContextWriter::open(string p)
// reset the context for a new ts start. // reset the context for a new ts start.
context->reset(); context->reset();
if ((ret = writer->open(path)) != ERROR_SUCCESS) { if ((err = writer->open(path)) != srs_success) {
return srs_error_new(ret, "ts: open writer"); return srs_error_wrap(err, "ts: open writer");
} }
return err; return err;

View file

@ -49,12 +49,12 @@ using namespace std;
// @see SRS_SYS_TIME_RESOLUTION_MS_TIMES // @see SRS_SYS_TIME_RESOLUTION_MS_TIMES
#define SYS_TIME_RESOLUTION_US 300*1000 #define SYS_TIME_RESOLUTION_US 300*1000
int srs_avc_nalu_read_uev(SrsBitBuffer* stream, int32_t& v) srs_error_t srs_avc_nalu_read_uev(SrsBitBuffer* stream, int32_t& v)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (stream->empty()) { if (stream->empty()) {
return ERROR_AVC_NALU_UEV; return srs_error_new(ERROR_AVC_NALU_UEV, "empty stream");
} }
// ue(v) in 9.1 Parsing process for Exp-Golomb codes // ue(v) in 9.1 Parsing process for Exp-Golomb codes
@ -71,7 +71,7 @@ int srs_avc_nalu_read_uev(SrsBitBuffer* stream, int32_t& v)
} }
if (leadingZeroBits >= 31) { if (leadingZeroBits >= 31) {
return ERROR_AVC_NALU_UEV; return srs_error_new(ERROR_AVC_NALU_UEV, "%dbits overflow 31bits", leadingZeroBits);
} }
v = (1 << leadingZeroBits) - 1; v = (1 << leadingZeroBits) - 1;
@ -80,20 +80,20 @@ int srs_avc_nalu_read_uev(SrsBitBuffer* stream, int32_t& v)
v += b << (leadingZeroBits - 1 - i); v += b << (leadingZeroBits - 1 - i);
} }
return ret; return err;
} }
int srs_avc_nalu_read_bit(SrsBitBuffer* stream, int8_t& v) srs_error_t srs_avc_nalu_read_bit(SrsBitBuffer* stream, int8_t& v)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
if (stream->empty()) { if (stream->empty()) {
return ERROR_AVC_NALU_UEV; return srs_error_new(ERROR_AVC_NALU_UEV, "empty stream");
} }
v = stream->read_bit(); v = stream->read_bit();
return ret; return err;
} }
static int64_t _srs_system_time_us_cache = 0; static int64_t _srs_system_time_us_cache = 0;
@ -961,7 +961,7 @@ int srs_do_create_dir_recursively(string dir)
*dst++ = v >> 4; *dst++ = v >> 4;
out1: out1:
out0: out0:
return bits & 1 ? -1 : dst - out; return bits & 1 ? -1 : (int)(dst - out);
} }
/***************************************************************************** /*****************************************************************************

View file

@ -37,8 +37,8 @@ class SrsBitBuffer;
#define srs_max(a, b) (((a) < (b))? (b) : (a)) #define srs_max(a, b) (((a) < (b))? (b) : (a))
// read nalu uev. // read nalu uev.
extern int srs_avc_nalu_read_uev(SrsBitBuffer* stream, int32_t& v); extern srs_error_t srs_avc_nalu_read_uev(SrsBitBuffer* stream, int32_t& v);
extern int srs_avc_nalu_read_bit(SrsBitBuffer* stream, int8_t& v); extern srs_error_t srs_avc_nalu_read_bit(SrsBitBuffer* stream, int8_t& v);
// get current system time in ms, use cache to avoid performance problem // get current system time in ms, use cache to avoid performance problem
extern int64_t srs_get_system_time_ms(); extern int64_t srs_get_system_time_ms();

View file

@ -129,16 +129,17 @@ srs_error_t srs_go_http_error(ISrsHttpResponseWriter* w, int code)
srs_error_t srs_go_http_error(ISrsHttpResponseWriter* w, int code, string error) srs_error_t srs_go_http_error(ISrsHttpResponseWriter* w, int code, string error)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
w->header()->set_content_type("text/plain; charset=utf-8"); w->header()->set_content_type("text/plain; charset=utf-8");
w->header()->set_content_length(error.length()); w->header()->set_content_length(error.length());
w->write_header(code); w->write_header(code);
if ((ret = w->write((char*)error.data(), (int)error.length())) != ERROR_SUCCESS) {
return srs_error_new(ret, "http write"); if ((err = w->write((char*)error.data(), (int)error.length())) != srs_success) {
return srs_error_wrap(err, "http write");
} }
return srs_success; return err;
} }
SrsHttpHeader::SrsHttpHeader() SrsHttpHeader::SrsHttpHeader()
@ -330,14 +331,13 @@ srs_error_t SrsHttpFileServer::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMes
srs_error_t SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath) srs_error_t SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath)
{ {
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success; srs_error_t err = srs_success;
// open the target file. // open the target file.
SrsFileReader fs; SrsFileReader fs;
if ((ret = fs.open(fullpath)) != ERROR_SUCCESS) { if ((err = fs.open(fullpath)) != srs_success) {
return srs_error_new(ret, "open file %s", fullpath.c_str()); return srs_error_wrap(err, "open file %s", fullpath.c_str());
} }
int64_t length = fs.filesize(); int64_t length = fs.filesize();
@ -397,8 +397,8 @@ srs_error_t SrsHttpFileServer::serve_file(ISrsHttpResponseWriter* w, ISrsHttpMes
return srs_error_wrap(err, "copy file=%s size=%d", fullpath.c_str(), left); return srs_error_wrap(err, "copy file=%s size=%d", fullpath.c_str(), left);
} }
if ((ret = w->final_request()) != ERROR_SUCCESS) { if ((err = w->final_request()) != srs_success) {
return srs_error_new(ret, "final request"); return srs_error_wrap(err, "final request");
} }
return err; return err;
@ -468,7 +468,7 @@ srs_error_t SrsHttpFileServer::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsH
srs_error_t SrsHttpFileServer::copy(ISrsHttpResponseWriter* w, SrsFileReader* fs, ISrsHttpMessage* r, int size) srs_error_t SrsHttpFileServer::copy(ISrsHttpResponseWriter* w, SrsFileReader* fs, ISrsHttpMessage* r, int size)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
int left = size; int left = size;
char* buf = r->http_ts_send_buffer(); char* buf = r->http_ts_send_buffer();
@ -476,21 +476,21 @@ srs_error_t SrsHttpFileServer::copy(ISrsHttpResponseWriter* w, SrsFileReader* fs
while (left > 0) { while (left > 0) {
ssize_t nread = -1; ssize_t nread = -1;
int max_read = srs_min(left, SRS_HTTP_TS_SEND_BUFFER_SIZE); int max_read = srs_min(left, SRS_HTTP_TS_SEND_BUFFER_SIZE);
if ((ret = fs->read(buf, max_read, &nread)) != ERROR_SUCCESS) { if ((err = fs->read(buf, max_read, &nread)) != srs_success) {
break; break;
} }
left -= nread; left -= nread;
if ((ret = w->write(buf, (int)nread)) != ERROR_SUCCESS) { if ((err = w->write(buf, (int)nread)) != srs_success) {
break; break;
} }
} }
if (ret != ERROR_SUCCESS) { if (err != srs_success) {
return srs_error_new(ret, "copy"); return srs_error_wrap(err, "copy");
} }
return srs_success; return err;
} }
SrsHttpMuxEntry::SrsHttpMuxEntry() SrsHttpMuxEntry::SrsHttpMuxEntry()
@ -765,7 +765,7 @@ srs_error_t SrsHttpCorsMux::initialize(ISrsHttpServeMux* worker, bool cros_enabl
srs_error_t SrsHttpCorsMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) srs_error_t SrsHttpCorsMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{ {
int ret = ERROR_SUCCESS; srs_error_t err = srs_success;
// If CORS enabled, and there is a "Origin" header, it's CORS. // If CORS enabled, and there is a "Origin" header, it's CORS.
if (enabled) { if (enabled) {
@ -795,8 +795,8 @@ srs_error_t SrsHttpCorsMux::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessag
} else { } else {
w->write_header(SRS_CONSTS_HTTP_MethodNotAllowed); w->write_header(SRS_CONSTS_HTTP_MethodNotAllowed);
} }
if ((ret = w->final_request()) != ERROR_SUCCESS) { if ((err = w->final_request()) != srs_success) {
return srs_error_new(ret, "final request"); return srs_error_wrap(err, "final request");
} }
} }

View file

@ -180,7 +180,7 @@ public:
// final the request to complete the chunked encoding. // final the request to complete the chunked encoding.
// for no-chunked mode, // for no-chunked mode,
// final to send request, for example, content-length is 0. // final to send request, for example, content-length is 0.
virtual int final_request() = 0; virtual srs_error_t final_request() = 0;
// Header returns the header map that will be sent by WriteHeader. // Header returns the header map that will be sent by WriteHeader.
// Changing the header after a call to WriteHeader (or Write) has // Changing the header after a call to WriteHeader (or Write) has
@ -193,12 +193,12 @@ public:
// Content-Type line, Write adds a Content-Type set to the result of passing // Content-Type line, Write adds a Content-Type set to the result of passing
// the initial 512 bytes of written data to DetectContentType. // the initial 512 bytes of written data to DetectContentType.
// @param data, the data to send. NULL to flush header only. // @param data, the data to send. NULL to flush header only.
virtual int write(char* data, int size) = 0; virtual srs_error_t write(char* data, int size) = 0;
/** /**
* for the HTTP FLV, to writev to improve performance. * for the HTTP FLV, to writev to improve performance.
* @see https://github.com/ossrs/srs/issues/405 * @see https://github.com/ossrs/srs/issues/405
*/ */
virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) = 0; virtual srs_error_t writev(const iovec* iov, int iovcnt, ssize_t* pnwrite) = 0;
// WriteHeader sends an HTTP response header with status code. // WriteHeader sends an HTTP response header with status code.
// If WriteHeader is not called explicitly, the first call to Write // If WriteHeader is not called explicitly, the first call to Write
@ -235,7 +235,7 @@ public:
* or by chunked), because the sdk never know whether there is no data or * or by chunked), because the sdk never know whether there is no data or
* infinite chunked. * infinite chunked.
*/ */
virtual int read(char* data, int nb_data, int* nb_read) = 0; virtual srs_error_t read(char* data, int nb_data, int* nb_read) = 0;
}; };
// Objects implementing the Handler interface can be // Objects implementing the Handler interface can be
@ -538,12 +538,12 @@ public:
* which is chunked encoding without chunked header. * which is chunked encoding without chunked header.
* @remark error when message is in chunked or content-length specified. * @remark error when message is in chunked or content-length specified.
*/ */
virtual int enter_infinite_chunked() = 0; virtual srs_error_t enter_infinite_chunked() = 0;
/** /**
* read body to string. * read body to string.
* @remark for small http body. * @remark for small http body.
*/ */
virtual int body_read_all(std::string& body) = 0; virtual srs_error_t body_read_all(std::string& body) = 0;
/** /**
* get the body reader, to read one by one. * get the body reader, to read one by one.
* @remark when body is very large, or chunked, use this. * @remark when body is very large, or chunked, use this.

View file

@ -1104,7 +1104,7 @@ int SrsKafkaProducerPartitionMessages::decode(SrsBuffer* buf)
// for the message set decode util empty, we must create a new buffer when // for the message set decode util empty, we must create a new buffer when
// there exists other objects after message set. // there exists other objects after message set.
if (buf->size() - buf->pos() != message_set_size) { if (buf->left() != message_set_size) {
SrsBuffer* tbuf = new SrsBuffer(); SrsBuffer* tbuf = new SrsBuffer();
SrsAutoFree(SrsBuffer, tbuf); SrsAutoFree(SrsBuffer, tbuf);