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

Log: Use object as context id for complex context

This commit is contained in:
winlin 2020-07-05 23:26:55 +08:00
parent bff7ef085d
commit 6624b8acca
34 changed files with 276 additions and 238 deletions

View file

@ -201,7 +201,7 @@ srs_error_t SrsConnection::cycle()
return srs_success; return srs_success;
} }
string SrsConnection::srs_id() SrsContextId SrsConnection::srs_id()
{ {
return trd->cid(); return trd->cid();
} }

View file

@ -93,7 +93,8 @@ public:
virtual srs_error_t cycle(); virtual srs_error_t cycle();
public: public:
// Get the srs id which identify the client. // Get the srs id which identify the client.
virtual std::string srs_id(); // TODO: FIXME: Rename to cid.
virtual SrsContextId srs_id();
// Get the remote ip of peer. // Get the remote ip of peer.
virtual std::string remote_ip(); virtual std::string remote_ip();
// Set connection to expired. // Set connection to expired.

View file

@ -533,7 +533,7 @@ srs_error_t SrsDvrMp4Segmenter::close_encoder()
return err; return err;
} }
SrsDvrAsyncCallOnDvr::SrsDvrAsyncCallOnDvr(std::string c, SrsRequest* r, string p) SrsDvrAsyncCallOnDvr::SrsDvrAsyncCallOnDvr(SrsContextId c, SrsRequest* r, string p)
{ {
cid = c; cid = c;
req = r->copy(); req = r->copy();
@ -673,7 +673,7 @@ srs_error_t SrsDvrPlan::on_reap_segment()
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string cid = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
SrsFragment* fragment = segment->current(); SrsFragment* fragment = segment->current();
string fullpath = fragment->fullpath(); string fullpath = fragment->fullpath();

View file

@ -159,11 +159,11 @@ protected:
class SrsDvrAsyncCallOnDvr : public ISrsAsyncCallTask class SrsDvrAsyncCallOnDvr : public ISrsAsyncCallTask
{ {
private: private:
std::string cid; SrsContextId cid;
std::string path; std::string path;
SrsRequest* req; SrsRequest* req;
public: public:
SrsDvrAsyncCallOnDvr(std::string c, SrsRequest* r, std::string p); SrsDvrAsyncCallOnDvr(SrsContextId c, SrsRequest* r, std::string p);
virtual ~SrsDvrAsyncCallOnDvr(); virtual ~SrsDvrAsyncCallOnDvr();
public: public:
virtual srs_error_t call(); virtual srs_error_t call();

View file

@ -82,7 +82,7 @@ void SrsHlsSegment::config_cipher(unsigned char* key,unsigned char* iv)
fw->config_cipher(key, iv); fw->config_cipher(key, iv);
} }
SrsDvrAsyncCallOnHls::SrsDvrAsyncCallOnHls(string c, SrsRequest* r, string p, string t, string m, string mu, int s, srs_utime_t d) SrsDvrAsyncCallOnHls::SrsDvrAsyncCallOnHls(SrsContextId c, SrsRequest* r, string p, string t, string m, string mu, int s, srs_utime_t d)
{ {
req = r->copy(); req = r->copy();
cid = c; cid = c;
@ -137,7 +137,7 @@ string SrsDvrAsyncCallOnHls::to_string()
return "on_hls: " + path; return "on_hls: " + path;
} }
SrsDvrAsyncCallOnHlsNotify::SrsDvrAsyncCallOnHlsNotify(string c, SrsRequest* r, string u) SrsDvrAsyncCallOnHlsNotify::SrsDvrAsyncCallOnHlsNotify(SrsContextId c, SrsRequest* r, string u)
{ {
cid = c; cid = c;
req = r->copy(); req = r->copy();

View file

@ -80,7 +80,7 @@ public:
class SrsDvrAsyncCallOnHls : public ISrsAsyncCallTask class SrsDvrAsyncCallOnHls : public ISrsAsyncCallTask
{ {
private: private:
std::string cid; SrsContextId cid;
std::string path; std::string path;
std::string ts_url; std::string ts_url;
std::string m3u8; std::string m3u8;
@ -90,7 +90,7 @@ private:
srs_utime_t duration; srs_utime_t duration;
public: public:
// TODO: FIXME: Use TBN 1000. // TODO: FIXME: Use TBN 1000.
SrsDvrAsyncCallOnHls(std::string c, SrsRequest* r, std::string p, std::string t, std::string m, std::string mu, int s, srs_utime_t d); SrsDvrAsyncCallOnHls(SrsContextId c, SrsRequest* r, std::string p, std::string t, std::string m, std::string mu, int s, srs_utime_t d);
virtual ~SrsDvrAsyncCallOnHls(); virtual ~SrsDvrAsyncCallOnHls();
public: public:
virtual srs_error_t call(); virtual srs_error_t call();
@ -101,11 +101,11 @@ public:
class SrsDvrAsyncCallOnHlsNotify : public ISrsAsyncCallTask class SrsDvrAsyncCallOnHlsNotify : public ISrsAsyncCallTask
{ {
private: private:
std::string cid; SrsContextId cid;
std::string ts_url; std::string ts_url;
SrsRequest* req; SrsRequest* req;
public: public:
SrsDvrAsyncCallOnHlsNotify(std::string c, SrsRequest* r, std::string u); SrsDvrAsyncCallOnHlsNotify(SrsContextId c, SrsRequest* r, std::string u);
virtual ~SrsDvrAsyncCallOnHlsNotify(); virtual ~SrsDvrAsyncCallOnHlsNotify();
public: public:
virtual srs_error_t call(); virtual srs_error_t call();

View file

@ -811,10 +811,10 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
// path: {pattern}{client_id} // path: {pattern}{client_id}
// e.g. /api/v1/clients/100 pattern= /api/v1/clients/, client_id=100 // e.g. /api/v1/clients/100 pattern= /api/v1/clients/, client_id=100
std::string cid = r->parse_rest_id(entry->pattern); std::string client_id = r->parse_rest_id(entry->pattern);
SrsStatisticClient* client = NULL; SrsStatisticClient* client = NULL;
if (cid != "" && (client = stat->find_client(cid)) == NULL) { if (client_id != "" && (client = stat->find_client(client_id)) == NULL) {
return srs_api_response_code(w, r, ERROR_RTMP_CLIENT_NOT_FOUND); return srs_api_response_code(w, r, ERROR_RTMP_CLIENT_NOT_FOUND);
} }
@ -854,7 +854,7 @@ srs_error_t SrsGoApiClients::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessa
} }
client->conn->expire(); client->conn->expire();
srs_warn("kickoff client id=%s ok", cid.c_str()); srs_warn("kickoff client id=%s ok", client_id.c_str());
} else { } else {
return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed); return srs_go_http_error(w, SRS_CONSTS_HTTP_MethodNotAllowed);
} }

View file

@ -60,15 +60,13 @@ srs_error_t SrsHttpHooks::on_connect(string url, SrsRequest* req)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// TODO: FIXME: check client_id must be int? SrsContextId cid = _srs_context->get_id();
std::string client_id = _srs_context->get_id();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_connect")); obj->set("action", SrsJsonAny::str("on_connect"));
// obj->set("client_id", SrsJsonAny::integer(client_id)); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("client_id", SrsJsonAny::str(client_id.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -82,11 +80,11 @@ srs_error_t SrsHttpHooks::on_connect(string url, SrsRequest* req)
SrsHttpClient http; SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: on_connect failed, client_id=%s, url=%s, request=%s, response=%s, code=%d", return srs_error_wrap(err, "http: on_connect failed, client_id=%s, url=%s, request=%s, response=%s, code=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code);
} }
srs_trace("http: on_connect ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_connect ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return err; return err;
} }
@ -95,13 +93,13 @@ void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_close")); obj->set("action", SrsJsonAny::str("on_close"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -117,12 +115,12 @@ void SrsHttpHooks::on_close(string url, SrsRequest* req, int64_t send_bytes, int
int ret = srs_error_code(err); int ret = srs_error_code(err);
srs_freep(err); srs_freep(err);
srs_warn("http: ignore on_close failed, client_id=%s, url=%s, request=%s, response=%s, code=%d, ret=%d", srs_warn("http: ignore on_close failed, client_id=%s, url=%s, request=%s, response=%s, code=%d, ret=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code, ret); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code, ret);
return; return;
} }
srs_trace("http: on_close ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_close ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return; return;
} }
@ -131,13 +129,13 @@ srs_error_t SrsHttpHooks::on_publish(string url, SrsRequest* req)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_publish")); obj->set("action", SrsJsonAny::str("on_publish"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -152,11 +150,11 @@ srs_error_t SrsHttpHooks::on_publish(string url, SrsRequest* req)
SrsHttpClient http; SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: on_publish failed, client_id=%s, url=%s, request=%s, response=%s, code=%d", return srs_error_wrap(err, "http: on_publish failed, client_id=%s, url=%s, request=%s, response=%s, code=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code);
} }
srs_trace("http: on_publish ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_publish ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return err; return err;
} }
@ -165,13 +163,13 @@ void SrsHttpHooks::on_unpublish(string url, SrsRequest* req)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_unpublish")); obj->set("action", SrsJsonAny::str("on_unpublish"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -187,12 +185,12 @@ void SrsHttpHooks::on_unpublish(string url, SrsRequest* req)
int ret = srs_error_code(err); int ret = srs_error_code(err);
srs_freep(err); srs_freep(err);
srs_warn("http: ignore on_unpublish failed, client_id=%s, url=%s, request=%s, response=%s, status=%d, ret=%d", srs_warn("http: ignore on_unpublish failed, client_id=%s, url=%s, request=%s, response=%s, status=%d, ret=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code, ret); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code, ret);
return; return;
} }
srs_trace("http: on_unpublish ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_unpublish ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return; return;
} }
@ -201,13 +199,13 @@ srs_error_t SrsHttpHooks::on_play(string url, SrsRequest* req)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_play")); obj->set("action", SrsJsonAny::str("on_play"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -222,11 +220,11 @@ srs_error_t SrsHttpHooks::on_play(string url, SrsRequest* req)
SrsHttpClient http; SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http: on_play failed, client_id=%s, url=%s, request=%s, response=%s, status=%d", return srs_error_wrap(err, "http: on_play failed, client_id=%s, url=%s, request=%s, response=%s, status=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code);
} }
srs_trace("http: on_play ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_play ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return err; return err;
} }
@ -235,13 +233,13 @@ void SrsHttpHooks::on_stop(string url, SrsRequest* req)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_stop")); obj->set("action", SrsJsonAny::str("on_stop"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -257,28 +255,28 @@ void SrsHttpHooks::on_stop(string url, SrsRequest* req)
int ret = srs_error_code(err); int ret = srs_error_code(err);
srs_freep(err); srs_freep(err);
srs_warn("http: ignore on_stop failed, client_id=%s, url=%s, request=%s, response=%s, code=%d, ret=%d", srs_warn("http: ignore on_stop failed, client_id=%s, url=%s, request=%s, response=%s, code=%d, ret=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code, ret); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code, ret);
return; return;
} }
srs_trace("http: on_stop ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_stop ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return; return;
} }
srs_error_t SrsHttpHooks::on_dvr(std::string cid, string url, SrsRequest* req, string file) srs_error_t SrsHttpHooks::on_dvr(SrsContextId c, string url, SrsRequest* req, string file)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = cid; SrsContextId cid = c;
std::string cwd = _srs_config->cwd(); std::string cwd = _srs_config->cwd();
SrsJsonObject* obj = SrsJsonAny::object(); SrsJsonObject* obj = SrsJsonAny::object();
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_dvr")); obj->set("action", SrsJsonAny::str("on_dvr"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -294,20 +292,20 @@ srs_error_t SrsHttpHooks::on_dvr(std::string cid, string url, SrsRequest* req, s
SrsHttpClient http; SrsHttpClient http;
if ((err = do_post(&http, url, data, status_code, res)) != srs_success) { if ((err = do_post(&http, url, data, status_code, res)) != srs_success) {
return srs_error_wrap(err, "http post on_dvr uri failed, client_id=%s, url=%s, request=%s, response=%s, code=%d", return srs_error_wrap(err, "http post on_dvr uri failed, client_id=%s, url=%s, request=%s, response=%s, code=%d",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code); cid.c_str(), url.c_str(), data.c_str(), res.c_str(), status_code);
} }
srs_trace("http hook on_dvr success. client_id=%s, url=%s, request=%s, response=%s", srs_trace("http hook on_dvr success. client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return err; return err;
} }
srs_error_t SrsHttpHooks::on_hls(std::string cid, string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, srs_utime_t duration) srs_error_t SrsHttpHooks::on_hls(SrsContextId c, string url, SrsRequest* req, string file, string ts_url, string m3u8, string m3u8_url, int sn, srs_utime_t duration)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = cid; SrsContextId cid = c;
std::string cwd = _srs_config->cwd(); std::string cwd = _srs_config->cwd();
// the ts_url is under the same dir of m3u8_url. // the ts_url is under the same dir of m3u8_url.
@ -320,7 +318,7 @@ srs_error_t SrsHttpHooks::on_hls(std::string cid, string url, SrsRequest* req, s
SrsAutoFree(SrsJsonObject, obj); SrsAutoFree(SrsJsonObject, obj);
obj->set("action", SrsJsonAny::str("on_hls")); obj->set("action", SrsJsonAny::str("on_hls"));
obj->set("client_id", SrsJsonAny::str(client_id.c_str())); obj->set("client_id", SrsJsonAny::str(cid.c_str()));
obj->set("ip", SrsJsonAny::str(req->ip.c_str())); obj->set("ip", SrsJsonAny::str(req->ip.c_str()));
obj->set("vhost", SrsJsonAny::str(req->vhost.c_str())); obj->set("vhost", SrsJsonAny::str(req->vhost.c_str()));
obj->set("app", SrsJsonAny::str(req->app.c_str())); obj->set("app", SrsJsonAny::str(req->app.c_str()));
@ -344,16 +342,16 @@ srs_error_t SrsHttpHooks::on_hls(std::string cid, string url, SrsRequest* req, s
} }
srs_trace("http: on_hls ok, client_id=%s, url=%s, request=%s, response=%s", srs_trace("http: on_hls ok, client_id=%s, url=%s, request=%s, response=%s",
client_id.c_str(), url.c_str(), data.c_str(), res.c_str()); cid.c_str(), url.c_str(), data.c_str(), res.c_str());
return err; return err;
} }
srs_error_t SrsHttpHooks::on_hls_notify(std::string cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify) srs_error_t SrsHttpHooks::on_hls_notify(SrsContextId c, std::string url, SrsRequest* req, std::string ts_url, int nb_notify)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
std::string client_id = cid; SrsContextId cid = c;
std::string cwd = _srs_config->cwd(); std::string cwd = _srs_config->cwd();
if (srs_string_is_http(ts_url)) { if (srs_string_is_http(ts_url)) {
@ -409,7 +407,7 @@ srs_error_t SrsHttpHooks::on_hls_notify(std::string cid, std::string url, SrsReq
int spenttime = (int)(srsu2ms(srs_update_system_time()) - starttime); int spenttime = (int)(srsu2ms(srs_update_system_time()) - starttime);
srs_trace("http hook on_hls_notify success. client_id=%s, url=%s, code=%d, spent=%dms, read=%dB, err=%s", srs_trace("http hook on_hls_notify success. client_id=%s, url=%s, code=%d, spent=%dms, read=%dB, err=%s",
client_id.c_str(), url.c_str(), msg->status_code(), spenttime, nb_read, srs_error_desc(err).c_str()); cid.c_str(), url.c_str(), msg->status_code(), spenttime, nb_read, srs_error_desc(err).c_str());
// ignore any error for on_hls_notify. // ignore any error for on_hls_notify.
srs_error_reset(err); srs_error_reset(err);

View file

@ -74,7 +74,7 @@ public:
// ignore if empty. // ignore if empty.
// @param file the file path, can be relative or absolute path. // @param file the file path, can be relative or absolute path.
// @param cid the source connection cid, for the on_dvr is async call. // @param cid the source connection cid, for the on_dvr is async call.
static srs_error_t on_dvr(std::string cid, std::string url, SrsRequest* req, std::string file); static srs_error_t on_dvr(SrsContextId cid, std::string url, SrsRequest* req, std::string file);
// When hls reap segment, callback. // When hls reap segment, callback.
// @param url the api server url, to process the event. // @param url the api server url, to process the event.
// ignore if empty. // ignore if empty.
@ -85,7 +85,7 @@ public:
// @param sn the seq_no, the sequence number of ts in hls/m3u8. // @param sn the seq_no, the sequence number of ts in hls/m3u8.
// @param duration the segment duration in srs_utime_t. // @param duration the segment duration in srs_utime_t.
// @param cid the source connection cid, for the on_dvr is async call. // @param cid the source connection cid, for the on_dvr is async call.
static srs_error_t on_hls(std::string cid, std::string url, SrsRequest* req, std::string file, std::string ts_url, static srs_error_t on_hls(SrsContextId cid, std::string url, SrsRequest* req, std::string file, std::string ts_url,
std::string m3u8, std::string m3u8_url, int sn, srs_utime_t duration); std::string m3u8, std::string m3u8_url, int sn, srs_utime_t duration);
// When hls reap segment, callback. // When hls reap segment, callback.
// @param url the api server url, to process the event. // @param url the api server url, to process the event.
@ -93,7 +93,7 @@ public:
// @param ts_url the ts uri, used to replace the variable [ts_url] in url. // @param ts_url the ts uri, used to replace the variable [ts_url] in url.
// @param nb_notify the max bytes to read from notify server. // @param nb_notify the max bytes to read from notify server.
// @param cid the source connection cid, for the on_dvr is async call. // @param cid the source connection cid, for the on_dvr is async call.
static srs_error_t on_hls_notify(std::string cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify); static srs_error_t on_hls_notify(SrsContextId cid, std::string url, SrsRequest* req, std::string ts_url, int nb_notify);
// Discover co-workers for origin cluster. // Discover co-workers for origin cluster.
static srs_error_t discover_co_workers(std::string url, std::string& host, int& port); static srs_error_t discover_co_workers(std::string url, std::string& host, int& port);
private: private:

View file

@ -94,7 +94,7 @@ void SrsFileLog::reopen()
open_log_file(); open_log_file();
} }
void SrsFileLog::verbose(const char* tag, const char* context_id, const char* fmt, ...) void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelVerbose) { if (level > SrsLogLevelVerbose) {
return; return;
@ -114,7 +114,7 @@ void SrsFileLog::verbose(const char* tag, const char* context_id, const char* fm
write_log(fd, log_data, size, SrsLogLevelVerbose); write_log(fd, log_data, size, SrsLogLevelVerbose);
} }
void SrsFileLog::info(const char* tag, const char* context_id, const char* fmt, ...) void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelInfo) { if (level > SrsLogLevelInfo) {
return; return;
@ -134,7 +134,7 @@ void SrsFileLog::info(const char* tag, const char* context_id, const char* fmt,
write_log(fd, log_data, size, SrsLogLevelInfo); write_log(fd, log_data, size, SrsLogLevelInfo);
} }
void SrsFileLog::trace(const char* tag, const char* context_id, const char* fmt, ...) void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelTrace) { if (level > SrsLogLevelTrace) {
return; return;
@ -154,7 +154,7 @@ void SrsFileLog::trace(const char* tag, const char* context_id, const char* fmt,
write_log(fd, log_data, size, SrsLogLevelTrace); write_log(fd, log_data, size, SrsLogLevelTrace);
} }
void SrsFileLog::warn(const char* tag, const char* context_id, const char* fmt, ...) void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelWarn) { if (level > SrsLogLevelWarn) {
return; return;
@ -174,7 +174,7 @@ void SrsFileLog::warn(const char* tag, const char* context_id, const char* fmt,
write_log(fd, log_data, size, SrsLogLevelWarn); write_log(fd, log_data, size, SrsLogLevelWarn);
} }
void SrsFileLog::error(const char* tag, const char* context_id, const char* fmt, ...) void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelError) { if (level > SrsLogLevelError) {
return; return;

View file

@ -55,11 +55,11 @@ public:
public: public:
virtual srs_error_t initialize(); virtual srs_error_t initialize();
virtual void reopen(); virtual void reopen();
virtual void verbose(const char* tag, const char* context_id, const char* fmt, ...); virtual void verbose(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void info(const char* tag, const char* context_id, const char* fmt, ...); virtual void info(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void trace(const char* tag, const char* context_id, const char* fmt, ...); virtual void trace(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void warn(const char* tag, const char* context_id, const char* fmt, ...); virtual void warn(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void error(const char* tag, const char* context_id, const char* fmt, ...); virtual void error(const char* tag, SrsContextId context_id, const char* fmt, ...);
// Interface ISrsReloadHandler. // Interface ISrsReloadHandler.
public: public:
virtual srs_error_t on_reload_utc_time(); virtual srs_error_t on_reload_utc_time();

View file

@ -180,7 +180,7 @@ srs_error_t SrsProcess::start()
srs_info("fork process: %s", cli.c_str()); srs_info("fork process: %s", cli.c_str());
// for log // for log
std::string cid = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
int ppid = getpid(); int ppid = getpid();
// TODO: fork or vfork? // TODO: fork or vfork?

View file

@ -57,7 +57,7 @@ ISrsMessagePumper::~ISrsMessagePumper()
{ {
} }
SrsRecvThread::SrsRecvThread(ISrsMessagePumper* p, SrsRtmpServer* r, srs_utime_t tm, std::string parent_cid) SrsRecvThread::SrsRecvThread(ISrsMessagePumper* p, SrsRtmpServer* r, srs_utime_t tm, SrsContextId parent_cid)
{ {
rtmp = r; rtmp = r;
pumper = p; pumper = p;
@ -71,7 +71,7 @@ SrsRecvThread::~SrsRecvThread()
srs_freep(trd); srs_freep(trd);
} }
std::string SrsRecvThread::cid() SrsContextId SrsRecvThread::cid()
{ {
return trd->cid(); return trd->cid();
} }
@ -161,7 +161,7 @@ srs_error_t SrsRecvThread::do_cycle()
return err; return err;
} }
SrsQueueRecvThread::SrsQueueRecvThread(SrsConsumer* consumer, SrsRtmpServer* rtmp_sdk, srs_utime_t tm, std::string parent_cid) SrsQueueRecvThread::SrsQueueRecvThread(SrsConsumer* consumer, SrsRtmpServer* rtmp_sdk, srs_utime_t tm, SrsContextId parent_cid)
: trd(this, rtmp_sdk, tm, parent_cid) : trd(this, rtmp_sdk, tm, parent_cid)
{ {
_consumer = consumer; _consumer = consumer;
@ -278,7 +278,7 @@ void SrsQueueRecvThread::on_stop()
} }
SrsPublishRecvThread::SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest* _req, SrsPublishRecvThread::SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest* _req,
int mr_sock_fd, srs_utime_t tm, SrsRtmpConn* conn, SrsSource* source, std::string parent_cid) int mr_sock_fd, srs_utime_t tm, SrsRtmpConn* conn, SrsSource* source, SrsContextId parent_cid)
: trd(this, rtmp_sdk, tm, parent_cid) : trd(this, rtmp_sdk, tm, parent_cid)
{ {
rtmp = rtmp_sdk; rtmp = rtmp_sdk;
@ -290,8 +290,7 @@ SrsPublishRecvThread::SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest*
_nb_msgs = 0; _nb_msgs = 0;
video_frames = 0; video_frames = 0;
error = srs_cond_new(); error = srs_cond_new();
ncid = cid = "";
req = _req; req = _req;
mr_fd = mr_sock_fd; mr_fd = mr_sock_fd;
@ -346,7 +345,7 @@ void SrsPublishRecvThread::set_cid(std::string v)
ncid = v; ncid = v;
} }
std::string SrsPublishRecvThread::get_cid() SrsContextId SrsPublishRecvThread::get_cid()
{ {
return ncid; return ncid;
} }
@ -374,7 +373,7 @@ srs_error_t SrsPublishRecvThread::consume(SrsCommonMessage* msg)
srs_error_t err = srs_success; srs_error_t err = srs_success;
// when cid changed, change it. // when cid changed, change it.
if (ncid != cid) { if (!ncid.equals(cid)) {
_srs_context->set_id(ncid); _srs_context->set_id(ncid);
cid = ncid; cid = ncid;
} }

View file

@ -81,16 +81,16 @@ protected:
SrsCoroutine* trd; SrsCoroutine* trd;
ISrsMessagePumper* pumper; ISrsMessagePumper* pumper;
SrsRtmpServer* rtmp; SrsRtmpServer* rtmp;
std::string _parent_cid; SrsContextId _parent_cid;
// The recv timeout in srs_utime_t. // The recv timeout in srs_utime_t.
srs_utime_t timeout; srs_utime_t timeout;
public: public:
// Constructor. // Constructor.
// @param tm The receive timeout in srs_utime_t. // @param tm The receive timeout in srs_utime_t.
SrsRecvThread(ISrsMessagePumper* p, SrsRtmpServer* r, srs_utime_t tm, std::string parent_cid); SrsRecvThread(ISrsMessagePumper* p, SrsRtmpServer* r, srs_utime_t tm, SrsContextId parent_cid);
virtual ~SrsRecvThread(); virtual ~SrsRecvThread();
public: public:
virtual std::string cid(); virtual SrsContextId cid();
public: public:
virtual srs_error_t start(); virtual srs_error_t start();
virtual void stop(); virtual void stop();
@ -117,7 +117,7 @@ private:
SrsConsumer* _consumer; SrsConsumer* _consumer;
public: public:
// TODO: FIXME: Refine timeout in time unit. // TODO: FIXME: Refine timeout in time unit.
SrsQueueRecvThread(SrsConsumer* consumer, SrsRtmpServer* rtmp_sdk, srs_utime_t tm, std::string parent_cid); SrsQueueRecvThread(SrsConsumer* consumer, SrsRtmpServer* rtmp_sdk, srs_utime_t tm, SrsContextId parent_cid);
virtual ~SrsQueueRecvThread(); virtual ~SrsQueueRecvThread();
public: public:
virtual srs_error_t start(); virtual srs_error_t start();
@ -168,11 +168,11 @@ private:
// @see https://github.com/ossrs/srs/issues/244 // @see https://github.com/ossrs/srs/issues/244
srs_cond_t error; srs_cond_t error;
// The merged context id. // The merged context id.
std::string cid; SrsContextId cid;
std::string ncid; SrsContextId ncid;
public: public:
SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest* _req, SrsPublishRecvThread(SrsRtmpServer* rtmp_sdk, SrsRequest* _req,
int mr_sock_fd, srs_utime_t tm, SrsRtmpConn* conn, SrsSource* source, std::string parent_cid); int mr_sock_fd, srs_utime_t tm, SrsRtmpConn* conn, SrsSource* source, SrsContextId parent_cid);
virtual ~SrsPublishRecvThread(); virtual ~SrsPublishRecvThread();
public: public:
// Wait for error for some timeout. // Wait for error for some timeout.
@ -181,7 +181,7 @@ public:
virtual uint64_t nb_video_frames(); virtual uint64_t nb_video_frames();
virtual srs_error_t error_code(); virtual srs_error_t error_code();
virtual void set_cid(std::string v); virtual void set_cid(std::string v);
virtual std::string get_cid(); virtual SrsContextId get_cid();
public: public:
virtual srs_error_t start(); virtual srs_error_t start();
virtual void stop(); virtual void stop();

View file

@ -271,7 +271,7 @@ SrsRtcOutgoingInfo::~SrsRtcOutgoingInfo()
{ {
} }
SrsRtcPlayer::SrsRtcPlayer(SrsRtcSession* s, string parent_cid) SrsRtcPlayer::SrsRtcPlayer(SrsRtcSession* s, SrsContextId parent_cid)
{ {
_parent_cid = parent_cid; _parent_cid = parent_cid;
trd = new SrsDummyCoroutine(); trd = new SrsDummyCoroutine();
@ -345,7 +345,7 @@ srs_error_t SrsRtcPlayer::on_reload_vhost_realtime(string vhost)
return on_reload_vhost_play(vhost); return on_reload_vhost_play(vhost);
} }
std::string SrsRtcPlayer::cid() SrsContextId SrsRtcPlayer::cid()
{ {
return trd->cid(); return trd->cid();
} }
@ -1725,8 +1725,8 @@ block +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
void SrsRtcPublisher::request_keyframe() void SrsRtcPublisher::request_keyframe()
{ {
std::string scid = _srs_context->get_id(); SrsContextId scid = _srs_context->get_id();
std::string pcid = session_->context_id(); SrsContextId pcid = session_->context_id();
srs_trace("RTC play=[%d][%s] request keyframe from publish=[%d][%s]", ::getpid(), scid.c_str(), ::getpid(), pcid.c_str()); srs_trace("RTC play=[%d][%s] request keyframe from publish=[%d][%s]", ::getpid(), scid.c_str(), ::getpid(), pcid.c_str());
request_keyframe_ = true; request_keyframe_ = true;
@ -1767,7 +1767,6 @@ void SrsRtcPublisher::simulate_drop_packet(SrsRtpHeader* h, int nn_bytes)
SrsRtcSession::SrsRtcSession(SrsRtcServer* s) SrsRtcSession::SrsRtcSession(SrsRtcServer* s)
{ {
req = NULL; req = NULL;
cid = "";
is_publisher_ = false; is_publisher_ = false;
encrypt = true; encrypt = true;
@ -1860,12 +1859,12 @@ void SrsRtcSession::switch_to_context()
_srs_context->set_id(cid); _srs_context->set_id(cid);
} }
std::string SrsRtcSession::context_id() SrsContextId SrsRtcSession::context_id()
{ {
return cid; return cid;
} }
srs_error_t SrsRtcSession::initialize(SrsRtcSource* source, SrsRequest* r, bool is_publisher, string username, std::string context_id) srs_error_t SrsRtcSession::initialize(SrsRtcSource* source, SrsRequest* r, bool is_publisher, string username, SrsContextId context_id)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;

View file

@ -185,7 +185,7 @@ public:
class SrsRtcPlayer : virtual public ISrsCoroutineHandler, virtual public ISrsReloadHandler class SrsRtcPlayer : virtual public ISrsCoroutineHandler, virtual public ISrsReloadHandler
{ {
protected: protected:
std::string _parent_cid; SrsContextId _parent_cid;
SrsCoroutine* trd; SrsCoroutine* trd;
SrsRtcSession* session_; SrsRtcSession* session_;
private: private:
@ -208,7 +208,7 @@ private:
// Whether enabled nack. // Whether enabled nack.
bool nack_enabled_; bool nack_enabled_;
public: public:
SrsRtcPlayer(SrsRtcSession* s, std::string parent_cid); SrsRtcPlayer(SrsRtcSession* s, SrsContextId parent_cid);
virtual ~SrsRtcPlayer(); virtual ~SrsRtcPlayer();
public: public:
srs_error_t initialize(uint32_t vssrc, uint32_t assrc, uint16_t v_pt, uint16_t a_pt); srs_error_t initialize(uint32_t vssrc, uint32_t assrc, uint16_t v_pt, uint16_t a_pt);
@ -217,7 +217,7 @@ public:
virtual srs_error_t on_reload_vhost_play(std::string vhost); virtual srs_error_t on_reload_vhost_play(std::string vhost);
virtual srs_error_t on_reload_vhost_realtime(std::string vhost); virtual srs_error_t on_reload_vhost_realtime(std::string vhost);
public: public:
virtual std::string cid(); virtual SrsContextId cid();
public: public:
virtual srs_error_t start(); virtual srs_error_t start();
virtual void stop(); virtual void stop();
@ -337,7 +337,7 @@ private:
srs_utime_t last_stun_time; srs_utime_t last_stun_time;
private: private:
// For each RTC session, we use a specified cid for debugging logs. // For each RTC session, we use a specified cid for debugging logs.
std::string cid; SrsContextId cid;
// For each RTC session, whether requires encrypt. // For each RTC session, whether requires encrypt.
// Read config value, rtc_server.encrypt, default to on. // Read config value, rtc_server.encrypt, default to on.
// Sepcifies by HTTP API, query encrypt, optional. // Sepcifies by HTTP API, query encrypt, optional.
@ -372,10 +372,10 @@ public:
std::string username(); std::string username();
void set_encrypt(bool v); void set_encrypt(bool v);
void switch_to_context(); void switch_to_context();
std::string context_id(); SrsContextId context_id();
public: public:
// Before initialize, user must set the local SDP, which is used to inititlize DTLS. // Before initialize, user must set the local SDP, which is used to inititlize DTLS.
srs_error_t initialize(SrsRtcSource* source, SrsRequest* r, bool is_publisher, std::string username, std::string context_id); srs_error_t initialize(SrsRtcSource* source, SrsRequest* r, bool is_publisher, std::string username, SrsContextId context_id);
// The peer address may change, we can identify that by STUN messages. // The peer address may change, we can identify that by STUN messages.
srs_error_t on_stun(SrsUdpMuxSocket* skt, SrsStunPacket* r); srs_error_t on_stun(SrsUdpMuxSocket* skt, SrsStunPacket* r);
srs_error_t on_dtls(char* data, int nb_data); srs_error_t on_dtls(char* data, int nb_data);

View file

@ -344,7 +344,7 @@ srs_error_t SrsRtcServer::create_session(
session->set_local_sdp(local_sdp); session->set_local_sdp(local_sdp);
session->set_state(WAITING_STUN); session->set_state(WAITING_STUN);
std::string cid = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
// Before session initialize, we must setup the local SDP. // Before session initialize, we must setup the local SDP.
if ((err = session->initialize(source, req, publish, username, cid)) != srs_success) { if ((err = session->initialize(source, req, publish, username, cid)) != srs_success) {
srs_freep(session); srs_freep(session);
@ -401,7 +401,7 @@ srs_error_t SrsRtcServer::setup_session2(SrsRtcSession* session, SrsRequest* req
// TODO: FIXME: Collision detect. // TODO: FIXME: Collision detect.
string username = session->get_local_sdp()->get_ice_ufrag() + ":" + remote_sdp.get_ice_ufrag(); string username = session->get_local_sdp()->get_ice_ufrag() + ":" + remote_sdp.get_ice_ufrag();
std::string cid = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
if ((err = session->initialize(source, req, false, username, cid)) != srs_success) { if ((err = session->initialize(source, req, false, username, cid)) != srs_success) {
return srs_error_wrap(err, "init"); return srs_error_wrap(err, "init");
} }

View file

@ -245,7 +245,6 @@ ISrsRtcPublisher::~ISrsRtcPublisher()
SrsRtcSource::SrsRtcSource() SrsRtcSource::SrsRtcSource()
{ {
_source_id = _pre_source_id = "";
_can_publish = true; _can_publish = true;
rtc_publisher_ = NULL; rtc_publisher_ = NULL;
@ -288,17 +287,17 @@ void SrsRtcSource::update_auth(SrsRequest* r)
req->update_auth(r); req->update_auth(r);
} }
srs_error_t SrsRtcSource::on_source_id_changed(std::string id) srs_error_t SrsRtcSource::on_source_id_changed(SrsContextId id)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (_source_id == id) { if (_source_id.equals(id)) {
return err; return err;
} }
if (_pre_source_id == "") { if (_pre_source_id.empty()) {
_pre_source_id = id; _pre_source_id = id;
} else if (_pre_source_id != _source_id) { } else if (!_pre_source_id.equals(_source_id)) {
_pre_source_id = _source_id; _pre_source_id = _source_id;
} }
@ -314,12 +313,12 @@ srs_error_t SrsRtcSource::on_source_id_changed(std::string id)
return err; return err;
} }
std::string SrsRtcSource::source_id() SrsContextId SrsRtcSource::source_id()
{ {
return _source_id; return _source_id;
} }
std::string SrsRtcSource::pre_source_id() SrsContextId SrsRtcSource::pre_source_id()
{ {
return _pre_source_id; return _pre_source_id;
} }
@ -395,7 +394,7 @@ void SrsRtcSource::on_unpublish()
srs_trace("cleanup when unpublish"); srs_trace("cleanup when unpublish");
_can_publish = true; _can_publish = true;
_source_id = -1; _source_id = SrsContextId();
// TODO: FIXME: Handle by statistic. // TODO: FIXME: Handle by statistic.
} }

View file

@ -109,9 +109,9 @@ private:
// For edge, it's the edge ingest id. // For edge, it's the edge ingest id.
// when source id changed, for example, the edge reconnect, // when source id changed, for example, the edge reconnect,
// invoke the on_source_id_changed() to let all clients know. // invoke the on_source_id_changed() to let all clients know.
std::string _source_id; SrsContextId _source_id;
// previous source id. // previous source id.
std::string _pre_source_id; SrsContextId _pre_source_id;
SrsRequest* req; SrsRequest* req;
ISrsRtcPublisher* rtc_publisher_; ISrsRtcPublisher* rtc_publisher_;
// Transmux RTMP to RTC. // Transmux RTMP to RTC.
@ -129,10 +129,10 @@ public:
// Update the authentication information in request. // Update the authentication information in request.
virtual void update_auth(SrsRequest* r); virtual void update_auth(SrsRequest* r);
// The source id changed. // The source id changed.
virtual srs_error_t on_source_id_changed(std::string id); virtual srs_error_t on_source_id_changed(SrsContextId id);
// Get current source id. // Get current source id.
virtual std::string source_id(); virtual SrsContextId source_id();
virtual std::string pre_source_id(); virtual SrsContextId pre_source_id();
// Get the bridger. // Get the bridger.
ISrsSourceBridger* bridger(); ISrsSourceBridger* bridger();
public: public:

View file

@ -1792,7 +1792,7 @@ void SrsSourceManager::dispose()
srs_error_t SrsSourceManager::cycle() srs_error_t SrsSourceManager::cycle()
{ {
std::string cid = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
srs_error_t err = do_cycle(); srs_error_t err = do_cycle();
_srs_context->set_id(cid); _srs_context->set_id(cid);
@ -1866,7 +1866,6 @@ SrsSource::SrsSource()
mix_queue = new SrsMixQueue(); mix_queue = new SrsMixQueue();
_can_publish = true; _can_publish = true;
_pre_source_id = _source_id = "";
die_at = 0; die_at = 0;
handler = NULL; handler = NULL;
@ -2065,17 +2064,17 @@ srs_error_t SrsSource::on_reload_vhost_play(string vhost)
return err; return err;
} }
srs_error_t SrsSource::on_source_id_changed(string id) srs_error_t SrsSource::on_source_id_changed(SrsContextId id)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
if (_source_id == id) { if (_source_id.equals(id)) {
return err; return err;
} }
if (_pre_source_id == "") { if (_pre_source_id.empty()) {
_pre_source_id = id; _pre_source_id = id;
} else if (_pre_source_id != _source_id) { } else if (!_pre_source_id.equals(_source_id)) {
_pre_source_id = _source_id; _pre_source_id = _source_id;
} }
@ -2091,12 +2090,12 @@ srs_error_t SrsSource::on_source_id_changed(string id)
return err; return err;
} }
string SrsSource::source_id() SrsContextId SrsSource::source_id()
{ {
return _source_id; return _source_id;
} }
string SrsSource::pre_source_id() SrsContextId SrsSource::pre_source_id()
{ {
return _pre_source_id; return _pre_source_id;
} }
@ -2560,7 +2559,7 @@ void SrsSource::on_unpublish()
srs_trace("cleanup when unpublish"); srs_trace("cleanup when unpublish");
_can_publish = true; _can_publish = true;
_source_id = -1; _source_id = SrsContextId();
// notify the handler. // notify the handler.
srs_assert(handler); srs_assert(handler);

View file

@ -508,9 +508,9 @@ private:
// For edge, it's the edge ingest id. // For edge, it's the edge ingest id.
// when source id changed, for example, the edge reconnect, // when source id changed, for example, the edge reconnect,
// invoke the on_source_id_changed() to let all clients know. // invoke the on_source_id_changed() to let all clients know.
std::string _source_id; SrsContextId _source_id;
// previous source id. // previous source id.
std::string _pre_source_id; SrsContextId _pre_source_id;
// deep copy of client request. // deep copy of client request.
SrsRequest* req; SrsRequest* req;
// To delivery stream to clients. // To delivery stream to clients.
@ -567,10 +567,10 @@ public:
virtual srs_error_t on_reload_vhost_play(std::string vhost); virtual srs_error_t on_reload_vhost_play(std::string vhost);
public: public:
// The source id changed. // The source id changed.
virtual srs_error_t on_source_id_changed(std::string id); virtual srs_error_t on_source_id_changed(SrsContextId id);
// Get current source id. // Get current source id.
virtual std::string source_id(); virtual SrsContextId source_id();
virtual std::string pre_source_id(); virtual SrsContextId pre_source_id();
// Whether source is inactive, which means there is no publishing stream source. // Whether source is inactive, which means there is no publishing stream source.
// @remark For edge, it's inactive util stream has been pulled from origin. // @remark For edge, it's inactive util stream has been pulled from origin.
virtual bool inactive(); virtual bool inactive();

View file

@ -74,18 +74,27 @@ srs_error_t SrsDummyCoroutine::pull()
return srs_error_new(ERROR_THREAD_DUMMY, "dummy pull"); return srs_error_new(ERROR_THREAD_DUMMY, "dummy pull");
} }
string SrsDummyCoroutine::cid() SrsContextId SrsDummyCoroutine::cid()
{ {
return ""; return SrsContextId();
} }
_ST_THREAD_CREATE_PFN _pfn_st_thread_create = (_ST_THREAD_CREATE_PFN)st_thread_create; _ST_THREAD_CREATE_PFN _pfn_st_thread_create = (_ST_THREAD_CREATE_PFN)st_thread_create;
SrsSTCoroutine::SrsSTCoroutine(string n, ISrsCoroutineHandler* h, std::string cid) SrsSTCoroutine::SrsSTCoroutine(string n, ISrsCoroutineHandler* h)
{ {
name = n; name = n;
handler = h; handler = h;
context = cid; trd = NULL;
trd_err = srs_success;
started = interrupted = disposed = cycle_done = false;
}
SrsSTCoroutine::SrsSTCoroutine(string n, ISrsCoroutineHandler* h, SrsContextId cid)
{
name = n;
handler = h;
cid_ = cid;
trd = NULL; trd = NULL;
trd_err = srs_success; trd_err = srs_success;
started = interrupted = disposed = cycle_done = false; started = interrupted = disposed = cycle_done = false;
@ -180,18 +189,18 @@ srs_error_t SrsSTCoroutine::pull()
return srs_error_copy(trd_err); return srs_error_copy(trd_err);
} }
string SrsSTCoroutine::cid() SrsContextId SrsSTCoroutine::cid()
{ {
return context; return cid_;
} }
srs_error_t SrsSTCoroutine::cycle() srs_error_t SrsSTCoroutine::cycle()
{ {
if (_srs_context) { if (_srs_context) {
if (!context.empty()) { if (!cid_.empty()) {
_srs_context->set_id(context); _srs_context->set_id(cid_);
} else { } else {
context = _srs_context->generate_id(); cid_ = _srs_context->generate_id();
} }
} }

View file

@ -28,6 +28,7 @@
#include <string> #include <string>
#include <srs_kernel_log.hpp>
#include <srs_service_st.hpp> #include <srs_service_st.hpp>
#include <srs_protocol_io.hpp> #include <srs_protocol_io.hpp>
@ -80,7 +81,7 @@ public:
// @return a copy of error, which should be freed by user. // @return a copy of error, which should be freed by user.
// NULL if not terminated and user should pull again. // NULL if not terminated and user should pull again.
virtual srs_error_t pull() = 0; virtual srs_error_t pull() = 0;
virtual std::string cid() = 0; virtual SrsContextId cid() = 0;
}; };
// An empty coroutine, user can default to this object before create any real coroutine. // An empty coroutine, user can default to this object before create any real coroutine.
@ -95,7 +96,7 @@ public:
virtual void stop(); virtual void stop();
virtual void interrupt(); virtual void interrupt();
virtual srs_error_t pull(); virtual srs_error_t pull();
virtual std::string cid(); virtual SrsContextId cid();
}; };
// For utest to mock the thread create. // For utest to mock the thread create.
@ -121,7 +122,7 @@ private:
ISrsCoroutineHandler* handler; ISrsCoroutineHandler* handler;
private: private:
srs_thread_t trd; srs_thread_t trd;
std::string context; SrsContextId cid_;
srs_error_t trd_err; srs_error_t trd_err;
private: private:
bool started; bool started;
@ -132,7 +133,8 @@ private:
public: public:
// Create a thread with name n and handler h. // Create a thread with name n and handler h.
// @remark User can specify a cid for thread to use, or we will allocate a new one. // @remark User can specify a cid for thread to use, or we will allocate a new one.
SrsSTCoroutine(std::string n, ISrsCoroutineHandler* h, std::string cid = ""); SrsSTCoroutine(std::string n, ISrsCoroutineHandler* h);
SrsSTCoroutine(std::string n, ISrsCoroutineHandler* h, SrsContextId cid);
virtual ~SrsSTCoroutine(); virtual ~SrsSTCoroutine();
public: public:
// Start the thread. // Start the thread.
@ -154,7 +156,7 @@ public:
// @remark Return ERROR_THREAD_INTERRUPED when thread is interrupted. // @remark Return ERROR_THREAD_INTERRUPED when thread is interrupted.
virtual srs_error_t pull(); virtual srs_error_t pull();
// Get the context id of thread. // Get the context id of thread.
virtual std::string cid(); virtual SrsContextId cid();
private: private:
virtual srs_error_t cycle(); virtual srs_error_t cycle();
static void* pfn(void* arg); static void* pfn(void* arg);

