fix: mp4 audio mux codecId miss

This commit is contained in:
langhuihui
2025-05-06 16:33:31 +08:00
parent 2c8e1a7f6e
commit e021131e06
7 changed files with 1694 additions and 2 deletions
+1
View File
@@ -25,6 +25,7 @@ import (
_ "m7s.live/v5/plugin/stress"
_ "m7s.live/v5/plugin/transcode"
_ "m7s.live/v5/plugin/webrtc"
_ "m7s.live/v5/plugin/webtransport"
)
func main() {
+6
View File
@@ -214,6 +214,12 @@ func (p *MP4Plugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch a.ICodecCtx.FourCC() {
case codec.FourCC_MP4A:
codecID = box.MP4_CODEC_AAC
case codec.FourCC_ALAW:
codecID = box.MP4_CODEC_G711A
case codec.FourCC_ULAW:
codecID = box.MP4_CODEC_G711U
case codec.FourCC_OPUS:
codecID = box.MP4_CODEC_OPUS
}
audio = muxer.AddTrack(codecID)
audio.Timescale = 1000
+43 -2
View File
@@ -1,7 +1,9 @@
package webtransport
import (
"embed"
"fmt"
"io"
"net"
"net/http"
"strings"
@@ -12,7 +14,9 @@ import (
)
var (
_ = m7s.InstallPlugin[WebTransportPlugin]()
//go:embed web
web embed.FS
_ = m7s.InstallPlugin[WebTransportPlugin]()
)
type WebTransportPlugin struct {
@@ -23,6 +27,12 @@ type WebTransportPlugin struct {
AllowedOrigins []string `desc:"允许的来源域名列表"`
}
func (p *WebTransportPlugin) RegisterHandler() map[string]http.HandlerFunc {
return map[string]http.HandlerFunc{
"/test/{name}": p.testPage,
}
}
func (p *WebTransportPlugin) OnInit() (err error) {
// Create a new HTTP mux for WebTransport
mux := http.NewServeMux()
@@ -30,7 +40,6 @@ func (p *WebTransportPlugin) OnInit() (err error) {
// Register the WebTransport handlers
mux.HandleFunc("/webtransport/play/", p.handlePlay)
mux.HandleFunc("/webtransport/push/", p.handlePush)
// Start the WebTransport server
server := &Server{
Handler: mux,
@@ -128,3 +137,35 @@ func (p *WebTransportPlugin) handlePush(w http.ResponseWriter, r *http.Request)
p.AddTask(job)
job.WaitStopped()
}
func (p *WebTransportPlugin) testPage(w http.ResponseWriter, r *http.Request) {
name := r.PathValue("name")
switch name {
case "screenshare":
name = "web/screenshare.html"
default:
name = "web/" + name
}
// Set appropriate MIME type based on file extension
if strings.HasSuffix(name, ".html") {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
} else if strings.HasSuffix(name, ".js") {
w.Header().Set("Content-Type", "application/javascript")
// } else if strings.HasSuffix(name, ".css") {
// w.Header().Set("Content-Type", "text/css")
// } else if strings.HasSuffix(name, ".json") {
// w.Header().Set("Content-Type", "application/json")
// } else if strings.HasSuffix(name, ".png") {
// w.Header().Set("Content-Type", "image/png")
// } else if strings.HasSuffix(name, ".jpg") || strings.HasSuffix(name, ".jpeg") {
// w.Header().Set("Content-Type", "image/jpeg")
// } else if strings.HasSuffix(name, ".svg") {
// w.Header().Set("Content-Type", "image/svg+xml")
}
f, err := web.Open(name)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
io.Copy(w, f)
}
+109
View File
@@ -0,0 +1,109 @@
async function getDisplayMedia() {
return navigator.mediaDevices.getDisplayMedia({
video: {
frameRate: {
ideal: 30,
},
},
audio: {
numberOfChannels: 2,
sampleRate: 44100,
},
});
}
let stream;
let recordingChunks = [];
const myWorker = new Worker("./worker.js");
async function startRecording() {
myWorker.onmessage = (chunk) => {
recordingChunks.push(chunk.data);
};
myWorker.postMessage({ type: "START" });
stream = await getDisplayMedia();
const videoTrack = stream.getVideoTracks()[0];
const audioTrack = stream.getAudioTracks()[0];
if (videoTrack) {
const videoTrackProcessor = new MediaStreamTrackProcessor({
track: videoTrack,
});
videoTrackProcessor.readable.pipeTo(
new WritableStream({
write: (chunk) => {
myWorker.postMessage(
{
type: "DATA_VIDEO",
chunk,
},
[chunk]
);
},
})
);
}
if (audioTrack) {
const audioTrackProcessor = new MediaStreamTrackProcessor({
track: audioTrack,
});
audioTrackProcessor.readable.pipeTo(
new WritableStream({
write: (chunk) => {
myWorker.postMessage(
{
type: "DATA_AUDIO",
chunk,
},
[chunk]
);
},
})
);
}
}
function pauseRecording() {
myWorker.postMessage({ type: "PAUSE" });
}
function resumeRecording() {
myWorker.postMessage({ type: "RESUME" });
}
async function stopRecording() {
try {
myWorker.postMessage({ type: "STOP" });
stream.getTracks().forEach((track) => {
track.stop();
});
// Save the file
const fileHandle = await window.showSaveFilePicker({
suggestedName: "recording.flv",
types: [
{
description: "FLV Video",
accept: { "video/x-flv": [".flv"] },
},
],
});
const writableFileStream = await fileHandle.createWritable();
await writableFileStream.write(new Blob(recordingChunks));
await writableFileStream.close();
recordingChunks = [];
} catch (error) {
console.error("Error stopping recording:", error);
}
}
File diff suppressed because it is too large Load Diff
+262
View File
@@ -0,0 +1,262 @@
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>FlvMuxer</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
body {
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.container {
background-color: white;
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
padding: 2rem;
width: 90%;
max-width: 600px;
text-align: center;
}
h1 {
color: #333;
margin-bottom: 2rem;
font-size: 2rem;
}
.controls {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 12px;
margin-bottom: 2rem;
}
.button {
border: none;
border-radius: 50px;
padding: 12px 24px;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
min-width: 130px;
}
.button:hover {
transform: translateY(-2px);
}
.button:active {
transform: translateY(0);
}
.start-button {
background-color: #4caf50;
color: white;
}
.stop-button {
background-color: #f44336;
color: white;
}
.pause-button {
background-color: #ffc107;
color: #333;
}
.resume-button {
background-color: #2196f3;
color: white;
}
.status {
font-size: 1.2rem;
color: #555;
margin-bottom: 1.5rem;
}
.status-indicator {
display: inline-block;
width: 12px;
height: 12px;
border-radius: 50%;
margin-right: 10px;
background-color: #ccc;
}
.recording .status-indicator {
background-color: #f44336;
animation: blink 1.5s infinite;
}
.paused .status-indicator {
background-color: #ffc107;
}
.timer {
font-size: 2.5rem;
font-weight: 700;
color: #333;
margin-bottom: 2rem;
font-family: monospace;
}
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0.4;
}
100% {
opacity: 1;
}
}
.preview {
width: 100%;
max-width: 500px;
height: 280px;
background-color: #f0f0f0;
border-radius: 8px;
margin: 0 auto 1.5rem;
display: flex;
align-items: center;
justify-content: center;
color: #777;
border: 1px solid #ddd;
}
@media (max-width: 640px) {
.controls {
flex-direction: column;
align-items: center;
}
.button {
width: 100%;
}
.preview {
height: 200px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>FlvMuxer</h1>
<div class="preview">Preview Area</div>
<div class="status">
<span class="status-indicator"></span>
<span class="status-text">Ready</span>
</div>
<div class="timer">00:00:00</div>
<div class="controls">
<button
class="button start-button"
type="button"
title="Start Recording"
onclick="startRecording()"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
>
<circle cx="12" cy="12" r="10"></circle>
<circle cx="12" cy="12" r="4" fill="currentColor"></circle>
</svg>
Start Recording
</button>
<button
class="button stop-button"
type="button"
title="Stop Recording"
onclick="stopRecording()"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
>
<rect x="6" y="6" width="12" height="12" fill="currentColor"></rect>
</svg>
Stop Recording
</button>
<button
class="button pause-button"
type="button"
title="Pause Recording"
onclick="pauseRecording()"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
>
<rect x="6" y="5" width="4" height="14" fill="currentColor"></rect>
<rect x="14" y="5" width="4" height="14" fill="currentColor"></rect>
</svg>
Pause Recording
</button>
<button
class="button resume-button"
type="button"
title="Resume Recording"
onclick="resumeRecording()"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
>
<polygon points="5 3 19 12 5 21" fill="currentColor"></polygon>
</svg>
Resume Recording
</button>
</div>
</div>
<script src="./example.js"></script>
</body>
</html>
+72
View File
@@ -0,0 +1,72 @@
// 如果是 mac 获取整个屏幕不让获取音频要么获取标签页,要么得把下面的 configureAudio 注掉,不然会始终等待音频帧
(async function () {
const transport = new WebTransport(
"https://localhost:4433/webtransport/push/live/test",
{
// 如果是自签名证书需要获取证书哈希值,否则校验不过去
// serverCertificateHashes: [
// {
// algorithm: "sha-256",
// value: new Uint8Array([
// 158, 126, 137, 36, 236, 220, 87, 184, 67, 11, 37, 63, 235, 45, 169,
// 235, 241, 132, 231, 16, 92, 234, 232, 178, 246, 61, 238, 79, 134,
// 137, 249, 25,
// ]),
// },
// ],
}
);
await transport.ready;
const transportWritable =
(await transport.createBidirectionalStream()).writable;
const transportWriter = transportWritable.getWriter();
const writable = new WritableStream({
write: (chunk) => {
console.log(chunk);
transportWriter.write(chunk);
},
});
importScripts("flv-muxer.iife.js");
flvMuxer = new FlvMuxer(writable, {
mode: "record",
chunked: false,
});
flvMuxer.configureVideo({
encoderConfig: {
codec: "avc1.640034",
width: 2560,
height: 1440,
framerate: 30,
},
keyframeInterval: 90,
});
flvMuxer.configureAudio({
encoderConfig: {
codec: "mp4a.40.29",
sampleRate: 44100,
numberOfChannels: 2,
},
});
self.onmessage = async (e) => {
if (e.data.type === "DATA_VIDEO") {
flvMuxer.addRawChunk("video", e.data.chunk);
} else if (e.data.type == "DATA_AUDIO") {
flvMuxer.addRawChunk("audio", e.data.chunk);
} else if (e.data.type === "START") {
flvMuxer.start();
} else if (e.data.type === "PAUSE") {
await flvMuxer.pause();
} else if (e.data.type === "RESUME") {
flvMuxer.resume();
} else if (e.data.type === "STOP") {
await flvMuxer.stop();
}
};
})();