From 88914e4934c8aa88f63704cb6ef97a770323f8b2 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 17:53:00 +0900 Subject: [PATCH 1/9] =?UTF-8?q?signaling=20message=20=E3=82=92=20ws/dc=20?= =?UTF-8?q?=E3=81=A7=E5=88=86=E3=81=91=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 7 ++++--- packages/sdk/src/types.ts | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index 7efc1214..0fa1c0e5 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -7,10 +7,10 @@ import type { Callbacks, ConnectionOptions, DataChannelConfiguration, + DataChannelSignalingMessage, JSONType, SignalingCloseMessage, SignalingConnectMessage, - SignalingMessage, SignalingNotifyMessage, SignalingOfferMessage, SignalingOfferMessageDataChannel, @@ -26,6 +26,7 @@ import type { SoraCloseEventInitDict, SoraCloseEventType, TransportType, + WebSocketSignalingMessage, } from './types' import { ConnectError, @@ -1185,7 +1186,7 @@ export default class ConnectionBase { if (typeof event.data !== 'string') { throw new Error('Received invalid signaling data') } - const message = JSON.parse(event.data) as SignalingMessage + const message = JSON.parse(event.data) as WebSocketSignalingMessage if (message.type === 'offer') { this.writeWebSocketSignalingLog('onmessage-offer', message) this.signalingOnMessageTypeOffer(message) @@ -2034,7 +2035,7 @@ export default class ConnectionBase { return } const data = await parseDataChannelEventData(event.data, dataChannelSettings.compress) - const message = JSON.parse(data) as SignalingMessage + const message = JSON.parse(data) as DataChannelSignalingMessage this.writeDataChannelSignalingLog(`onmessage-${message.type}`, channel, message) if (message.type === 're-offer') { await this.signalingOnMessageTypeReOffer(message) diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index e3d454c5..d72efd83 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -84,17 +84,19 @@ export type SignalingConnectMessage = { forwarding_filter?: JSONType } -export type SignalingMessage = +export type WebSocketSignalingMessage = + | SignalingConnectMessage | SignalingOfferMessage | SignalingUpdateMessage | SignalingReOfferMessage | SignalingPingMessage | SignalingPushMessage | SignalingNotifyMessage - | SignalingReqStatsMessage | SignalingSwitchedMessage | SignalingRedirectMessage - | SignalingCloseMessage + +export type DataChannelSignalingMessage = SignalingReOfferMessage | SignalingCloseMessage + export type SignalingOfferMessageDataChannel = { label: string direction: DataChannelDirection From 6d8496753806232b79244528b98aaf542fbac473 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 18:23:27 +0900 Subject: [PATCH 2/9] =?UTF-8?q?=E5=AE=9A=E6=95=B0=E5=8C=96=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 25 ++++++++++++++++--------- packages/sdk/src/constants.ts | 6 ++++++ packages/sdk/src/types.ts | 15 ++++++++++++--- 3 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 packages/sdk/src/constants.ts diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index 0fa1c0e5..a8b5173b 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -1,3 +1,10 @@ +import { + SORA_ROLE_RECVONLY, + SORA_ROLE_SENDONLY, + SORA_ROLE_SENDRECV, + TRANSPORT_TYPE_DATACHANNEL, + TRANSPORT_TYPE_WEBSOCKET, +} from './constants' import { DisconnectDataChannelError, DisconnectInternalError, @@ -1201,14 +1208,14 @@ export default class ConnectionBase { } else if (message.type === 'ping') { await this.signalingOnMessageTypePing(message) } else if (message.type === 'push') { - this.callbacks.push(message, 'websocket') + this.callbacks.push(message, TRANSPORT_TYPE_WEBSOCKET) } else if (message.type === 'notify') { if (message.event_type === 'connection.created') { this.writeWebSocketTimelineLog('notify-connection.created', message) } else if (message.event_type === 'connection.destroyed') { this.writeWebSocketTimelineLog('notify-connection.destroyed', message) } - this.signalingOnMessageTypeNotify(message, 'websocket') + this.signalingOnMessageTypeNotify(message, TRANSPORT_TYPE_WEBSOCKET) } else if (message.type === 'switched') { this.writeWebSocketSignalingLog('onmessage-switched', message) this.signalingOnMessageTypeSwitched(message) @@ -1343,12 +1350,12 @@ export default class ConnectionBase { // mid と transceiver.direction を合わせる for (const mid of Object.values(this.mids)) { const transceiver = this.pc.getTransceivers().find((t) => t.mid === mid) - if (transceiver && transceiver.direction === 'recvonly') { - transceiver.direction = 'sendrecv' + if (transceiver && transceiver.direction === SORA_ROLE_RECVONLY) { + transceiver.direction = SORA_ROLE_SENDRECV } } // simulcast の場合 - if (this.simulcast && (this.role === 'sendrecv' || this.role === 'sendonly')) { + if (this.simulcast && (this.role === SORA_ROLE_SENDRECV || this.role === SORA_ROLE_SENDONLY)) { const transceiver = this.pc.getTransceivers().find((t) => { if (t.mid === null) { return @@ -1356,7 +1363,7 @@ export default class ConnectionBase { if (t.sender.track === null) { return } - if (t.currentDirection !== null && t.currentDirection !== 'sendonly') { + if (t.currentDirection !== null && t.currentDirection !== SORA_ROLE_SENDONLY) { return } if (this.mids.video !== '' && this.mids.video === t.mid) { @@ -1674,7 +1681,7 @@ export default class ConnectionBase { * @param data - イベントデータ */ protected writeWebSocketSignalingLog(eventType: string, data?: unknown): void { - this.callbacks.signaling(createSignalingEvent(eventType, data, 'websocket')) + this.callbacks.signaling(createSignalingEvent(eventType, data, TRANSPORT_TYPE_WEBSOCKET)) this.writeWebSocketTimelineLog(eventType, data) } @@ -1689,7 +1696,7 @@ export default class ConnectionBase { channel: RTCDataChannel, data?: unknown, ): void { - this.callbacks.signaling(createSignalingEvent(eventType, data, 'datachannel')) + this.callbacks.signaling(createSignalingEvent(eventType, data, TRANSPORT_TYPE_DATACHANNEL)) this.writeDataChannelTimelineLog(eventType, channel, data) } @@ -1700,7 +1707,7 @@ export default class ConnectionBase { * @param data - イベントデータ */ protected writeWebSocketTimelineLog(eventType: string, data?: unknown): void { - const event = createTimelineEvent(eventType, data, 'websocket') + const event = createTimelineEvent(eventType, data, TRANSPORT_TYPE_WEBSOCKET) this.callbacks.timeline(event) } diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts new file mode 100644 index 00000000..66e3a4a2 --- /dev/null +++ b/packages/sdk/src/constants.ts @@ -0,0 +1,6 @@ +export const TRANSPORT_TYPE_WEBSOCKET = 'websocket' as const +export const TRANSPORT_TYPE_DATACHANNEL = 'datachannel' as const + +export const SORA_ROLE_SENDRECV = 'sendrecv' as const +export const SORA_ROLE_SENDONLY = 'sendonly' as const +export const SORA_ROLE_RECVONLY = 'recvonly' as const diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index d72efd83..074c35c8 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,3 +1,11 @@ +import type { + SORA_ROLE_RECVONLY, + SORA_ROLE_SENDONLY, + SORA_ROLE_SENDRECV, + TRANSPORT_TYPE_DATACHANNEL, + TRANSPORT_TYPE_WEBSOCKET, +} from './constants' + export type JSONType = | null | boolean @@ -44,7 +52,7 @@ export type SignalingVideo = av1_params?: JSONType } -export type Role = 'sendrecv' | 'sendonly' | 'recvonly' +export type Role = typeof SORA_ROLE_SENDRECV | typeof SORA_ROLE_SENDONLY | typeof SORA_ROLE_RECVONLY export type SignalingConnectDataChannel = { label?: string @@ -352,9 +360,10 @@ export type Callbacks = { export type Browser = 'edge' | 'chrome' | 'safari' | 'opera' | 'firefox' | null -export type TransportType = 'websocket' | 'datachannel' | 'peerconnection' +export type TransportType = typeof TRANSPORT_TYPE_WEBSOCKET | typeof TRANSPORT_TYPE_DATACHANNEL +export type SignalingMessageDirection = 'sent' | 'received' -export type TimelineEventLogType = 'websocket' | 'datachannel' | 'peerconnection' | 'sora' +export type TimelineEventLogType = TransportType | 'peerconnection' | 'sora' export interface SignalingEvent extends Event { transportType: TransportType From 623f52f3b63d9145db0c799098a044914ee48dc5 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 18:35:21 +0900 Subject: [PATCH 3/9] =?UTF-8?q?=E5=9E=8B=E3=81=A8=E5=AE=9A=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 47 +++++++++++++++++++++++------------ packages/sdk/src/constants.ts | 19 ++++++++++++++ packages/sdk/src/types.ts | 42 +++++++++++++++++++------------ 3 files changed, 76 insertions(+), 32 deletions(-) diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index a8b5173b..c8e4fb86 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -1,4 +1,16 @@ import { + SIGNALING_MESSAGE_TYPE_ANSWER, + SIGNALING_MESSAGE_TYPE_CANDIDATE, + SIGNALING_MESSAGE_TYPE_NOTIFY, + SIGNALING_MESSAGE_TYPE_OFFER, + SIGNALING_MESSAGE_TYPE_PING, + SIGNALING_MESSAGE_TYPE_PONG, + SIGNALING_MESSAGE_TYPE_PUSH, + SIGNALING_MESSAGE_TYPE_REDIRECT, + SIGNALING_MESSAGE_TYPE_RE_ANSWER, + SIGNALING_MESSAGE_TYPE_RE_OFFER, + SIGNALING_MESSAGE_TYPE_SWITCHED, + SIGNALING_MESSAGE_TYPE_UPDATE, SORA_ROLE_RECVONLY, SORA_ROLE_SENDONLY, SORA_ROLE_SENDRECV, @@ -1194,32 +1206,32 @@ export default class ConnectionBase { throw new Error('Received invalid signaling data') } const message = JSON.parse(event.data) as WebSocketSignalingMessage - if (message.type === 'offer') { + if (message.type === SIGNALING_MESSAGE_TYPE_OFFER) { this.writeWebSocketSignalingLog('onmessage-offer', message) this.signalingOnMessageTypeOffer(message) this.connectedSignalingUrl = ws.url resolve(message) - } else if (message.type === 'update') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_UPDATE) { this.writeWebSocketSignalingLog('onmessage-update', message) await this.signalingOnMessageTypeUpdate(message) - } else if (message.type === 're-offer') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_RE_OFFER) { this.writeWebSocketSignalingLog('onmessage-re-offer', message) await this.signalingOnMessageTypeReOffer(message) - } else if (message.type === 'ping') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_PING) { await this.signalingOnMessageTypePing(message) - } else if (message.type === 'push') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_PUSH) { this.callbacks.push(message, TRANSPORT_TYPE_WEBSOCKET) - } else if (message.type === 'notify') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_NOTIFY) { if (message.event_type === 'connection.created') { this.writeWebSocketTimelineLog('notify-connection.created', message) } else if (message.event_type === 'connection.destroyed') { this.writeWebSocketTimelineLog('notify-connection.destroyed', message) } this.signalingOnMessageTypeNotify(message, TRANSPORT_TYPE_WEBSOCKET) - } else if (message.type === 'switched') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_SWITCHED) { this.writeWebSocketSignalingLog('onmessage-switched', message) this.signalingOnMessageTypeSwitched(message) - } else if (message.type === 'redirect') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_REDIRECT) { this.writeWebSocketSignalingLog('onmessage-redirect', message) try { const redirectMessage = await this.signalingOnMessageTypeRedirect(message) @@ -1325,7 +1337,7 @@ export default class ConnectionBase { const sdp = this.processOfferSdp(message.sdp) const sessionDescription = new RTCSessionDescription({ - type: 'offer', + type: SIGNALING_MESSAGE_TYPE_OFFER, sdp, }) await this.pc.setRemoteDescription(sessionDescription) @@ -1420,7 +1432,7 @@ export default class ConnectionBase { if (this.pc && this.ws && this.pc.localDescription) { this.trace('ANSWER SDP', this.pc.localDescription.sdp) const sdp = this.pc.localDescription.sdp - const message = { type: 'answer', sdp } + const message = { type: SIGNALING_MESSAGE_TYPE_ANSWER, sdp } this.ws.send(JSON.stringify(message)) this.writeWebSocketSignalingLog('send-answer', message) } @@ -1456,7 +1468,9 @@ export default class ConnectionBase { resolve() } else { const candidate = event.candidate.toJSON() - const message = Object.assign(candidate, { type: 'candidate' }) as { + const message = Object.assign(candidate, { + type: SIGNALING_MESSAGE_TYPE_CANDIDATE, + }) as { type: string [key: string]: unknown } @@ -1817,9 +1831,9 @@ export default class ConnectionBase { */ private async sendUpdateAnswer(): Promise { if (this.pc && this.ws && this.pc.localDescription) { - this.trace('ANSWER SDP', this.pc.localDescription.sdp) + this.trace('UPDATE ANSWER SDP', this.pc.localDescription.sdp) await this.sendSignalingMessage({ - type: 'update', + type: SIGNALING_MESSAGE_TYPE_UPDATE, sdp: this.pc.localDescription.sdp, }) } @@ -1827,12 +1841,13 @@ export default class ConnectionBase { /** * シグナリングサーバーに type re-answer を投げるメソッド + * @deprecated このメソッドは非推奨です。将来のバージョンで削除される可能性があります。 */ private async sendReAnswer(): Promise { if (this.pc?.localDescription) { this.trace('RE ANSWER SDP', this.pc.localDescription.sdp) await this.sendSignalingMessage({ - type: 're-answer', + type: SIGNALING_MESSAGE_TYPE_RE_ANSWER, sdp: this.pc.localDescription.sdp, }) } @@ -1880,8 +1895,8 @@ export default class ConnectionBase { * @param message - type ping メッセージ */ private async signalingOnMessageTypePing(message: SignalingPingMessage): Promise { - const pongMessage: { type: 'pong'; stats?: RTCStatsReport[] } = { - type: 'pong', + const pongMessage: { type: typeof SIGNALING_MESSAGE_TYPE_PONG; stats?: RTCStatsReport[] } = { + type: SIGNALING_MESSAGE_TYPE_PONG, } if (message.stats) { const stats = await this.getStats() diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index 66e3a4a2..ecaa5d75 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -4,3 +4,22 @@ export const TRANSPORT_TYPE_DATACHANNEL = 'datachannel' as const export const SORA_ROLE_SENDRECV = 'sendrecv' as const export const SORA_ROLE_SENDONLY = 'sendonly' as const export const SORA_ROLE_RECVONLY = 'recvonly' as const + +export const SIGNALING_MESSAGE_TYPE_CONNECT = 'connect' as const +export const SIGNALING_MESSAGE_TYPE_REDIRECT = 'redirect' as const +export const SIGNALING_MESSAGE_TYPE_OFFER = 'offer' as const +export const SIGNALING_MESSAGE_TYPE_ANSWER = 'answer' as const +export const SIGNALING_MESSAGE_TYPE_CANDIDATE = 'candidate' as const +export const SIGNALING_MESSAGE_TYPE_SWITCHED = 'switched' as const +export const SIGNALING_MESSAGE_TYPE_RE_OFFER = 're-offer' as const +export const SIGNALING_MESSAGE_TYPE_RE_ANSWER = 're-answer' as const +export const SIGNALING_MESSAGE_TYPE_CLOSE = 'close' as const +export const SIGNALING_MESSAGE_TYPE_DISCONNECT = 'disconnect' as const + +export const SIGNALING_MESSAGE_TYPE_NOTIFY = 'notify' as const +export const SIGNALING_MESSAGE_TYPE_PUSH = 'push' as const +export const SIGNALING_MESSAGE_TYPE_PING = 'ping' as const +export const SIGNALING_MESSAGE_TYPE_PONG = 'pong' as const + +// @deprecated この定数は将来的に削除される予定です +export const SIGNALING_MESSAGE_TYPE_UPDATE = 'update' as const diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 074c35c8..e66a4229 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,4 +1,14 @@ import type { + SIGNALING_MESSAGE_TYPE_CLOSE, + SIGNALING_MESSAGE_TYPE_CONNECT, + SIGNALING_MESSAGE_TYPE_NOTIFY, + SIGNALING_MESSAGE_TYPE_OFFER, + SIGNALING_MESSAGE_TYPE_PING, + SIGNALING_MESSAGE_TYPE_PUSH, + SIGNALING_MESSAGE_TYPE_REDIRECT, + SIGNALING_MESSAGE_TYPE_RE_OFFER, + SIGNALING_MESSAGE_TYPE_SWITCHED, + SIGNALING_MESSAGE_TYPE_UPDATE, SORA_ROLE_RECVONLY, SORA_ROLE_SENDONLY, SORA_ROLE_SENDRECV, @@ -65,7 +75,7 @@ export type SignalingConnectDataChannel = { } export type SignalingConnectMessage = { - type: 'connect' + type: typeof SIGNALING_MESSAGE_TYPE_CONNECT role: Role channel_id: string client_id?: string @@ -112,7 +122,7 @@ export type SignalingOfferMessageDataChannel = { } export type SignalingOfferMessage = { - type: 'offer' + type: typeof SIGNALING_MESSAGE_TYPE_OFFER sdp: string multistream: boolean @@ -141,22 +151,22 @@ export type SignalingOfferMessage = { // @deprecated この型は非推奨です。将来のバージョンで削除される可能性があります。 export type SignalingUpdateMessage = { - type: 'update' + type: typeof SIGNALING_MESSAGE_TYPE_UPDATE sdp: string } export type SignalingReOfferMessage = { - type: 're-offer' + type: typeof SIGNALING_MESSAGE_TYPE_RE_OFFER sdp: string } export type SignalingPingMessage = { - type: 'ping' + type: typeof SIGNALING_MESSAGE_TYPE_PING stats: boolean } export type SignalingPushMessage = { - type: 'push' + type: typeof SIGNALING_MESSAGE_TYPE_PUSH data: Record } @@ -165,18 +175,18 @@ export type SignalingReqStatsMessage = { } export type SignalingSwitchedMessage = { - type: 'switched' + type: typeof SIGNALING_MESSAGE_TYPE_SWITCHED ignore_disconnect_websocket: boolean } export type SignalingRedirectMessage = { - type: 'redirect' + type: typeof SIGNALING_MESSAGE_TYPE_REDIRECT location: string } // DataChannel シグナリングでのみ利用される export type SignalingCloseMessage = { - type: 'close' + type: typeof SIGNALING_MESSAGE_TYPE_CLOSE code: number reason: string } @@ -199,7 +209,7 @@ export type SignalingNotifyMetadata = { } export type SignalingNotifyConnectionCreated = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'connection.created' role: Role client_id?: string @@ -220,7 +230,7 @@ export type SignalingNotifyConnectionCreated = { } export type SignalingNotifyConnectionUpdated = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'connection.updated' role: Role client_id?: string @@ -236,7 +246,7 @@ export type SignalingNotifyConnectionUpdated = { } export type SignalingNotifyConnectionDestroyed = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'connection.destroyed' role: Role client_id?: string @@ -255,7 +265,7 @@ export type SignalingNotifyConnectionDestroyed = { } export type SignalingNotifySpotlightChanged = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'spotlight.changed' client_id: string | null connection_id: string | null @@ -266,7 +276,7 @@ export type SignalingNotifySpotlightChanged = { } export type SignalingNotifySpotlightFocused = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'spotlight.focused' client_id: string | null connection_id: string @@ -276,7 +286,7 @@ export type SignalingNotifySpotlightFocused = { } export type SignalingNotifySpotlightUnfocused = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'spotlight.unfocused' client_id: string | null connection_id: string @@ -286,7 +296,7 @@ export type SignalingNotifySpotlightUnfocused = { } export type SignalingNotifyNetworkStatus = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'network.status' unstable_level: 0 | 1 | 2 | 3 } From b938823d0891ad6521f0141e879e85ded67d99ca Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 18:38:06 +0900 Subject: [PATCH 4/9] =?UTF-8?q?=E5=AE=9A=E6=95=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 13 ++++++++----- packages/sdk/src/constants.ts | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index c8e4fb86..a48c953e 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -1,14 +1,17 @@ import { SIGNALING_MESSAGE_TYPE_ANSWER, SIGNALING_MESSAGE_TYPE_CANDIDATE, + SIGNALING_MESSAGE_TYPE_DISCONNECT, SIGNALING_MESSAGE_TYPE_NOTIFY, SIGNALING_MESSAGE_TYPE_OFFER, SIGNALING_MESSAGE_TYPE_PING, SIGNALING_MESSAGE_TYPE_PONG, SIGNALING_MESSAGE_TYPE_PUSH, SIGNALING_MESSAGE_TYPE_REDIRECT, + SIGNALING_MESSAGE_TYPE_REQ_STATS, SIGNALING_MESSAGE_TYPE_RE_ANSWER, SIGNALING_MESSAGE_TYPE_RE_OFFER, + SIGNALING_MESSAGE_TYPE_STATS, SIGNALING_MESSAGE_TYPE_SWITCHED, SIGNALING_MESSAGE_TYPE_UPDATE, SORA_ROLE_RECVONLY, @@ -698,7 +701,7 @@ export default class ConnectionBase { } // 終了処理を開始する if (this.soraDataChannels.signaling) { - const message = { type: 'disconnect', reason: title } + const message = { type: SIGNALING_MESSAGE_TYPE_DISCONNECT, reason: title } if ( this.signalingOfferMessageDataChannels.signaling && this.signalingOfferMessageDataChannels.signaling.compress === true @@ -832,7 +835,7 @@ export default class ConnectionBase { return resolve({ code: event.code, reason: event.reason }) } if (this.ws.readyState === 1) { - const message = { type: 'disconnect', reason: title } + const message = { type: SIGNALING_MESSAGE_TYPE_DISCONNECT, reason: title } this.ws.send(JSON.stringify(message)) this.writeWebSocketSignalingLog('send-disconnect', message) // WebSocket 切断を待つ @@ -910,7 +913,7 @@ export default class ConnectionBase { const dataChannelClosePromise = Promise.all(onDataChannelClosePromises) // 準備はできたのでメッセージを送る - const message = { type: 'disconnect', reason: 'NO-ERROR' } + const message = { type: SIGNALING_MESSAGE_TYPE_DISCONNECT, reason: 'NO-ERROR' } if ( this.signalingOfferMessageDataChannels.signaling && this.signalingOfferMessageDataChannels.signaling.compress === true @@ -2113,7 +2116,7 @@ export default class ConnectionBase { } const data = await parseDataChannelEventData(event.data, dataChannelSettings.compress) const message = JSON.parse(data) as SignalingReqStatsMessage - if (message.type === 'req-stats') { + if (message.type === SIGNALING_MESSAGE_TYPE_REQ_STATS) { const stats = await this.getStats() await this.sendStatsMessage(stats) } @@ -2191,7 +2194,7 @@ export default class ConnectionBase { private async sendStatsMessage(reports: RTCStatsReport[]): Promise { if (this.soraDataChannels.stats) { const message = { - type: 'stats', + type: SIGNALING_MESSAGE_TYPE_STATS, reports: reports, } if ( diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index ecaa5d75..e27f931e 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -21,5 +21,8 @@ export const SIGNALING_MESSAGE_TYPE_PUSH = 'push' as const export const SIGNALING_MESSAGE_TYPE_PING = 'ping' as const export const SIGNALING_MESSAGE_TYPE_PONG = 'pong' as const +export const SIGNALING_MESSAGE_TYPE_REQ_STATS = 'req-stats' as const +export const SIGNALING_MESSAGE_TYPE_STATS = 'stats' as const + // @deprecated この定数は将来的に削除される予定です export const SIGNALING_MESSAGE_TYPE_UPDATE = 'update' as const From ca89f9be012bd0c1eafbcf5921f525e888bcd172 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 18:47:28 +0900 Subject: [PATCH 5/9] =?UTF-8?q?=E5=AE=9A=E6=95=B0=E5=90=8D=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 17 ++++++++++------- packages/sdk/src/constants.ts | 25 +++++++++++++++---------- packages/sdk/src/types.ts | 11 +++++++---- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index a48c953e..b06bf59e 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -14,9 +14,9 @@ import { SIGNALING_MESSAGE_TYPE_STATS, SIGNALING_MESSAGE_TYPE_SWITCHED, SIGNALING_MESSAGE_TYPE_UPDATE, - SORA_ROLE_RECVONLY, - SORA_ROLE_SENDONLY, - SORA_ROLE_SENDRECV, + SIGNALING_ROLE_RECVONLY, + SIGNALING_ROLE_SENDONLY, + SIGNALING_ROLE_SENDRECV, TRANSPORT_TYPE_DATACHANNEL, TRANSPORT_TYPE_WEBSOCKET, } from './constants' @@ -1365,12 +1365,15 @@ export default class ConnectionBase { // mid と transceiver.direction を合わせる for (const mid of Object.values(this.mids)) { const transceiver = this.pc.getTransceivers().find((t) => t.mid === mid) - if (transceiver && transceiver.direction === SORA_ROLE_RECVONLY) { - transceiver.direction = SORA_ROLE_SENDRECV + if (transceiver && transceiver.direction === SIGNALING_ROLE_RECVONLY) { + transceiver.direction = SIGNALING_ROLE_SENDRECV } } // simulcast の場合 - if (this.simulcast && (this.role === SORA_ROLE_SENDRECV || this.role === SORA_ROLE_SENDONLY)) { + if ( + this.simulcast && + (this.role === SIGNALING_ROLE_SENDRECV || this.role === SIGNALING_ROLE_SENDONLY) + ) { const transceiver = this.pc.getTransceivers().find((t) => { if (t.mid === null) { return @@ -1378,7 +1381,7 @@ export default class ConnectionBase { if (t.sender.track === null) { return } - if (t.currentDirection !== null && t.currentDirection !== SORA_ROLE_SENDONLY) { + if (t.currentDirection !== null && t.currentDirection !== SIGNALING_ROLE_SENDONLY) { return } if (this.mids.video !== '' && this.mids.video === t.mid) { diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index e27f931e..db38926b 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -1,28 +1,33 @@ +// シグナリングトランスポートタイプ export const TRANSPORT_TYPE_WEBSOCKET = 'websocket' as const export const TRANSPORT_TYPE_DATACHANNEL = 'datachannel' as const -export const SORA_ROLE_SENDRECV = 'sendrecv' as const -export const SORA_ROLE_SENDONLY = 'sendonly' as const -export const SORA_ROLE_RECVONLY = 'recvonly' as const +// シグナリング ROLE +export const SIGNALING_ROLE_SENDRECV = 'sendrecv' as const +export const SIGNALING_ROLE_SENDONLY = 'sendonly' as const +export const SIGNALING_ROLE_RECVONLY = 'recvonly' as const +// WebSocket シグナリングでのみ利用する export const SIGNALING_MESSAGE_TYPE_CONNECT = 'connect' as const export const SIGNALING_MESSAGE_TYPE_REDIRECT = 'redirect' as const export const SIGNALING_MESSAGE_TYPE_OFFER = 'offer' as const export const SIGNALING_MESSAGE_TYPE_ANSWER = 'answer' as const export const SIGNALING_MESSAGE_TYPE_CANDIDATE = 'candidate' as const export const SIGNALING_MESSAGE_TYPE_SWITCHED = 'switched' as const -export const SIGNALING_MESSAGE_TYPE_RE_OFFER = 're-offer' as const -export const SIGNALING_MESSAGE_TYPE_RE_ANSWER = 're-answer' as const -export const SIGNALING_MESSAGE_TYPE_CLOSE = 'close' as const -export const SIGNALING_MESSAGE_TYPE_DISCONNECT = 'disconnect' as const - -export const SIGNALING_MESSAGE_TYPE_NOTIFY = 'notify' as const -export const SIGNALING_MESSAGE_TYPE_PUSH = 'push' as const export const SIGNALING_MESSAGE_TYPE_PING = 'ping' as const export const SIGNALING_MESSAGE_TYPE_PONG = 'pong' as const +// DataChannel シグナリングでのみ利用する export const SIGNALING_MESSAGE_TYPE_REQ_STATS = 'req-stats' as const export const SIGNALING_MESSAGE_TYPE_STATS = 'stats' as const +export const SIGNALING_MESSAGE_TYPE_CLOSE = 'close' as const + +// WebSocket と DataChannel シグナリング両方で了する +export const SIGNALING_MESSAGE_TYPE_RE_OFFER = 're-offer' as const +export const SIGNALING_MESSAGE_TYPE_RE_ANSWER = 're-answer' as const +export const SIGNALING_MESSAGE_TYPE_DISCONNECT = 'disconnect' as const +export const SIGNALING_MESSAGE_TYPE_NOTIFY = 'notify' as const +export const SIGNALING_MESSAGE_TYPE_PUSH = 'push' as const // @deprecated この定数は将来的に削除される予定です export const SIGNALING_MESSAGE_TYPE_UPDATE = 'update' as const diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index e66a4229..58a3a17c 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -9,9 +9,9 @@ import type { SIGNALING_MESSAGE_TYPE_RE_OFFER, SIGNALING_MESSAGE_TYPE_SWITCHED, SIGNALING_MESSAGE_TYPE_UPDATE, - SORA_ROLE_RECVONLY, - SORA_ROLE_SENDONLY, - SORA_ROLE_SENDRECV, + SIGNALING_ROLE_RECVONLY, + SIGNALING_ROLE_SENDONLY, + SIGNALING_ROLE_SENDRECV, TRANSPORT_TYPE_DATACHANNEL, TRANSPORT_TYPE_WEBSOCKET, } from './constants' @@ -62,7 +62,10 @@ export type SignalingVideo = av1_params?: JSONType } -export type Role = typeof SORA_ROLE_SENDRECV | typeof SORA_ROLE_SENDONLY | typeof SORA_ROLE_RECVONLY +export type Role = + | typeof SIGNALING_ROLE_SENDRECV + | typeof SIGNALING_ROLE_SENDONLY + | typeof SIGNALING_ROLE_RECVONLY export type SignalingConnectDataChannel = { label?: string From 249b8a90dca092af8da85adfec1ebb9ced15f06c Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 18:55:31 +0900 Subject: [PATCH 6/9] =?UTF-8?q?=E5=AE=9A=E6=95=B0=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 17 +++++++++++------ packages/sdk/src/constants.ts | 6 ++++++ packages/sdk/src/types.ts | 12 +++++++++++- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index b06bf59e..76290336 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -1,6 +1,11 @@ import { + DATA_CHANNEL_LABEL_NOTIFY, + DATA_CHANNEL_LABEL_PUSH, + DATA_CHANNEL_LABEL_SIGNALING, + DATA_CHANNEL_LABEL_STATS, SIGNALING_MESSAGE_TYPE_ANSWER, SIGNALING_MESSAGE_TYPE_CANDIDATE, + SIGNALING_MESSAGE_TYPE_CLOSE, SIGNALING_MESSAGE_TYPE_DISCONNECT, SIGNALING_MESSAGE_TYPE_NOTIFY, SIGNALING_MESSAGE_TYPE_OFFER, @@ -2051,7 +2056,7 @@ export default class ConnectionBase { }) } // onmessage - if (dataChannelEvent.channel.label === 'signaling') { + if (dataChannelEvent.channel.label === DATA_CHANNEL_LABEL_SIGNALING) { dataChannelEvent.channel.onmessage = async (event): Promise => { const channel = event.currentTarget as RTCDataChannel const label = channel.label @@ -2065,13 +2070,13 @@ export default class ConnectionBase { const data = await parseDataChannelEventData(event.data, dataChannelSettings.compress) const message = JSON.parse(data) as DataChannelSignalingMessage this.writeDataChannelSignalingLog(`onmessage-${message.type}`, channel, message) - if (message.type === 're-offer') { + if (message.type === SIGNALING_MESSAGE_TYPE_RE_OFFER) { await this.signalingOnMessageTypeReOffer(message) - } else if (message.type === 'close') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_CLOSE) { await this.signalingOnMessageTypeClose(message) } } - } else if (dataChannelEvent.channel.label === 'notify') { + } else if (dataChannelEvent.channel.label === DATA_CHANNEL_LABEL_NOTIFY) { dataChannelEvent.channel.onmessage = async (event): Promise => { const channel = event.currentTarget as RTCDataChannel const label = channel.label @@ -2091,7 +2096,7 @@ export default class ConnectionBase { } this.signalingOnMessageTypeNotify(message, 'datachannel') } - } else if (dataChannelEvent.channel.label === 'push') { + } else if (dataChannelEvent.channel.label === DATA_CHANNEL_LABEL_PUSH) { dataChannelEvent.channel.onmessage = async (event): Promise => { const channel = event.currentTarget as RTCDataChannel const label = channel.label @@ -2106,7 +2111,7 @@ export default class ConnectionBase { const message = JSON.parse(data) as SignalingPushMessage this.callbacks.push(message, 'datachannel') } - } else if (dataChannelEvent.channel.label === 'stats') { + } else if (dataChannelEvent.channel.label === DATA_CHANNEL_LABEL_STATS) { dataChannelEvent.channel.onmessage = async (event): Promise => { const channel = event.currentTarget as RTCDataChannel const label = channel.label diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index db38926b..43388719 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -31,3 +31,9 @@ export const SIGNALING_MESSAGE_TYPE_PUSH = 'push' as const // @deprecated この定数は将来的に削除される予定です export const SIGNALING_MESSAGE_TYPE_UPDATE = 'update' as const + +// データチャネル必須ラベル +export const DATA_CHANNEL_LABEL_SIGNALING = 'signaling' as const +export const DATA_CHANNEL_LABEL_PUSH = 'push' as const +export const DATA_CHANNEL_LABEL_NOTIFY = 'notify' as const +export const DATA_CHANNEL_LABEL_STATS = 'stats' as const diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 58a3a17c..9d71d115 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,4 +1,8 @@ import type { + DATA_CHANNEL_LABEL_NOTIFY, + DATA_CHANNEL_LABEL_PUSH, + DATA_CHANNEL_LABEL_SIGNALING, + DATA_CHANNEL_LABEL_STATS, SIGNALING_MESSAGE_TYPE_CLOSE, SIGNALING_MESSAGE_TYPE_CONNECT, SIGNALING_MESSAGE_TYPE_NOTIFY, @@ -67,8 +71,14 @@ export type Role = | typeof SIGNALING_ROLE_SENDONLY | typeof SIGNALING_ROLE_RECVONLY +export type DataChannelSignalingLabel = + | typeof DATA_CHANNEL_LABEL_SIGNALING + | typeof DATA_CHANNEL_LABEL_PUSH + | typeof DATA_CHANNEL_LABEL_NOTIFY + | typeof DATA_CHANNEL_LABEL_STATS + export type SignalingConnectDataChannel = { - label?: string + label?: DataChannelSignalingLabel | string direction?: DataChannelDirection compress?: boolean max_packet_life_time?: number From 77d6da999ad0bf137df526ff578749d890603abc Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 18:59:16 +0900 Subject: [PATCH 7/9] =?UTF-8?q?=E3=81=9A=E3=82=8C=E3=81=A6=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/base.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index 76290336..f2bc5f4a 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -1839,6 +1839,7 @@ export default class ConnectionBase { /** * シグナリングサーバーに type update を投げるメソッド + * @deprecated このメソッドは非推奨です。将来のバージョンで削除される可能性があります。 */ private async sendUpdateAnswer(): Promise { if (this.pc && this.ws && this.pc.localDescription) { @@ -1852,7 +1853,6 @@ export default class ConnectionBase { /** * シグナリングサーバーに type re-answer を投げるメソッド - * @deprecated このメソッドは非推奨です。将来のバージョンで削除される可能性があります。 */ private async sendReAnswer(): Promise { if (this.pc?.localDescription) { From cd65c17e41db4842b290cb48999ece8697a1d7b8 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 19:02:34 +0900 Subject: [PATCH 8/9] =?UTF-8?q?=E5=8B=98=E9=81=95=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/types.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 9d71d115..52c41eac 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -71,14 +71,8 @@ export type Role = | typeof SIGNALING_ROLE_SENDONLY | typeof SIGNALING_ROLE_RECVONLY -export type DataChannelSignalingLabel = - | typeof DATA_CHANNEL_LABEL_SIGNALING - | typeof DATA_CHANNEL_LABEL_PUSH - | typeof DATA_CHANNEL_LABEL_NOTIFY - | typeof DATA_CHANNEL_LABEL_STATS - export type SignalingConnectDataChannel = { - label?: DataChannelSignalingLabel | string + label?: string direction?: DataChannelDirection compress?: boolean max_packet_life_time?: number From 68c2bc10e5a83d059146bf9fb0fc81b0c9e72cf7 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 2 Oct 2024 19:06:03 +0900 Subject: [PATCH 9/9] =?UTF-8?q?SignalingMessageEvent=20=E3=81=AE=E3=82=A4?= =?UTF-8?q?=E3=83=B3=E3=82=BF=E3=83=BC=E3=83=95=E3=82=A7=E3=83=BC=E3=82=B9?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/sdk/src/types.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 52c41eac..7966f6c1 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,8 +1,4 @@ import type { - DATA_CHANNEL_LABEL_NOTIFY, - DATA_CHANNEL_LABEL_PUSH, - DATA_CHANNEL_LABEL_SIGNALING, - DATA_CHANNEL_LABEL_STATS, SIGNALING_MESSAGE_TYPE_CLOSE, SIGNALING_MESSAGE_TYPE_CONNECT, SIGNALING_MESSAGE_TYPE_NOTIFY, @@ -10,6 +6,7 @@ import type { SIGNALING_MESSAGE_TYPE_PING, SIGNALING_MESSAGE_TYPE_PUSH, SIGNALING_MESSAGE_TYPE_REDIRECT, + SIGNALING_MESSAGE_TYPE_REQ_STATS, SIGNALING_MESSAGE_TYPE_RE_OFFER, SIGNALING_MESSAGE_TYPE_SWITCHED, SIGNALING_MESSAGE_TYPE_UPDATE, @@ -178,7 +175,7 @@ export type SignalingPushMessage = { } export type SignalingReqStatsMessage = { - type: 'req-stats' + type: typeof SIGNALING_MESSAGE_TYPE_REQ_STATS } export type SignalingSwitchedMessage = { @@ -382,6 +379,13 @@ export type SignalingMessageDirection = 'sent' | 'received' export type TimelineEventLogType = TransportType | 'peerconnection' | 'sora' +// @todo 未実装 +export interface SignalingMessageEvent extends Event { + type: TransportType + direction: SignalingMessageDirection + message: WebSocketSignalingMessage | DataChannelSignalingMessage +} + export interface SignalingEvent extends Event { transportType: TransportType data?: unknown