Skip to content

Commit

Permalink
Remove vhost in query if not present it
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Dec 27, 2020
1 parent 6e43ef6 commit 831c780
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
11 changes: 8 additions & 3 deletions trunk/src/app/srs_app_edge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,12 @@ srs_error_t SrsEdgeRtmpUpstream::connect(SrsRequest* r, SrsLbRoundRobin* lb)

// For RTMP client, we pass the vhost in tcUrl when connecting,
// so we publish without vhost in stream.
if ((err = sdk->play(_srs_config->get_chunk_size(req->vhost), false)) != srs_success) {
string stream;
if ((err = sdk->play(_srs_config->get_chunk_size(req->vhost), false, &stream)) != srs_success) {
return srs_error_wrap(err, "edge pull %s stream failed", url.c_str());
}

srs_trace("edge-pull publish url %s, stream=%s%s as %s", url.c_str(), req->stream.c_str(), req->param.c_str(), stream.c_str());

return err;
}
Expand Down Expand Up @@ -509,7 +512,8 @@ srs_error_t SrsEdgeForwarder::start()

// For RTMP client, we pass the vhost in tcUrl when connecting,
// so we publish without vhost in stream.
if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false)) != srs_success) {
string stream;
if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false, &stream)) != srs_success) {
return srs_error_wrap(err, "sdk publish");
}

Expand All @@ -519,7 +523,8 @@ srs_error_t SrsEdgeForwarder::start()
if ((err = trd->start()) != srs_success) {
return srs_error_wrap(err, "coroutine");
}
srs_trace("edge-fwr publish url %s, stream=%s%s", url.c_str(), req->stream.c_str(), req->param.c_str());

srs_trace("edge-fwr publish url %s, stream=%s%s as %s", url.c_str(), req->stream.c_str(), req->param.c_str(), stream.c_str());

return err;
}
Expand Down
5 changes: 4 additions & 1 deletion trunk/src/app/srs_app_forward.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ srs_error_t SrsForwarder::do_cycle()

// For RTMP client, we pass the vhost in tcUrl when connecting,
// so we publish without vhost in stream.
if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false)) != srs_success) {
string stream;
if ((err = sdk->publish(_srs_config->get_chunk_size(req->vhost), false, &stream)) != srs_success) {
return srs_error_wrap(err, "sdk publish");
}

Expand All @@ -236,6 +237,8 @@ srs_error_t SrsForwarder::do_cycle()
if ((err = forward()) != srs_success) {
return srs_error_wrap(err, "forward");
}

srs_trace("forward publish url %s, stream=%s%s as %s", url.c_str(), req->stream.c_str(), req->param.c_str(), stream.c_str());

return err;
}
Expand Down
39 changes: 27 additions & 12 deletions trunk/src/protocol/srs_protocol_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,35 @@ string srs_generate_stream_with_query(string host, string vhost, string stream,
string url = stream;
string query = param;

if (with_vhost) {
// If no vhost in param, try to append one.
string guessVhost;
if (query.find("vhost=") == string::npos) {
if (vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) {
guessVhost = vhost;
} else if (!srs_is_ipv4(host)) {
guessVhost = host;
}
// If no vhost in param, try to append one.
string guessVhost;
if (query.find("vhost=") == string::npos) {
if (vhost != SRS_CONSTS_RTMP_DEFAULT_VHOST) {
guessVhost = vhost;
} else if (!srs_is_ipv4(host)) {
guessVhost = host;
}
}

// Well, if vhost exists, always append in query string.
if (!guessVhost.empty() && query.find("vhost=") == string::npos) {
query += "&vhost=" + guessVhost;
}

// If not pass in query, remove it.
if (!with_vhost) {
size_t pos = query.find("&vhost=");
if (pos == string::npos) {
pos = query.find("vhost=");
}

size_t end = query.find("&", pos + 1);
if (end == string::npos) {
end = query.length();
}

// Well, if vhost exists, always append in query string.
if (!guessVhost.empty()) {
query += "&vhost=" + guessVhost;
if (pos != string::npos && end != string::npos && end > pos) {
query = query.substr(0, pos) + query.substr(end);
}
}

Expand Down
14 changes: 12 additions & 2 deletions trunk/src/service/srs_service_rtmp_conn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ srs_error_t SrsBasicRtmpClient::do_connect_app(string local_ip, bool debug)
return err;
}

srs_error_t SrsBasicRtmpClient::publish(int chunk_size, bool with_vhost)
srs_error_t SrsBasicRtmpClient::publish(int chunk_size, bool with_vhost, std::string* pstream)
{
srs_error_t err = srs_success;

// Pass params in stream, @see https://github.com/ossrs/srs/issues/1031#issuecomment-409745733
string stream = srs_generate_stream_with_query(req->host, req->vhost, req->stream, req->param, with_vhost);

// Return the generated stream.
if (pstream) {
*pstream = stream;
}

// publish.
if ((err = client->publish(stream, stream_id, chunk_size)) != srs_success) {
Expand All @@ -162,12 +167,17 @@ srs_error_t SrsBasicRtmpClient::publish(int chunk_size, bool with_vhost)
return err;
}

srs_error_t SrsBasicRtmpClient::play(int chunk_size, bool with_vhost)
srs_error_t SrsBasicRtmpClient::play(int chunk_size, bool with_vhost, std::string* pstream)
{
srs_error_t err = srs_success;

// Pass params in stream, @see https://github.com/ossrs/srs/issues/1031#issuecomment-409745733
string stream = srs_generate_stream_with_query(req->host, req->vhost, req->stream, req->param, with_vhost);

// Return the generated stream.
if (pstream) {
*pstream = stream;
}

if ((err = client->play(stream, stream_id, chunk_size)) != srs_success) {
return srs_error_wrap(err, "connect with server failed, stream=%s, stream_id=%d", stream.c_str(), stream_id);
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/service/srs_service_rtmp_conn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class SrsBasicRtmpClient
virtual srs_error_t connect_app();
virtual srs_error_t do_connect_app(std::string local_ip, bool debug);
public:
virtual srs_error_t publish(int chunk_size, bool with_vhost = true);
virtual srs_error_t play(int chunk_size, bool with_vhost = true);
virtual srs_error_t publish(int chunk_size, bool with_vhost = true, std::string* pstream = NULL);
virtual srs_error_t play(int chunk_size, bool with_vhost = true, std::string* pstream = NULL);
virtual void kbps_sample(const char* label, int64_t age);
virtual void kbps_sample(const char* label, int64_t age, int msgs);
virtual int sid();
Expand Down

0 comments on commit 831c780

Please # to comment.