From b5a3e0cf5bd6207336821080ce2d77b12df44537 Mon Sep 17 00:00:00 2001 From: shinyoshiaki Date: Mon, 13 Nov 2023 19:13:11 +0900 Subject: [PATCH] chore --- examples/save_to_disk/mp4/av_old.ts | 207 -------------------------- examples/save_to_disk/mp4/h264_old.ts | 140 ----------------- examples/save_to_disk/mp4/opus_old.ts | 119 --------------- packages/rtp/src/processor/mp4.ts | 3 +- 4 files changed, 1 insertion(+), 468 deletions(-) delete mode 100644 examples/save_to_disk/mp4/av_old.ts delete mode 100644 examples/save_to_disk/mp4/h264_old.ts delete mode 100644 examples/save_to_disk/mp4/opus_old.ts diff --git a/examples/save_to_disk/mp4/av_old.ts b/examples/save_to_disk/mp4/av_old.ts deleted file mode 100644 index bbde4520..00000000 --- a/examples/save_to_disk/mp4/av_old.ts +++ /dev/null @@ -1,207 +0,0 @@ -import { - annexb2avcc, - buffer2ArrayBuffer, - Mp4Container, - DepacketizeCallback, - JitterBufferCallback, - LipsyncCallback, - NtpTimeCallback, - OpusRtpPayload, - PromiseQueue, - RTCPeerConnection, - RtcpSourceCallback, - RTCRtpCodecParameters, - RtpSourceCallback, -} from "../../../packages/webrtc/src"; -import { Server } from "ws"; -import { appendFile, 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 container = new Mp4Container({ track: { audio: true, video: true } }); - - const audio = new RtpSourceCallback(); - const video = new RtpSourceCallback(); - const audioRtcp = new RtcpSourceCallback(); - const videoRtcp = new RtcpSourceCallback(); - const lipsync = new LipsyncCallback({ - syncInterval: 1000, - bufferLength: 5, - // fillDummyAudioPacket: Buffer.from([0xf8, 0xff, 0xfe]), - }); - - { - const depacketizer = new DepacketizeCallback("opus"); - const ntpTime = new NtpTimeCallback(48000); - - audio.pipe(ntpTime.input); - audioRtcp.pipe(ntpTime.input); - - ntpTime.pipe(depacketizer.input); - depacketizer.pipe(lipsync.inputAudio); - - lipsync.pipeAudio(async ({ frame }) => { - if (frame) { - if (!container.audioTrack) { - container.write({ - codec: "opus", - description: buffer2ArrayBuffer( - OpusRtpPayload.createCodecPrivate() - ), - numberOfChannels: 2, - sampleRate: 48000, - track: "audio", - }); - } else { - container.write({ - byteLength: frame.data.length, - duration: null, - timestamp: frame.time * 1000, - type: "key", - copyTo: (destination: Uint8Array) => { - frame.data.copy(destination); - }, - track: "audio", - }); - } - } - }); - } - { - 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(lipsync.inputVideo); - - lipsync.pipeVideo(async ({ frame }) => { - if (frame) { - if (!container.videoTrack) { - if (frame.isKeyframe) { - const avcc = annexb2avcc(frame.data); - container.write({ - codec: "avc1", - codedHeight: 480, - codedWidth: 640, - description: avcc.buffer, - displayAspectHeight: 3, - displayAspectWidth: 4, - track: "video", - }); - container.write({ - byteLength: frame.data.length, - duration: null, - timestamp: frame.time * 1000, - type: "key", - copyTo: (destination: Uint8Array) => { - frame.data.copy(destination); - }, - track: "video", - }); - } - } else { - container.write({ - byteLength: frame.data.length, - duration: null, - timestamp: frame.time * 1000, - type: frame.isKeyframe ? "key" : "delta", - copyTo: (destination: Uint8Array) => { - frame.data.copy(destination); - }, - track: "video", - }); - } - } - }); - } - - await unlink(output).catch(() => {}); - - 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(); - }, 30_000); - - const queue = new PromiseQueue(); - container.onData.subscribe(async (value) => { - await queue.push(async () => { - await appendFile(output, value.data); - }); - }); -}); diff --git a/examples/save_to_disk/mp4/h264_old.ts b/examples/save_to_disk/mp4/h264_old.ts deleted file mode 100644 index 2f4e93b4..00000000 --- a/examples/save_to_disk/mp4/h264_old.ts +++ /dev/null @@ -1,140 +0,0 @@ -import { - annexb2avcc, - Mp4Container, - DepacketizeCallback, - JitterBufferCallback, - NtpTimeCallback, - PromiseQueue, - RTCPeerConnection, - RtcpSourceCallback, - RTCRtpCodecParameters, - RtpSourceCallback, -} from "../../../packages/webrtc/src"; -import { Server } from "ws"; -import { appendFile, 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 container = new Mp4Container({ track: { video: true, audio: false } }); - - 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(({ frame }) => { - if (frame) { - if (!container.videoTrack) { - if (frame.isKeyframe) { - const avcc = annexb2avcc(frame.data); - container.write({ - codec: "avc1", - codedHeight: 480, - codedWidth: 640, - description: avcc.buffer, - displayAspectHeight: 3, - displayAspectWidth: 4, - track: "video", - }); - container.write({ - byteLength: frame.data.length, - duration: null, - timestamp: frame.time * 1000, - type: "key", - copyTo: (destination: Uint8Array) => { - frame.data.copy(destination); - }, - track: "video", - }); - } - } else { - container.write({ - byteLength: frame.data.length, - duration: null, - timestamp: frame.time * 1000, - type: frame.isKeyframe ? "key" : "delta", - copyTo: (destination: Uint8Array) => { - frame.data.copy(destination); - }, - track: "video", - }); - } - } - }); - } - - await unlink(output).catch(() => {}); - - 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) => { - pc.setRemoteDescription(JSON.parse(data)); - }); - - setTimeout(async () => { - console.log("stop"); - video.stop(); - await pc.close(); - }, 10_000); - - const queue = new PromiseQueue(); - container.onData.subscribe(async (value) => { - await queue.push(async () => { - await appendFile(output, value.data); - }); - }); -}); diff --git a/examples/save_to_disk/mp4/opus_old.ts b/examples/save_to_disk/mp4/opus_old.ts deleted file mode 100644 index 39db6212..00000000 --- a/examples/save_to_disk/mp4/opus_old.ts +++ /dev/null @@ -1,119 +0,0 @@ -import { - buffer2ArrayBuffer, - Mp4Container, - DepacketizeCallback, - NtpTimeCallback, - OpusRtpPayload, - PromiseQueue, - RTCPeerConnection, - RtcpSourceCallback, - RTCRtpCodecParameters, - RtpSourceCallback, -} from "../../../packages/webrtc/src"; -import { Server } from "ws"; -import { appendFile, unlink } from "fs/promises"; - -// open ./answer.html - -const server = new Server({ port: 8878 }); -console.log("start"); - -server.on("connection", async (socket) => { - const output = `./original.m4a`; - 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 container = new Mp4Container({ track: { audio: true, video: false } }); - - const audio = new RtpSourceCallback(); - const audioRtcp = 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(({ frame }) => { - if (frame) { - if (!container.audioTrack) { - container.write({ - codec: "opus", - description: buffer2ArrayBuffer( - OpusRtpPayload.createCodecPrivate() - ), - numberOfChannels: 2, - sampleRate: 48000, - track: "audio", - }); - } else { - container.write({ - byteLength: frame.data.length, - duration: null, - timestamp: frame.time * 1000, - type: "key", - copyTo: (destination: Uint8Array) => { - frame.data.copy(destination); - }, - track: "audio", - }); - } - } - }); - } - - await unlink(output).catch(() => {}); - - 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) => { - pc.setRemoteDescription(JSON.parse(data)); - }); - - setTimeout(async () => { - console.log("stop"); - audio.stop(); - await pc.close(); - }, 5_000); - - const queue = new PromiseQueue(); - container.onData.subscribe(async (value) => { - await queue.push(async () => { - await appendFile(output, value.data); - }); - }); -}); diff --git a/packages/rtp/src/processor/mp4.ts b/packages/rtp/src/processor/mp4.ts index 5f03d6a9..a61d74ee 100644 --- a/packages/rtp/src/processor/mp4.ts +++ b/packages/rtp/src/processor/mp4.ts @@ -3,10 +3,9 @@ import Event from "rx.mini"; import { annexb2avcc, buffer2ArrayBuffer, - MediaKind, Mp4Container, - OpusRtpPayload, Mp4SupportedCodec, + OpusRtpPayload, } from ".."; import { AVProcessor } from "./interface";