From d1d626b8b8955aa045aef373ef20f9484e4a33b3 Mon Sep 17 00:00:00 2001 From: shinyoshiaki Date: Thu, 8 Aug 2024 15:20:51 +0900 Subject: [PATCH] improve example --- examples/send/fixture.ts | 46 ---------------------- examples/send/main.ts | 78 -------------------------------------- examples/send/package.json | 19 ---------- examples/sendrecv/audio.ts | 49 +++++++++++------------- examples/sendrecv/video.ts | 37 +++++++----------- submodules/mediasoup | 2 +- 6 files changed, 36 insertions(+), 195 deletions(-) delete mode 100644 examples/send/fixture.ts delete mode 100644 examples/send/main.ts delete mode 100644 examples/send/package.json diff --git a/examples/send/fixture.ts b/examples/send/fixture.ts deleted file mode 100644 index 76199c0..0000000 --- a/examples/send/fixture.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { SkyWayAuthToken, uuidV4 } from '../../packages/room/src'; -import { appId, secret } from '../../env'; - -const testToken = new SkyWayAuthToken({ - jti: uuidV4(), - exp: Date.now() / 1000 + 60 * 60, - iat: Date.now() / 1000, - scope: { - app: { - turn: true, - id: appId, - actions: ['read'], - channels: [ - { - id: '*', - name: '*', - actions: ['read', 'write'], - members: [ - { - id: '*', - name: '*', - actions: ['write'], - publication: { - actions: ['write'], - }, - subscription: { - actions: ['write'], - }, - }, - ], - sfuBots: [ - { - actions: ['write'], - forwardings: [ - { - actions: ['write'], - }, - ], - }, - ], - }, - ], - }, - }, -}); -export const testTokenString = testToken.encode(secret); diff --git a/examples/send/main.ts b/examples/send/main.ts deleted file mode 100644 index d9cef0b..0000000 --- a/examples/send/main.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { - LocalVideoStream, - MediaStreamTrack, - SkyWayContext, - SkyWayRoom, - randomPort, -} from '../../packages/room/src/index'; -import { testTokenString } from './fixture'; -import { createSocket } from 'dgram'; -import Gst from '@girs/node-gst-1.0'; - -const gst = require('node-gtk').require('Gst', '1.0') as typeof Gst; -gst.init([]); - -(async () => { - const context = await SkyWayContext.Create(testTokenString, { - codecCapabilities: [ - { - mimeType: 'video/h264', - parameters: { - 'level-asymmetry-allowed': 1, - 'packetization-mode': 0, - 'profile-level-id': '42001f', - }, - }, - ], - }); - const room = await SkyWayRoom.Create(context, { - type: 'sfu', - }); - console.log('roomId', room.id); - const member = await room.join(); - - const port = await randomPort(); - - const udp = createSocket('udp4'); - udp.bind(port); - - const track = new MediaStreamTrack({ kind: 'video' }); - udp.addListener('message', (data) => { - track.writeRtp(data); - }); - - const stream = new LocalVideoStream(track); - const publication = await member.publish(stream); - - const src = gst.ElementFactory.make('videotestsrc', null)!; - const capsfilter = gst.ElementFactory.make('capsfilter', null)!; - capsfilter.setProperty( - 'caps', - Gst.capsFromString( - 'video/x-raw,width=640,height=480,format=I420,framerate=60/1' - ) - ); - const enc = gst.ElementFactory.make('x264enc', null)!; - // every seconds - enc.setProperty('key-int-max', 60); - // const enc = gst.ElementFactory.make('vp8enc', null)!; - // enc.setProperty('keyframe-max-dist', 60); - // enc.setProperty('cpu-used', 5); - - const pay = gst.ElementFactory.make('rtph264pay', null)!; - // const pay = gst.ElementFactory.make('rtpvp8pay', null)!; - pay.setProperty('picture-id-mode', 1); - - const sink = gst.ElementFactory.make('udpsink', null)!; - sink.setProperty('host', '127.0.0.1'); - sink.setProperty('port', port); - const pipeline = new gst.Pipeline(); - [src, capsfilter, enc, pay, sink].reduce((acc, cur) => { - pipeline.add(cur); - if (acc) { - acc.link(cur); - } - return cur; - }, undefined as Gst.Element | undefined); - pipeline.setState(gst.State.PLAYING); -})(); diff --git a/examples/send/package.json b/examples/send/package.json deleted file mode 100644 index e28db58..0000000 --- a/examples/send/package.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "send", - "version": "1.0.0", - "description": "", - "author": "", - "main": "", - "scripts": { - "dev": "ts-node-dev ./main.ts" - }, - "dependencies": { - "@shinyoshiaki/skyway-nodejs-sdk": "file:../../packages/room", - "node-gtk": "file:../../submodules/node-gtk", - "@girs/node-gst-1.0": "^1.23.0-3.2.2" - }, - "devDependencies": { - "@types/node": "^20.8.8", - "ts-node-dev": "^2.0.0" - } -} diff --git a/examples/sendrecv/audio.ts b/examples/sendrecv/audio.ts index de9ab72..bb93d71 100644 --- a/examples/sendrecv/audio.ts +++ b/examples/sendrecv/audio.ts @@ -1,15 +1,12 @@ import { - Event, - LocalAudioStream, - MediaStreamTrack, + MediaStreamTrackFactory, RemoteVideoStream, RtpPacket, SkyWayContext, SkyWayRoom, - randomPort, + SkyWayStreamFactory, } from '../../packages/room/src'; import { testTokenString } from './fixture'; -import { createSocket } from 'dgram'; import Gst from '@girs/node-gst-1.0'; import { deserializeAudioLevelIndication, @@ -23,34 +20,32 @@ gst.init([]); const context = await SkyWayContext.Create(testTokenString, { codecCapabilities: [{ mimeType: 'audio/opus' }], }); + const [track, port] = await MediaStreamTrackFactory.rtpSource({ + kind: 'audio', + cb: (buf) => { + const rtp = RtpPacket.deSerialize(buf); + rtp.header.extension = true; + rtp.header.extensions.push({ + id: 3, + payload: serializeAudioLevelIndication(25), + }); + return rtp.serialize(); + }, + }); + Gst.parseLaunch( + `audiotestsrc wave=ticks ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! udpsink host=127.0.0.1 port=${port}` + ).setState(Gst.State.PLAYING); + SkyWayStreamFactory.registerMediaDevices({ audio: track }); + const room = await SkyWayRoom.Create(context, { type: 'sfu', }); console.log('roomId', room.id); const sender = await room.join(); - const audio = await randomPort(); - const onAudio = new Event(); - createSocket('udp4') - .on('message', (buf) => { - onAudio.emit(buf); - }) - .bind(audio); - Gst.parseLaunch( - `audiotestsrc wave=ticks ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! udpsink host=127.0.0.1 port=${audio}` - ).setState(Gst.State.PLAYING); - const audioTrack = new MediaStreamTrack({ kind: 'audio' }); - onAudio.add((buf) => { - const rtp = RtpPacket.deSerialize(buf); - rtp.header.extension = true; - rtp.header.extensions.push({ - id: 3, - payload: serializeAudioLevelIndication(25), - }); - audioTrack.writeRtp(rtp); - }); - - const publication = await sender.publish(new LocalAudioStream(audioTrack)); + const publication = await sender.publish( + await SkyWayStreamFactory.createMicrophoneAudioStream() + ); const receiver = await (await SkyWayRoom.Find(context, room, 'sfu')).join(); const { stream: remoteStream } = await receiver.subscribe( diff --git a/examples/sendrecv/video.ts b/examples/sendrecv/video.ts index 684fd32..6f3f881 100644 --- a/examples/sendrecv/video.ts +++ b/examples/sendrecv/video.ts @@ -1,15 +1,12 @@ import { - LocalVideoStream, - MediaStreamTrack, RemoteVideoStream, SkyWayContext, SkyWayRoom, dePacketizeRtpPackets, - randomPort, - Event, + MediaStreamTrackFactory, + SkyWayStreamFactory, } from '../../packages/room/src'; import { testTokenString } from './fixture'; -import { createSocket } from 'dgram'; import Gst from '@girs/node-gst-1.0'; const gst = require('node-gtk').require('Gst', '1.0') as typeof Gst; @@ -27,32 +24,24 @@ gst.init([]); }, }, ], - rtcConfig: { turnPolicy: 'disable' }, }); - const room = await SkyWayRoom.Create(context, { - type: 'sfu', + const [track, port] = await MediaStreamTrackFactory.rtpSource({ + kind: 'video', }); - console.log('roomId', room.id); - const sender = await room.join(); - - const video = await randomPort(); - const onVideo = new Event(); - createSocket('udp4') - .on('message', (buf) => { - onVideo.emit(buf); - }) - .bind(video); - Gst.parseLaunch( - `videotestsrc ! video/x-raw,width=640,height=480,format=I420 ! x264enc key-int-max=60 ! rtph264pay ! udpsink host=127.0.0.1 port=${video}` + `videotestsrc ! video/x-raw,width=640,height=480,format=I420 ! x264enc key-int-max=60 ! rtph264pay ! udpsink host=127.0.0.1 port=${port}` ).setState(Gst.State.PLAYING); + SkyWayStreamFactory.registerMediaDevices({ video: track }); - const track = new MediaStreamTrack({ kind: 'video' }); - onVideo.add((data) => { - track.writeRtp(data); + const room = await SkyWayRoom.Create(context, { + type: 'sfu', }); + console.log('roomId', room.id); + const sender = await room.join(); - const publication = await sender.publish(new LocalVideoStream(track)); + const publication = await sender.publish( + await SkyWayStreamFactory.createCameraVideoStream() + ); const receiver = await (await SkyWayRoom.Find(context, room, 'sfu')).join(); const { stream: remoteStream } = await receiver.subscribe( diff --git a/submodules/mediasoup b/submodules/mediasoup index b59c835..5a1bd96 160000 --- a/submodules/mediasoup +++ b/submodules/mediasoup @@ -1 +1 @@ -Subproject commit b59c835e7c8671edb6c16d1e8f77fee98aaf3a15 +Subproject commit 5a1bd9663979ad764c76fdf4af4eebf6a9c1d594