mirror of
https://github.com/ossrs/srs.git
synced 2025-03-09 15:49:59 +00:00
For #913, use complex error for http
This commit is contained in:
parent
661eb8b37c
commit
9f5224c34a
24 changed files with 388 additions and 453 deletions
|
@ -478,7 +478,7 @@ int SrsLiveStream::update(SrsSource* s, SrsRequest* r)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int 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;
|
||||
|
@ -503,17 +503,14 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
|||
w->header()->set_content_type("video/MP2T");
|
||||
enc = new SrsTsStreamEncoder();
|
||||
} else {
|
||||
ret = ERROR_HTTP_LIVE_STREAM_EXT;
|
||||
srs_error("http: unsupported pattern %s", entry->pattern.c_str());
|
||||
return ret;
|
||||
return srs_error_new(ERROR_HTTP_LIVE_STREAM_EXT, "invalid pattern=%s", entry->pattern.c_str());
|
||||
}
|
||||
SrsAutoFree(ISrsBufferEncoder, enc);
|
||||
|
||||
// create consumer of souce, ignore gop cache, use the audio gop cache.
|
||||
SrsConsumer* consumer = NULL;
|
||||
if ((ret = source->create_consumer(NULL, consumer, true, true, !enc->has_cache())) != ERROR_SUCCESS) {
|
||||
srs_error("http: create consumer failed. ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_new(ret, "create consumer");
|
||||
}
|
||||
SrsAutoFree(SrsConsumer, consumer);
|
||||
srs_verbose("http: consumer created success.");
|
||||
|
@ -526,22 +523,19 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
|||
// update the statistic when source disconveried.
|
||||
SrsStatistic* stat = SrsStatistic::instance();
|
||||
if ((ret = stat->on_client(_srs_context->get_id(), req, NULL, SrsRtmpConnPlay)) != ERROR_SUCCESS) {
|
||||
srs_error("stat client failed. ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_new(ret, "stat on client");
|
||||
}
|
||||
|
||||
// the memory writer.
|
||||
SrsBufferWriter writer(w);
|
||||
if ((ret = enc->initialize(&writer, cache)) != ERROR_SUCCESS) {
|
||||
srs_error("http: initialize stream encoder failed. ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_new(ret, "init encoder");
|
||||
}
|
||||
|
||||
// if gop cache enabled for encoder, dump to consumer.
|
||||
if (enc->has_cache()) {
|
||||
if ((ret = enc->dump_cache(consumer, source->jitter())) != ERROR_SUCCESS) {
|
||||
srs_error("http: dump cache to consumer failed. ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_new(ret, "encoder dump cache");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -558,12 +552,7 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
|||
SrsAutoFree(SrsHttpRecvThread, trd);
|
||||
|
||||
if ((err = trd->start()) != srs_success) {
|
||||
// TODO: FIXME: Use error
|
||||
ret = srs_error_code(err);
|
||||
srs_freep(err);
|
||||
|
||||
srs_error("http: start notify thread failed, ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_wrap(err, "start recv thread");
|
||||
}
|
||||
|
||||
// TODO: free and erase the disabled entry after all related connections is closed.
|
||||
|
@ -571,16 +560,15 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
|||
pprint->elapse();
|
||||
|
||||
// Whether client closed the FD.
|
||||
if ((ret = trd->error_code()) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
if ((err = trd->pull()) != srs_success) {
|
||||
return srs_error_wrap(err, "recv thread");
|
||||
}
|
||||
|
||||
// get messages from consumer.
|
||||
// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
|
||||
int count = 0;
|
||||
if ((ret = consumer->dump_packets(&msgs, count)) != ERROR_SUCCESS) {
|
||||
srs_error("http: get messages from consumer failed. ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_new(ret, "consumer dump packets");
|
||||
}
|
||||
|
||||
if (count <= 0) {
|
||||
|
@ -594,7 +582,7 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
|||
|
||||
if (pprint->can_print()) {
|
||||
srs_info("-> " SRS_CONSTS_LOG_HTTP_STREAM " http: got %d msgs, age=%d, min=%d, mw=%d",
|
||||
count, pprint->age(), SRS_PERF_MW_MIN_MSGS, SRS_CONSTS_RTMP_PULSE_TMMS);
|
||||
count, pprint->age(), SRS_PERF_MW_MIN_MSGS, SRS_CONSTS_RTMP_PULSE_TMMS);
|
||||
}
|
||||
|
||||
// sendout all messages.
|
||||
|
@ -616,14 +604,11 @@ int SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
|||
|
||||
// check send error code.
|
||||
if (ret != ERROR_SUCCESS) {
|
||||
if (!srs_is_client_gracefully_close(ret)) {
|
||||
srs_error("http: send messages to client failed. ret=%d", ret);
|
||||
}
|
||||
return ret;
|
||||
return srs_error_new(ret, "send messages");
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int SrsLiveStream::streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs)
|
||||
|
@ -895,26 +880,27 @@ int SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int 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;
|
||||
|
||||
// when handler not the root, we think the handler is ok.
|
||||
ISrsHttpHandler* h = *ph? *ph : NULL;
|
||||
if (h && h->entry && h->entry->pattern != "/") {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
// only hijack for http streaming, http-flv/ts/mp3/aac.
|
||||
std::string ext = request->ext();
|
||||
if (ext.empty()) {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
// find the actually request vhost.
|
||||
SrsConfDirective* vhost = _srs_config->get_vhost(request->host());
|
||||
if (!vhost || !_srs_config->get_vhost_enabled(vhost)) {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
// find the entry template for the stream.
|
||||
|
@ -923,7 +909,7 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
|
|||
// no http streaming on vhost, ignore.
|
||||
std::map<std::string, SrsLiveEntry*>::iterator it = tflvs.find(vhost->arg0());
|
||||
if (it == tflvs.end()) {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
// hstrs always enabled.
|
||||
|
@ -936,22 +922,22 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
|
|||
// check entry and request extension.
|
||||
if (entry->is_flv()) {
|
||||
if (ext != ".flv") {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
} else if (entry->is_ts()) {
|
||||
if (ext != ".ts") {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
} else if (entry->is_mp3()) {
|
||||
if (ext != ".mp3") {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
} else if (entry->is_aac()) {
|
||||
if (ext != ".aac") {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
} else {
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -973,21 +959,20 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
|
|||
// for the http flv edge use hijack to trigger the edge ingester, we always mount it
|
||||
// eventhough the origin does not exists the specified stream.
|
||||
if (!_srs_config->get_vhost_http_remux_enabled(r->vhost)) {
|
||||
srs_error("stream is disabled, hijack failed. ret=%d", ret);
|
||||
return ret;
|
||||
return srs_error_new(ERROR_HTTP_HIJACK, "stream disabled");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SrsSource* s = NULL;
|
||||
if ((ret = SrsSource::fetch_or_create(r, server, &s)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
return srs_error_new(ret, "source create");
|
||||
}
|
||||
srs_assert(s != NULL);
|
||||
|
||||
// create http streaming handler.
|
||||
if ((ret = http_mount(s, r)) != ERROR_SUCCESS) {
|
||||
return ret;
|
||||
return srs_error_new(ret, "http mount");
|
||||
}
|
||||
|
||||
// use the handler if exists.
|
||||
|
@ -1001,9 +986,9 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
|
|||
// trigger edge to fetch from origin.
|
||||
bool vhost_is_edge = _srs_config->get_vhost_is_edge(r->vhost);
|
||||
srs_trace("flv: source url=%s, is_edge=%d, source_id=%d[%d]",
|
||||
r->get_stream_url().c_str(), vhost_is_edge, s->source_id(), s->source_id());
|
||||
r->get_stream_url().c_str(), vhost_is_edge, s->source_id(), s->source_id());
|
||||
|
||||
return ret;
|
||||
return err;
|
||||
}
|
||||
|
||||
int SrsHttpStreamServer::initialize_flv_streaming()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue