Skip to content

Commit

Permalink
more
Browse files Browse the repository at this point in the history
  • Loading branch information
ibc committed Aug 11, 2023
1 parent e041524 commit a38dc68
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/rtcp/XrPacket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,64 @@ export function parseChunk(chunk: number): ExtendedReportChunk
{
return { chunkType: 'terminating-null' };
}

// Bit Vector Chunk.
if (readBit({ value: chunk, bit: 15 }))
{
return {
chunkType : 'bit-vector',
bitVector : chunk & 0b0111111111111111
};
}
// Run Length Chunk.
else
{
return {
chunkType : 'run-length',
runType : readBit({ value: chunk, bit: 14 }) ? 'ones' : 'zeros',
runLength : chunk & 0b0011111111111111
};
}
}

/**
* Create a Run Length Chunk.
* Create a Run Length Chunk and return a 2 bytes number representing it.
*/
export function createRunLengthChunk(
runType: 'zeros' | 'ones',
runLength: number
): void
): number
{
// TODO
if (runLength < 0 || runLength > 16383)
{
throw new TypeError('invalid run length value');
}

let chunk = runLength;

chunk = writeBit({ value: chunk, bit: 15, flag: false });
chunk = writeBit(
{ value: chunk, bit: 14, flag: runType === 'ones' ? true : false }
);

return chunk;
}

/**
* Create a Bit Vector Chunk.
* Create a Bit Vector Chunk and return a 2 bytes number representing it.
*/
export function createBitVectorChunk(bitVector: number): void
export function createBitVectorChunk(bitVector: number): number
{
// TODO
if (bitVector < 0 || bitVector > 32767)
{
throw new TypeError('invalid bit vector value');
}

let chunk = bitVector;

chunk = writeBit({ value: chunk, bit: 15, flag: true });

return chunk;
}

/**
Expand Down
55 changes: 55 additions & 0 deletions src/tests/rtcp/XrPacket.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
parseChunk,
createRunLengthChunk,
createBitVectorChunk
} from '../../RTCP/XrPacket';

describe('chunks parsing and creation', () =>
{
const runLengthZerosChunk = 0b0010101010101010;
const runLengthOnesChunk = 0b0110101010101010;
const bitVectorChunk = 0b1110101010101010;
const terminatingNullChunk = 0;

test('parseChunk()', () =>
{
expect(parseChunk(runLengthZerosChunk)).toEqual(
{
chunkType : 'run-length',
runType : 'zeros',
runLength : 0b10101010101010
}
);

expect(parseChunk(runLengthOnesChunk)).toEqual(
{
chunkType : 'run-length',
runType : 'ones',
runLength : 0b10101010101010
}
);

expect(parseChunk(bitVectorChunk)).toEqual(
{
chunkType : 'bit-vector',
bitVector : 0b110101010101010
}
);

expect(parseChunk(terminatingNullChunk)).toEqual(
{ chunkType: 'terminating-null' }
);
});

test('createRunLengthChunk()', () =>
{
expect(createRunLengthChunk('zeros', 0b10101010101010))
.toBe(runLengthZerosChunk);

expect(createRunLengthChunk('ones', 0b10101010101010))
.toBe(runLengthOnesChunk);

expect(createBitVectorChunk(0b110101010101010))
.toBe(bitVectorChunk);
});
});

0 comments on commit a38dc68

Please sign in to comment.