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

add example client-record-format-mjpeg-from-image (#489) #512

Merged
merged 1 commit into from
Jan 27, 2024
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Features:
* [client-record-format-h265](examples/client-record-format-h265/main.go)
* [client-record-format-lpcm](examples/client-record-format-lpcm/main.go)
* [client-record-format-mjpeg](examples/client-record-format-mjpeg/main.go)
* [client-record-format-mjpeg-from-image](examples/client-record-format-mjpeg-from-image/main.go)
* [client-record-format-mpeg4audio](examples/client-record-format-mpeg4audio/main.go)
* [client-record-format-opus](examples/client-record-format-opus/main.go)
* [client-record-format-vp8](examples/client-record-format-vp8/main.go)
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-g711/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/G711 packets with GStreamer
// 2. connect to a RTSP server, announce a G711 media
// 2. connect to a RTSP server, announce a G711 format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-g722/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/G722 packets with GStreamer
// 2. connect to a RTSP server, announce a G722 media
// 2. connect to a RTSP server, announce a G722 format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-h264/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/H264 packets with GStreamer
// 2. connect to a RTSP server, announce an H264 media
// 2. connect to a RTSP server, announce an H264 format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-h265/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/H265 packets with GStreamer
// 2. connect to a RTSP server, announce an H265 media
// 2. connect to a RTSP server, announce an H265 format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-lpcm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/LPCM packets with GStreamer
// 2. connect to a RTSP server, announce an LPCM media
// 2. connect to a RTSP server, announce an LPCM format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
104 changes: 104 additions & 0 deletions examples/client-record-format-mjpeg-from-image/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"bytes"
"image"
"image/color"
"image/jpeg"
"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/rtptime"
)

// This example shows how to
// 1. connect to a RTSP server, announce a M-JPEG format
// 2. generate an image
// 3. encode the image with JPEG
// 4. generate RTP/M-JPEG packets from the JPEG image
// 5. write packets to the server

func main() {
// create a description that contains a M-JPEG format
forma := &format.MJPEG{}
desc := &description.Session{
Medias: []*description.Media{{
Type: description.MediaTypeVideo,
Formats: []format.Format{forma},
}},
}

// connect to the server, announce the format and start recording
c := gortsplib.Client{}
err := c.StartRecording("rtsp://localhost:8554/mystream", desc)
if err != nil {
panic(err)
}
defer c.Close()

// setup JPEG -> RTP/M-JPEG encoder
rtpEnc, err := forma.CreateEncoder()
if err != nil {
panic(err)
}

// setup timestamp generator
rtpTime := rtptime.NewEncoder(forma.ClockRate(), 0)
start := time.Now()

// setup a ticker to sleep between frames
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()

i := 0

for range ticker.C {
// create a RGBA image
image := image.NewRGBA(image.Rect(0, 0, 640, 480))

// fill the image
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 < image.Rect.Dy(); y++ {
for x := 0; x < image.Rect.Dx(); x++ {
image.SetRGBA(x, y, cl)
}
}
i = (i + 1) % 3

// encode the image with JPEG
var buf bytes.Buffer
err := jpeg.Encode(&buf, image, &jpeg.Options{Quality: 80})
if err != nil {
panic(err)
}

// generate RTP/M-JPEG packets from the JPEG image
pkts, err := rtpEnc.Encode(buf.Bytes())
if err != nil {
panic(err)
}

// get current timestamp
ts := rtpTime.Encode(time.Now().Sub(start))

// write packets to the server
for _, pkt := range pkts {
pkt.Timestamp = ts

err = c.WritePacketRTP(desc.Medias[0], pkt)
if err != nil {
panic(err)
}
}
}
}
2 changes: 1 addition & 1 deletion examples/client-record-format-mjpeg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/M-JPEG packets with GStreamer
// 2. connect to a RTSP server, announce a M-JPEG media
// 2. connect to a RTSP server, announce a M-JPEG format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-mpeg4audio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// This example shows how to
// 1. generate RTP/MPEG-4 audio packets with GStreamer
// 2. connect to a RTSP server, announce an MPEG-4 audio media
// 2. connect to a RTSP server, announce an MPEG-4 audio format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-opus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/Opus packets with GStreamer
// 2. connect to a RTSP server, announce an Opus media
// 2. connect to a RTSP server, announce an Opus format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-vp8/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/VP8 packets with GStreamer
// 2. connect to a RTSP server, announce a VP8 media
// 2. connect to a RTSP server, announce a VP8 format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-format-vp9/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// This example shows how to
// 1. generate RTP/VP9 packets with GStreamer
// 2. connect to a RTSP server, announce a VP9 media
// 2. connect to a RTSP server, announce a VP9 format
// 3. route the packets from GStreamer to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-options/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// This example shows how to
// 1. set additional client options
// 2. generate RTP/H264 frames from a file with GStreamer
// 3. connect to a RTSP server, announce an H264 media
// 3. connect to a RTSP server, announce an H264 format
// 4. write the frames to the server

func main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/client-record-pause/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

// This example shows how to
// 1. generate RTP/H264 frames from a file with GStreamer
// 2. connect to a RTSP server, announce an H264 media
// 2. connect to a RTSP server, announce an H264 format
// 3. write the frames to the server for 5 seconds
// 4. pause for 5 seconds
// 5. repeat
Expand Down
Loading