Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtmp: Support Opus writes from internal stream. #4168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion internal/protocols/rtmp/from_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

var errNoSupportedCodecsFrom = errors.New(
"the stream doesn't contain any supported codec, which are currently H264, MPEG-4 Audio, MPEG-1/2 Audio")
"the stream doesn't contain any supported codec, which are currently H264, MPEG-4 Audio, MPEG-1/2 Audio, Opus")

func multiplyAndDivide2(v, m, d time.Duration) time.Duration {
secs := v / d
Expand Down Expand Up @@ -179,6 +179,24 @@ func setupAudio(
return audioFormatMPEG1
}

var audioFormatOpus *format.Opus
audioMedia = strea.Desc().FindFormat(&audioFormatOpus)
if audioMedia != nil {
strea.AddReader(
reader,
audioMedia,
audioFormatOpus,
func(u unit.Unit) error {
tunit := u.(*unit.Opus)
if tunit.Packets == nil {
return nil
}
nconn.SetWriteDeadline(time.Now().Add(writeTimeout))
return (*w).WriteOpus(timestampToDuration(tunit.PTS, audioFormatOpus.ClockRate()), tunit.Packets)
})
return audioFormatOpus
}

return nil
}

Expand Down
34 changes: 34 additions & 0 deletions internal/protocols/rtmp/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
case *format.MPEG4Audio:
return message.CodecMPEG4Audio

case *format.Opus:
return float64(message.FourCCOpus)

default:
return 0
}
Expand Down Expand Up @@ -162,6 +165,21 @@
}
}

if track, ok := audioTrack.(*format.Opus); ok {
err = w.conn.Write(&message.AudioExSequenceStart{
ChunkStreamID: message.AudioChunkStreamID,
MessageStreamID: 0x1000000,
FourCC: message.FourCCOpus,
OpusHeader: &message.OpusIDHeader{
ChannelCount: uint8(track.ChannelCount),
PreSkip: 3840,
},
})
if err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -212,3 +230,19 @@
DTS: pts,
})
}

func (w *Writer) WriteOpus(pts time.Duration, packets [][]byte) error {

Check warning on line 234 in internal/protocols/rtmp/writer.go

View workflow job for this annotation

GitHub Actions / golangci_lint

exported: exported method Writer.WriteOpus should have comment or be unexported (revive)
// only write one packet for now
// additional packets will require self-delimiting framing
// see Appendix B of RFC 6716
for _, packet := range packets {
return w.conn.Write(&message.AudioExCodedFrames{
ChunkStreamID: message.AudioChunkStreamID,
MessageStreamID: 0x1000000,
DTS: pts,
FourCC: message.FourCCOpus,
Payload: packet,
})
}
return nil
}
Loading