View file

@ -101,8 +101,7 @@ SrsStatisticStream::SrsStatisticStream()
id = srs_generate_id(); id = srs_generate_id();
vhost = NULL; vhost = NULL;
active = false; active = false;
connection_cid = -1;
has_video = false; has_video = false;
vcodec = SrsVideoCodecIdReserved; vcodec = SrsVideoCodecIdReserved;
avc_profile = SrsAvcProfileReserved; avc_profile = SrsAvcProfileReserved;
@ -184,7 +183,7 @@ srs_error_t SrsStatisticStream::dumps(SrsJsonObject* obj)
return err; return err;
} }
void SrsStatisticStream::publish(string cid) void SrsStatisticStream::publish(SrsContextId cid)
{ {
connection_cid = cid; connection_cid = cid;
active = true; active = true;
@ -337,10 +336,10 @@ SrsStatisticStream* SrsStatistic::find_stream(string sid)
return NULL; return NULL;
} }
SrsStatisticClient* SrsStatistic::find_client(string cid) SrsStatisticClient* SrsStatistic::find_client(string client_id)
{ {
std::map<std::string, SrsStatisticClient*>::iterator it; std::map<std::string, SrsStatisticClient*>::iterator it;
if ((it = clients.find(cid)) != clients.end()) { if ((it = clients.find(client_id)) != clients.end()) {
return it->second; return it->second;
} }
return NULL; return NULL;
@ -392,7 +391,7 @@ srs_error_t SrsStatistic::on_video_frames(SrsRequest* req, int nb_frames)
return err; return err;
} }
void SrsStatistic::on_stream_publish(SrsRequest* req, string cid) void SrsStatistic::on_stream_publish(SrsRequest* req, SrsContextId cid)
{ {
SrsStatisticVhost* vhost = create_vhost(req); SrsStatisticVhost* vhost = create_vhost(req);
SrsStatisticStream* stream = create_stream(vhost, req); SrsStatisticStream* stream = create_stream(vhost, req);
@ -423,10 +422,13 @@ void SrsStatistic::on_stream_close(SrsRequest* req)
} }
} }
srs_error_t SrsStatistic::on_client(std::string id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type) srs_error_t SrsStatistic::on_client(SrsContextId cid, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type)
{ {
srs_error_t err = srs_success; srs_error_t err = srs_success;
// TODO: FIXME: We should use UUID for client ID.
std::string id = cid.c_str();
SrsStatisticVhost* vhost = create_vhost(req); SrsStatisticVhost* vhost = create_vhost(req);
SrsStatisticStream* stream = create_stream(vhost, req); SrsStatisticStream* stream = create_stream(vhost, req);
@ -451,8 +453,11 @@ srs_error_t SrsStatistic::on_client(std::string id, SrsRequest* req, SrsConnecti
return err; return err;
} }
void SrsStatistic::on_disconnect(std::string id) void SrsStatistic::on_disconnect(SrsContextId cid)
{ {
// TODO: FIXME: We should use UUID for client ID.
std::string id = cid.c_str();
std::map<std::string, SrsStatisticClient*>::iterator it; std::map<std::string, SrsStatisticClient*>::iterator it;
if ((it = clients.find(id)) == clients.end()) { if ((it = clients.find(id)) == clients.end()) {
return; return;
@ -471,7 +476,8 @@ void SrsStatistic::on_disconnect(std::string id)
void SrsStatistic::kbps_add_delta(SrsConnection* conn) void SrsStatistic::kbps_add_delta(SrsConnection* conn)
{ {
std::string id = conn->srs_id(); // TODO: FIXME: Should not use context id as connection id.
std::string id = conn->srs_id().c_str();
if (clients.find(id) == clients.end()) { if (clients.find(id) == clients.end()) {
return; return;
} }

View file

@ -67,7 +67,7 @@ public:
std::string stream; std::string stream;
std::string url; std::string url;
bool active; bool active;
std::string connection_cid; SrsContextId connection_cid;
int nb_clients; int nb_clients;
uint64_t nb_frames; uint64_t nb_frames;
public: public:
@ -101,7 +101,7 @@ public:
virtual srs_error_t dumps(SrsJsonObject* obj); virtual srs_error_t dumps(SrsJsonObject* obj);
public: public:
// Publish the stream. // Publish the stream.
virtual void publish(std::string cid); virtual void publish(SrsContextId cid);
// Close the stream. // Close the stream.
virtual void close(); virtual void close();
}; };
@ -181,7 +181,7 @@ public:
public: public:
virtual SrsStatisticVhost* find_vhost(std::string vid); virtual SrsStatisticVhost* find_vhost(std::string vid);
virtual SrsStatisticStream* find_stream(std::string sid); virtual SrsStatisticStream* find_stream(std::string sid);
virtual SrsStatisticClient* find_client(std::string cid); virtual SrsStatisticClient* find_client(std::string client_id);
public: public:
// When got video info for stream. // When got video info for stream.
virtual srs_error_t on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile, virtual srs_error_t on_video_info(SrsRequest* req, SrsVideoCodecId vcodec, SrsAvcProfile avc_profile,
@ -195,7 +195,7 @@ public:
// When publish stream. // When publish stream.
// @param req the request object of publish connection. // @param req the request object of publish connection.
// @param cid the cid of publish connection. // @param cid the cid of publish connection.
virtual void on_stream_publish(SrsRequest* req, std::string cid); virtual void on_stream_publish(SrsRequest* req, SrsContextId cid);
// When close stream. // When close stream.
virtual void on_stream_close(SrsRequest* req); virtual void on_stream_close(SrsRequest* req);
public: public:
@ -204,12 +204,14 @@ public:
// @param req, the client request object. // @param req, the client request object.
// @param conn, the physical absract connection object. // @param conn, the physical absract connection object.
// @param type, the type of connection. // @param type, the type of connection.
virtual srs_error_t on_client(std::string id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type); // TODO: FIXME: We should not use context id as client id.
virtual srs_error_t on_client(SrsContextId id, SrsRequest* req, SrsConnection* conn, SrsRtmpConnType type);
// Client disconnect // Client disconnect
// @remark the on_disconnect always call, while the on_client is call when // @remark the on_disconnect always call, while the on_client is call when
// only got the request object, so the client specified by id maybe not // only got the request object, so the client specified by id maybe not
// exists in stat. // exists in stat.
virtual void on_disconnect(std::string id); // TODO: FIXME: We should not use context id as client id.
virtual void on_disconnect(SrsContextId id);
// Sample the kbps, add delta bytes of conn. // Sample the kbps, add delta bytes of conn.
// Use kbps_sample() to get all result of kbps stat. // Use kbps_sample() to get all result of kbps stat.
// TODO: FIXME: the add delta must use ISrsKbpsDelta interface instead. // TODO: FIXME: the add delta must use ISrsKbpsDelta interface instead.

View file

@ -113,4 +113,29 @@
class SrsCplxError; class SrsCplxError;
typedef SrsCplxError* srs_error_t; typedef SrsCplxError* srs_error_t;
// The context ID, it default to a string object, we can also use other objects.
#include <string>
struct _SrsContextId
{
std::string v_;
_SrsContextId() {
}
_SrsContextId(std::string v) {
v_ = v;
}
_SrsContextId(const _SrsContextId& cp) {
v_ = cp.v_;
}
const char* c_str() {
return v_.c_str();
}
bool empty() {
return v_.empty();
}
bool equals(const _SrsContextId& to) {
return v_ == to.v_;
}
};
typedef _SrsContextId SrsContextId;
#endif #endif

View file

@ -57,7 +57,7 @@ SrsCplxError::SrsCplxError()
{ {
code = ERROR_SUCCESS; code = ERROR_SUCCESS;
wrapped = NULL; wrapped = NULL;
cid = rerrno = line = 0; rerrno = line = 0;
} }
SrsCplxError::~SrsCplxError() SrsCplxError::~SrsCplxError()
@ -79,7 +79,7 @@ std::string SrsCplxError::description() {
next = this; next = this;
while (next) { while (next) {
ss << "thread [" << getpid() << "][" << next->cid << "]: " ss << "thread [" << getpid() << "][" << next->cid.c_str() << "]: "
<< next->func << "() [" << next->file << ":" << next->line << "]" << next->func << "() [" << next->file << ":" << next->line << "]"
<< "[errno=" << next->rerrno << "]"; << "[errno=" << next->rerrno << "]";

View file

@ -414,7 +414,7 @@ private:
std::string file; std::string file;
int line; int line;
std::string cid; SrsContextId cid;
int rerrno; int rerrno;
std::string desc; std::string desc;

View file

@ -65,24 +65,24 @@ public:
virtual void reopen() = 0; virtual void reopen() = 0;
public: public:
// The log for verbose, very verbose information. // The log for verbose, very verbose information.
virtual void verbose(const char* tag, const char* context_id, const char* fmt, ...) = 0; virtual void verbose(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
// The log for debug, detail information. // The log for debug, detail information.
virtual void info(const char* tag, const char* context_id, const char* fmt, ...) = 0; virtual void info(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
// The log for trace, important information. // The log for trace, important information.
virtual void trace(const char* tag, const char* context_id, const char* fmt, ...) = 0; virtual void trace(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
// The log for warn, warn is something should take attention, but not a error. // The log for warn, warn is something should take attention, but not a error.
virtual void warn(const char* tag, const char* context_id, const char* fmt, ...) = 0; virtual void warn(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
// The log for error, something error occur, do something about the error, ie. close the connection, // The log for error, something error occur, do something about the error, ie. close the connection,
// but we will donot abort the program. // but we will donot abort the program.
virtual void error(const char* tag, const char* context_id, const char* fmt, ...) = 0; virtual void error(const char* tag, SrsContextId context_id, const char* fmt, ...) = 0;
}; };
// The logic context for a RTMP connection, or RTC Session. // The logic context, for example, a RTMP connection, or RTC Session, etc.
// We can grep the context id to identify the logic unit, for debugging. // We can grep the context id to identify the logic unit, for debugging.
// For example: // For example:
// _srs_context->generate_id(); // Generate a new context. // _srs_context->generate_id(); // Generate a new context id.
// _srs_context->get_id(); // Get current context. // _srs_context->get_id(); // Get current context id.
// int old_id = _srs_context->set_id("1000"); // Change the context. // int old_id = _srs_context->set_id("1000"); // Change the context id.
class ISrsContext class ISrsContext
{ {
public: public:
@ -90,12 +90,12 @@ public:
virtual ~ISrsContext(); virtual ~ISrsContext();
public: public:
// Generate the id for current context. // Generate the id for current context.
virtual std::string generate_id() = 0; virtual SrsContextId generate_id() = 0;
// Get the generated id of current context. // Get the generated id of current context.
virtual std::string get_id() = 0; virtual SrsContextId get_id() = 0;
// Set the id of current context. // Set the id of current context.
// @return the previous id value; 0 if no context. // @return the previous id value; 0 if no context.
virtual std::string set_id(std::string v) = 0; virtual SrsContextId set_id(SrsContextId v) = 0;
}; };
// @global User must provides a log object // @global User must provides a log object
@ -108,25 +108,25 @@ extern ISrsContext* _srs_context;
// Use __FUNCTION__ to print c method // Use __FUNCTION__ to print c method
// Use __PRETTY_FUNCTION__ to print c++ class:method // Use __PRETTY_FUNCTION__ to print c++ class:method
#if 1 #if 1
#define srs_verbose(msg, ...) _srs_log->verbose(NULL, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_verbose(msg, ...) _srs_log->verbose(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info(msg, ...) _srs_log->info(NULL, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_info(msg, ...) _srs_log->info(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace(msg, ...) _srs_log->trace(NULL, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_trace(msg, ...) _srs_log->trace(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn(msg, ...) _srs_log->warn(NULL, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_warn(msg, ...) _srs_log->warn(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error(msg, ...) _srs_log->error(NULL, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_error(msg, ...) _srs_log->error(NULL, _srs_context->get_id(), msg, ##__VA_ARGS__)
#endif #endif
#if 0 #if 0
#define srs_verbose(msg, ...) _srs_log->verbose(__FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_verbose(msg, ...) _srs_log->verbose(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info(msg, ...) _srs_log->info(__FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_info(msg, ...) _srs_log->info(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace(msg, ...) _srs_log->trace(__FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_trace(msg, ...) _srs_log->trace(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn(msg, ...) _srs_log->warn(__FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_warn(msg, ...) _srs_log->warn(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error(msg, ...) _srs_log->error(__FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_error(msg, ...) _srs_log->error(__FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#endif #endif
#if 0 #if 0
#define srs_verbose(msg, ...) _srs_log->verbose(__PRETTY_FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_verbose(msg, ...) _srs_log->verbose(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_info(msg, ...) _srs_log->info(__PRETTY_FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_info(msg, ...) _srs_log->info(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_trace(msg, ...) _srs_log->trace(__PRETTY_FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_trace(msg, ...) _srs_log->trace(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_warn(msg, ...) _srs_log->warn(__PRETTY_FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_warn(msg, ...) _srs_log->warn(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#define srs_error(msg, ...) _srs_log->error(__PRETTY_FUNCTION__, _srs_context->get_id().c_str(), msg, ##__VA_ARGS__) #define srs_error(msg, ...) _srs_log->error(__PRETTY_FUNCTION__, _srs_context->get_id(), msg, ##__VA_ARGS__)
#endif #endif
// TODO: FIXME: Add more verbose and info logs. // TODO: FIXME: Add more verbose and info logs.

View file

@ -42,7 +42,7 @@ SrsThreadContext::~SrsThreadContext()
{ {
} }
string SrsThreadContext::generate_id() SrsContextId SrsThreadContext::generate_id()
{ {
static int id = 0; static int id = 0;
@ -51,22 +51,21 @@ string SrsThreadContext::generate_id()
} }
int gid = id++; int gid = id++;
stringstream ss; SrsContextId cid = SrsContextId(srs_int2str(gid));
ss << gid; cache[srs_thread_self()] = cid;
cache[srs_thread_self()] = ss.str(); return cid;
return ss.str();
} }
string SrsThreadContext::get_id() SrsContextId SrsThreadContext::get_id()
{ {
return cache[srs_thread_self()]; return cache[srs_thread_self()];
} }
string SrsThreadContext::set_id(string v) SrsContextId SrsThreadContext::set_id(SrsContextId v)
{ {
srs_thread_t self = srs_thread_self(); srs_thread_t self = srs_thread_self();
string ov; SrsContextId ov;
if (cache.find(self) != cache.end()) { if (cache.find(self) != cache.end()) {
ov = cache[self]; ov = cache[self];
} }
@ -79,7 +78,7 @@ string SrsThreadContext::set_id(string v)
void SrsThreadContext::clear_cid() void SrsThreadContext::clear_cid()
{ {
srs_thread_t self = srs_thread_self(); srs_thread_t self = srs_thread_self();
std::map<srs_thread_t, string>::iterator it = cache.find(self); std::map<srs_thread_t, SrsContextId>::iterator it = cache.find(self);
if (it != cache.end()) { if (it != cache.end()) {
cache.erase(it); cache.erase(it);
} }
@ -108,7 +107,7 @@ void SrsConsoleLog::reopen()
{ {
} }
void SrsConsoleLog::verbose(const char* tag, const char* context_id, const char* fmt, ...) void SrsConsoleLog::verbose(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelVerbose) { if (level > SrsLogLevelVerbose) {
return; return;
@ -128,7 +127,7 @@ void SrsConsoleLog::verbose(const char* tag, const char* context_id, const char*
fprintf(stdout, "%s\n", buffer); fprintf(stdout, "%s\n", buffer);
} }
void SrsConsoleLog::info(const char* tag, const char* context_id, const char* fmt, ...) void SrsConsoleLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelInfo) { if (level > SrsLogLevelInfo) {
return; return;
@ -148,7 +147,7 @@ void SrsConsoleLog::info(const char* tag, const char* context_id, const char* fm
fprintf(stdout, "%s\n", buffer); fprintf(stdout, "%s\n", buffer);
} }
void SrsConsoleLog::trace(const char* tag, const char* context_id, const char* fmt, ...) void SrsConsoleLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelTrace) { if (level > SrsLogLevelTrace) {
return; return;
@ -168,7 +167,7 @@ void SrsConsoleLog::trace(const char* tag, const char* context_id, const char* f
fprintf(stdout, "%s\n", buffer); fprintf(stdout, "%s\n", buffer);
} }
void SrsConsoleLog::warn(const char* tag, const char* context_id, const char* fmt, ...) void SrsConsoleLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelWarn) { if (level > SrsLogLevelWarn) {
return; return;
@ -188,7 +187,7 @@ void SrsConsoleLog::warn(const char* tag, const char* context_id, const char* fm
fprintf(stderr, "%s\n", buffer); fprintf(stderr, "%s\n", buffer);
} }
void SrsConsoleLog::error(const char* tag, const char* context_id, const char* fmt, ...) void SrsConsoleLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...)
{ {
if (level > SrsLogLevelError) { if (level > SrsLogLevelError) {
return; return;
@ -214,7 +213,7 @@ void SrsConsoleLog::error(const char* tag, const char* context_id, const char* f
} }
// LCOV_EXCL_STOP // LCOV_EXCL_STOP
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, const char* cid, const char* level, int* psize) bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, SrsContextId cid, const char* level, int* psize)
{ {
// clock time // clock time
timeval tv; timeval tv;
@ -240,24 +239,24 @@ bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char
written = snprintf(buffer, size, written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%s][%d] ", "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%s][%d] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000), 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, tag, getpid(), cid, errno); level, tag, getpid(), cid.c_str(), errno);
} else { } else {
written = snprintf(buffer, size, written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d] ", "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s][%d] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000), 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, getpid(), cid, errno); level, getpid(), cid.c_str(), errno);
} }
} else { } else {
if (tag) { if (tag) {
written = snprintf(buffer, size, written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%s] ", "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%s][%d][%s] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000), 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, tag, getpid(), cid); level, tag, getpid(), cid.c_str());
} else { } else {
written = snprintf(buffer, size, written = snprintf(buffer, size,
"[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s] ", "[%d-%02d-%02d %02d:%02d:%02d.%03d][%s][%d][%s] ",
1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000), 1900 + tm->tm_year, 1 + tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)(tv.tv_usec / 1000),
level, getpid(), cid); level, getpid(), cid.c_str());
} }
} }

