@@ -33,6 +33,7 @@ using namespace std;
3333#include < srs_kernel_utility.hpp>
3434#include < srs_protocol_amf0.hpp>
3535#include < srs_protocol_format.hpp>
36+ #include < srs_protocol_http_stack.hpp>
3637#include < srs_protocol_rtmp_stack.hpp>
3738#include < srs_protocol_stream.hpp>
3839
@@ -45,6 +46,33 @@ using namespace std;
4546// reset the piece id when deviation overflow this.
4647#define SRS_JUMP_WHEN_PIECE_DEVIATION 20
4748
49+ // Build the full key URL by appending key_file to hls_key_url with proper query string handling.
50+ // If hls_key_url contains query string like "http://localhost:8080/?token=abc",
51+ // the result will be "http://localhost:8080/live/livestream-0.key?token=abc"
52+ // @param hls_key_url The base URL which may contain query string
53+ // @param key_file The key file path like "live/livestream-0.key"
54+ // @return The full key URL with query string properly appended
55+ string srs_hls_build_key_url (const string &hls_key_url, const string &key_file)
56+ {
57+ if (hls_key_url.empty ()) {
58+ return key_file;
59+ }
60+
61+ // Find the query string separator
62+ size_t pos = hls_key_url.find (" ?" );
63+ if (pos != string::npos) {
64+ // URL contains query string, split and rebuild
65+ // Example: "http://localhost:8080/?token=abc" + "live/livestream-0.key"
66+ // Result: "http://localhost:8080/live/livestream-0.key?token=abc"
67+ string base_url = hls_key_url.substr (0 , pos);
68+ string query_string = hls_key_url.substr (pos); // Include the '?'
69+ return base_url + key_file + query_string;
70+ }
71+
72+ // No query string, simple concatenation
73+ return hls_key_url + key_file;
74+ }
75+
4876SrsHlsSegment::SrsHlsSegment (SrsTsContext *c, SrsAudioCodecId ac, SrsVideoCodecId vc, ISrsFileWriter *w)
4977{
5078 sequence_no_ = 0 ;
@@ -1107,11 +1135,7 @@ srs_error_t SrsHlsFmp4Muxer::do_refresh_m3u8_segment(SrsHlsM4sSegment *segment,
11071135 string key_file = srs_path_build_stream (hls_key_file_, req_->vhost_ , req_->app_ , req_->stream_ );
11081136 key_file = srs_strings_replace (key_file, " [seq]" , srs_strconv_format_int (segment->sequence_no_ ));
11091137
1110- string key_path = key_file;
1111- // if key_url is not set,only use the file name
1112- if (!hls_key_url_.empty ()) {
1113- key_path = hls_key_url_ + key_file;
1114- }
1138+ string key_path = srs_hls_build_key_url (hls_key_url_, key_file);
11151139
11161140 ss << " #EXT-X-KEY:METHOD=SAMPLE-AES,URI=" << " \" " << key_path << " \" ,IV=0x" << hexiv << SRS_CONSTS_LF;
11171141 }
@@ -2040,11 +2064,7 @@ srs_error_t SrsHlsMuxer::do_refresh_m3u8_segment(SrsHlsSegment *segment, std::st
20402064 string key_file = srs_path_build_stream (hls_key_file_, req_->vhost_ , req_->app_ , req_->stream_ );
20412065 key_file = srs_strings_replace (key_file, " [seq]" , srs_strconv_format_int (segment->sequence_no_ ));
20422066
2043- string key_path = key_file;
2044- // if key_url is not set,only use the file name
2045- if (!hls_key_url_.empty ()) {
2046- key_path = hls_key_url_ + key_file;
2047- }
2067+ string key_path = srs_hls_build_key_url (hls_key_url_, key_file);
20482068
20492069 ss << " #EXT-X-KEY:METHOD=AES-128,URI=" << " \" " << key_path << " \" ,IV=0x" << hexiv << SRS_CONSTS_LF;
20502070 }
0 commit comments