Skip to content

Commit

Permalink
use mediacommon/pkg/mpegts in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 30, 2023
1 parent 7396a73 commit eb9f865
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 118 deletions.
62 changes: 48 additions & 14 deletions examples/muxer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package main
import (
_ "embed"
"log"
"net"
"net/http"
"time"

"github.com/bluenviron/mediacommon/pkg/formats/mpegts"

"github.com/bluenviron/gohlslib"
"github.com/bluenviron/gohlslib/pkg/codecs"
)
Expand All @@ -32,19 +35,13 @@ func handleIndex(wrapped http.HandlerFunc) http.HandlerFunc {
}

func main() {
// create MPEG-TS receiver
receiver, err := newMPEGTSReceiver()
if err != nil {
panic(err)
}

// create the HLS muxer
mux := &gohlslib.Muxer{
VideoTrack: &gohlslib.Track{
Codec: &codecs.H264{},
},
}
err = mux.Start()
err := mux.Start()
if err != nil {
panic(err)
}
Expand All @@ -57,22 +54,59 @@ func main() {
log.Println("HTTP server created on :8080")
go s.ListenAndServe()

// create a socket to receive MPEG-TS packets
pc, err := net.ListenPacket("udp", "localhost:9000")
if err != nil {
panic(err)
}
defer pc.Close()

log.Println("Waiting for a MPEG-TS/H264 stream on UDP port 9000 - you can send one with GStreamer:\n" +
"gst-launch-1.0 videotestsrc ! video/x-raw,width=1920,height=1080" +
" ! x264enc speed-preset=ultrafast bitrate=3000 key-int-max=60" +
" ! video/x-h264,profile=high" +
" ! mpegtsmux alignment=6 ! udpsink host=127.0.0.1 port=9000")

// create a MPEG-TS reader
r, err := mpegts.NewReader(mpegts.NewBufferedReader(newPacketConnReader(pc)))
if err != nil {
panic(err)
}

var timeDec *mpegts.TimeDecoder

// find the H264 track
found := false
for _, track := range r.Tracks() {
if _, ok := track.Codec.(*mpegts.CodecH264); ok {
// setup a callback that is called once a H264 access unit is received
r.OnDataH26x(track, func(rawPTS int64, _ int64, au [][]byte) error {
// decode the time
if timeDec == nil {
timeDec = mpegts.NewTimeDecoder(rawPTS)
}
pts := timeDec.Decode(rawPTS)

// pass the access unit to the HLS muxer
log.Printf("visit http://localhost:8080 - encoding access unit with PTS = %v", pts)
mux.WriteH26x(time.Now(), pts, au)

return nil
})
found = true
break
}
}

if !found {
panic("H264 track not found")
}

// read from the MPEG-TS stream
for {
// read a H264 access unit from the stream
au, pts, err := receiver.Read()
err := r.Read()
if err != nil {
panic(err)
}

log.Printf("visit http://localhost:8080 - encoding access unit with PTS = %v", pts)

// encode access unit in HLS format
mux.WriteH26x(time.Now(), pts, au)
}
}
104 changes: 0 additions & 104 deletions examples/muxer/mpegtsreceiver.go

This file was deleted.

20 changes: 20 additions & 0 deletions examples/muxer/packet_conn_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"net"
)

type packetConnReader struct {
net.PacketConn
}

func newPacketConnReader(pc net.PacketConn) *packetConnReader {
return &packetConnReader{
PacketConn: pc,
}
}

func (r *packetConnReader) Read(p []byte) (int, error) {
n, _, err := r.PacketConn.ReadFrom(p)
return n, err
}

0 comments on commit eb9f865

Please sign in to comment.