Skip to content

Commit

Permalink
feat: use the latest protocol (preferred_publish_options)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlaz committed Nov 14, 2024
1 parent b463392 commit fd81f09
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 80 deletions.
35 changes: 21 additions & 14 deletions packages/client/src/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ import { ReconnectDetails } from './gen/video/sfu/event/events';
import {
ClientDetails,
Codec,
PublishOptions,
PublishOption,
TrackType,
WebsocketReconnectStrategy,
} from './gen/video/sfu/models/models';
Expand Down Expand Up @@ -204,7 +204,7 @@ export class Call {
private readonly dispatcher = new Dispatcher();

private clientPublishOptions?: ClientPublishOptions;
private initialPublishOptions?: PublishOptions;
private initialPublishOptions?: PublishOption[];
private statsReporter?: StatsReporter;
private sfuStatsReporter?: SfuStatsReporter;
private dropTimeout: ReturnType<typeof setTimeout> | undefined;
Expand Down Expand Up @@ -803,7 +803,9 @@ export class Call {
clientDetails,
fastReconnect: performingFastReconnect,
reconnectDetails,
preferredCodec: this.getPreferredCodec(publishingCapabilitiesSdp),
preferredPublishOptions: this.getPreferredCodec(
publishingCapabilitiesSdp,
),
});

this.initialPublishOptions = publishOptions;
Expand Down Expand Up @@ -836,7 +838,7 @@ export class Call {
connectionConfig,
clientDetails,
statsOptions,
publishOptions: this.initialPublishOptions!,
publishOptions: this.initialPublishOptions || [],
closePreviousInstances: !performingMigration,
});
}
Expand Down Expand Up @@ -906,20 +908,25 @@ export class Call {
* This is an experimental client feature and subject to change.
* @internal
*/
private getPreferredCodec = (sdp: string): Codec | undefined => {
private getPreferredCodec = (sdp: string): PublishOption[] => {
const { preferredCodec } = this.clientPublishOptions || {};
if (!preferredCodec) return;
if (!preferredCodec) return [];

const codec = findCodec(`video/${preferredCodec}`);
if (!codec) return;
if (!codec) return [];

const { clockRate, mimeType, sdpFmtpLine } = codec;
return Codec.create({
name: preferredCodec, // e.g. 'vp9'
fmtp: sdpFmtpLine || '',
clockRate: clockRate,
payloadType: getPayloadTypeForCodec(sdp, mimeType, sdpFmtpLine),
});
return [
PublishOption.create({
trackType: TrackType.VIDEO,
codec: Codec.create({
name: preferredCodec, // e.g. 'vp9'
fmtp: sdpFmtpLine || '',
clockRate: clockRate,
payloadType: getPayloadTypeForCodec(sdp, mimeType, sdpFmtpLine),
}),
}),
];
};

/**
Expand Down Expand Up @@ -960,7 +967,7 @@ export class Call {
connectionConfig: RTCConfiguration;
statsOptions: StatsOptions;
clientDetails: ClientDetails;
publishOptions: PublishOptions;
publishOptions: PublishOption[];
closePreviousInstances: boolean;
}) => {
const {
Expand Down
59 changes: 31 additions & 28 deletions packages/client/src/gen/video/sfu/event/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,25 @@
// @generated from protobuf file "video/sfu/event/events.proto" (package "stream.video.sfu.event", syntax proto3)
// tslint:disable
import { MessageType } from '@protobuf-ts/runtime';
import {
CallEndedReason,
CallGrants,
CallState,
ClientDetails,
Codec,
ConnectionQuality,
Error as Error$,
GoAwayReason,
ICETrickle as ICETrickle$,
Participant,
ParticipantCount,
PeerType,
Pin,
PublishOption,
PublishOptions,
TrackInfo,
TrackType,
TrackUnpublishReason,
WebsocketReconnectStrategy,
} from '../models/models';
import { CallEndedReason } from '../models/models';
import { GoAwayReason } from '../models/models';
import { CallGrants } from '../models/models';
import { Codec } from '../models/models';
import { ConnectionQuality } from '../models/models';
import { CallState } from '../models/models';
import { TrackSubscriptionDetails } from '../signal_rpc/signal';

import { TrackInfo } from '../models/models';
import { ClientDetails } from '../models/models';
import { TrackUnpublishReason } from '../models/models';
import { Participant } from '../models/models';
import { TrackType } from '../models/models';
import { ParticipantCount } from '../models/models';
import { PeerType } from '../models/models';
import { WebsocketReconnectStrategy } from '../models/models';
import { Error as Error$ } from '../models/models';
import { Pin } from '../models/models';
import { PublishOption } from '../models/models';
import { ICETrickle as ICETrickle$ } from '../models/models';
/**
* SFUEvent is a message that is sent from the SFU to the client.
*
Expand Down Expand Up @@ -508,9 +504,9 @@ export interface JoinRequest {
*/
reconnectDetails?: ReconnectDetails;
/**
* @generated from protobuf field: stream.video.sfu.models.Codec preferred_codec = 9;
* @generated from protobuf field: repeated stream.video.sfu.models.PublishOption preferred_publish_options = 9;
*/
preferredCodec?: Codec;
preferredPublishOptions: PublishOption[];
}
/**
* @generated from protobuf message stream.video.sfu.event.ReconnectDetails
Expand Down Expand Up @@ -577,9 +573,9 @@ export interface JoinResponse {
*/
fastReconnectDeadlineSeconds: number;
/**
* @generated from protobuf field: stream.video.sfu.models.PublishOptions publish_options = 4;
* @generated from protobuf field: repeated stream.video.sfu.models.PublishOption publish_options = 4;
*/
publishOptions?: PublishOptions;
publishOptions: PublishOption[];
}
/**
* ParticipantJoined is fired when a user joins a call
Expand Down Expand Up @@ -1313,7 +1309,13 @@ class JoinRequest$Type extends MessageType<JoinRequest> {
kind: 'message',
T: () => ReconnectDetails,
},
{ no: 9, name: 'preferred_codec', kind: 'message', T: () => Codec },
{
no: 9,
name: 'preferred_publish_options',
kind: 'message',
repeat: 1 /*RepeatType.PACKED*/,
T: () => PublishOption,
},
]);
}
}
Expand Down Expand Up @@ -1421,7 +1423,8 @@ class JoinResponse$Type extends MessageType<JoinResponse> {
no: 4,
name: 'publish_options',
kind: 'message',
T: () => PublishOptions,
repeat: 1 /*RepeatType.PACKED*/,
T: () => PublishOption,
},
]);
}
Expand Down
28 changes: 0 additions & 28 deletions packages/client/src/gen/video/sfu/models/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { MessageType } from '@protobuf-ts/runtime';
import { Struct } from '../../../google/protobuf/struct';
import { Timestamp } from '../../../google/protobuf/timestamp';