View file

@ -37,14 +37,14 @@
class SrsThreadContext : public ISrsContext class SrsThreadContext : public ISrsContext
{ {
private: private:
std::map<srs_thread_t, std::string> cache; std::map<srs_thread_t, SrsContextId> cache;
public: public:
SrsThreadContext(); SrsThreadContext();
virtual ~SrsThreadContext(); virtual ~SrsThreadContext();
public: public:
virtual std::string generate_id(); virtual SrsContextId generate_id();
virtual std::string get_id(); virtual SrsContextId get_id();
virtual std::string set_id(std::string v); virtual SrsContextId set_id(SrsContextId v);
public: public:
virtual void clear_cid(); virtual void clear_cid();
}; };
@ -64,11 +64,11 @@ public:
public: public:
virtual srs_error_t initialize(); virtual srs_error_t initialize();
virtual void reopen(); virtual void reopen();
virtual void verbose(const char* tag, const char* context_id, const char* fmt, ...); virtual void verbose(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void info(const char* tag, const char* context_id, const char* fmt, ...); virtual void info(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void trace(const char* tag, const char* context_id, const char* fmt, ...); virtual void trace(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void warn(const char* tag, const char* context_id, const char* fmt, ...); virtual void warn(const char* tag, SrsContextId context_id, const char* fmt, ...);
virtual void error(const char* tag, const char* context_id, const char* fmt, ...); virtual void error(const char* tag, SrsContextId context_id, const char* fmt, ...);
}; };
// Generate the log header. // Generate the log header.
@ -76,6 +76,6 @@ public:
// @param utc Whether use UTC time format in the log header. // @param utc Whether use UTC time format in the log header.
// @param psize Output the actual header size. // @param psize Output the actual header size.
// @remark It's a internal API. // @remark It's a internal API.
bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, const char* cid, const char* level, int* psize); bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char* tag, SrsContextId cid, const char* level, int* psize);
#endif #endif

