Skip to content

Commit

Permalink
Merge branch 'release/2020.4.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki Ito committed Oct 30, 2020
2 parents 88579b8 + 1f02ca9 commit 8754e2f
Show file tree
Hide file tree
Showing 14 changed files with 1,091 additions and 192 deletions.
22 changes: 22 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,28 @@
- FIX
- バグ修正

## 2020.4.0

- [CHANGE] signaling 時に処理に失敗した場合の reject の引数を CloseEvent オブジェクトから Error オブジェクトに変更する
- @yuitowest
- [CHANGE] connect() のタイムアウト処理にデフォルト値を設定する
- 60000 ms でタイムアウトするように設定する
- @yuitowest
- [UPDATE] connect() 実行時に PeerConnection connectionState が 'connected' になったら処理が完了するように変更する
- @yuitowest
- [UPDATE] disconnect 処理を修正する
- websocket で type: "disconnect" を send するように変更する
- websocket の readyState の監視をやめる
- peerConnection の 切断監視を signalingState から connectionState に変更する
- @yuitowest
- [UPDATE] sora-e2ee のバージョンを 2020.3.0 に更新する
- @yuitowest
- [FIX] `package.json` に定義されている `module` の向き先を `dist/sora.mjs` に変更し、対象ファイルがビルドされるよう Rollup の設定を追加する
- https://github.com/shiguredo/sora-js-sdk/pull/44
- @rosylilly
- [UPDATE] simulcast で active パラメーターを有効にするための実装を追加する
- @yuitowest

## 2020.3.0

- [UPDATE] Safari 14 以降で Simulcast が使えるように変更
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ In other languages, we won't be able to deal with them. Thank you for your under
## システム条件

- WebRTC SFU Sora 2020.1 以降
- TypeScript 3.8 以降

## サンプル

