structure.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package m3u8
  2. /*
  3. Part of M3U8 parser & generator library.
  4. This file defines data structures related to package.
  5. Copyright 2013-2017 The Project Developers.
  6. See the AUTHORS and LICENSE files at the top-level directory of this distribution
  7. and at https://github.com/grafov/m3u8/
  8. ॐ तारे तुत्तारे तुरे स्व
  9. */
  10. import (
  11. "bytes"
  12. "io"
  13. "time"
  14. )
  15. const (
  16. /*
  17. Compatibility rules described in section 7:
  18. Clients and servers MUST implement protocol version 2 or higher to use:
  19. o The IV attribute of the EXT-X-KEY tag.
  20. Clients and servers MUST implement protocol version 3 or higher to use:
  21. o Floating-point EXTINF duration values.
  22. Clients and servers MUST implement protocol version 4 or higher to use:
  23. o The EXT-X-BYTERANGE tag.
  24. o The EXT-X-I-FRAME-STREAM-INF tag.
  25. o The EXT-X-I-FRAMES-ONLY tag.
  26. o The EXT-X-MEDIA tag.
  27. o The AUDIO and VIDEO attributes of the EXT-X-STREAM-INF tag.
  28. */
  29. minver = uint8(3)
  30. DATETIME = time.RFC3339Nano // Format for EXT-X-PROGRAM-DATE-TIME defined in section 3.4.5
  31. )
  32. type ListType uint
  33. const (
  34. // use 0 for not defined type
  35. MASTER ListType = iota + 1
  36. MEDIA
  37. )
  38. // for EXT-X-PLAYLIST-TYPE tag
  39. type MediaType uint
  40. const (
  41. // use 0 for not defined type
  42. EVENT MediaType = iota + 1
  43. VOD
  44. )
  45. // SCTE35Syntax defines the format of the SCTE-35 cue points which do not use
  46. // the draft-pantos-http-live-streaming-19 EXT-X-DATERANGE tag and instead
  47. // have their own custom tags
  48. type SCTE35Syntax uint
  49. const (
  50. // SCTE35_67_2014 will be the default due to backwards compatibility reasons.
  51. SCTE35_67_2014 SCTE35Syntax = iota // SCTE35_67_2014 defined in http://www.scte.org/documents/pdf/standards/SCTE%2067%202014.pdf
  52. SCTE35_OATCLS // SCTE35_OATCLS is a non-standard but common format
  53. )
  54. // SCTE35CueType defines the type of cue point, used by readers and writers to
  55. // write a different syntax
  56. type SCTE35CueType uint
  57. const (
  58. SCTE35Cue_Start SCTE35CueType = iota // SCTE35Cue_Start indicates an out cue point
  59. SCTE35Cue_Mid // SCTE35Cue_Mid indicates a segment between start and end cue points
  60. SCTE35Cue_End // SCTE35Cue_End indicates an in cue point
  61. )
  62. /*
  63. This structure represents a single bitrate playlist aka media playlist.
  64. It related to both a simple media playlists and a sliding window media playlists.
  65. URI lines in the Playlist point to media segments.
  66. Simple Media Playlist file sample:
  67. #EXTM3U
  68. #EXT-X-VERSION:3
  69. #EXT-X-TARGETDURATION:5220
  70. #EXTINF:5219.2,
  71. http://media.example.com/entire.ts
  72. #EXT-X-ENDLIST
  73. Sample of Sliding Window Media Playlist, using HTTPS:
  74. #EXTM3U
  75. #EXT-X-VERSION:3
  76. #EXT-X-TARGETDURATION:8
  77. #EXT-X-MEDIA-SEQUENCE:2680
  78. #EXTINF:7.975,
  79. https://priv.example.com/fileSequence2680.ts
  80. #EXTINF:7.941,
  81. https://priv.example.com/fileSequence2681.ts
  82. #EXTINF:7.975,
  83. https://priv.example.com/fileSequence2682.ts
  84. */
  85. type MediaPlaylist struct {
  86. TargetDuration float64
  87. SeqNo uint64 // EXT-X-MEDIA-SEQUENCE
  88. Segments []*MediaSegment
  89. Args string // optional arguments placed after URIs (URI?Args)
  90. Iframe bool // EXT-X-I-FRAMES-ONLY
  91. Closed bool // is this VOD (closed) or Live (sliding) playlist?
  92. MediaType MediaType
  93. durationAsInt bool // output durations as integers of floats?
  94. keyformat int
  95. winsize uint // max number of segments displayed in an encoded playlist; need set to zero for VOD playlists
  96. capacity uint // total capacity of slice used for the playlist
  97. head uint // head of FIFO, we add segments to head
  98. tail uint // tail of FIFO, we remove segments from tail
  99. count uint // number of segments added to the playlist
  100. buf bytes.Buffer
  101. ver uint8
  102. Key *Key // EXT-X-KEY is optional encryption key displayed before any segments (default key for the playlist)
  103. Map *Map // EXT-X-MAP is optional tag specifies how to obtain the Media Initialization Section (default map for the playlist)
  104. WV *WV // Widevine related tags outside of M3U8 specs
  105. }
  106. /*
  107. This structure represents a master playlist which combines media playlists for multiple bitrates.
  108. URI lines in the playlist identify media playlists.
  109. Sample of Master Playlist file:
  110. #EXTM3U
  111. #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1280000
  112. http://example.com/low.m3u8
  113. #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2560000
  114. http://example.com/mid.m3u8
  115. #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=7680000
  116. http://example.com/hi.m3u8
  117. #EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=65000,CODECS="mp4a.40.5"
  118. http://example.com/audio-only.m3u8
  119. */
  120. type MasterPlaylist struct {
  121. Variants []*Variant
  122. Args string // optional arguments placed after URI (URI?Args)
  123. CypherVersion string // non-standard tag for Widevine (see also WV struct)
  124. buf bytes.Buffer
  125. ver uint8
  126. }
  127. // This structure represents variants for master playlist.
  128. // Variants included in a master playlist and point to media playlists.
  129. type Variant struct {
  130. URI string
  131. Chunklist *MediaPlaylist
  132. VariantParams
  133. }
  134. // This structure represents additional parameters for a variant
  135. // used in EXT-X-STREAM-INF and EXT-X-I-FRAME-STREAM-INF
  136. type VariantParams struct {
  137. ProgramId uint32
  138. Bandwidth uint32
  139. Codecs string
  140. Resolution string
  141. Audio string // EXT-X-STREAM-INF only
  142. Video string
  143. Subtitles string // EXT-X-STREAM-INF only
  144. Captions string // EXT-X-STREAM-INF only
  145. Name string // EXT-X-STREAM-INF only (non standard Wowza/JWPlayer extension to name the variant/quality in UA)
  146. Iframe bool // EXT-X-I-FRAME-STREAM-INF
  147. Alternatives []*Alternative // EXT-X-MEDIA
  148. }
  149. // This structure represents EXT-X-MEDIA tag in variants.
  150. type Alternative struct {
  151. GroupId string
  152. URI string
  153. Type string
  154. Language string
  155. Name string
  156. Default bool
  157. Autoselect string
  158. Forced string
  159. Characteristics string
  160. Subtitles string
  161. }
  162. // This structure represents a media segment included in a media playlist.
  163. // Media segment may be encrypted.
  164. // Widevine supports own tags for encryption metadata.
  165. type MediaSegment struct {
  166. SeqId uint64
  167. Title string // optional second parameter for EXTINF tag
  168. URI string
  169. Duration float64 // first parameter for EXTINF tag; duration must be integers if protocol version is less than 3 but we are always keep them float
  170. Limit int64 // EXT-X-BYTERANGE <n> is length in bytes for the file under URI
  171. Offset int64 // EXT-X-BYTERANGE [@o] is offset from the start of the file under URI
  172. Key *Key // EXT-X-KEY displayed before the segment and means changing of encryption key (in theory each segment may have own key)
  173. Map *Map // EXT-X-MAP displayed before the segment
  174. Discontinuity bool // EXT-X-DISCONTINUITY indicates an encoding discontinuity between the media segment that follows it and the one that preceded it (i.e. file format, number and type of tracks, encoding parameters, encoding sequence, timestamp sequence)
  175. SCTE *SCTE // SCTE-35 used for Ad signaling in HLS
  176. ProgramDateTime time.Time // EXT-X-PROGRAM-DATE-TIME tag associates the first sample of a media segment with an absolute date and/or time
  177. }
  178. // SCTE holds custom, non EXT-X-DATERANGE, SCTE-35 tags
  179. type SCTE struct {
  180. Syntax SCTE35Syntax // Syntax defines the format of the SCTE-35 cue tag
  181. CueType SCTE35CueType // CueType defines whether the cue is a start, mid, end (if applicable)
  182. Cue string
  183. ID string
  184. Time float64
  185. Elapsed float64
  186. }
  187. // This structure represents information about stream encryption.
  188. //
  189. // Realizes EXT-X-KEY tag.
  190. type Key struct {
  191. Method string
  192. URI string
  193. IV string
  194. Keyformat string
  195. Keyformatversions string
  196. }
  197. // This structure represents specifies how to obtain the Media
  198. // Initialization Section required to parse the applicable
  199. // Media Segments.
  200. // It applies to every Media Segment that appears after it in the
  201. // Playlist until the next EXT-X-MAP tag or until the end of the
  202. // playlist.
  203. //
  204. // Realizes EXT-MAP tag.
  205. type Map struct {
  206. URI string
  207. Limit int64 // <n> is length in bytes for the file under URI
  208. Offset int64 // [@o] is offset from the start of the file under URI
  209. }
  210. // This structure represents metadata for Google Widevine playlists.
  211. // This format not described in IETF draft but provied by Widevine Live Packager as
  212. // additional tags with #WV-prefix.
  213. type WV struct {
  214. AudioChannels uint
  215. AudioFormat uint
  216. AudioProfileIDC uint
  217. AudioSampleSize uint
  218. AudioSamplingFrequency uint
  219. CypherVersion string
  220. ECM string
  221. VideoFormat uint
  222. VideoFrameRate uint
  223. VideoLevelIDC uint
  224. VideoProfileIDC uint
  225. VideoResolution string
  226. VideoSAR string
  227. }
  228. // Interface applied to various playlist types.
  229. type Playlist interface {
  230. Encode() *bytes.Buffer
  231. Decode(bytes.Buffer, bool) error
  232. DecodeFrom(reader io.Reader, strict bool) error
  233. String() string
  234. }
  235. // Internal structure for decoding a line of input stream with a list type detection
  236. type decodingState struct {
  237. listType ListType
  238. m3u bool
  239. tagWV bool
  240. tagStreamInf bool
  241. tagInf bool
  242. tagSCTE35 bool
  243. tagRange bool
  244. tagDiscontinuity bool
  245. tagProgramDateTime bool
  246. tagKey bool
  247. tagMap bool
  248. programDateTime time.Time
  249. limit int64
  250. offset int64
  251. duration float64
  252. title string
  253. variant *Variant
  254. alternatives []*Alternative
  255. xkey *Key
  256. xmap *Map
  257. scte *SCTE
  258. }