View file

@ -71,7 +71,7 @@ srs_error_t srs_st_init()
} }
// Before ST init, we might have already inited the background cid. // Before ST init, we might have already inited the background cid.
string cid = _srs_context->get_id(); SrsContextId cid = _srs_context->get_id();
if (cid.empty()) { if (cid.empty()) {
cid = _srs_context->generate_id(); cid = _srs_context->generate_id();
} }

View file

@ -36,7 +36,7 @@ VOID TEST(AppCoroutineTest, Dummy)
SrsDummyCoroutine dc; SrsDummyCoroutine dc;
if (true) { if (true) {
EXPECT_EQ("", dc.cid()); EXPECT_TRUE(dc.cid().empty());
srs_error_t err = dc.pull(); srs_error_t err = dc.pull();
EXPECT_TRUE(err != srs_success); EXPECT_TRUE(err != srs_success);
@ -52,7 +52,7 @@ VOID TEST(AppCoroutineTest, Dummy)
if (true) { if (true) {
dc.stop(); dc.stop();
EXPECT_EQ("", dc.cid()); EXPECT_TRUE(dc.cid().empty());
srs_error_t err = dc.pull(); srs_error_t err = dc.pull();
EXPECT_TRUE(err != srs_success); EXPECT_TRUE(err != srs_success);
@ -68,7 +68,7 @@ VOID TEST(AppCoroutineTest, Dummy)
if (true) { if (true) {
dc.interrupt(); dc.interrupt();
EXPECT_EQ("", dc.cid()); EXPECT_TRUE(dc.cid().empty());
srs_error_t err = dc.pull(); srs_error_t err = dc.pull();
EXPECT_TRUE(err != srs_success); EXPECT_TRUE(err != srs_success);
@ -88,7 +88,7 @@ public:
srs_error_t err; srs_error_t err;
srs_cond_t running; srs_cond_t running;
srs_cond_t exited; srs_cond_t exited;
std::string cid; SrsContextId cid;
// Quit without error. // Quit without error.
bool quit; bool quit;
public: public:
@ -128,12 +128,12 @@ VOID TEST(AppCoroutineTest, StartStop)
MockCoroutineHandler ch; MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch); SrsSTCoroutine sc("test", &ch);
ch.trd = &sc; ch.trd = &sc;
EXPECT_EQ("", sc.cid()); EXPECT_TRUE(sc.cid().empty());
// Thread stop after created. // Thread stop after created.
sc.stop(); sc.stop();
EXPECT_EQ("", sc.cid()); EXPECT_TRUE(sc.cid().empty());
srs_error_t err = sc.pull(); srs_error_t err = sc.pull();
EXPECT_TRUE(srs_success != err); EXPECT_TRUE(srs_success != err);
@ -151,7 +151,7 @@ VOID TEST(AppCoroutineTest, StartStop)
MockCoroutineHandler ch; MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch); SrsSTCoroutine sc("test", &ch);
ch.trd = &sc; ch.trd = &sc;
EXPECT_EQ("", sc.cid()); EXPECT_TRUE(sc.cid().empty());
EXPECT_TRUE(srs_success == sc.start()); EXPECT_TRUE(srs_success == sc.start());
EXPECT_TRUE(srs_success == sc.pull()); EXPECT_TRUE(srs_success == sc.pull());
@ -178,7 +178,7 @@ VOID TEST(AppCoroutineTest, StartStop)
MockCoroutineHandler ch; MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch); SrsSTCoroutine sc("test", &ch);
ch.trd = &sc; ch.trd = &sc;
EXPECT_EQ("", sc.cid()); EXPECT_TRUE(sc.cid().empty());
EXPECT_TRUE(srs_success == sc.start()); EXPECT_TRUE(srs_success == sc.start());
EXPECT_TRUE(srs_success == sc.pull()); EXPECT_TRUE(srs_success == sc.pull());
@ -220,16 +220,16 @@ VOID TEST(AppCoroutineTest, Cycle)
if (true) { if (true) {
MockCoroutineHandler ch; MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch, "250"); SrsSTCoroutine sc("test", &ch, SrsContextId("250"));
ch.trd = &sc; ch.trd = &sc;
EXPECT_TRUE("250" == sc.cid()); EXPECT_TRUE(sc.cid().equals(SrsContextId("250")));
EXPECT_TRUE(srs_success == sc.start()); EXPECT_TRUE(srs_success == sc.start());
EXPECT_TRUE(srs_success == sc.pull()); EXPECT_TRUE(srs_success == sc.pull());
// After running, the cid in cycle should equal to the thread. // After running, the cid in cycle should equal to the thread.
srs_cond_timedwait(ch.running, 100 * SRS_UTIME_MILLISECONDS); srs_cond_timedwait(ch.running, 100 * SRS_UTIME_MILLISECONDS);
EXPECT_TRUE("250" == ch.cid); EXPECT_TRUE(ch.cid.equals(SrsContextId("250")));
} }
if (true) { if (true) {

View file

@ -898,7 +898,7 @@ class MockOnCycleThread : public ISrsCoroutineHandler
public: public:
SrsSTCoroutine trd; SrsSTCoroutine trd;
srs_cond_t cond; srs_cond_t cond;
MockOnCycleThread() : trd("mock", this, "0") { MockOnCycleThread() : trd("mock", this) {
cond = srs_cond_new(); cond = srs_cond_new();
}; };
virtual ~MockOnCycleThread() { virtual ~MockOnCycleThread() {
@ -938,7 +938,7 @@ class MockOnCycleThread2 : public ISrsCoroutineHandler
public: public:
SrsSTCoroutine trd; SrsSTCoroutine trd;
srs_mutex_t lock; srs_mutex_t lock;
MockOnCycleThread2() : trd("mock", this, "0") { MockOnCycleThread2() : trd("mock", this) {
lock = srs_mutex_new(); lock = srs_mutex_new();
}; };
virtual ~MockOnCycleThread2() { virtual ~MockOnCycleThread2() {
@ -979,7 +979,7 @@ class MockOnCycleThread3 : public ISrsCoroutineHandler
public: public:
SrsSTCoroutine trd; SrsSTCoroutine trd;
srs_netfd_t fd; srs_netfd_t fd;
MockOnCycleThread3() : trd("mock", this, "0") { MockOnCycleThread3() : trd("mock", this) {
fd = NULL; fd = NULL;
}; };
virtual ~MockOnCycleThread3() { virtual ~MockOnCycleThread3() {
@ -1230,7 +1230,7 @@ class MockOnCycleThread4 : public ISrsCoroutineHandler
public: public:
SrsSTCoroutine trd; SrsSTCoroutine trd;
srs_netfd_t fd; srs_netfd_t fd;
MockOnCycleThread4() : trd("mock", this, "0") { MockOnCycleThread4() : trd("mock", this) {
fd = NULL; fd = NULL;
}; };
virtual ~MockOnCycleThread4() { virtual ~MockOnCycleThread4() {
@ -1382,19 +1382,19 @@ VOID TEST(TCPServerTest, ContextUtility)
if (true) { if (true) {
SrsThreadContext ctx; SrsThreadContext ctx;
EXPECT_TRUE("" == ctx.set_id("100")); EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty());
EXPECT_TRUE("100" == ctx.set_id("1000")); EXPECT_TRUE(ctx.set_id(SrsContextId("1000")).equals(SrsContextId("100")));
EXPECT_TRUE("1000" == ctx.get_id()); EXPECT_TRUE(ctx.get_id().equals(SrsContextId("1000")));
ctx.clear_cid(); ctx.clear_cid();
EXPECT_TRUE("" == ctx.set_id("100")); EXPECT_TRUE(ctx.set_id(SrsContextId("100")).empty());
} }
int base_size = 0; int base_size = 0;
if (true) { if (true) {
errno = 0; errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0); int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, true, true, "SRS", "100", "Trace", &size)); ASSERT_TRUE(srs_log_header(buf, 1024, true, true, "SRS", SrsContextId("100"), "Trace", &size));
base_size = size; base_size = size;
EXPECT_TRUE(base_size > 0); EXPECT_TRUE(base_size > 0);
} }
@ -1402,21 +1402,21 @@ VOID TEST(TCPServerTest, ContextUtility)
if (true) { if (true) {
errno = 0; errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0); int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, false, true, "SRS", "100", "Trace", &size)); ASSERT_TRUE(srs_log_header(buf, 1024, false, true, "SRS", SrsContextId("100"), "Trace", &size));
EXPECT_EQ(base_size, size); EXPECT_EQ(base_size, size);
} }
if (true) { if (true) {
errno = 0; errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0); int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, false, true, NULL, "100", "Trace", &size)); ASSERT_TRUE(srs_log_header(buf, 1024, false, true, NULL, SrsContextId("100"), "Trace", &size));
EXPECT_EQ(base_size - 5, size); EXPECT_EQ(base_size - 5, size);
} }
if (true) { if (true) {
errno = 0; errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0); int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, false, false, NULL, "100", "Trace", &size)); ASSERT_TRUE(srs_log_header(buf, 1024, false, false, NULL, SrsContextId("100"), "Trace", &size));
EXPECT_EQ(base_size - 8, size); EXPECT_EQ(base_size - 8, size);
} }