Expand Down
2 changes: 2 additions & 0 deletions dist/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export default class ConnectionBase {
protected sendAnswer(): void;
protected sendUpdateAnswer(): void;
protected onIceCandidate(): Promise<void>;
protected waitChangeConnectionStateConnected(): Promise<void>;
protected setConnectionTimeout(): Promise<MediaStream>;
protected trace(title: string, message: any): void;
private update;
private setSenderParameters;
Expand Down
168 changes: 78 additions & 90 deletions dist/sora.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/sora.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/sora.min.js.map

Large diffs are not rendered by default.

884 changes: 884 additions & 0 deletions dist/sora.mjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "sora-js-sdk",
"version": "2020.3.0",
"version": "2020.4.0",
"description": "WebRTC SFU Sora JavaScript SDK",
"main": "dist/sora.min.js",
"module": "dist/sora.min.js",
"module": "dist/sora.mjs",
"types": "dist/sora.d.ts",
"scripts": {
"build": "NODE_ENV=development rollup -c",
Expand Down Expand Up @@ -46,6 +46,6 @@
"typescript": "^3.8.3"
},
"dependencies": {
"sora-e2ee": "^2020.2.0"
"sora-e2ee": "^2020.3.0"
}
}
20 changes: 20 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,25 @@ export default [
name: 'Sora',
banner: banner
}
},
{
input: 'src/sora.ts',
plugins: [
replace({
SORA_JS_SDK_VERSION: `'${pkg.version}'`
}),
resolve(),
typescript({
tsconfig: './tsconfig.json'
}),
commonjs(),
],
output: {
sourcemap: false,
file: 'dist/sora.mjs',
format: 'module',
name: 'Sora',
banner: banner
}
}
];
100 changes: 64 additions & 36 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export default class ConnectionBase {
this.metadata = metadata;
this.signalingUrl = signalingUrl;
this.options = options;
// client timeout の初期値をセットする
if (this.options.timeout === undefined) {
this.options.timeout = 60000;
}
this.constraints = null;
this.debug = debug;
this.clientId = null;
Expand Down Expand Up @@ -99,56 +103,34 @@ export default class ConnectionBase {
this.stream = null;
return resolve();
});
const closeWebSocket: Promise<void> = new Promise((resolve, reject) => {
const closeWebSocket: Promise<void> = new Promise((resolve, _reject) => {
if (!this.ws) return resolve();

let counter = 5;
const timerId = setInterval(() => {
if (!this.ws) {
clearInterval(timerId);
return resolve();
}
if (this.ws.readyState === 3) {
this.ws = null;
clearInterval(timerId);
return resolve();
}
--counter;
if (counter < 0) {
clearInterval(timerId);
return reject("WebSocket Closing Error");
}
}, 1000);
if (this.ws.readyState === 1) {
this.ws.send(JSON.stringify({ type: "disconnect" }));
}
this.ws.close();
this.ws = null;
return resolve();
});
const closePeerConnection: Promise<void> = new Promise((resolve, reject) => {
// Safari は signalingState が常に stable のため個別に処理する
if (isSafari() && this.pc) {
this.pc.oniceconnectionstatechange = null;
this.pc.close();
this.pc = null;
return resolve();
}
if (!this.pc || this.pc.signalingState === "closed") return resolve();

let counter = 5;
const closePeerConnection: Promise<void> = new Promise((resolve, _reject) => {
if (!this.pc || this.pc.connectionState === "closed" || this.pc.connectionState === undefined) return resolve();
let counter = 50;
const timerId = setInterval(() => {
if (!this.pc) {
clearInterval(timerId);
return resolve();
}
if (this.pc.signalingState === "closed") {
if (this.pc.connectionState === "closed") {
clearInterval(timerId);
this.pc.oniceconnectionstatechange = null;
this.pc = null;
return resolve();
}
--counter;
if (counter < 0) {
clearInterval(timerId);
return reject("PeerConnection Closing Error");
return resolve();
}
}, 1000);
}, 100);
this.pc.close();
});
if (this.e2ee) {
Expand Down Expand Up @@ -181,8 +163,10 @@ export default class ConnectionBase {
if (this.ws === null) {
this.ws = new WebSocket(this.signalingUrl);
}
this.ws.onclose = (e): void => {
reject(e);
this.ws.onclose = (event): void => {
const error = new Error();
error.message = `Signaling failed. CloseEventCode:${event.code} CloseEventReason:'${event.reason}'`;
reject(error);
};
this.ws.onopen = (): void => {
this.trace("SIGNALING CONNECT MESSAGE", signalingMessage);
Expand Down Expand Up @@ -296,6 +280,8 @@ export default class ConnectionBase {
}
await this.setSenderParameters(transceiver, message.encodings);
await this.setRemoteDescription(message);
// setRemoteDescription 後でないと active が反映されないのでもう一度呼ぶ
await this.setSenderParameters(transceiver, message.encodings);
}
const sessionDescription = await this.pc.createAnswer();
await this.pc.setLocalDescription(sessionDescription);
Expand Down Expand Up @@ -351,6 +337,48 @@ export default class ConnectionBase {
}
});
}

protected waitChangeConnectionStateConnected(): Promise<void> {
return new Promise((resolve, reject) => {
// connectionState が存在しない場合はそのまま抜ける
if (this.pc && this.pc.connectionState === undefined) {
resolve();
}
const timerId = setInterval(() => {
if (!this.pc) {
const error = new Error();
error.message = "PeerConnection connectionState did not change to 'connected'";
clearInterval(timerId);
reject(error);
} else if (!this.ws || this.ws.readyState !== 1) {
const error = new Error();
error.message = "PeerConnection connectionState did not change to 'connected'";
clearInterval(timerId);
reject(error);
} else if (this.pc && this.pc.connectionState === "connected") {
clearInterval(timerId);
resolve();
}
}, 100);
});
}

protected setConnectionTimeout(): Promise<MediaStream> {
return new Promise((_, reject) => {
if (this.options.timeout && 0 < this.options.timeout) {
setTimeout(() => {
if (this.pc && this.pc.connectionState !== "connected") {
const error = new Error();
error.message = "CONNECTION TIMEOUT";
this.callbacks.timeout();
this.disconnect();
reject(error);
}
}, this.options.timeout);
}
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
protected trace(title: string, message: any): void {
this.callbacks.log(title, message);
Expand Down
31 changes: 5 additions & 26 deletions src/publisher.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,15 @@
import ConnectionBase from "./base";

export default class ConnectionPublisher extends ConnectionBase {
connect(stream: MediaStream): Promise<MediaStream> {
async connect(stream: MediaStream): Promise<MediaStream> {
if (this.options.multistream) {
return this.multiStream(stream);
return await Promise.race([this.multiStream(stream), this.setConnectionTimeout()]);
} else {
return this.singleStream(stream);
return await Promise.race([this.singleStream(stream), this.setConnectionTimeout()]);
}
}

private async singleStream(stream: MediaStream): Promise<MediaStream> {
let timeoutTimerId = 0;
if (this.options.timeout && 0 < this.options.timeout) {
timeoutTimerId = setTimeout(() => {
const error = new Error();
error.message = "CONNECTION TIMEOUT";
this.callbacks.timeout();
this.disconnect();
Promise.reject(error);
}, this.options.timeout);
}
await this.disconnect();
this.startE2EE();
const offer = await this.createOffer();
Expand All @@ -42,22 +32,11 @@ export default class ConnectionPublisher extends ConnectionBase {
});
}
await this.onIceCandidate();
clearTimeout(timeoutTimerId);
await this.waitChangeConnectionStateConnected();
return stream;
}

private async multiStream(stream: MediaStream): Promise<MediaStream> {
let timeoutTimerId = 0;
if (this.options.timeout && 0 < this.options.timeout) {
timeoutTimerId = setTimeout(() => {
const error = new Error();
error.message = "CONNECTION TIMEOUT";
this.callbacks.timeout();
this.disconnect();
Promise.reject(error);
}, this.options.timeout);
}

await this.disconnect();
this.startE2EE();
const offer = await this.createOffer();
Expand Down Expand Up @@ -110,7 +89,7 @@ export default class ConnectionPublisher extends ConnectionBase {
});
}
await this.onIceCandidate();
clearTimeout(timeoutTimerId);
await this.waitChangeConnectionStateConnected();
return stream;
}
}
32 changes: 5 additions & 27 deletions src/subscriber.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
import ConnectionBase from "./base";

export default class ConnectionSubscriber extends ConnectionBase {
connect(): Promise<MediaStream | void> {
async connect(): Promise<MediaStream | void> {
if (this.options.multistream) {
return this.multiStream();
return await Promise.race([this.multiStream(), this.setConnectionTimeout()]);
} else {
return this.singleStream();
return await Promise.race([this.singleStream(), this.setConnectionTimeout()]);
}
}

private async singleStream(): Promise<MediaStream> {
let timeoutTimerId = 0;
if (this.options.timeout && 0 < this.options.timeout) {
timeoutTimerId = setTimeout(() => {
const error = new Error();
error.message = "CONNECTION TIMEOUT";
this.callbacks.timeout();
this.disconnect();
Promise.reject(error);
}, this.options.timeout);
}

await this.disconnect();
this.startE2EE();
const offer = await this.createOffer();
Expand Down Expand Up @@ -59,22 +48,11 @@ export default class ConnectionSubscriber extends ConnectionBase {
await this.createAnswer(signalingMessage);
this.sendAnswer();
await this.onIceCandidate();
clearTimeout(timeoutTimerId);
await this.waitChangeConnectionStateConnected();
return this.stream || new MediaStream();
}

private async multiStream(): Promise<void> {
let timeoutTimerId = 0;
if (this.options.timeout && 0 < this.options.timeout) {
timeoutTimerId = setTimeout(() => {
const error = new Error();
error.message = "CONNECTION TIMEOUT";
this.callbacks.timeout();
this.disconnect();
Promise.reject(error);
}, this.options.timeout);
}

await this.disconnect();
this.startE2EE();
const offer = await this.createOffer();
Expand Down Expand Up @@ -113,7 +91,7 @@ export default class ConnectionSubscriber extends ConnectionBase {
await this.createAnswer(signalingMessage);
this.sendAnswer();
await this.onIceCandidate();
clearTimeout(timeoutTimerId);
await this.waitChangeConnectionStateConnected();
return;
}
}
1 change: 0 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function enabledSimulcast(role: Role, video: SignalingVideo): boolean {
return false;
}
if (browser() === "safari") {
console.log("safari");
const appVersion = window.navigator.appVersion.toLowerCase();
const versions = /version\/([\d.]+)/.exec(appVersion);
if (!versions) {
Expand Down
10 changes: 4 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2165,8 +2165,6 @@ get-stream@^4.0.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-4.1.0.tgz#c1b255575f3dc21d59bfc79cd3d2b46b1c3a54b5"
integrity sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==
dependencies:
pump "^3.0.0"

get-stream@^5.0.0:
version "5.1.0"
Expand Down Expand Up @@ -4119,10 +4117,10 @@ snapdragon@^0.8.1:
source-map-resolve "^0.5.0"
use "^3.1.0"

sora-e2ee@^2020.2.0:
version "2020.2.0"
resolved "https://registry.yarnpkg.com/sora-e2ee/-/sora-e2ee-2020.2.0.tgz#332ec78f1bce0b3cf843edfe0bf460ce1deba168"
integrity sha512-beP2iXJphHxyTQ1ngYg4b+KlOH4RHhWdWxixsR57KXD9ZHggNhlDSCw0ydw5RtkDL8rbrxM043Qgcq3XVgeLDQ==
sora-e2ee@^2020.3.0:
version "2020.3.0"
resolved "https://registry.yarnpkg.com/sora-e2ee/-/sora-e2ee-2020.3.0.tgz#0dd2e952e2c98044dbeeb7ba97a1cf36b1b571d5"
integrity sha512-DEKkG3MH05D5t4iUvp230JzV9sc3CT/OzzHMN7FyyQ6aRt59wc2+9TLO3BY2f3RPXTDJMyLbM+ca8X8oroXq0A==

source-map-resolve@^0.5.0:
version "0.5.3"
Expand Down

0 comments on commit 8754e2f

Please sign in to comment.