Skip to content

Commit

Permalink
API: Fix duplicated on_stop callback event bug. (#3349)
Browse files Browse the repository at this point in the history
* fix hls bug:Duplicated on_stop callback

* improve utest

* Refine magic number.

* API: Fix duplicated on_stop callback event bug. v5.0.125

Co-authored-by: winlin <winlin@vip.126.com>
  • Loading branch information
duiniuluantanqin and winlinvip committed Jan 1, 2023
1 parent 6caca90 commit 3727d05
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
1 change: 1 addition & 0 deletions trunk/doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The changelog for SRS.

## SRS 5.0 Changelog

* v5.0, 2023-01-01, For [#3349](https://github.com/ossrs/srs/issues/3349): API: Fix duplicated on_stop callback event bug. v5.0.125
* v5.0, 2022-12-31, GB28181: Enable regression test for gb28181. v5.0.122
* v5.0, 2022-12-31, Refine configure to guess OS automatically. v5.0.121
* v5.0, 2022-12-31, Refine default config file for SRS. v5.0.120
Expand Down
19 changes: 11 additions & 8 deletions trunk/src/app/srs_app_http_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ SrsHlsStream::~SrsHlsStream()

srs_error_t SrsHlsStream::serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, ISrsFileReaderFactory* factory, string fullpath, SrsRequest* req, bool* served)
{
srs_error_t err = srs_success;

string ctx = r->query_get(SRS_CONTEXT_IN_HLS);

// If HLS stream is disabled, use SrsHttpFileServer to serve HLS, which is normal file server.
Expand All @@ -82,9 +84,6 @@ srs_error_t SrsHlsStream::serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMess
// @remark Be careful that the stream has extension now, might cause identify fail.
req->stream = srs_path_basename(r->path());

// Always make the ctx alive now.
alive(ctx, req);

// Served by us.
*served = true;

Expand All @@ -95,11 +94,16 @@ srs_error_t SrsHlsStream::serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMess
*served = false;
return srs_success;
}
return serve_exists_session(w, r, factory, fullpath);
err = serve_exists_session(w, r, factory, fullpath);
} else {
// Create a m3u8 in memory, contains the session id(ctx).
err = serve_new_session(w, r, req, ctx);
}

// Create a m3u8 in memory, contains the session id(ctx).
return serve_new_session(w, r, req);
// Always make the ctx alive now.
alive(ctx, req);

return err;
}

void SrsHlsStream::on_serve_ts_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
Expand All @@ -124,14 +128,13 @@ void SrsHlsStream::on_serve_ts_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
SrsStatistic::instance()->kbps_add_delta(ctx, delta);
}

srs_error_t SrsHlsStream::serve_new_session(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, SrsRequest* req)
srs_error_t SrsHlsStream::serve_new_session(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, SrsRequest* req, std::string& ctx)
{
srs_error_t err = srs_success;

SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
srs_assert(hr);

string ctx;
if (ctx.empty()) {
// make sure unique
do {
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/app/srs_app_http_static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SrsHlsStream : public ISrsFastTimer
virtual srs_error_t serve_m3u8_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, ISrsFileReaderFactory* factory, std::string fullpath, SrsRequest* req, bool* served);
virtual void on_serve_ts_ctx(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
private:
srs_error_t serve_new_session(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, SrsRequest *req);
srs_error_t serve_new_session(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, SrsRequest *req, std::string& ctx);
srs_error_t serve_exists_session(ISrsHttpResponseWriter* w, ISrsHttpMessage* r, ISrsFileReaderFactory* factory, std::string fullpath);
bool ctx_is_exist(std::string ctx);
void alive(std::string ctx, SrsRequest* req);
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/core/srs_core_version5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@

#define VERSION_MAJOR 5
#define VERSION_MINOR 0
#define VERSION_REVISION 124
#define VERSION_REVISION 125

#endif
24 changes: 22 additions & 2 deletions trunk/src/utest/srs_utest_http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ string mock_http_response3(int status, string content)
return ss.str();
}

string mock_http_response4(int status, string content)
{
string m3u8_header = "#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1,AVERAGE-BANDWIDTH=1\n";

stringstream ss;
ss << "HTTP/1.1 " << status << " " << srs_generate_http_status_text(status) << "\r\n"
<< "Content-Length: " << content.length() + m3u8_header.length() << "\r\n\r\n"
<< m3u8_header
<< content;
return ss.str();
}

bool is_string_contain(string substr, string str)
{
return (string::npos != str.find(substr));
Expand Down Expand Up @@ -1286,7 +1298,11 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?hls_ctx=123456", false));

HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r));
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w);
__MOCK_HTTP_EXPECT_STREQ4(200, "/index.m3u8?hls_ctx=123456", w);

MockResponseWriter w2;
HELPER_ASSERT_SUCCESS(h.serve_http(&w2, &r));
__MOCK_HTTP_EXPECT_STREQ(200, "Hello, world!", w2);
}

// Should return "hls_ctx"
Expand All @@ -1304,7 +1320,11 @@ VOID TEST(ProtocolHTTPTest, VodStreamHandlers)
HELPER_ASSERT_SUCCESS(r.set_url("/index.m3u8?hls_ctx=123456", false));

HELPER_ASSERT_SUCCESS(h.serve_http(&w, &r));
__MOCK_HTTP_EXPECT_STREQ(200, "livestream-13.ts?hls_ctx=123456", w);
__MOCK_HTTP_EXPECT_STREQ4(200, "/index.m3u8?hls_ctx=123456", w);

MockResponseWriter w2;
HELPER_ASSERT_SUCCESS(h.serve_http(&w2, &r));
__MOCK_HTTP_EXPECT_STREQ(200, "livestream-13.ts?hls_ctx=123456", w2);
}
}

Expand Down
4 changes: 4 additions & 0 deletions trunk/src/utest/srs_utest_http.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class MockMSegmentsReader : public ISrsReader

string mock_http_response(int status, string content);
string mock_http_response2(int status, string content);
string mock_http_response4(int status, string content);
bool is_string_contain(string substr, string str);

#define __MOCK_HTTP_EXPECT_STREQ(status, text, w) \
Expand All @@ -59,6 +60,9 @@ bool is_string_contain(string substr, string str);
#define __MOCK_HTTP_EXPECT_STREQ2(status, text, w) \
EXPECT_STREQ(mock_http_response2(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str())

#define __MOCK_HTTP_EXPECT_STREQ4(status, text, w) \
EXPECT_STREQ(mock_http_response4(status, text).c_str(), HELPER_BUFFER2STR(&w.io.out_buffer).c_str())

#define __MOCK_HTTP_EXPECT_STRHAS(status, text, w) \
EXPECT_PRED2(is_string_contain, text, HELPER_BUFFER2STR(&w.io.out_buffer).c_str())

Expand Down

0 comments on commit 3727d05

Please sign in to comment.