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

Allow createEncodedStreams on PCs without encodedInsertableStreams parameter #300

Closed
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
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 @@
stopTracks = true,
disableTrackOnPause = true,
zeroRtpOnPause = false,
onRtpSender,
appData = {} as ProducerAppData,
}: ProducerOptions<ProducerAppData> = {}): Promise<
Producer<ProducerAppData>
Expand Down Expand Up @@ -570,6 +571,7 @@
encodings: normalizedEncodings,
codecOptions,
codec,
onRtpSender,
});

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

Expand Down Expand Up @@ -876,13 +880,14 @@
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 @@
encodings,
codecOptions,
codec,
onRtpSender,
}: HandlerSendOptions): Promise<HandlerSendResult> {
this.assertNotClosed();
this.assertSendDirection();
Expand Down Expand Up @@ -378,6 +379,11 @@
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 @@

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;
};

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

** please

* 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;
};

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

* 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
Loading