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,85 +1,129 @@
//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/mediacommon/v2/pkg/codecs/mpeg4audio"
"github.com/pion/rtp"
)
// This example shows how to
// 1. generate a MPEG-4 audio stream and RTP packets with GStreamer
// 2. connect to a RTSP server, announce an MPEG-4 audio format
// 3. route the packets from GStreamer to the server
// 1. connect to a RTSP server, announce a MPEG-4 Audio (AAC) format
// 2. generate dummy LPCM audio samples
// 3. encode audio samples with MPEG-4 Audio (AAC)
// 3. generate RTP packets from MPEG-4 Audio units
// 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 libswresample-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 main() {
// open a listener to receive RTP/MPEG-4 audio packets
pc, err := net.ListenPacket("udp", "localhost:9000")
if err != nil {
panic(err)
// create a description that contains a MPEG-4 Audio format
forma := &format.MPEG4Audio{
PayloadTyp: 96,
Config: &mpeg4audio.Config{
Type: mpeg4audio.ObjectTypeAACLC,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}
defer pc.Close()
log.Println("Waiting for a RTP/MPEG-4 audio 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" +
" ! avenc_aac bitrate=128000 ! rtpmp4gpay ! 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 MPEG-4 audio format
desc := &description.Session{
Medias: []*description.Media{{
Type: description.MediaTypeVideo,
Formats: []format.Format{&format.MPEG4Audio{
PayloadTyp: 96,
Config: &mpeg4audio.Config{
Type: mpeg4audio.ObjectTypeAACLC,
SampleRate: 48000,
ChannelCount: 2,
},
SizeLength: 13,
IndexLength: 3,
IndexDeltaLength: 3,
}},
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 -> MPEG-4 Audio encoder
mp4aEnc := &mp4aEncoder{}
err = mp4aEnc.initialize()
if err != nil {
panic(err)
}
// setup MPEG-4 Audio -> 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 MPEG-4 Audio
aus, outPTS, err := mp4aEnc.encode(samples)
if err != nil {
panic(err)
}
if aus == nil {
continue
}
// generate RTP packets from MPEG-4 audio access units
pkts, err := rtpEnc.Encode(aus)
if err != nil {
panic(err)
}
// route RTP packet to the server
err = c.WritePacketRTP(desc.Medias[0], &pkt)
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)
}
}
// read another RTP packet from source
n, _, err = pc.ReadFrom(buf)
if err != nil {
panic(err)
}
prevPTS = pts
}
}

View File

@@ -0,0 +1,230 @@
package main
import (
"fmt"
"runtime"
"unsafe"
)
// #cgo pkg-config: libavcodec libavutil libswresample
// #include <libavcodec/avcodec.h>
// #include <libswresample/swresample.h>
// #include <libavutil/opt.h>
// #include <libavutil/channel_layout.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
}
func littleEndianToFloat(swrCtx *C.struct_SwrContext, samples []byte) ([]byte, error) {
sampleCount := len(samples) / 2
outSize := len(samples) * 2
outSamples := make([]byte, outSize)
var p runtime.Pinner
p.Pin(&outSamples[0])
p.Pin(&samples[0])
defer p.Unpin()
outBufs := (*C.uint8_t)(&outSamples[0])
inBufs := (*C.uint8_t)(&samples[0])
res := C.swr_convert(swrCtx, &outBufs, (C.int)(sampleCount), &inBufs, (C.int)(sampleCount))
if res < 0 {
return nil, fmt.Errorf("swr_convert() failed")
}
return outSamples, nil
}
// mp4aEncoder is a wrapper around FFmpeg's MPEG-4 Audio encoder.
type mp4aEncoder struct {
Width int
Height int
FPS int
codecCtx *C.AVCodecContext
frame *C.AVFrame
swrCtx *C.struct_SwrContext
pkt *C.AVPacket
samplesBuffer []byte
samplesBufferPTS int64
}
// initialize initializes a mp4aEncoder.
func (d *mp4aEncoder) initialize() error {
codec := C.avcodec_find_encoder(C.AV_CODEC_ID_AAC)
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_FLT
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.swrCtx = C.swr_alloc()
if d.swrCtx == nil {
C.av_frame_free(&d.frame)
C.avcodec_close(d.codecCtx)
return fmt.Errorf("swr_alloc() failed")
}
cstr := C.CString("out_channel_layout")
defer C.free(unsafe.Pointer(cstr))
C.av_opt_set_channel_layout(unsafe.Pointer(d.swrCtx), cstr, (C.int64_t)(d.codecCtx.channel_layout), 0)
cstr = C.CString("out_sample_fmt")
defer C.free(unsafe.Pointer(cstr))
C.av_opt_set_int(unsafe.Pointer(d.swrCtx), cstr, C.AV_SAMPLE_FMT_FLTP, 0)
cstr = C.CString("out_sample_rate")
defer C.free(unsafe.Pointer(cstr))
C.av_opt_set_int(unsafe.Pointer(d.swrCtx), cstr, 48000, 0)
cstr = C.CString("in_channel_layout")
defer C.free(unsafe.Pointer(cstr))
C.av_opt_set_channel_layout(unsafe.Pointer(d.swrCtx), cstr, (C.int64_t)(d.codecCtx.channel_layout), 0)
cstr = C.CString("in_sample_fmt")
defer C.free(unsafe.Pointer(cstr))
C.av_opt_set_int(unsafe.Pointer(d.swrCtx), cstr, C.AV_SAMPLE_FMT_S16, 0)
cstr = C.CString("in_sample_rate")
defer C.free(unsafe.Pointer(cstr))
C.av_opt_set_int(unsafe.Pointer(d.swrCtx), cstr, 48000, 0)
res = C.swr_init(d.swrCtx)
if res < 0 {
C.swr_free(&d.swrCtx)
C.av_frame_free(&d.frame)
C.avcodec_close(d.codecCtx)
return fmt.Errorf("swr_init() failed")
}
d.pkt = C.av_packet_alloc()
if d.pkt == nil {
C.swr_free(&d.swrCtx)
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 *mp4aEncoder) close() {
C.av_packet_free(&d.pkt)
C.swr_free(&d.swrCtx)
C.av_frame_free(&d.frame)
C.avcodec_close(d.codecCtx)
}
// encode encodes LPCM samples into Opus packets.
func (d *mp4aEncoder) encode(samples []byte) ([][]byte, int64, error) {
// convert from big-endian to little-endian
samples = switchEndianness16(samples)
// convert from little-endian to float
samples, err := littleEndianToFloat(d.swrCtx, samples)
if err != nil {
return nil, 0, err
}
// put samples into an internal buffer
d.samplesBuffer = append(d.samplesBuffer, samples...)
// split buffer into AVFrames
requiredSampleSize := (int)(d.codecCtx.frame_size) * 4
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) / 4)
// 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 == -C.EAGAIN {
return nil, 0, nil
}
if res < 0 {
fmt.Println(res)
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
}