Skip to content

Commit

Permalink
rtp marker tweaks on webrtc talkback (#1187)
Browse files Browse the repository at this point in the history
* set rtp marker

* set marker if last packet was recevied 1s+ ago

* fix after merge

* reorder

* set marker if last packet was recevied 1s+ ago
  • Loading branch information
bjia56 authored Nov 18, 2023
1 parent 7460c71 commit eaeae02
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 5 additions & 2 deletions plugins/webrtc/src/ffmpeg-to-wrtc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export async function createTrackForwarder(options: {
}

let opusRepacketizer: OpusRepacketizer;
let lastPacketTs: number = 0;
const audioRtpTrack: RtpTrack = {
codecCopy: audioCodecCopy,
onRtp: buffer => {
Expand All @@ -230,11 +231,13 @@ export async function createTrackForwarder(options: {
}
else {
const rtp = RtpPacket.deSerialize(buffer);
rtp.header.marker = false;
const now = Date.now();
rtp.header.marker = now - lastPacketTs > 1000; // set the marker if it's been more than 1s since the last packet
rtp.header.payloadType = audioTransceiver.sender.codec.payloadType;
// pcm audio can be concatenated.
// hikvision seems to send 40ms duration packets, so 25 packets per second.
audioTransceiver.sender.sendRtp(rtp.serialize());
lastPacketTs = now;
}
},
encoderArguments: [
Expand All @@ -257,7 +260,7 @@ export async function createTrackForwarder(options: {
// 1/9/2023:
// 1378 is what homekit requests, regardless of local or remote network.
// so setting 1378 as the fixed value seems wise, given apple probably has
// better knowledge of network capabilities, and also mirrors
// better knowledge of network capabilities, and also mirrors
// from my cursory research into ipv6, the MTU is no lesser than ipv4, in fact
// the min mtu is larger.
const videoPacketSize = 1378;
Expand Down
5 changes: 4 additions & 1 deletion plugins/webrtc/src/wrtc-to-rtsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,18 @@ export async function createRTCPeerConnectionSource(options: {

const ffmpegInput = await mediaManager.convertMediaObjectToJSON<FFmpegInput>(media, ScryptedMimeTypes.FFmpegInput);

let lastPacketTs: number = 0;
const { kill: destroy } = await startRtpForwarderProcess(console, ffmpegInput, {
audio: {
codecCopy: audioCodec.name,
encoderArguments: getFFmpegRtpAudioOutputArguments(ffmpegInput.mediaStreamOptions?.audio?.codec, audioTransceiver.sender.codec, maximumCompatibilityMode),
onRtp: (rtp) => {
const packet = RtpPacket.deSerialize(rtp);
const now = Date.now();
packet.header.payloadType = audioCodec.payloadType;
packet.header.marker = false;
packet.header.marker = now - lastPacketTs > 1000; // set the marker if it's been more than 1s since the last packet
audioTransceiver.sender.sendRtp(packet.serialize());
lastPacketTs = now;
},
},
});
Expand Down

0 comments on commit eaeae02

Please sign in to comment.