Skip to content

Commit

Permalink
fix serializeAudioLevelIndication
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Dec 13, 2023
1 parent 2717adc commit a50154c
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/rtp/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "werift-rtp",
"version": "0.7.25",
"version": "0.7.26",
"description": "RTP,RTCP,SRTP,SRTCP implementation for TypeScript.",
"homepage": "https://github.com/shinyoshiaki/werift-webrtc",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/rtp/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const timer = {
args[0]();
},
//@ts-ignore
...args.slice(1)
...args.slice(1),
);
return () => clearInterval(id);
},
Expand Down
7 changes: 5 additions & 2 deletions packages/rtp/src/rtp/headerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ export function rtpHeaderExtensionsParser(
return extensions
.map((extension) => {
const uri = extIdUriMap[extension.id];
if (!uri) {
return { uri: "unknown", value: extension.payload };
}
switch (uri) {
case RTP_EXTENSION_URI.sdesMid:
case RTP_EXTENSION_URI.sdesRTPStreamID:
Expand All @@ -43,7 +46,7 @@ export function rtpHeaderExtensionsParser(
};
}
default:
return { uri, value: 0 };
return { uri, value: extension.payload };
}
})
.reduce((acc: { [uri: string]: any }, cur) => {
Expand Down Expand Up @@ -78,7 +81,7 @@ export function serializeAbsSendTime(ntpTime: bigint) {
export function serializeAudioLevelIndication(level: number) {
const stream = new BitStream(Buffer.alloc(1));
stream.writeBits(1, 1);
stream.writeBits(level, 7);
stream.writeBits(7, level);

return stream.uint8Array;
}
Expand Down
31 changes: 30 additions & 1 deletion packages/rtp/tests/rtp/packet.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RtpPacket } from "../../src/rtp/rtp";
import { ExtensionProfiles, RtpHeader, RtpPacket } from "../../src/rtp/rtp";
import { load } from "../utils";

describe("packet", () => {
Expand Down Expand Up @@ -125,4 +125,33 @@ describe("packet", () => {
const buf = p.serialize();
expect(buf).toEqual(data);
});

test("serialize_deserialize", () => {
const packet = new RtpPacket(
new RtpHeader({
csrc: [],
csrcLength: 0,
extension: true,
extensionProfile: ExtensionProfiles.OneByte,
extensions: [
{ id: 1, payload: Buffer.from([48]) },
{ id: 2, payload: Buffer.from([40, 1, 222]) },
{ id: 10, payload: Buffer.from([128]) },
],
marker: false,
padding: false,
paddingSize: 0,
payloadType: 109,
sequenceNumber: 15546,
ssrc: 2882400001,
timestamp: 144,
version: 2,
}),
Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
);
const buf = packet.serialize();
const parsed = RtpPacket.deSerialize(buf);
const buf2 = parsed.serialize();
expect(buf2).toEqual(buf);
});
});
2 changes: 1 addition & 1 deletion packages/webrtc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "werift",
"version": "0.18.16",
"version": "0.18.17",
"description": "WebRTC Implementation for TypeScript (Node.js)",
"keywords": [
"WebRTC",
Expand Down
44 changes: 44 additions & 0 deletions packages/webrtc/tests/integrate/mediaChannel.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
deserializeAudioLevelIndication,
isMedia,
MediaStreamTrack,
RTCPeerConnection,
RtpHeader,
RtpPacket,
serializeAudioLevelIndication,
useSdesMid,
} from "../../src";

Expand Down Expand Up @@ -122,4 +124,46 @@ describe("media", () => {
);
await pc1.setRemoteDescription(pc2.localDescription!);
});

test("test_sendonly_recvonly_audioLevel", async () =>
new Promise<void>(async (done) => {
const sendonly = new RTCPeerConnection();
const track = new MediaStreamTrack({ kind: "audio" });
sendonly.addTransceiver(track, { direction: "sendonly" });
sendonly.connectionStateChange
.watch((v) => v === "connected")
.then(() => {
const rtpPacket = new RtpPacket(
new RtpHeader({
extensions: [
{ id: 10, payload: serializeAudioLevelIndication(25) },
],
}),
Buffer.from("test")
).serialize();
expect(isMedia(rtpPacket)).toBe(true);
track.writeRtp(rtpPacket);
});

const recvonly = new RTCPeerConnection();
recvonly.onRemoteTransceiverAdded.subscribe((transceiver) => {
transceiver.onTrack.subscribe((track) => {
track.onReceiveRtp.subscribe(async (rtp) => {
expect(rtp.header.extensions[0].id).toBe(10);
expect(
deserializeAudioLevelIndication(rtp.header.extensions[0].payload)
.level
).toEqual(25);
expect(rtp.payload).toEqual(Buffer.from("test"));
await Promise.all([sendonly.close(), recvonly.close()]);
done();
});
});
});

await sendonly.setLocalDescription(await sendonly.createOffer());
await recvonly.setRemoteDescription(sendonly.localDescription!);
await recvonly.setLocalDescription(await recvonly.createAnswer());
await sendonly.setRemoteDescription(recvonly.localDescription!);
}));
});

0 comments on commit a50154c

Please sign in to comment.