Skip to content

Commit edce854

Browse files
author
Haibo Chen
committed
make code readable
1 parent 48d55ce commit edce854

File tree

5 files changed

+63
-48
lines changed

5 files changed

+63
-48
lines changed

trunk/src/app/srs_app_http_static.cpp

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ using namespace std;
3737
#include <srs_app_http_hooks.hpp>
3838
#include <srs_app_statistic.hpp>
3939

40-
#define SRS_SECRET_IN_HLS "srs_secret"
40+
#define SRS_CONTEXT_IN_HLS "hls_ctx"
4141

4242
SrsVodStream::SrsVodStream(string root_dir) : SrsHttpFileServer(root_dir)
4343
{
@@ -47,12 +47,11 @@ SrsVodStream::SrsVodStream(string root_dir) : SrsHttpFileServer(root_dir)
4747
SrsVodStream::~SrsVodStream()
4848
{
4949
_srs_hybrid->timer5s()->unsubscribe(this);
50-
std::map<std::string, SrsRequest*>::iterator it;
51-
for (it = map_secret_req_.begin(); it != map_secret_req_.end(); ++it) {
52-
srs_freep(it->second);
50+
std::map<std::string, SrsM3u8CtxInfo>::iterator it;
51+
for (it = map_ctx_info_.begin(); it != map_ctx_info_.end(); ++it) {
52+
srs_freep(it->second.req);
5353
}
54-
map_secret_req_.clear();
55-
map_secret_validity_.clear();
54+
map_ctx_info_.clear();
5655
}
5756

5857
srs_error_t SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int offset)
@@ -186,7 +185,7 @@ srs_error_t SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMe
186185
return err;
187186
}
188187

