Skip to content

Commit

Permalink
型と定数
Browse files Browse the repository at this point in the history
  • Loading branch information
voluntas committed Oct 2, 2024
1 parent 6d84967 commit 623f52f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 32 deletions.
47 changes: 31 additions & 16 deletions packages/sdk/src/base.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -1817,22 +1831,23 @@ export default class ConnectionBase {
*/
private async sendUpdateAnswer(): Promise<void> {
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,
})
}
}

/**
* シグナリングサーバーに type re-answer を投げるメソッド
* @deprecated このメソッドは非推奨です。将来のバージョンで削除される可能性があります。
*/
private async sendReAnswer(): Promise<void> {
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,
})
}
Expand Down Expand Up @@ -1880,8 +1895,8 @@ export default class ConnectionBase {
* @param message - type ping メッセージ
*/
private async signalingOnMessageTypePing(message: SignalingPingMessage): Promise<void> {
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()
Expand Down
19 changes: 19 additions & 0 deletions packages/sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
42 changes: 26 additions & 16 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -112,7 +122,7 @@ export type SignalingOfferMessageDataChannel = {
}

export type SignalingOfferMessage = {
type: 'offer'
type: typeof SIGNALING_MESSAGE_TYPE_OFFER
sdp: string

multistream: boolean
Expand Down Expand Up @@ -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<string, unknown>
}

Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 623f52f

Please sign in to comment.