Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use relative URLs inside dash and hls playlists #842

Merged
merged 8 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# checks locally.
inherit_from: ./.rubocop_hound.yml

AllCops:
TargetRubyVersion: 2.1

# Use double quotes only for interpolation.
Style/StringLiterals:
EnforcedStyle: single_quotes
Expand Down
4 changes: 4 additions & 0 deletions app/models/pageflow/video_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def attachment_s3_url
"s3://#{File.join(attachment_on_s3.bucket_name, attachment_on_s3.path)}"
end

def encode_highdef?
entry.feature_state('highdef_video_encoding')
end

def mp4_4k
ZencoderAttachment.new(self, '4k.mp4')
end
Expand Down
21 changes: 16 additions & 5 deletions app/models/pageflow/zencoder_attachment.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
require 'uri'

module Pageflow
class ZencoderAttachment

cattr_accessor :default_options
self.default_options = {
path: "/:zencoder_asset_version/:host/:class/:id_partition/:filename",
url: ":zencoder_protocol//:zencoder_host_alias:zencoder_path",
hls_url: ":zencoder_protocol//:zencoder_hls_host_alias:zencoder_path",
hls_origin_url: ":zencoder_protocol//:zencoder_hls_origin_host_alias:zencoder_path"
path: '/:zencoder_asset_version/:host/:class/:id_partition/:filename',
url: ':zencoder_protocol//:zencoder_host_alias:zencoder_path',
hls_url: ':zencoder_protocol//:zencoder_hls_host_alias:zencoder_path',
hls_origin_url: ':zencoder_protocol//:zencoder_hls_origin_host_alias:zencoder_path'
}

attr_reader :file_name_pattern, :instance, :options, :styles
Expand Down Expand Up @@ -44,6 +45,16 @@ def url(url_options = {})
url_options)
end

def url_relative_to(attachment)
dir_path = File.dirname(URI.parse(attachment.url).path)

unless URI.parse(url).path.start_with?(dir_path)
raise("Could not generate relative url for #{url} based on #{attachment.url}.")
end

url.split("#{dir_path}/", 2).last
end

private

def ensure_default_protocol(url, url_options)
Expand Down
32 changes: 16 additions & 16 deletions lib/pageflow/zencoder_video_output_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def outputs
private

def mp4_highdef_definitions
return [] unless video_file.entry.feature_state('highdef_video_encoding')
return [] unless video_file.encode_highdef?
[transferable(mp4_4k_definition), transferable(mp4_fullhd_definition)]
end

Expand Down Expand Up @@ -128,7 +128,7 @@ def dash_definitions
end

def dash_highdef_definitions
return [] unless video_file.entry.feature_state('highdef_video_encoding')
return [] unless video_file.encode_highdef?

[
non_transferable(dash_fullhd_definition),
Expand All @@ -149,7 +149,7 @@ def hls_definitions
end

def hls_highdef_definitions
return [] unless video_file.entry.feature_state('highdef_video_encoding')
return [] unless video_file.encode_highdef?

[
non_transferable(hls_fullhd_definition),
Expand Down Expand Up @@ -243,29 +243,29 @@ def dash_stream_definitions
[
{
source: 'dash-low',
path: video_file.dash_low.url
path: video_file.dash_low.url_relative_to(video_file.dash_playlist)
},
{
source: 'dash-medium',
path: video_file.dash_medium.url
path: video_file.dash_medium.url_relative_to(video_file.dash_playlist)
},
{
source: 'dash-high',
path: video_file.dash_high.url
path: video_file.dash_high.url_relative_to(video_file.dash_playlist)
}
] + dash_highdef_stream_definitions
end

def dash_highdef_stream_definitions
return [] unless video_file.entry.feature_state('highdef_video_encoding')
return [] unless video_file.encode_highdef?
[
{
source: 'dash-fullhd',
path: video_file.dash_fullhd.url
path: video_file.dash_fullhd.url_relative_to(video_file.dash_playlist)
},
{
source: 'dash-4k',
path: video_file.dash_4k.url
path: video_file.dash_4k.url_relative_to(video_file.dash_playlist)
}
]
end
Expand Down Expand Up @@ -350,33 +350,33 @@ def hls_stream_definitions
[
{
source: 'hls-medium',
path: video_file.hls_medium.url(host: :hls_origin, default_protocol: 'http'),
path: video_file.hls_medium.url_relative_to(video_file.hls_playlist),
bandwidth: 1769
},
{
source: 'hls-low',
path: video_file.hls_low.url(host: :hls_origin, default_protocol: 'http'),
path: video_file.hls_low.url_relative_to(video_file.hls_playlist),
bandwidth: 619
},
{
source: 'hls-high',
path: video_file.hls_high.url(host: :hls_origin, default_protocol: 'http'),
path: video_file.hls_high.url_relative_to(video_file.hls_playlist),
bandwidth: 3538
}
] + hls_highdef_stream_definitions
end

def hls_highdef_stream_definitions
return [] unless video_file.entry.feature_state('highdef_video_encoding')
return [] unless video_file.encode_highdef?
[
{
source: 'hls-fullhd',
path: video_file.hls_fullhd.url(host: :hls_origin, default_protocol: 'http'),
path: video_file.hls_fullhd.url_relative_to(video_file.hls_playlist),
bandwidth: 8575
},
{
source: 'hls-4k',
path: video_file.hls_4k.url(host: :hls_origin, default_protocol: 'http'),
path: video_file.hls_4k.url_relative_to(video_file.hls_playlist),
bandwidth: 32000
}
]
Expand Down Expand Up @@ -404,7 +404,7 @@ def smil_definitions
# these cases the input file is just a tiny bit larger than the
# next lower resolution, so we do not really care if the SMIL
# file which does not include the higher quality does not win.
if video_file.entry.feature_state('highdef_video_encoding')
if video_file.encode_highdef?
[
non_transferable(smil_definition.merge(streams: smil_default_stream_definitions,
skip: {
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ module Pageflow
create(:revision, :published, entry: entry, password_protected: true)
end
end

trait :with_highdef_video_encoding do
feature_states('highdef_video_encoding' => true)
end
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/video_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ module Pageflow

trait :encoded do
end

trait :with_highdef_encoding do
association :entry, :with_highdef_video_encoding
end
end
end
end
14 changes: 14 additions & 0 deletions spec/models/pageflow/video_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,19 @@ module Pageflow
expect(video_file.present_outputs).to include(:'hls-playlist')
end
end

describe '#encode_highdef?' do
it 'returns false by default' do
video_file = build(:video_file)

expect(video_file.encode_highdef?).to eq(false)
end

it 'returns true if entry has highdef_video_encoding feature enabled' do
video_file = build(:video_file, :with_highdef_encoding)

expect(video_file.encode_highdef?).to eq(true)
end
end
end
end
Loading