lib/m3u8/playlist.rb in m3u8-0.2.1 vs lib/m3u8/playlist.rb in m3u8-0.3.0
- old
+ new
@@ -1,13 +1,8 @@
module M3u8
class Playlist
attr_accessor :items, :version, :cache, :target, :sequence
- NON_MASTER_ERROR_MESSAGE = 'Playlist is not a master playlist, playlist' \
- ' can not be added.'
- MASTER_ERROR_MESSAGE = 'Playlist is a master playlist, segment can not ' \
- 'be added.'
- MIXED_TYPE_ERROR_MESSAGE = 'Playlist contains mixed types of items'
def initialize(options = {})
assign_options options
self.items = []
end
@@ -15,41 +10,18 @@
def self.codecs(options = {})
item = PlaylistItem.new options
item.codecs
end
- def add_playlist(program_id, playlist, bitrate, options = {})
- validate_playlist_type true
-
- params = { program_id: program_id, playlist: playlist, bitrate: bitrate,
- width: options[:width], height: options[:height],
- audio: options[:audio], profile: options[:profile],
- level: options[:level] }
- item = PlaylistItem.new params
- items.push item
+ def self.read(input)
+ reader = Reader.new
+ reader.read input
end
- def add_segment(duration, segment)
- validate_playlist_type false
-
- params = { duration: duration, segment: segment }
- item = SegmentItem.new params
- items.push item
- end
-
def write(output)
- validate
-
- output.puts '#EXTM3U'
- write_header(output) unless master?
-
- items.each do |item|
- output.puts item.to_s
- end
-
- return if master?
- output.puts '#EXT-X-ENDLIST'
+ writer = Writer.new output
+ writer.write self
end
def master?
return false if playlist_size == 0 && segment_size == 0
playlist_size > 0
@@ -80,39 +52,14 @@
self.sequence = options[:sequence]
self.cache = options[:cache]
self.target = options[:target]
end
- def validate
- return if valid?
- fail PlaylistTypeError, MIXED_TYPE_ERROR_MESSAGE
- end
-
- def validate_playlist_type(master)
- return if items.size == 0
- if master && !master?
- fail PlaylistTypeError, NON_MASTER_ERROR_MESSAGE
- elsif !master && master?
- fail PlaylistTypeError, MASTER_ERROR_MESSAGE
- end
- end
-
def playlist_size
items.select { |item| item.is_a?(PlaylistItem) }.size
end
def segment_size
items.select { |item| item.is_a?(SegmentItem) }.size
- end
-
- def write_header(output)
- output.puts "#EXT-X-VERSION:#{version}"
- output.puts "#EXT-X-MEDIA-SEQUENCE:#{sequence}"
- output.puts "#EXT-X-ALLOW-CACHE:#{cache_string}"
- output.puts "#EXT-X-TARGETDURATION:#{target}"
- end
-
- def cache_string
- cache ? 'YES' : 'NO'
end
end
end