partial.test.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. var Transmuxer = require('../lib/partial/transmuxer.js');
  2. var utils = require('./utils');
  3. var generatePMT = utils.generatePMT;
  4. var videoPes = utils.videoPes;
  5. var audioPes = utils.audioPes;
  6. var packetize = utils.packetize;
  7. var PAT = utils.PAT;
  8. QUnit.module('Partial Transmuxer - Options');
  9. [
  10. {options: {keepOriginalTimestamps: false}},
  11. {options: {keepOriginalTimestamps: true}},
  12. {options: {keepOriginalTimestamps: false, baseMediaDecodeTime: 15000}},
  13. {options: {keepOriginalTimestamps: true, baseMediaDecodeTime: 15000}},
  14. {options: {keepOriginalTimestamps: false}, baseMediaSetter: 15000},
  15. {options: {keepOriginalTimestamps: true}, baseMediaSetter: 15000}
  16. ].forEach(function(test) {
  17. var createTransmuxer = function() {
  18. var transmuxer = new Transmuxer(test.options);
  19. if (test.baseMediaSetter) {
  20. transmuxer.setBaseMediaDecodeTime(test.baseMediaSetter);
  21. }
  22. return transmuxer;
  23. };
  24. var name = '';
  25. Object.keys(test.options).forEach(function(optionName) {
  26. name += '' + optionName + ' ' + test.options[optionName] + ' ';
  27. });
  28. if (test.baseMediaSetter) {
  29. name += 'baseMediaDecodeTime setter ' + test.baseMediaSetter;
  30. }
  31. QUnit.test('Audio frames after video not trimmed, ' + name, function(assert) {
  32. var
  33. segments = [],
  34. earliestDts = 15000,
  35. transmuxer = createTransmuxer();
  36. transmuxer.on('data', function(segment) {
  37. segments.push(segment);
  38. });
  39. // the following transmuxer pushes add tiny video and
  40. // audio data to the transmuxer. When we add the data
  41. // we also set the pts/dts time so that audio should
  42. // not be trimmed.
  43. transmuxer.push(packetize(PAT));
  44. transmuxer.push(packetize(generatePMT({
  45. hasVideo: true,
  46. hasAudio: true
  47. })));
  48. transmuxer.push(packetize(audioPes([
  49. 0x19, 0x47
  50. ], true, earliestDts + 1)));
  51. transmuxer.push(packetize(videoPes([
  52. 0x09, 0x01 // access_unit_delimiter_rbsp
  53. ], true, earliestDts)));
  54. transmuxer.push(packetize(videoPes([
  55. 0x08, 0x01 // pic_parameter_set_rbsp
  56. ], true, earliestDts)));
  57. transmuxer.push(packetize(videoPes([
  58. 0x07, // seq_parameter_set_rbsp
  59. 0x27, 0x42, 0xe0, 0x0b,
  60. 0xa9, 0x18, 0x60, 0x9d,
  61. 0x80, 0x53, 0x06, 0x01,
  62. 0x06, 0xb6, 0xc2, 0xb5,
  63. 0xef, 0x7c, 0x04
  64. ], false, earliestDts)));
  65. transmuxer.push(packetize(videoPes([
  66. 0x05, 0x01 // slice_layer_without_partitioning_rbsp_idr
  67. ], true, earliestDts)));
  68. transmuxer.flush();
  69. // the partial transmuxer only generates a video segment
  70. // when all audio frames are trimmed. So we should have an audio and video
  71. // segment
  72. assert.equal(segments.length, 2, 'generated a video and an audio segment');
  73. assert.equal(segments[0].type, 'video', 'video segment exists');
  74. assert.equal(segments[1].type, 'audio', 'audio segment exists');
  75. });
  76. QUnit.test('Audio frames trimmed before video, ' + name, function(assert) {
  77. var
  78. segments = [],
  79. earliestDts = 15000,
  80. baseTime = test.options.baseMediaDecodeTime || test.baseMediaSetter || 0,
  81. transmuxer = createTransmuxer();
  82. transmuxer.on('data', function(segment) {
  83. segments.push(segment);
  84. });
  85. // the following transmuxer pushes add tiny video and
  86. // audio data to the transmuxer. When we add the data
  87. // we also set the pts/dts time so that audio should
  88. // be trimmed.
  89. transmuxer.push(packetize(PAT));
  90. transmuxer.push(packetize(generatePMT({
  91. hasVideo: true,
  92. hasAudio: true
  93. })));
  94. transmuxer.push(packetize(audioPes([
  95. 0x19, 0x47
  96. ], true, earliestDts - baseTime - 1)));
  97. transmuxer.push(packetize(videoPes([
  98. 0x09, 0x01 // access_unit_delimiter_rbsp
  99. ], true, earliestDts)));
  100. transmuxer.push(packetize(videoPes([
  101. 0x08, 0x01 // pic_parameter_set_rbsp
  102. ], true, earliestDts)));
  103. transmuxer.push(packetize(videoPes([
  104. 0x07, // seq_parameter_set_rbsp
  105. 0x27, 0x42, 0xe0, 0x0b,
  106. 0xa9, 0x18, 0x60, 0x9d,
  107. 0x80, 0x53, 0x06, 0x01,
  108. 0x06, 0xb6, 0xc2, 0xb5,
  109. 0xef, 0x7c, 0x04
  110. ], false, earliestDts)));
  111. transmuxer.push(packetize(videoPes([
  112. 0x05, 0x01 // slice_layer_without_partitioning_rbsp_idr
  113. ], true, earliestDts)));
  114. transmuxer.flush();
  115. // the partial transmuxer only generates a video segment
  116. // when all audio frames are trimmed.
  117. if (test.options.keepOriginalTimestamps && !baseTime) {
  118. assert.equal(segments.length, 2, 'generated both a video/audio segment');
  119. assert.equal(segments[0].type, 'video', 'segment is video');
  120. assert.equal(segments[1].type, 'audio', 'segment is audio');
  121. } else {
  122. assert.equal(segments.length, 1, 'generated only a video segment');
  123. assert.equal(segments[0].type, 'video', 'segment is video');
  124. }
  125. });
  126. });