improve examples (#708)

This commit is contained in:
Alessandro Ros
2025-02-22 14:28:02 +01:00
committed by GitHub
parent 3829fef787
commit 90cac184c9
58 changed files with 1593 additions and 929 deletions

View File

@@ -0,0 +1,24 @@
package main
import "math"
const (
sampleRate = 48000
frequency = 400
amplitude = (1 << 14) - 1
)
func createDummyAudio(pts int64, prevPTS int64) []byte {
sampleCount := (pts - prevPTS)
n := 0
ret := make([]byte, sampleCount*2)
for i := int64(0); i < sampleCount; i++ {
v := int16(amplitude * math.Sin((float64(prevPTS+i)*frequency*math.Pi*2)/sampleRate))
ret[n] = byte(v >> 8)
ret[n+1] = byte(v)
n += 2
}
return ret
}

View File

@@ -1,77 +1,139 @@
//go:build cgo
package main
import (
"crypto/rand"
"log"
"net"
"time"
"github.com/bluenviron/gortsplib/v4"
"github.com/bluenviron/gortsplib/v4/pkg/description"
"github.com/bluenviron/gortsplib/v4/pkg/format"
"github.com/bluenviron/gortsplib/v4/pkg/format/rtpsimpleaudio"
"github.com/bluenviron/mediacommon/v2/pkg/codecs/opus"
"github.com/pion/rtp"
)
// This example shows how to
// 1. generate a Opus stream and RTP packets with GStreamer
// 2. connect to a RTSP server, announce an Opus format
// 3. route the packets from GStreamer to the server
// 1. connect to a RTSP server, announce a Opus format
// 2. generate dummy LPCM audio samples
// 3. encode audio samples with Opus
// 3. generate RTP packets from Opus packets
// 4. write RTP packets to the server
// This example requires the FFmpeg libraries, that can be installed with this command:
// apt install -y libavcodec-dev gcc pkg-config
func multiplyAndDivide(v, m, d int64) int64 {
secs := v / d
dec := v % d
return (secs*m + dec*m/d)
}
func randUint32() (uint32, error) {
var b [4]byte
_, err := rand.Read(b[:])
if err != nil {
return 0, err
}
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
}
func encodeMultiple(rtpEnc *rtpsimpleaudio.Encoder, opusPkts [][]byte) ([]*rtp.Packet, error) {
ret := make([]*rtp.Packet, len(opusPkts))
pts := uint32(0)
for i, opusPkt := range opusPkts {
var err error
ret[i], err = rtpEnc.Encode(opusPkt)
if err != nil {
return nil, err
}
ret[i].Timestamp += pts
pts += uint32(opus.PacketDuration2(opusPkt))
}
return ret, nil
}
func main() {
// open a listener to receive RTP/Opus packets
pc, err := net.ListenPacket("udp", "localhost:9000")
if err != nil {
panic(err)
}
defer pc.Close()
log.Println("Waiting for a RTP/Opus stream on UDP port 9000 - you can send one with GStreamer:\n" +
"gst-launch-1.0 audiotestsrc freq=300 ! audioconvert ! audioresample ! audio/x-raw,rate=48000" +
" ! opusenc ! rtpopuspay ! udpsink host=127.0.0.1 port=9000")
// wait for first packet
buf := make([]byte, 2048)
n, _, err := pc.ReadFrom(buf)
if err != nil {
panic(err)
}
log.Println("stream connected")
// create a description that contains a Opus format
forma := &format.Opus{
PayloadTyp: 96,
ChannelCount: 1,
}
desc := &description.Session{
Medias: []*description.Media{{
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.Opus{
PayloadTyp: 96,
IsStereo: false,
}},
Type: description.MediaTypeAudio,
Formats: []format.Format{forma},
}},
}
// connect to the server and start recording
c := gortsplib.Client{}
err = c.StartRecording("rtsp://myuser:mypass@localhost:8554/mystream", desc)
err := c.StartRecording("rtsp://myuser:mypass@localhost:8554/mystream", desc)
if err != nil {
panic(err)
}
defer c.Close()
var pkt rtp.Packet
for {
// parse RTP packet
err = pkt.Unmarshal(buf[:n])
// setup LPCM -> Opus encoder
opusEnc := &opusEncoder{}
err = opusEnc.initialize()
if err != nil {
panic(err)
}
// setup Opus -> RTP encoder
rtpEnc, err := forma.CreateEncoder()
if err != nil {
panic(err)
}
start := time.Now()
prevPTS := int64(0)
randomStart, err := randUint32()
if err != nil {
panic(err)
}
// setup a ticker to sleep between writings
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
// get current timestamp
pts := multiplyAndDivide(int64(time.Since(start)), int64(forma.ClockRate()), int64(time.Second))
// generate dummy LPCM audio samples
samples := createDummyAudio(pts, prevPTS)
// encode samples with Opus
opusPkts, outPTS, err := opusEnc.encode(samples)
if err != nil {
panic(err)
}
// route RTP packet to the server
err = c.WritePacketRTP(desc.Medias[0], &pkt)
// generate RTP packets from Opus packets
pkts, err := encodeMultiple(rtpEnc, opusPkts)
if err != nil {
panic(err)
}
// read another RTP packet from source
n, _, err = pc.ReadFrom(buf)
if err != nil {
panic(err)
log.Printf("writing RTP packets with PTS=%d, packet count=%d", outPTS, len(pkts))
for _, pkt := range pkts {
pkt.Timestamp += uint32(int64(randomStart) + outPTS)
err = c.WritePacketRTP(desc.Medias[0], pkt)
if err != nil {
panic(err)
}
}
prevPTS = pts
}
}

View File

@@ -0,0 +1,153 @@
package main
import (
"fmt"
"unsafe"
)
// #cgo pkg-config: libavcodec libavutil
// #include <libavcodec/avcodec.h>
import "C"
func frameData(frame *C.AVFrame) **C.uint8_t {
return (**C.uint8_t)(unsafe.Pointer(&frame.data[0]))
}
func frameLineSize(frame *C.AVFrame) *C.int {
return (*C.int)(unsafe.Pointer(&frame.linesize[0]))
}
func switchEndianness16(samples []byte) []byte {
ls := len(samples)
for i := 0; i < ls; i += 2 {
samples[i], samples[i+1] = samples[i+1], samples[i]
}
return samples
}
// opusEncoder is a wrapper around FFmpeg's Opus encoder.
type opusEncoder struct {
Width int
Height int
FPS int
codecCtx *C.AVCodecContext
frame *C.AVFrame
pkt *C.AVPacket
samplesBuffer []byte
samplesBufferPTS int64
}
// initialize initializes a opusEncoder.
func (d *opusEncoder) initialize() error {
codec := C.avcodec_find_encoder(C.AV_CODEC_ID_OPUS)
if codec == nil {
return fmt.Errorf("avcodec_find_encoder() failed")
}
d.codecCtx = C.avcodec_alloc_context3(codec)
if d.codecCtx == nil {
return fmt.Errorf("avcodec_alloc_context3() failed")
}
d.codecCtx.bit_rate = 64000
d.codecCtx.sample_fmt = C.AV_SAMPLE_FMT_S16
d.codecCtx.sample_rate = 48000
d.codecCtx.channel_layout = C.AV_CH_LAYOUT_MONO
d.codecCtx.channels = C.av_get_channel_layout_nb_channels(d.codecCtx.channel_layout)
res := C.avcodec_open2(d.codecCtx, codec, nil)
if res < 0 {
C.avcodec_close(d.codecCtx)
return fmt.Errorf("avcodec_open2() failed")
}
d.frame = C.av_frame_alloc()
if d.frame == nil {
C.avcodec_close(d.codecCtx)
return fmt.Errorf("av_frame_alloc() failed")
}
d.frame.nb_samples = d.codecCtx.frame_size
d.frame.format = (C.int)(d.codecCtx.sample_fmt)
d.frame.channel_layout = d.codecCtx.channel_layout
res = C.av_frame_get_buffer(d.frame, 0)
if res < 0 {
C.av_frame_free(&d.frame)
C.avcodec_close(d.codecCtx)
return fmt.Errorf("av_frame_get_buffer() failed")
}
d.pkt = C.av_packet_alloc()
if d.pkt == nil {
C.av_frame_free(&d.frame)
C.avcodec_close(d.codecCtx)
return fmt.Errorf("av_packet_alloc() failed")
}
return nil
}
// close closes the decoder.
func (d *opusEncoder) close() {
C.av_packet_free(&d.pkt)
C.av_frame_free(&d.frame)
C.avcodec_close(d.codecCtx)
}
// encode encodes LPCM samples into Opus packets.
func (d *opusEncoder) encode(samples []byte) ([][]byte, int64, error) {
// convert from big-endian to little-endian
samples = switchEndianness16(samples)
// put samples into an internal buffer
d.samplesBuffer = append(d.samplesBuffer, samples...)
// split buffer into AVFrames
requiredSampleSize := (int)(d.codecCtx.frame_size) * 2
frameCount := len(d.samplesBuffer) / requiredSampleSize
if frameCount == 0 {
return nil, 0, fmt.Errorf("sample buffer is not filled enough")
}
ret := make([][]byte, frameCount)
var pts int64
for i := 0; i < frameCount; i++ {
samples = d.samplesBuffer[:requiredSampleSize]
d.samplesBuffer = d.samplesBuffer[requiredSampleSize:]
samplePTS := d.samplesBufferPTS
d.samplesBufferPTS += int64(len(samples) / 2)
// pass samples pointer to frame
d.frame.data[0] = (*C.uint8_t)(&samples[0])
// send frame to the encoder
d.frame.pts = (C.int64_t)(samplePTS)
res := C.avcodec_send_frame(d.codecCtx, d.frame)
if res < 0 {
return nil, 0, fmt.Errorf("avcodec_send_frame() failed")
}
// wait for result
res = C.avcodec_receive_packet(d.codecCtx, d.pkt)
if res < 0 {
return nil, 0, fmt.Errorf("avcodec_receive_packet() failed")
}
// perform a deep copy of the data before unreferencing the packet
data := C.GoBytes(unsafe.Pointer(d.pkt.data), d.pkt.size)
if i == 0 {
pts = (int64)(d.pkt.pts)
}
C.av_packet_unref(d.pkt)
ret[i] = data
}
return ret, pts, nil
}