Skip to content

Commit

Permalink
Add wav format to magic producer
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed May 21, 2024
1 parent a518488 commit 54c8ca0
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/magic/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/AlexxIT/go2rtc/pkg/magic/mjpeg"
"github.com/AlexxIT/go2rtc/pkg/mpegts"
"github.com/AlexxIT/go2rtc/pkg/multipart"
"github.com/AlexxIT/go2rtc/pkg/wav"
)

func Open(r io.Reader) (core.Producer, error) {
Expand All @@ -28,6 +29,9 @@ func Open(r io.Reader) (core.Producer, error) {
case string(b) == annexb.StartCode:
return bitstream.Open(rd)

case string(b) == wav.FourCC:
return wav.Open(rd)

case bytes.HasPrefix(b, []byte{0xFF, 0xD8}):
return mjpeg.Open(rd)

Expand Down
127 changes: 127 additions & 0 deletions pkg/wav/wav.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package wav

import (
"bufio"
"encoding/binary"
"errors"
"io"

"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtp"
)

const FourCC = "RIFF"

func Open(r io.Reader) (*Producer, error) {
// https://en.wikipedia.org/wiki/WAV
// https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
rd := bufio.NewReaderSize(r, core.BufferSize)

// skip Master RIFF chunk
if _, err := rd.Discard(12); err != nil {
return nil, err
}

codec := &core.Codec{}

for {
chunkID, data, err := readChunk(rd)
if err != nil {
return nil, err
}

if chunkID == "data" {
break
}

if chunkID == "fmt " {
// https://audiocoding.cc/articles/2008-05-22-wav-file-structure/wav_formats.txt
switch data[0] {
case 1:
codec.Name = core.CodecPCML
case 6:
codec.Name = core.CodecPCMA
case 7:
codec.Name = core.CodecPCMU
}

codec.Channels = uint16(data[2])
codec.ClockRate = binary.LittleEndian.Uint32(data[4:])
}
}

if codec.Name == "" {
return nil, errors.New("waw: unsupported codec")
}

prod := &Producer{rd: rd, cl: r.(io.Closer)}
prod.Type = "WAV producer"
prod.Medias = []*core.Media{
{
Kind: core.KindAudio,
Direction: core.DirectionRecvonly,
Codecs: []*core.Codec{codec},
},
}
return prod, nil
}

type Producer struct {
core.SuperProducer
rd *bufio.Reader
cl io.Closer
}

func (c *Producer) Start() error {
var seq uint16
var ts uint32

const PacketSize = 0.040 * 8000 // 40ms

for {
payload := make([]byte, PacketSize)
if _, err := io.ReadFull(c.rd, payload); err != nil {
return err
}

c.Recv += PacketSize

if len(c.Receivers) == 0 {
continue
}

pkt := &rtp.Packet{
Header: rtp.Header{
Version: 2,
Marker: true,
SequenceNumber: seq,
Timestamp: ts,
},
Payload: payload,
}
c.Receivers[0].WriteRTP(pkt)

seq++
ts += PacketSize
}
}

func (c *Producer) Stop() error {
_ = c.SuperProducer.Close()
return c.cl.Close()
}

func readChunk(r io.Reader) (chunkID string, data []byte, err error) {
b := make([]byte, 8)
if _, err = io.ReadFull(r, b); err != nil {
return
}

if chunkID = string(b[:4]); chunkID != "data" {
size := binary.LittleEndian.Uint32(b[4:])
data = make([]byte, size)
_, err = io.ReadFull(r, data)
}

return
}

0 comments on commit 54c8ca0

Please sign in to comment.