Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for sending abs-capture-time RTP extension #311

Open
wants to merge 6 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/handlers/Chrome74.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,33 @@ export class Chrome74 extends HandlerInterface {
});
let offer = await this._pc.createOffer();
let localSdpObject = sdpTransform.parse(offer.sdp);
let offerMediaObject;
let offerMediaObject = localSdpObject.media[mediaSectionIdx.idx];

// May force abs-capture-time RTP extension negotiation.
{
const exten = sdpCommonUtils.addRtpExtensionToMediaObject(
{
mediaObject : offerMediaObject,
uri : 'http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time'
});

if (exten)
{
offer = { type: 'offer', sdp: sdpTransform.write(localSdpObject) };

sdpCommonUtils.addRtpExtensionToRtpParameters(
{
rtpParameters : sendingRtpParameters,
extension : exten
});

sdpCommonUtils.addRtpExtensionToRtpParameters(
{
rtpParameters : sendingRemoteRtpParameters,
extension : exten
});
}
}

if (!this._transportReady) {
await this.setupTransport({
Expand Down
28 changes: 27 additions & 1 deletion src/handlers/ReactNativeUnifiedPlan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,33 @@ export class ReactNativeUnifiedPlan extends HandlerInterface {

let offer = await this._pc.createOffer();
let localSdpObject = sdpTransform.parse(offer.sdp);
let offerMediaObject;
let offerMediaObject = localSdpObject.media[mediaSectionIdx.idx];

// May force abs-capture-time RTP extension negotiation.
{
const exten = sdpCommonUtils.addRtpExtensionToMediaObject(
{
mediaObject : offerMediaObject,
uri : 'http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time'
});

if (exten)
{
offer = { type: 'offer', sdp: sdpTransform.write(localSdpObject) };

sdpCommonUtils.addRtpExtensionToRtpParameters(
{
rtpParameters : sendingRtpParameters,
extension : exten
});

sdpCommonUtils.addRtpExtensionToRtpParameters(
{
rtpParameters : sendingRemoteRtpParameters,
extension : exten
});
}
}

if (!this._transportReady) {
await this.setupTransport({
Expand Down
87 changes: 87 additions & 0 deletions src/handlers/sdp/commonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
RtpCapabilities,
RtpCodecCapability,
RtpHeaderExtension,
RtpHeaderExtensionParameters,
RtpParameters,
RtcpFeedback,
RtpHeaderExtensionUri,
} from '../../RtpParameters';

/**
Expand Down Expand Up @@ -285,3 +287,88 @@ export function applyCodecParameters({
}
}
}

/**
* Adds the given RTP extension to the given SDP media object and returns a
* RtpHeaderExtensionParameters object.
* If the extension is already present, this function doesn't add anything and
* doesn't return anything.
*/
export function addRtpExtensionToMediaObject(
{ mediaObject, uri }:
{ mediaObject: any; uri: RtpHeaderExtensionUri }
): RtpHeaderExtensionParameters | undefined
{
if (!Array.isArray(mediaObject.ext))
{
mediaObject.ext = [];
}

let id = 1;

for (const exten of mediaObject.ext)
{
// If extension uri is already present, don't do anything.
if (exten.uri === uri)
{
id = 0;

break;
}

if (exten.value >= id)
id = exten.value + 1;
}

if (id > 0)
{
mediaObject.ext.push({ value: id, uri });

// NOTE: No support for encrypt/parameters fields.
return { uri, id };
}
}

/**
* Adds the given RTP extension to the given RTP parameters.
* If the extension is already present (with same id), this function doesn't
* add anything. If the extension is present with a different id, then existing
* one is removed and the new one is added.
*/
export function addRtpExtensionToRtpParameters(
{
rtpParameters,
extension
}:
{
rtpParameters: RtpParameters;
extension: RtpHeaderExtensionParameters;
}
): void
{
if (!Array.isArray(rtpParameters.headerExtensions))
{
rtpParameters.headerExtensions = [];
}

let replaced = false;

for (const exten of rtpParameters.headerExtensions)
{
if (exten.uri === extension.uri && exten.id === extension.id)
{
return;
}
else if (exten.uri === extension.uri && exten.id !== extension.id)
{
exten.id = extension.id;

replaced = true;
}
}

if (!replaced)
{
rtpParameters.headerExtensions.push(extension);
}
}