Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyoshiaki committed Sep 12, 2024
1 parent a3475e9 commit 5efc9ad
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 8 deletions.
105 changes: 100 additions & 5 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
"name": "example",
"version": "0.0.31",
"description": "WebRTC Implementation for TypeScript (Node.js)",
"keywords": ["WebRTC", "node.js"],
"keywords": [
"WebRTC",
"node.js"
],
"homepage": "https://github.com/shinyoshiaki/werift-webrtc",
"repository": {
"type": "git",
Expand All @@ -13,7 +16,10 @@
"name": "shinyoshiaki"
},
"main": "lib/index.js",
"files": ["lib", "src"],
"files": [
"lib",
"src"
],
"scripts": {
"format": "biome check --write ../examples"
},
Expand Down Expand Up @@ -49,7 +55,8 @@
"ws": "^8.17.1",
"wtfnode": "^0.9.1",
"xmlbuilder2": "^3.0.2",
"yargs": "^17.7.2"
"yargs": "^17.7.2",
"zx": "^8.1.6"
},
"engines": {
"node": ">=14"
Expand Down
26 changes: 26 additions & 0 deletions examples/save_to_disk/rtp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { randomUUID } from "crypto";
import {
MediaRecorder,
Navigator,
} from "../../packages/webrtc/src/nonstandard";
import { randomPort } from "../../packages/webrtc/src";
import { $ } from "zx";

(async () => {
const path = `${__dirname}/tmp${randomUUID()}.webm`;
const recorder = new MediaRecorder({
numOfTracks: 1,
path,
disableNtp: true,
});

const port = await randomPort();
const navigator = new Navigator();
const { track } = navigator.mediaDevices.getUdpMedia({
port,
codec: { clockRate: 48000, mimeType: "audio/opus", payloadType: 96 },
});
await recorder.addTrack(track);

$`gst-launch-1.0 audiotestsrc ! audioconvert ! audioresample ! opusenc ! rtpopuspay ! udpsink host=127.0.0.1 port=${port}`;
})();
27 changes: 27 additions & 0 deletions packages/webrtc/src/nonstandard/navigator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { randomBytes } from "crypto";
import { jspack } from "@shinyoshiaki/jspack";
import { MediaStream, MediaStreamTrack } from "../media/track";
import { createSocket } from "dgram";
import { RTCRtpCodecParameters } from "..";

export class Navigator {
mediaDevices: MediaDevices;
Expand Down Expand Up @@ -58,6 +60,31 @@ export class MediaDevices extends EventTarget {
};

readonly getDisplayMedia = this.getUserMedia;

readonly getUdpMedia = ({
port,
codec,
}: {
port: number;
codec: ConstructorParameters<typeof RTCRtpCodecParameters>[0];
}) => {
const track = new MediaStreamTrack({
kind: "audio",
codec: new RTCRtpCodecParameters(codec),
});

const udp = createSocket("udp4");
udp.bind(port);
udp.on("message", (data) => {
track.writeRtp(data);
});

const disposer = () => {
udp.close();
};

return { track, disposer };
};
}

interface MediaStreamConstraints {
Expand Down
4 changes: 4 additions & 0 deletions packages/webrtc/src/nonstandard/recorder/writer/webm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export class WebmFactory extends MediaWriter {
});
const lipsync = new LipsyncCallback();

if (inputTracks.length === 1) {
this.props.disableLipSync = true;
}

this.rtpSources = inputTracks.map(({ track, clockRate, codec }) => {
const rtpSource = new RtpSourceCallback();
const rtcpSource = new RtcpSourceCallback();
Expand Down

0 comments on commit 5efc9ad

Please sign in to comment.