Fix chunk size for livestreams

This commit is contained in:
Omar Roth 2019-07-05 11:34:22 -05:00
parent 8033d1ca6d
commit 1277c3d156
No known key found for this signature in database
GPG key ID: B8254FB7EC3D37F2

View file

@ -4429,6 +4429,7 @@ get "/api/manifest/hls_variant/*" do |env|
manifest
end
# TODO: Fix redirect for local streams
get "/api/manifest/hls_playlist/*" do |env|
client = make_client(YT_URL)
manifest = client.get(env.request.path)
@ -4641,6 +4642,34 @@ get "/videoplayback" do |env|
next
end
if url.includes? "&file=seg.ts"
begin
client = make_client(URI.parse(host), region)
client.get(url, headers) do |response|
response.headers.each do |key, value|
if !RESPONSE_HEADERS_BLACKLIST.includes?(key)
env.response.headers[key] = value
end
end
env.response.headers["Access-Control-Allow-Origin"] = "*"
if location = response.headers["Location"]?
location = URI.parse(location)
location = "#{location.full_path}&host=#{location.host}"
if region
location += "&region=#{region}"
end
next env.redirect location
end
IO.copy(response.body_io, env.response)
end
rescue ex
end
else
content_length = nil
first_chunk = true
range_start, range_end = parse_range(env.request.headers["Range"]?)
@ -4666,8 +4695,9 @@ get "/videoplayback" do |env|
end
headers["Range"] = "bytes=#{chunk_start}-#{chunk_end}"
client = make_client(URI.parse(host), region)
begin
client = make_client(URI.parse(host), region)
client.get(url, headers) do |response|
if first_chunk
if !env.request.headers["Range"]? && response.status_code == 206
@ -4714,11 +4744,6 @@ get "/videoplayback" do |env|
proxy_file(response, env)
end
# For livestream segments, break after first chunk
if url.includes? "&file=seg.ts"
break
end
rescue ex
if ex.message != "Error reading socket: Connection reset by peer"
break
@ -4729,6 +4754,7 @@ get "/videoplayback" do |env|
chunk_end += HTTP_CHUNK_SIZE
first_chunk = false
end
end
end
# We need this so the below route works as expected