Skip to content
This repository has been archived by the owner on Jul 20, 2024. It is now read-only.

Commit

Permalink
fix: sender receiver create logic (#51)
Browse files Browse the repository at this point in the history
* dont request updateSdp if not required, auto alloc 3 audio receivers with mixer mode

* fix: wrong receiver name, missing audio mix minus predefined receivers, wrong lazy receiver track addTrack logic

* refactor createReceiver api

* chore: lint
  • Loading branch information
giangndm authored Jan 30, 2024
1 parent 2f528a0 commit e96dfdc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"lint": "eslint \"*/**/*.{ts,js,json}\"",
"lint:fix": "eslint \"*/**/*.{ts,js,json}\" --fix",
"build": "rollup --config ./rollup.config.mjs",
"dev": "rollup --config ./rollup.config.mjs --watch",
"prepublishOnly": "npm run doc && npm run lint && npm run test && npm run build",
"config:prerelease": "node ./scripts/configurePrerelease.mjs"
},
Expand Down
11 changes: 7 additions & 4 deletions src/lib/core/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ export class RealtimeSocket extends TypedEventEmitter<IRealtimeSocketCallbacks>
receiver.stream.getTracks().length === 0 &&
receiver.info.kind === track.kind
) {
receiver.stream = stream;
receiver.addTrack(track);
}
}
Expand Down Expand Up @@ -131,8 +130,8 @@ export class RealtimeSocket extends TypedEventEmitter<IRealtimeSocketCallbacks>
this.logger.log('connect :: try connect to media server:', serverUrl);

const offer = await this._pc.createOffer({
offerToReceiveAudio: true,
offerToReceiveVideo: true,
offerToReceiveAudio: this._countReceiver(StreamKinds.AUDIO) > 0,
offerToReceiveVideo: this._countReceiver(StreamKinds.VIDEO) > 0,
});
this.logger.log('connect :: transceivers:', this._pc.getTransceivers());
this.logger.debug('connect :: created offer:', offer.sdp);
Expand Down Expand Up @@ -228,13 +227,13 @@ export class RealtimeSocket extends TypedEventEmitter<IRealtimeSocketCallbacks>
}

public createReceiverTrack(
id: string,
kind: StreamKinds,
opts?: {
codecs?: Codecs[];
latencyMode?: LatencyMode;
},
): ReceiverTrack {
const id = `${kind}_${this._countReceiver(kind)}`;
this.logger.log('createReceiverTrack :: (id, kind):', id, kind);
const transceiver = this._pc.addTransceiver(kind, {
direction: 'recvonly',
Expand Down Expand Up @@ -338,4 +337,8 @@ export class RealtimeSocket extends TypedEventEmitter<IRealtimeSocketCallbacks>
await delay(500);
this._pc?.close();
}

_countReceiver(kind: StreamKinds) {
return Array.from(this._recvStreams.values()).filter((s) => s.info.kind === kind).length;
}
}
3 changes: 1 addition & 2 deletions src/lib/interfaces/rtsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ export interface IRealtimeSocket extends TypedEventEmitter<IRealtimeSocketCallba

/**
* Creates a `ReceiverTrack` with the specified ID and kind.
* @param id - The ID of the receiver track.
* @param kind - The kind of the receiver track.
* @returns The created receiver track.
*/
createReceiverTrack(id: string, kind: StreamKinds): ReceiverTrack;
createReceiverTrack(kind: StreamKinds): ReceiverTrack;

/**
* Creates a `SenderTrack` with the specified configuration.
Expand Down
26 changes: 20 additions & 6 deletions src/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export class Session extends TypedEventEmitter<ISessionCallbacks> {
});
this._rpc = new RPC(this._socket);
if (_cfg.mixMinusAudio) {
if (_cfg.receivers) {
_cfg.receivers.audio = (_cfg.receivers.audio || 0) + 3;
} else {
_cfg.receivers = { audio: 3, video: 0 };
}
this._mixminus = new ReceiverMixMinusAudio('default', this, this._rpc, _cfg.mixMinusAudio.elements);
}
this._rpc.on('stream_added', this.onStreamEvent);
Expand All @@ -114,15 +119,15 @@ export class Session extends TypedEventEmitter<ISessionCallbacks> {

if (this._cfg.receivers?.video) {
for (let i = 0; i < this._cfg.receivers.video; i++) {
const recvrTrack = this._socket.createReceiverTrack(`video_${i}`, StreamKinds.VIDEO);
const recvrTrack = this._socket.createReceiverTrack(StreamKinds.VIDEO);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
this._videoReceivers.push(receiver);
}
}

if (this._cfg.receivers?.audio) {
for (let i = 0; i < this._cfg.receivers.audio; i++) {
const recvrTrack = this._socket.createReceiverTrack(`audio_${i}`, StreamKinds.AUDIO);
const recvrTrack = this._socket.createReceiverTrack(StreamKinds.AUDIO);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
this._audioReceivers.push(receiver);
}
Expand Down Expand Up @@ -178,7 +183,9 @@ export class Session extends TypedEventEmitter<ISessionCallbacks> {
if (sender.kind === StreamKinds.VIDEO) {
this._videoSenders.delete(sender.name);
}
this.update();
if (this.wasConnected) {
this.update();
}
};

async disconnect() {
Expand Down Expand Up @@ -211,20 +218,26 @@ export class Session extends TypedEventEmitter<ISessionCallbacks> {
if (cfg.kind === StreamKinds.VIDEO) {
this._videoSenders.set(cfg.name, sender);
}
this.update();
if (this.wasConnected) {
this.logger.info('create sender after connected, update sdp');
this.update();
}
return sender;
}

createReceiver(kind: StreamKinds) {
const recvrTrack = this._socket.createReceiverTrack(`${kind}_${this._audioReceivers.length}`, kind);
const recvrTrack = this._socket.createReceiverTrack(kind);
const receiver = new StreamReceiver(this._rpc, recvrTrack, this._streams);
if (kind === StreamKinds.AUDIO) {
this._audioReceivers.push(receiver);
}
if (kind === StreamKinds.VIDEO) {
this._videoReceivers.push(receiver);
}
this.update();
if (this.wasConnected) {
this.logger.info('create receiver after connected, update sdp');
this.update();
}
return receiver;
}

Expand Down Expand Up @@ -274,6 +287,7 @@ export class Session extends TypedEventEmitter<ISessionCallbacks> {
});

private async updateSdp() {
this.logger.info('will update sdp now');
const { offer, meta } = await this._socket.generateOffer();
this.logger.info('send updated sdp:', meta);
const res = await this._rpc!.request<{ sdp: string }>('peer.updateSdp', meta);
Expand Down

0 comments on commit e96dfdc

Please sign in to comment.