/**
* CallState is the current state of the call
* as seen by an SFU.
Expand Down Expand Up @@ -194,15 +193,6 @@ export interface VideoLayer {
*/
quality: VideoQuality;
}
/**
* @generated from protobuf message stream.video.sfu.models.PublishOptions
*/
export interface PublishOptions {
/**
* @generated from protobuf field: repeated stream.video.sfu.models.PublishOption codecs = 1;
*/
codecs: PublishOption[];
}
/**
* @generated from protobuf message stream.video.sfu.models.PublishOption
*/
Expand Down Expand Up @@ -1103,24 +1093,6 @@ class VideoLayer$Type extends MessageType<VideoLayer> {
*/
export const VideoLayer = new VideoLayer$Type();
// @generated message type with reflection information, may provide speed optimized methods
class PublishOptions$Type extends MessageType<PublishOptions> {
constructor() {
super('stream.video.sfu.models.PublishOptions', [
{
no: 1,
name: 'codecs',
kind: 'message',
repeat: 1 /*RepeatType.PACKED*/,
T: () => PublishOption,
},
]);
}
}
/**
* @generated MessageType for protobuf message stream.video.sfu.models.PublishOptions
*/
export const PublishOptions = new PublishOptions$Type();
// @generated message type with reflection information, may provide speed optimized methods
class PublishOption$Type extends MessageType<PublishOption> {
constructor() {
super('stream.video.sfu.models.PublishOption', [
Expand Down
19 changes: 9 additions & 10 deletions packages/client/src/rtc/Publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
import {
PeerType,
PublishOption,
PublishOptions,
TrackInfo,
TrackType,
VideoLayer,
Expand All @@ -25,7 +24,7 @@ import { TargetResolutionResponse } from '../gen/shims';
import { withoutConcurrency } from '../helpers/concurrency';

export type PublisherConstructorOpts = BasePeerConnectionOpts & {
publishOptions: PublishOptions;
publishOptions: PublishOption[];
};

/**
Expand All @@ -49,7 +48,7 @@ export class Publisher extends BasePeerConnection {
private readonly unsubscribeChangePublishOptions: () => void;
private unsubscribeCodecNegotiationComplete?: () => void;

private publishOptions: PublishOptions;
private publishOptions: PublishOption[];

/**
* Constructs a new `Publisher` instance.
Expand Down Expand Up @@ -90,11 +89,10 @@ export class Publisher extends BasePeerConnection {
({ publishOption }) => {
withoutConcurrency('publisher.changePublishOptions', async () => {
if (!publishOption) return;
this.publishOptions.codecs = this.publishOptions.codecs.map(
(option) =>
option.trackType === publishOption.trackType
? publishOption
: option,
this.publishOptions = this.publishOptions.map((option) =>
option.trackType === publishOption.trackType
? publishOption
: option,
);
if (this.isPublishing(publishOption.trackType)) {
this.switchCodec(publishOption);
Expand Down Expand Up @@ -539,8 +537,9 @@ export class Publisher extends BasePeerConnection {
};

private getPublishOptionFor = (trackType: TrackType) => {
const options = this.publishOptions.codecs;
const publishOption = options.find((o) => o.trackType === trackType);
const publishOption = this.publishOptions.find(
(option) => option.trackType === trackType,
);
if (!publishOption) {
throw new Error(`No publish options found for ${TrackType[trackType]}`);
}
Expand Down

0 comments on commit fd81f09

Please sign in to comment.