-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples の spotlight sendonly / recvonly を class 化
- Loading branch information
Showing
9 changed files
with
250 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ <h1>Sendonly test</h1> | |
</div> | ||
|
||
<script type="module" src="./main.mts"></script> | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import Sora, { | ||
type SoraConnection, | ||
type SignalingNotifyMessage, | ||
type ConnectionSubscriber, | ||
} from '../../dist/sora' | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
// 環境変数の読み込み | ||
const SORA_SIGNALING_URL = import.meta.env.VITE_SORA_SIGNALING_URL | ||
const SORA_CHANNEL_ID_PREFIX = import.meta.env.VITE_SORA_CHANNEL_ID_PREFIX || '' | ||
const SORA_CHANNEL_ID_SUFFIX = import.meta.env.VITE_SORA_CHANNEL_ID_SUFFIX || '' | ||
const ACCESS_TOKEN = import.meta.env.VITE_ACCESS_TOKEN || '' | ||
|
||
// Sora クライアントの初期化 | ||
const client = new SoraClient( | ||
SORA_SIGNALING_URL, | ||
SORA_CHANNEL_ID_PREFIX, | ||
SORA_CHANNEL_ID_SUFFIX, | ||
ACCESS_TOKEN, | ||
) | ||
|
||
document.querySelector('#start')?.addEventListener('click', async () => { | ||
await client.connect() | ||
}) | ||
|
||
document.querySelector('#stop')?.addEventListener('click', async () => { | ||
await client.disconnect() | ||
}) | ||
}) | ||
|
||
class SoraClient { | ||
private debug = false | ||
private channelId: string | ||
private metadata: { access_token: string } | ||
private options: object = {} | ||
|
||
private sora: SoraConnection | ||
private connection: ConnectionSubscriber | ||
|
||
constructor( | ||
signaling_url: string, | ||
channel_id_prefix: string, | ||
channel_id_suffix: string, | ||
access_token: string, | ||
) { | ||
this.sora = Sora.connection(signaling_url, this.debug) | ||
|
||
this.options = { | ||
multistream: true, | ||
simulcast: true, | ||
spotlight: true, | ||
} | ||
|
||
// channel_id の生成 | ||
this.channelId = `${channel_id_prefix}spotlight_sendonly_recvonly${channel_id_suffix}` | ||
// access_token を指定する metadata の生成 | ||
this.metadata = { access_token: access_token } | ||
|
||
this.connection = this.sora.recvonly(this.channelId, this.metadata, this.options) | ||
this.connection.on('notify', this.onnotify.bind(this)) | ||
this.connection.on('track', this.ontrack.bind(this)) | ||
this.connection.on('removetrack', this.onremovetrack.bind(this)) | ||
} | ||
|
||
async connect(): Promise<void> { | ||
await this.connection.connect() | ||
} | ||
|
||
async disconnect(): Promise<void> { | ||
await this.connection.disconnect() | ||
const remoteVideos = document.querySelector('#remote-videos') | ||
if (remoteVideos) { | ||
remoteVideos.innerHTML = '' | ||
} | ||
|
||
const connectionIdElement = document.querySelector<HTMLDivElement>('#connection-id') | ||
if (connectionIdElement) { | ||
connectionIdElement.textContent = null | ||
} | ||
} | ||
|
||
private onnotify(event: SignalingNotifyMessage) { | ||
// 自分の connection_id を取得する | ||
if ( | ||
event.event_type === 'connection.created' && | ||
this.connection.connectionId === event.connection_id | ||
) { | ||
const connectionIdElement = document.querySelector<HTMLDivElement>('#connection-id') | ||
if (connectionIdElement) { | ||
connectionIdElement.textContent = event.connection_id | ||
} | ||
} | ||
} | ||
|
||
private ontrack(event: RTCTrackEvent) { | ||
// Sora の場合、event.streams には MediaStream が 1 つだけ含まれる | ||
const stream = event.streams[0] | ||
const remoteVideoId = `remotevideo-${stream.id}` | ||
const remoteVideos = document.querySelector<HTMLDivElement>('#remote-videos') | ||
if (remoteVideos && !remoteVideos.querySelector(`#${remoteVideoId}`)) { | ||
const remoteVideo = document.createElement('video') | ||
remoteVideo.id = remoteVideoId | ||
remoteVideo.style.border = '1px solid red' | ||
remoteVideo.autoplay = true | ||
remoteVideo.playsInline = true | ||
remoteVideo.controls = true | ||
remoteVideo.srcObject = stream | ||
remoteVideos.appendChild(remoteVideo) | ||
} | ||
} | ||
|
||
private onremovetrack(event: MediaStreamTrackEvent) { | ||
// このトラックが属している MediaStream の id を取得する | ||
const stream = event.target as MediaStream | ||
const remoteVideo = document.querySelector(`#remotevideo-${stream.id}`) | ||
if (remoteVideo) { | ||
document.querySelector('#remote-videos')?.removeChild(remoteVideo) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.