Skip to content

Commit

Permalink
Add onRtcSender and onRtcReceiver callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvenalainen committed Jun 12, 2024
1 parent 921eb1d commit f0d659d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
import { InvalidStateError } from './errors';
import { MediaKind, RtpParameters } from './RtpParameters';
import { AppData } from './types';
import { AppData, ReceiverCallback } from './types';

const logger = new Logger('Consumer');

Expand All @@ -12,6 +12,7 @@ export type ConsumerOptions<ConsumerAppData extends AppData = AppData> = {
kind?: 'audio' | 'video';
rtpParameters: RtpParameters;
streamId?: string;
onRtpReceiver?: ReceiverCallback;
appData?: ConsumerAppData;
};

Expand Down
3 changes: 2 additions & 1 deletion src/Producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
RtpParameters,
RtpEncodingParameters,
} from './RtpParameters';
import { AppData } from './types';
import { AppData, SenderCallback } from './types';

const logger = new Logger('Producer');

Expand All @@ -19,6 +19,7 @@ export type ProducerOptions<ProducerAppData extends AppData = AppData> = {
stopTracks?: boolean;
disableTrackOnPause?: boolean;
zeroRtpOnPause?: boolean;
onRtpSender?: SenderCallback;
appData?: ProducerAppData;
};

Expand Down
7 changes: 6 additions & 1 deletion src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ export class Transport<
stopTracks = true,
disableTrackOnPause = true,
zeroRtpOnPause = false,
onRtpSender,
appData = {} as ProducerAppData,
}: ProducerOptions<ProducerAppData> = {}): Promise<
Producer<ProducerAppData>
Expand Down Expand Up @@ -570,6 +571,7 @@ export class Transport<
encodings: normalizedEncodings,
codecOptions,
codec,
onRtpSender,
});

try {
Expand Down Expand Up @@ -639,6 +641,7 @@ export class Transport<
kind,
rtpParameters,
streamId,
onRtpReceiver,
appData = {} as ConsumerAppData,
}: ConsumerOptions<ConsumerAppData>): Promise<Consumer<ConsumerAppData>> {
logger.debug('consume()');
Expand Down Expand Up @@ -681,6 +684,7 @@ export class Transport<
kind,
rtpParameters: clonedRtpParameters,
streamId,
onRtpReceiver,
appData,
});

Expand Down Expand Up @@ -876,13 +880,14 @@ export class Transport<
const optionsList: HandlerReceiveOptions[] = [];

for (const task of pendingConsumerTasks) {
const { id, kind, rtpParameters, streamId } = task.consumerOptions;
const { id, kind, rtpParameters, streamId, onRtpReceiver } = task.consumerOptions;

Check failure on line 883 in src/Transport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04, 18)

Replace `·` with `⏎↹↹↹↹↹↹`

Check failure on line 883 in src/Transport.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Replace `·` with `⏎↹↹↹↹↹↹`

Check failure on line 883 in src/Transport.ts

View workflow job for this annotation

GitHub Actions / ci (macos-14, 20)

Replace `·` with `⏎↹↹↹↹↹↹`

optionsList.push({
trackId: id!,
kind: kind as MediaKind,
rtpParameters,
streamId,
onRtpReceiver,
});
}

Expand Down
22 changes: 22 additions & 0 deletions src/handlers/Chrome111.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export class Chrome111 extends HandlerInterface {
encodings,
codecOptions,
codec,
onRtpSender,
}: HandlerSendOptions): Promise<HandlerSendResult> {
this.assertNotClosed();
this.assertSendDirection();
Expand Down Expand Up @@ -378,6 +379,11 @@ export class Chrome111 extends HandlerInterface {
streams: [this._sendStream],
sendEncodings: encodings,
});

if (onRtpSender) {
onRtpSender(transceiver.sender);
}

const offer = await this._pc.createOffer();
let localSdpObject = sdpTransform.parse(offer.sdp);

Expand Down Expand Up @@ -826,6 +832,22 @@ export class Chrome111 extends HandlerInterface {

await this._pc.setRemoteDescription(offer);

for (const options of optionsList) {
const { trackId, onRtpReceiver } = options;

if (onRtpReceiver) {
const localId = mapLocalId.get(trackId);
const transceiver = this._pc.getTransceivers()

Check failure on line 840 in src/handlers/Chrome111.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04, 18)

Insert `⏎↹↹↹↹↹`

Check failure on line 840 in src/handlers/Chrome111.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Insert `⏎↹↹↹↹↹`

Check failure on line 840 in src/handlers/Chrome111.ts

View workflow job for this annotation

GitHub Actions / ci (macos-14, 20)

Insert `⏎↹↹↹↹↹`
.find((t: RTCRtpTransceiver) => t.mid === localId);

Check failure on line 842 in src/handlers/Chrome111.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-20.04, 18)

Delete `↹↹↹↹`

Check failure on line 842 in src/handlers/Chrome111.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-22.04, 20)

Delete `↹↹↹↹`

Check failure on line 842 in src/handlers/Chrome111.ts

View workflow job for this annotation

GitHub Actions / ci (macos-14, 20)

Delete `↹↹↹↹`
if (!transceiver) {
throw new Error('transceiver not found');
}

onRtpReceiver(transceiver.receiver);
}
}

let answer = await this._pc.createAnswer();
const localSdpObject = sdpTransform.parse(answer.sdp);

Expand Down
14 changes: 14 additions & 0 deletions src/handlers/HandlerInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ export type HandlerRunOptions = {
extendedRtpCapabilities: any;
};

/*
* Invoked synchronously immediately after a new RTCRtpSender is created.
* This allows for creating encoded streams in chromium browsers.
*/
export type SenderCallback = (sender: RTCRtpSender) => void;

export type HandlerSendOptions = {
track: MediaStreamTrack;
encodings?: RtpEncodingParameters[];
codecOptions?: ProducerCodecOptions;
codec?: RtpCodecCapability;
onRtpSender?: SenderCallback;
};

export type HandlerSendResult = {
Expand All @@ -47,6 +54,12 @@ export type HandlerSendResult = {
rtpSender?: RTCRtpSender;
};

/*
* Invoked synchronously immediately after a new RTCRtpReceiver is created.
* This allows for creating encoded streams in chromium browsers.
*/
export type ReceiverCallback = (receiver: RTCRtpReceiver) => void;

export type HandlerReceiveOptions = {
trackId: string;
kind: 'audio' | 'video';
Expand All @@ -58,6 +71,7 @@ export type HandlerReceiveOptions = {
* can just synchronize up to one audio stream with one video stream.
*/
streamId?: string;
onRtpReceiver?: ReceiverCallback;
};

export type HandlerReceiveResult = {
Expand Down

0 comments on commit f0d659d

Please sign in to comment.