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,31 @@
package main
import (
"image"
"image/color"
)
var dummyImageCount = 0
func createDummyImage() *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
var cl color.RGBA
switch dummyImageCount {
case 0:
cl = color.RGBA{255, 0, 0, 0}
case 1:
cl = color.RGBA{0, 255, 0, 0}
case 2:
cl = color.RGBA{0, 0, 255, 0}
}
dummyImageCount = (dummyImageCount + 1) % 3
for y := 0; y < img.Rect.Dy(); y++ {
for x := 0; x < img.Rect.Dx(); x++ {
img.SetRGBA(x, y, cl)
}
}
return img
}

View File

@@ -86,6 +86,8 @@ func (d *h264Encoder) initialize() error {
res = C.av_frame_get_buffer(d.rgbaFrame, 0)
if res < 0 {
C.av_frame_free(&d.rgbaFrame)
C.avcodec_close(d.codecCtx)
return fmt.Errorf("av_frame_get_buffer() failed")
}
@@ -102,6 +104,9 @@ func (d *h264Encoder) initialize() error {
res = C.av_frame_get_buffer(d.yuv420Frame, 0)
if res < 0 {
C.av_frame_free(&d.yuv420Frame)
C.av_frame_free(&d.rgbaFrame)
C.avcodec_close(d.codecCtx)
return fmt.Errorf("av_frame_get_buffer() failed")
}
@@ -116,7 +121,6 @@ func (d *h264Encoder) initialize() error {
d.pkt = C.av_packet_alloc()
if d.pkt == nil {
C.av_packet_free(&d.pkt)
C.av_frame_free(&d.yuv420Frame)
C.av_frame_free(&d.rgbaFrame)
C.avcodec_close(d.codecCtx)

View File

@@ -4,8 +4,6 @@ package main
import (
"crypto/rand"
"image"
"image/color"
"log"
"time"
@@ -24,7 +22,7 @@ import (
// 7. repeat
// This example requires the FFmpeg libraries, that can be installed with this command:
// apt install -y libavformat-dev libswscale-dev gcc pkg-config
// apt install -y libavcodec-dev libswscale-dev gcc pkg-config
func multiplyAndDivide(v, m, d int64) int64 {
secs := v / d
@@ -41,28 +39,6 @@ func randUint32() (uint32, error) {
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
}
func createDummyImage(i int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
var cl color.RGBA
switch i {
case 0:
cl = color.RGBA{255, 0, 0, 0}
case 1:
cl = color.RGBA{0, 255, 0, 0}
case 2:
cl = color.RGBA{0, 0, 255, 0}
}
for y := 0; y < img.Rect.Dy(); y++ {
for x := 0; x < img.Rect.Dx(); x++ {
img.SetRGBA(x, y, cl)
}
}
return img
}
func main() {
// create a stream description that contains a H264 format
forma := &format.H264{
@@ -114,16 +90,13 @@ func main() {
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
i := 0
for range ticker.C {
// create a dummy image
img := createDummyImage(i)
i = (i + 1) % 3
// get current timestamp
pts := multiplyAndDivide(int64(time.Since(start)), int64(forma.ClockRate()), int64(time.Second))
// create a dummy image
img := createDummyImage()
// encode the image with H264
au, pts, err := h264enc.encode(img, pts)
if err != nil {
@@ -145,7 +118,7 @@ func main() {
// write RTP packets to the server
for _, pkt := range pkts {
pkt.Timestamp = uint32(int64(randomStart) + pts)
pkt.Timestamp += uint32(int64(randomStart) + pts)
err = c.WritePacketRTP(desc.Medias[0], pkt)
if err != nil {