189-
srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
188+
srs_error_t SrsVodStream::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
190189
{
191190
srs_error_t err = srs_success;
192191

@@ -196,27 +195,33 @@ srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttp
196195
SrsRequest* req = hr->to_request(hr->host())->as_http();
197196
SrsAutoFree(SrsRequest, req);
198197

199-
string secret = r->query_get(SRS_SECRET_IN_HLS);
200-
if (!secret.empty() && secret_is_exist(secret)) {
201-
alive(secret);
202-
return SrsHttpFileServer::serve_m3u8_secret(w, r, fullpath);
198+
string ctx = hr->query_get(SRS_CONTEXT_IN_HLS);
199+
if (!ctx.empty() && ctx_is_exist(ctx)) {
200+
alive(ctx, NULL);
201+
return SrsHttpFileServer::serve_m3u8_ctx(w, r, fullpath);
203202
}
204203

205204
if ((err = http_hooks_on_play(req)) != srs_success) {
206205
return srs_error_wrap(err, "HLS: http_hooks_on_play");
207206
}
208207

209-
if (secret.empty()) {
208+
if (ctx.empty()) {
210209
// make sure unique
211210
do {
212-
secret = srs_random_str(8);
213-
} while (secret_is_exist(secret));
211+
ctx = srs_random_str(8); // the same as cid
212+
} while (ctx_is_exist(ctx));
214213
}
215214

216-
std::string res = "#EXTM3U\r";
217-
res += "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1\r";
218-
res += hr->path() + "?" + SRS_SECRET_IN_HLS + "=" + secret;
215+
std::stringstream ss;
216+
ss << "#EXTM3U" << SRS_CONSTS_LF;
217+
ss << "#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1" << SRS_CONSTS_LF;
218+
ss << hr->path() << "?" << SRS_CONTEXT_IN_HLS << "=" << ctx;
219+
if (!hr->query().empty() && !srs_string_contains(hr->query(), "hls_ctx"))
220+
{
221+
ss << "&" << hr->query();
222+
}
219223

224+
std::string res = ss.str();
220225
int length = res.length();
221226

222227
w->header()->set_content_length(length);
@@ -233,25 +238,31 @@ srs_error_t SrsVodStream::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttp
233238

234239
// update the statistic when source disconveried.
235240
SrsStatistic* stat = SrsStatistic::instance();
236-
if ((err = stat->on_client(secret, req, NULL, SrsRtmpConnPlay)) != srs_success) {
241+
if ((err = stat->on_client(ctx, req, NULL, SrsRtmpConnPlay)) != srs_success) {
237242
return srs_error_wrap(err, "stat on client");
238243
}
239244

240-
// save req for on_disconnect when timeout
241-
map_secret_req_.insert(make_pair(secret, req->copy()));
242-
alive(secret);
245+
alive(ctx, req->copy());
243246

244247
return err;
245248
}
246249

247-
bool SrsVodStream::secret_is_exist(std::string secret)
250+
bool SrsVodStream::ctx_is_exist(std::string secret)
248251
{
249-
return (map_secret_validity_.find(secret) != map_secret_validity_.end());
252+
return (map_ctx_info_.find(secret) != map_ctx_info_.end());
250253
}
251254

252-
void SrsVodStream::alive(std::string secret)
255+
void SrsVodStream::alive(std::string secret, SrsRequest* req)
253256
{
254-
map_secret_validity_[secret] = srs_get_system_time();
257+
std::map<std::string, SrsM3u8CtxInfo>::iterator it;
258+
if ((it = map_ctx_info_.find(secret)) != map_ctx_info_.end()) {
259+
it->second.request_time = srs_get_system_time();
260+
} else {
261+
SrsM3u8CtxInfo info;
262+
info.req = req;
263+
info.request_time = srs_get_system_time();
264+
map_ctx_info_.insert(make_pair(secret, info));
265+
}
255266
}
256267

257268
srs_error_t SrsVodStream::http_hooks_on_play(SrsRequest* req)
@@ -321,19 +332,18 @@ srs_error_t SrsVodStream::on_timer(srs_utime_t interval)
321332
{
322333
srs_error_t err = srs_success;
323334

324-
std::map<std::string, srs_utime_t>::iterator it;
325-
for (it = map_secret_validity_.begin(); it != map_secret_validity_.end(); ++it) {
335+
std::map<std::string, SrsM3u8CtxInfo>::iterator it;
336+
for (it = map_ctx_info_.begin(); it != map_ctx_info_.end(); ++it) {
326337
string secret = it->first;
327-
SrsRequest* req = map_secret_req_[secret];
338+
SrsRequest* req = it->second.req;
328339
srs_utime_t hls_window = _srs_config->get_hls_window(req->vhost);
329-
if (it->second + (2 * hls_window) < srs_get_system_time()) {
340+
if (it->second.request_time + (2 * hls_window) < srs_get_system_time()) {
330341
http_hooks_on_stop(req);
331342
srs_freep(req);
332-
map_secret_req_.erase(secret);
333343

334344
SrsStatistic* stat = SrsStatistic::instance();
335345
stat->on_disconnect(secret);
336-
map_secret_validity_.erase(it);
346+
map_ctx_info_.erase(it);
337347

338348
break;
339349
}

trunk/src/app/srs_app_http_static.hpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
#include <srs_app_http_conn.hpp>
1313

14+
struct SrsM3u8CtxInfo
15+
{
16+
srs_utime_t request_time;
17+
SrsRequest* req;
18+
};
19+
1420
// The flv vod stream supports flv?start=offset-bytes.
1521
// For example, http://server/file.flv?start=10240
1622
// server will write flv header and sequence header,
@@ -19,18 +25,17 @@ class SrsVodStream : public SrsHttpFileServer, public ISrsFastTimer
1925
{
2026
private:
2127
// The period of validity of the secret
22-
std::map<std::string, srs_utime_t> map_secret_validity_;
23-
std::map<std::string, SrsRequest*> map_secret_req_;
28+
std::map<std::string, SrsM3u8CtxInfo> map_ctx_info_;
2429
public:
2530
SrsVodStream(std::string root_dir);
2631
virtual ~SrsVodStream();
2732
protected:
2833
virtual srs_error_t serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int offset);
2934
virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end);
30-
virtual srs_error_t serve_m3u8_secret(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath);
35+
virtual srs_error_t serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath);
3136
private:
32-
virtual bool secret_is_exist(std::string secret);
33-
virtual void alive(std::string secret);
37+
virtual bool ctx_is_exist(std::string secret);
38+
virtual void alive(std::string secret, SrsRequest* req);
3439
virtual srs_error_t http_hooks_on_play(SrsRequest* req);
3540
virtual void http_hooks_on_stop(SrsRequest* req);
3641
// interface ISrsFastTimer

trunk/src/protocol/srs_http_stack.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ srs_error_t SrsHttpFileServer::serve_mp4_file(ISrsHttpResponseWriter* w, ISrsHtt
528528

529529
srs_error_t SrsHttpFileServer::serve_m3u8_file(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
530530
{
531-
return serve_m3u8_secret(w, r, fullpath);
531+
return serve_m3u8_ctx(w, r, fullpath);
532532
}
533533

534534
srs_error_t SrsHttpFileServer::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, string fullpath, int offset)
@@ -545,7 +545,7 @@ srs_error_t SrsHttpFileServer::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsH
545545
return serve_file(w, r, fullpath);
546546
}
547547

548-
srs_error_t SrsHttpFileServer::serve_m3u8_secret(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
548+
srs_error_t SrsHttpFileServer::serve_m3u8_ctx(ISrsHttpResponseWriter * w, ISrsHttpMessage * r, std::string fullpath)
549549
{
550550
// @remark For common http file server, we don't support stream request, please use SrsVodStream instead.
551551
// TODO: FIXME: Support range in header https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Range_requests

trunk/src/protocol/srs_http_stack.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,14 @@ class SrsHttpFileServer : public ISrsHttpHandler
296296
virtual srs_error_t serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath, int start, int end);
297297
// For HLS protocol.
298298
// When the request url, like as "http://127.0.0.1:8080/live/livestream.m3u8",
299-
// returns the response like as "http://127.0.0.1:8080/live/livestream.m3u8?srs_secret=12345678" .
300-
// SRS use "srs_secret" to keep track of subsequent requests that is short-connection.
299+
// returns the response like as "http://127.0.0.1:8080/live/livestream.m3u8?hls_ctx=12345678" .
300+
// SRS use "hls_ctx" to keep track of subsequent requests that is short-connection.
301301
// Remark 1:
302-
// Fill the parameter "srs_secret" by yourself in the first request is allowed, SRS will use it.
302+
// Fill the parameter "hls_ctx" by yourself in the first request is allowed, SRS will use it.
303303
// And MUST make sure it is unique.
304304
// Remark 2:
305-
// If use two same "srs_secret" in different requests, SRS cannot detect so that they will be treated as one.
306-
virtual srs_error_t serve_m3u8_secret(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath);
305+
// If use two same "hls_ctx" in different requests, SRS cannot detect so that they will be treated as one.
306+
virtual srs_error_t serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, std::string fullpath);
307307
protected:
308308
// Copy the fs to response writer in size bytes.
309309
virtual srs_error_t copy(ISrsHttpResponseWriter* w, SrsFileReader* fs, ISrsHttpMessage* r, int size);

trunk/src/utest/srs_utest_http.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
11891189
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
11901190
}
11911191

1192-
// should return "srs_secret"
1192+
// should return "hls_ctx"
11931193
if (true) {
11941194
SrsHttpMuxEntry e;
11951195
e.pattern = "/";
@@ -1204,10 +1204,10 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
12041204
HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8", false));
12051205

12061206
HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r));
1207-
__MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?srs_secret=", w);
1207+
__MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=", w);
12081208
}
12091209

1210-
// should return "srs_secret"
1210+
// should return "hls_ctx"
12111211
if (true) {
12121212
SrsHttpMuxEntry e;
12131213
e.pattern = "/";
@@ -1219,10 +1219,10 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
12191219

12201220
MockResponseWriter w;
12211221
SrsHttpMessage r(NULL, NULL);
1222-
HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?srs_secret=123456", false));
1222+
HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?hls_ctx=123456", false));
12231223

12241224
HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r));
1225-
__MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?srs_secret=123456", w);
1225+
__MOCK_HTTP_EXPECT_STRCT(200, "index.m3u8?hls_ctx=123456", w);
12261226
}
12271227
}
12281228

0 commit comments

Comments
 (0)