Skip to content

Commit

Permalink
Merge pull request #354 from shinyoshiaki/feature/mp4
Browse files Browse the repository at this point in the history
Initial MP4 Support
  • Loading branch information
shinyoshiaki authored Nov 13, 2023
2 parents df5362e + e017eae commit 729f6b0
Show file tree
Hide file tree
Showing 36 changed files with 3,579 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ dist
*.key
*.mpd
*.mp3
*.mp4
*.m4a
credential.env
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"args": [
"--project",
"${workspaceRoot}/tsconfig.json",
"${workspaceRoot}/examples/google-nest/server.ts"
"${workspaceRoot}/examples/save_to_disk/mp4/av.ts"
],
"env": {
"DEBUG": "werift*"
Expand Down
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"cSpell.words": [
"ack",
"acked",
"annexb",
"avcc",
"cwnd",
"dcep",
"Depacketize",
Expand Down
28 changes: 28 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,31 @@ The above copyright notice and this permission notice shall be included in all c

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
```

# https://github.com/kixelated/moq-js

MIT License

Copyright (c) 2023 Luke Curley

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

# https://github.com/xqq/mpegts.js/tree/master

Apache-2.0 license
6 changes: 3 additions & 3 deletions examples/save_to_disk/answer.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@
stream.addTrack(e.track);
videoRef.current.srcObject = stream;
};
// const video = createTestTrack(640, 360);
const [video] = (
const video = createTestTrack(640, 360);
/*const [video] = (
await navigator.mediaDevices.getUserMedia({
video: true,
})
).getTracks();
).getTracks();*/
const [audio] = (
await navigator.mediaDevices.getUserMedia({
audio: true,
Expand Down
139 changes: 139 additions & 0 deletions examples/save_to_disk/mp4/av.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import {
DepacketizeCallback,
JitterBufferCallback,
LipsyncCallback,
NtpTimeCallback,
RTCPeerConnection,
RtcpSourceCallback,
RTCRtpCodecParameters,
RtpSourceCallback,
MP4Callback,
} from "../../../packages/webrtc/src";
import { Server } from "ws";
import { unlink } from "fs/promises";

// open ./answer.html

const server = new Server({ port: 8878 });
console.log("start");

server.on("connection", async (socket) => {
const output = `./output-${Date.now()}.mp4`;
console.log("connected", output);
const pc = new RTCPeerConnection({
codecs: {
video: [
new RTCRtpCodecParameters({
mimeType: "video/H264",
clockRate: 90000,
rtcpFeedback: [
{ type: "nack" },
{ type: "nack", parameter: "pli" },
{ type: "goog-remb" },
],
}),
],
audio: [
new RTCRtpCodecParameters({
mimeType: "audio/opus",
clockRate: 48000,
channels: 2,
}),
],
},
});

const mp4 = new MP4Callback([
{
kind: "audio",
codec: "opus",
clockRate: 48000,
trackNumber: 1,
},
{
width: 640,
height: 360,
kind: "video",
codec: "avc1",
clockRate: 90000,
trackNumber: 2,
},
]);
await unlink(output).catch(() => {});
mp4.pipe(MP4Callback.saveToFileSystem(output));

const audio = new RtpSourceCallback();
const video = new RtpSourceCallback();
const audioRtcp = new RtcpSourceCallback();
const videoRtcp = new RtcpSourceCallback();

{
const depacketizer = new DepacketizeCallback("opus");
const ntpTime = new NtpTimeCallback(48000);

audio.pipe(ntpTime.input);
audioRtcp.pipe(ntpTime.input);

ntpTime.pipe(depacketizer.input);
depacketizer.pipe(mp4.inputAudio);
}
{
const jitterBuffer = new JitterBufferCallback(90000);
const ntpTime = new NtpTimeCallback(jitterBuffer.clockRate);
const depacketizer = new DepacketizeCallback("MPEG4/ISO/AVC", {
isFinalPacketInSequence: (h) => h.marker,
});

video.pipe(jitterBuffer.input);
videoRtcp.pipe(ntpTime.input);

jitterBuffer.pipe(ntpTime.input);
ntpTime.pipe(depacketizer.input);
depacketizer.pipe(mp4.inputVideo);
}

pc.addTransceiver("video").onTrack.subscribe((track, transceiver) => {
transceiver.sender.replaceTrack(track);
track.onReceiveRtp.subscribe((rtp) => {
video.input(rtp);
});
track.onReceiveRtcp.once((rtcp) => {
videoRtcp.input(rtcp);
});
track.onReceiveRtp.once(() => {
setInterval(() => {
transceiver.receiver.sendRtcpPLI(track.ssrc);
}, 2_000);
});
});
pc.addTransceiver("audio").onTrack.subscribe((track, transceiver) => {
transceiver.sender.replaceTrack(track);
track.onReceiveRtp.subscribe((rtp) => {
audio.input(rtp);
});
track.onReceiveRtcp.once((rtcp) => {
audioRtcp.input(rtcp);
});
});

await pc.setLocalDescription(await pc.createOffer());
const sdp = JSON.stringify(pc.localDescription);
socket.send(sdp);

socket.on("message", (data: any) => {
const message = JSON.parse(data);
console.log("message", message);
if (message.sdp) {
pc.setRemoteDescription(message);
} else {
pc.addIceCandidate(message);
}
});

setTimeout(async () => {
console.log("stop");
audio.stop();
video.stop();
await pc.close();
}, 60_000);
});
108 changes: 108 additions & 0 deletions examples/save_to_disk/mp4/h264.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {
DepacketizeCallback,
JitterBufferCallback,
NtpTimeCallback,
RTCPeerConnection,
RtcpSourceCallback,
RTCRtpCodecParameters,
RtpSourceCallback,
MP4Callback,
} from "../../../packages/webrtc/src";
import { Server } from "ws";
import { unlink } from "fs/promises";

// open ./answer.html

const server = new Server({ port: 8878 });
console.log("start");

server.on("connection", async (socket) => {
const output = `./output-${Date.now()}.mp4`;
console.log("connected", output);
const pc = new RTCPeerConnection({
codecs: {
video: [
new RTCRtpCodecParameters({
mimeType: "video/H264",
clockRate: 90000,
rtcpFeedback: [
{ type: "nack" },
{ type: "nack", parameter: "pli" },
{ type: "goog-remb" },
],
}),
],
audio: [
new RTCRtpCodecParameters({
mimeType: "audio/opus",
clockRate: 48000,
channels: 2,
}),
],
},
});

const mp4 = new MP4Callback([
{
width: 640,
height: 360,
kind: "video",
codec: "avc1",
clockRate: 90000,
trackNumber: 1,
},
]);
await unlink(output).catch(() => {});
mp4.pipe(MP4Callback.saveToFileSystem(output));

const video = new RtpSourceCallback();
const videoRtcp = new RtcpSourceCallback();

{
const jitterBuffer = new JitterBufferCallback(90000);
const ntpTime = new NtpTimeCallback(jitterBuffer.clockRate);
const depacketizer = new DepacketizeCallback("MPEG4/ISO/AVC", {
isFinalPacketInSequence: (h) => h.marker,
});

video.pipe(jitterBuffer.input);
videoRtcp.pipe(ntpTime.input);

jitterBuffer.pipe(ntpTime.input);
ntpTime.pipe(depacketizer.input);
depacketizer.pipe(mp4.inputVideo);
}

pc.addTransceiver("video").onTrack.subscribe((track, transceiver) => {
transceiver.sender.replaceTrack(track);
track.onReceiveRtp.subscribe((rtp) => {
video.input(rtp);
});
track.onReceiveRtcp.once((rtcp) => {
videoRtcp.input(rtcp);
});
setInterval(() => {
transceiver.receiver.sendRtcpPLI(track.ssrc);
}, 2_000);
});

await pc.setLocalDescription(await pc.createOffer());
const sdp = JSON.stringify(pc.localDescription);
socket.send(sdp);

socket.on("message", (data: any) => {
const message = JSON.parse(data);
console.log("message", message);
if (message.sdp) {
pc.setRemoteDescription(message);
} else {
pc.addIceCandidate(message);
}
});

setTimeout(async () => {
console.log("stop");
video.stop();
await pc.close();
}, 10_000);
});
Loading

0 comments on commit 729f6b0

Please sign in to comment.