Files
rkKVM/http/hw/stream/audio.go

48 lines
1.2 KiB
Go

package stream
import (
"io"
"net/http"
"os/exec"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
type Audio struct {
}
func AudioHandler(c *gin.Context) {
cmd := exec.Command("ffmpeg",
"-f", "alsa",
"-i", "hw:0,0",
"-acodec", "aac",
"-f", "mp4", "-")
//c := "arecord -D hw:0,0 -f cd -r 44100 -c 2 | /app/ffmpeg -re -f wav -i pipe:0 -c:a aac -b:a 128k -ar 44100 -ac 2 -f rtp rtp://127.0.0.1:5006?pkt_size=1200"
//cmd := exec.Command("sh", "-c", c)
// ffmpeg -f alsa -i hw:0,0 -acodec aac -f mp4 -
stdout, err := cmd.StdoutPipe()
if err != nil {
c.String(http.StatusInternalServerError, "Failed to capture audio: %v", err)
log.Errorf("Failed to capture audio: %v", err)
return
}
if err := cmd.Start(); err != nil {
c.String(http.StatusInternalServerError, "Failed to start ffmpeg: %v", err)
log.Errorf("Failed to start ffmpeg: %v", err)
return
}
defer cmd.Wait()
//c.Header("Content-Type", "audio/aac")
c.Header("Content-Type", "audio/wav")
c.Header("Transfer-Encoding", "chunked")
if _, err := io.Copy(c.Writer, stdout); err != nil {
c.String(http.StatusInternalServerError, "Failed to stream audio: %v", err)
log.Errorf("Failed to stream audio: %v", err)
return
}
}