segmentBase.test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. import QUnit from 'qunit';
  2. import {
  3. segmentsFromBase,
  4. addSidxSegmentsToPlaylist
  5. } from '../../src/segment/segmentBase';
  6. import errors from '../../src/errors';
  7. import window from 'global/window';
  8. QUnit.module('segmentBase - segmentsFromBase');
  9. QUnit.test('sets segment to baseUrl', function(assert) {
  10. const inputAttributes = {
  11. baseUrl: 'http://www.example.com/i.fmp4',
  12. initialization: { sourceURL: 'http://www.example.com/init.fmp4' },
  13. periodStart: 0,
  14. type: 'static'
  15. };
  16. assert.deepEqual(segmentsFromBase(inputAttributes), [{
  17. map: {
  18. resolvedUri: 'http://www.example.com/init.fmp4',
  19. uri: 'http://www.example.com/init.fmp4'
  20. },
  21. resolvedUri: 'http://www.example.com/i.fmp4',
  22. uri: 'http://www.example.com/i.fmp4',
  23. presentationTime: 0,
  24. number: 0
  25. }]);
  26. });
  27. QUnit.test('sets duration based on sourceDuration', function(assert) {
  28. const inputAttributes = {
  29. baseUrl: 'http://www.example.com/i.fmp4',
  30. initialization: { sourceURL: 'http://www.example.com/init.fmp4' },
  31. sourceDuration: 10,
  32. periodStart: 0,
  33. type: 'static'
  34. };
  35. assert.deepEqual(segmentsFromBase(inputAttributes), [{
  36. duration: 10,
  37. timeline: 0,
  38. map: {
  39. resolvedUri: 'http://www.example.com/init.fmp4',
  40. uri: 'http://www.example.com/init.fmp4'
  41. },
  42. resolvedUri: 'http://www.example.com/i.fmp4',
  43. uri: 'http://www.example.com/i.fmp4',
  44. presentationTime: 0,
  45. number: 0
  46. }]);
  47. });
  48. // sourceDuration comes from mediaPresentationDuration. The DASH spec defines the type of
  49. // mediaPresentationDuration as xs:duration, which follows ISO 8601. It does not need to
  50. // be adjusted based on timescale.
  51. //
  52. // References:
  53. // https://www.w3.org/TR/xmlschema-2/#duration
  54. // https://en.wikipedia.org/wiki/ISO_8601
  55. QUnit.test('sets duration based on sourceDuration and not @timescale', function(assert) {
  56. const inputAttributes = {
  57. baseUrl: 'http://www.example.com/i.fmp4',
  58. initialization: { sourceURL: 'http://www.example.com/init.fmp4' },
  59. sourceDuration: 10,
  60. timescale: 2,
  61. periodStart: 0,
  62. type: 'static'
  63. };
  64. assert.deepEqual(segmentsFromBase(inputAttributes), [{
  65. duration: 10,
  66. timeline: 0,
  67. map: {
  68. resolvedUri: 'http://www.example.com/init.fmp4',
  69. uri: 'http://www.example.com/init.fmp4'
  70. },
  71. resolvedUri: 'http://www.example.com/i.fmp4',
  72. uri: 'http://www.example.com/i.fmp4',
  73. presentationTime: 0,
  74. number: 0
  75. }]);
  76. });
  77. QUnit.test('sets duration based on @duration', function(assert) {
  78. const inputAttributes = {
  79. duration: 10,
  80. sourceDuration: 20,
  81. baseUrl: 'http://www.example.com/i.fmp4',
  82. initialization: { sourceURL: 'http://www.example.com/init.fmp4' },
  83. periodStart: 0,
  84. type: 'static'
  85. };
  86. assert.deepEqual(segmentsFromBase(inputAttributes), [{
  87. duration: 10,
  88. timeline: 0,
  89. map: {
  90. resolvedUri: 'http://www.example.com/init.fmp4',
  91. uri: 'http://www.example.com/init.fmp4'
  92. },
  93. resolvedUri: 'http://www.example.com/i.fmp4',
  94. uri: 'http://www.example.com/i.fmp4',
  95. presentationTime: 0,
  96. number: 0
  97. }]);
  98. });
  99. QUnit.test('sets duration based on @duration and @timescale', function(assert) {
  100. const inputAttributes = {
  101. duration: 10,
  102. sourceDuration: 20,
  103. timescale: 5,
  104. baseUrl: 'http://www.example.com/i.fmp4',
  105. initialization: { sourceURL: 'http://www.example.com/init.fmp4' },
  106. periodStart: 0,
  107. type: 'static'
  108. };
  109. assert.deepEqual(segmentsFromBase(inputAttributes), [{
  110. duration: 2,
  111. timeline: 0,
  112. map: {
  113. resolvedUri: 'http://www.example.com/init.fmp4',
  114. uri: 'http://www.example.com/init.fmp4'
  115. },
  116. resolvedUri: 'http://www.example.com/i.fmp4',
  117. uri: 'http://www.example.com/i.fmp4',
  118. presentationTime: 0,
  119. number: 0
  120. }]);
  121. });
  122. QUnit.test('translates ranges in <Initialization> node', function(assert) {
  123. const inputAttributes = {
  124. duration: 10,
  125. sourceDuration: 20,
  126. timescale: 5,
  127. baseUrl: 'http://www.example.com/i.fmp4',
  128. initialization: {
  129. sourceURL: 'http://www.example.com/init.fmp4',
  130. range: '121-125'
  131. },
  132. periodStart: 0,
  133. type: 'static'
  134. };
  135. assert.deepEqual(segmentsFromBase(inputAttributes), [{
  136. duration: 2,
  137. timeline: 0,
  138. map: {
  139. resolvedUri: 'http://www.example.com/init.fmp4',
  140. uri: 'http://www.example.com/init.fmp4',
  141. byterange: {
  142. length: 5,
  143. offset: 121
  144. }
  145. },
  146. resolvedUri: 'http://www.example.com/i.fmp4',
  147. uri: 'http://www.example.com/i.fmp4',
  148. presentationTime: 0,
  149. number: 0
  150. }]);
  151. });
  152. QUnit.test('errors if no baseUrl exists', function(assert) {
  153. assert.throws(() => segmentsFromBase({}), new Error(errors.NO_BASE_URL));
  154. });
  155. QUnit.module('segmentBase - addSidxSegmentsToPlaylist');
  156. QUnit.test('generates playlist from sidx references', function(assert) {
  157. const baseUrl = 'http://www.example.com/i.fmp4';
  158. const playlist = {
  159. sidx: {
  160. map: {
  161. byterange: {
  162. offset: 0,
  163. length: 10
  164. }
  165. },
  166. duration: 10,
  167. byterange: {
  168. offset: 9,
  169. length: 11
  170. },
  171. timeline: 0
  172. },
  173. segments: [],
  174. endList: true
  175. };
  176. const sidx = {
  177. timescale: 1,
  178. firstOffset: 0,
  179. references: [{
  180. referenceType: 0,
  181. referencedSize: 5,
  182. subsegmentDuration: 2
  183. }]
  184. };
  185. assert.deepEqual(addSidxSegmentsToPlaylist(playlist, sidx, baseUrl).segments, [{
  186. map: {
  187. byterange: {
  188. offset: 0,
  189. length: 10
  190. }
  191. },
  192. uri: 'http://www.example.com/i.fmp4',
  193. resolvedUri: 'http://www.example.com/i.fmp4',
  194. byterange: {
  195. offset: 20,
  196. length: 5
  197. },
  198. duration: 2,
  199. timeline: 0,
  200. presentationTime: 0,
  201. number: 0
  202. }]);
  203. });
  204. if (window.BigInt) {
  205. const BigInt = window.BigInt;
  206. QUnit.test('generates playlist from sidx references with BigInt', function(assert) {
  207. const baseUrl = 'http://www.example.com/i.fmp4';
  208. const playlist = {
  209. sidx: {
  210. map: {
  211. byterange: {
  212. offset: 0,
  213. length: 10
  214. }
  215. },
  216. timeline: 0,
  217. duration: 10,
  218. byterange: {
  219. offset: 9,
  220. length: 11
  221. }
  222. },
  223. segments: []
  224. };
  225. const offset = BigInt(Number.MAX_SAFE_INTEGER) + BigInt(10);
  226. const sidx = {
  227. timescale: 1,
  228. firstOffset: offset,
  229. references: [{
  230. referenceType: 0,
  231. referencedSize: 5,
  232. subsegmentDuration: 2
  233. }]
  234. };
  235. const segments = addSidxSegmentsToPlaylist(playlist, sidx, baseUrl).segments;
  236. assert.equal(typeof segments[0].byterange.offset, 'bigint', 'bigint offset');
  237. segments[0].byterange.offset = segments[0].byterange.offset.toString();
  238. assert.deepEqual(segments, [{
  239. map: {
  240. byterange: {
  241. offset: 0,
  242. length: 10
  243. }
  244. },
  245. uri: 'http://www.example.com/i.fmp4',
  246. resolvedUri: 'http://www.example.com/i.fmp4',
  247. byterange: {
  248. // sidx byterange offset + length = 20
  249. offset: (window.BigInt(20) + offset).toString(),
  250. length: 5
  251. },
  252. number: 0,
  253. presentationTime: 0
  254. }]);
  255. });
  256. }