You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

red.spec.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { RFC2198Encoder } from './red.js';
  2. describe('RED', () => {
  3. let encoder;
  4. const opusPayloadType = 111;
  5. beforeEach(() => {
  6. encoder = new RFC2198Encoder();
  7. encoder.setPayloadType(opusPayloadType);
  8. });
  9. describe('addRedundancy with a redundancy of 1', () => {
  10. beforeEach(() => {
  11. encoder.setRedundancy(1);
  12. });
  13. it('adds redundancy on the first packet', () => {
  14. const spy = jasmine.createSpy();
  15. encoder.addRedundancy({
  16. data: new Uint8Array([ 0x00 ]),
  17. timestamp: 0
  18. }, { enqueue: spy });
  19. expect(spy.calls.count()).toEqual(1);
  20. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0x00 ]).buffer);
  21. });
  22. it('adds redundancy on the first and second packet', () => {
  23. const spy = jasmine.createSpy();
  24. encoder.addRedundancy({
  25. data: new Uint8Array([ 0xde ]),
  26. timestamp: 0
  27. }, { enqueue: spy });
  28. encoder.addRedundancy({
  29. data: new Uint8Array([ 0xad, 0xbe ]),
  30. timestamp: 960
  31. }, { enqueue: spy });
  32. expect(spy.calls.count()).toEqual(2);
  33. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  34. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([
  35. 0xef, 0x0f, 0x00, 0x01, 0x6f, 0xde, 0xad, 0xbe ]).buffer);
  36. });
  37. it('does not add redundancy for the first packet on the third packet', () => {
  38. const spy = jasmine.createSpy();
  39. encoder.addRedundancy({
  40. data: new Uint8Array([ 0xde ]),
  41. timestamp: 0
  42. }, { enqueue: spy });
  43. encoder.addRedundancy({
  44. data: new Uint8Array([ 0xad, 0xbe ]),
  45. timestamp: 960
  46. }, { enqueue: spy });
  47. encoder.addRedundancy({
  48. data: new Uint8Array([ 0xef, 0xff, 0xff ]),
  49. timestamp: 1920
  50. }, { enqueue: spy });
  51. expect(spy.calls.count()).toEqual(3);
  52. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  53. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([
  54. 0xef, 0x0f, 0x00, 0x01, 0x6f, 0xde, 0xad, 0xbe ]).buffer);
  55. expect(spy.calls.argsFor(2)[0].data).toEqual(new Uint8Array([
  56. 0xef, 0x0f, 0x00, 0x02, 0x6f, 0xad, 0xbe, 0xef, 0xff, 0xff ]).buffer);
  57. });
  58. it('does not add redundancy for DTX packets with a 400ms timestamp gap', () => {
  59. const spy = jasmine.createSpy();
  60. encoder.addRedundancy({
  61. data: new Uint8Array([ 0xde ]),
  62. timestamp: 0
  63. }, { enqueue: spy });
  64. encoder.addRedundancy({
  65. data: new Uint8Array([ 0xad, 0xbe ]),
  66. timestamp: 19200
  67. }, { enqueue: spy });
  68. encoder.addRedundancy({
  69. data: new Uint8Array([ 0xef, 0xff, 0xff ]),
  70. timestamp: 20160
  71. }, { enqueue: spy });
  72. expect(spy.calls.count()).toEqual(3);
  73. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  74. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([ 0x6f, 0xad, 0xbe ]).buffer);
  75. expect(spy.calls.argsFor(2)[0].data).toEqual(new Uint8Array([
  76. 0xef, 0x0f, 0x00, 0x02, 0x6f, 0xad, 0xbe, 0xef, 0xff, 0xff ]).buffer);
  77. });
  78. });
  79. describe('addRedundancy with a redundancy of 2', () => {
  80. beforeEach(() => {
  81. encoder.setRedundancy(2);
  82. });
  83. it('adds redundancy on the first, second and third packet', () => {
  84. const spy = jasmine.createSpy();
  85. encoder.addRedundancy({
  86. data: new Uint8Array([ 0xde ]),
  87. timestamp: 0
  88. }, { enqueue: spy });
  89. encoder.addRedundancy({
  90. data: new Uint8Array([ 0xad, 0xbe ]),
  91. timestamp: 960
  92. }, { enqueue: spy });
  93. encoder.addRedundancy({
  94. data: new Uint8Array([ 0xef, 0xff, 0xff ]),
  95. timestamp: 1920
  96. }, { enqueue: spy });
  97. expect(spy.calls.count()).toEqual(3);
  98. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  99. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([
  100. 0xef, 0x0f, 0x00, 0x01, 0x6f, 0xde, 0xad, 0xbe ]).buffer);
  101. expect(spy.calls.argsFor(2)[0].data).toEqual(new Uint8Array([
  102. 0xef, 0x1e, 0x00, 0x01, 0xef, 0x0f, 0x00, 0x02, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0xff, 0xff ]).buffer);
  103. });
  104. it('does not add redundancy for the first packet on the fourth packet', () => {
  105. const spy = jasmine.createSpy();
  106. encoder.addRedundancy({
  107. data: new Uint8Array([ 0xde ]),
  108. timestamp: 0
  109. }, { enqueue: spy });
  110. encoder.addRedundancy({
  111. data: new Uint8Array([ 0xad, 0xbe ]),
  112. timestamp: 960
  113. }, { enqueue: spy });
  114. encoder.addRedundancy({
  115. data: new Uint8Array([ 0xef, 0xff, 0xff ]),
  116. timestamp: 1920
  117. }, { enqueue: spy });
  118. encoder.addRedundancy({
  119. data: new Uint8Array([ 0xfa, 0x1f, 0xfa, 0x1f ]),
  120. timestamp: 2880
  121. }, { enqueue: spy });
  122. expect(spy.calls.count()).toEqual(4);
  123. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  124. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([
  125. 0xef, 0x0f, 0x00, 0x01, 0x6f, 0xde, 0xad, 0xbe ]).buffer);
  126. expect(spy.calls.argsFor(2)[0].data).toEqual(new Uint8Array([
  127. 0xef, 0x1e, 0x00, 0x01, 0xef, 0x0f, 0x00, 0x02, 0x6f, 0xde, 0xad, 0xbe, 0xef, 0xff, 0xff ]).buffer);
  128. expect(spy.calls.argsFor(3)[0].data).toEqual(new Uint8Array([
  129. 0xef, 0x1e, 0x00, 0x02, 0xef, 0x0f, 0x00, 0x03, 0x6f,
  130. 0xad, 0xbe, 0xef, 0xff, 0xff, 0xfa, 0x1f, 0xfa, 0x1f ]).buffer);
  131. });
  132. });
  133. describe('setRedundancy', () => {
  134. it('reduces the redundancy', () => {
  135. const spy = jasmine.createSpy();
  136. encoder.setRedundancy(2);
  137. encoder.addRedundancy({
  138. data: new Uint8Array([ 0xde ]),
  139. timestamp: 0
  140. }, { enqueue: spy });
  141. encoder.addRedundancy({
  142. data: new Uint8Array([ 0xad, 0xbe ]),
  143. timestamp: 960
  144. }, { enqueue: spy });
  145. encoder.setRedundancy(1);
  146. encoder.addRedundancy({
  147. data: new Uint8Array([ 0xef, 0xff, 0xff ]),
  148. timestamp: 1920
  149. }, { enqueue: spy });
  150. expect(spy.calls.count()).toEqual(3);
  151. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  152. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([
  153. 0xef, 0x0f, 0x00, 0x01, 0x6f, 0xde, 0xad, 0xbe ]).buffer);
  154. expect(spy.calls.argsFor(2)[0].data).toEqual(new Uint8Array([
  155. 0xef, 0x0f, 0x00, 0x02, 0x6f, 0xad, 0xbe, 0xef, 0xff, 0xff ]).buffer);
  156. });
  157. it('increases the redundancy', () => {
  158. const spy = jasmine.createSpy();
  159. encoder.addRedundancy({
  160. data: new Uint8Array([ 0xde ]),
  161. timestamp: 0
  162. }, { enqueue: spy });
  163. encoder.setRedundancy(2);
  164. encoder.addRedundancy({
  165. data: new Uint8Array([ 0xad, 0xbe ]),
  166. timestamp: 960
  167. }, { enqueue: spy });
  168. encoder.addRedundancy({
  169. data: new Uint8Array([ 0xef, 0xff, 0xff ]),
  170. timestamp: 1920
  171. }, { enqueue: spy });
  172. expect(spy.calls.count()).toEqual(3);
  173. expect(spy.calls.argsFor(0)[0].data).toEqual(new Uint8Array([ 0x6f, 0xde ]).buffer);
  174. expect(spy.calls.argsFor(1)[0].data).toEqual(new Uint8Array([
  175. 0xef, 0x0f, 0x00, 0x01, 0x6f, 0xde, 0xad, 0xbe ]).buffer);
  176. expect(spy.calls.argsFor(2)[0].data).toEqual(new Uint8Array([
  177. 0xef, 0x1e, 0x00, 0x01, 0xef, 0x0f, 0x00, 0x02,
  178. 0x6f, 0xde, 0xad, 0xbe, 0xef, 0xff, 0xff ]).buffer);
  179. });
  180. });
  181. });