Skip to content

Commit

Permalink
Implement a generic jsonrpc2 call/notify function; fix issue with mic…
Browse files Browse the repository at this point in the history
… switching while muted (#129)

* Implement a genric jsonrpc2 call/notify function

* Update readme for signal import

* mark offer/join async

* Always remove prev track (fixes issue for switching mics while muted)
  • Loading branch information
billylindeman authored Dec 9, 2020
1 parent 7e8823f commit b4ffaf0
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Frontend sdk for the Ion backend.

```ts
import { Client, LocalStream, RemoteStream } from 'ion-sdk-js';
import { IonSFUJSONRPCSignal } from 'ion-sdk-js/lib/signal/json-rpc-impl';
const signal = new IonSFUJSONRPCSignal("wss://ion-sfu:7000");
const client = new Client(signal);
signal.onopen = () => client.join("test session")
Expand Down
60 changes: 23 additions & 37 deletions src/signal/json-rpc-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ class IonSFUJSONRPCSignal implements Signal {
});
}

join(sid: string, offer: RTCSessionDescriptionInit) {
const id = uuidv4();
// JsonRPC2 Call
async call<T>(method: string, params: any): Promise<T> {
const id = uuidv4()
this.socket.send(
JSON.stringify({
method: 'join',
params: { sid, offer },
method,
params,
id,
}),
);

return new Promise<RTCSessionDescriptionInit>((resolve, reject) => {
return new Promise<T>((resolve, reject) => {
const handler = (event: MessageEvent<any>) => {
const resp = JSON.parse(event.data);
if (resp.id === id) {
Expand All @@ -55,48 +56,33 @@ class IonSFUJSONRPCSignal implements Signal {
}
};
this.socket.addEventListener('message', handler);
});
})
}

trickle(trickle: Trickle) {
// JsonRPC2 Notification
notify(method: string, params: any) {
this.socket.send(
JSON.stringify({
method: 'trickle',
params: trickle,
}),
);
method,
params
})
)
}

offer(offer: RTCSessionDescriptionInit) {
const id = uuidv4();
this.socket.send(
JSON.stringify({
method: 'offer',
params: { desc: offer },
id,
}),
);
async join(sid: string, offer: RTCSessionDescriptionInit) {
return this.call<RTCSessionDescriptionInit>('join', {sid, offer})
}

return new Promise<RTCSessionDescriptionInit>((resolve, reject) => {
const handler = (event: MessageEvent<any>) => {
const resp = JSON.parse(event.data);
if (resp.id === id) {
if (resp.error) reject(resp.error);
else resolve(resp.result);
this.socket.removeEventListener('message', handler);
}
};
this.socket.addEventListener('message', handler);
});
trickle(trickle: Trickle) {
this.notify('trickle', trickle)
}

async offer(offer: RTCSessionDescriptionInit) {
return this.call<RTCSessionDescriptionInit>('offer', {desc: offer})
}

answer(answer: RTCSessionDescriptionInit) {
this.socket.send(
JSON.stringify({
method: 'answer',
params: { desc: answer },
}),
);
this.notify('answer', {desc: answer})
}

close() {
Expand Down
2 changes: 1 addition & 1 deletion src/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class LocalStream extends MediaStream {
this.addTrack(next);

// If published, replace published track with track from new device
if (prev && prev.enabled) {
if (prev) {
this.removeTrack(prev);
prev.stop();

Expand Down
Loading

0 comments on commit b4ffaf0

Please sign in to comment.