Class: SamveraHls::HlsPlaylistGenerator

Inherits:
Object
  • Object
show all
Defined in:
app/services/samvera_hls/hls_playlist_generator.rb

Class Method Summary collapse

Class Method Details

.derivative_url(file_set_id, destination_name) ⇒ Object



78
79
80
# File 'app/services/samvera_hls/hls_playlist_generator.rb', line 78

def derivative_url file_set_id, destination_name 
  derivative_path(file_set_id, destination_name).gsub(Hyrax.config.derivatives_path,"")
end

.hls_dir(file_set_id) ⇒ Object



74
75
76
# File 'app/services/samvera_hls/hls_playlist_generator.rb', line 74

def hls_dir file_set_id
  derivative_path(file_set_id,"hls").gsub(/\.hls\z/,"")
end

.hls_master_playlist(file_set_id, root_url) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/samvera_hls/hls_playlist_generator.rb', line 9

def hls_master_playlist file_set_id, root_url
  # make sure the root url doesn't get any default  params attached (e.g. locale)
  parsed_url = URI::parse(root_url)
  parsed_url.fragment = parsed_url.query = nil
  root_url = parsed_url.to_s

  type = SolrDocument.find(file_set_id).mime_type.split('/')[0]
  config = hls_config[type]
  return nil if config.nil?
  defaults = config["default"]
  playlist = "#EXTM3U\n"
  playlist << "#EXT-X-VERSION:6\n"
  variants(file_set_id).each do |variant| 
    next if variant.blank?
    path, filename = File.split(variant)
    format = filename.gsub(".m3u8","")
    options = defaults.merge(config[format])
    playlist << "#EXT-X-STREAM-INF:PROGRAM-ID=1,"
    playlist << "BANDWIDTH=#{options["maxrate"]},"
    playlist << "CODECS=\"#{options["codec_code"]}\","
    playlist << "RESOLUTION=#{options["resolution"]}\n"
    playlist << File.join(root_url,variant_url(file_set_id,format)) + "\n"
  end
  playlist
end

.hls_master_url(file_set_id) ⇒ Object



5
6
7
# File 'app/services/samvera_hls/hls_playlist_generator.rb', line 5

def hls_master_url file_set_id
  File.join("file_set",file_set_id,"hls.m3u8")
end

.hls_segment_playlist(file_set_id, root_url, format) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/services/samvera_hls/hls_playlist_generator.rb', line 35

def hls_segment_playlist file_set_id, root_url, format
  # make sure the root url doesn't get any default  params attached (e.g. locale)
  parsed_url = URI::parse(root_url)
  parsed_url.fragment = parsed_url.query = nil
  root_url = parsed_url.to_s

  # initialize timestamp once for whole playlist
  timestamp = Time.now.to_i + 7200

  playlist = ""

  File.open(segment_playlist_path(file_set_id, format),'r') {|file|
    
    variant_token = false;
    file.each_line do |line|
      if line.include? ".ts" then
        # We only need to set the token once for each variant file. 
        # It doesn't depend on which segment we're playing, so we 
        # speed things up by only calculating one.
        variant_token ||= token(file_set_id,line, timestamp)
        if Rails.env == "development" || Rails.env == "test"
          playlist << File.join(root_url,
                                segment_url_base(file_set_id),
                                line).strip+"\n"
        else
          playlist << File.join(root_url,
                                segment_url_base(file_set_id),
                                timestamp.to_s,
                                variant_token,
                                line).strip+"\n"
        end
      else
        playlist << line.strip+"\n"
      end
    end
  }
  playlist
end