forked from bluenviron/gohlslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (94 loc) · 2.53 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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"
)
// This example shows how to:
// 1. generate a MPEG-TS/H264 stream with GStreamer
// 2. re-encode the stream into HLS and serve it with an HTTP server.
//go:embed index.html
var index []byte
// handleIndex wraps an HTTP handler and serves the home page
func handleIndex(wrapped http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write([]byte(index))
return
}
wrapped(w, r)
}
}
func main() {
// create the HLS muxer
mux := &gohlslib.Muxer{
VideoTrack: &gohlslib.Track{
Codec: &codecs.H264{},
},
}
err := mux.Start()
if err != nil {
panic(err)
}
// create an HTTP server and link it to the HLS muxer
s := &http.Server{
Addr: ":8080",
Handler: handleIndex(mux.Handle),
}
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 {
err := r.Read()
if err != nil {
panic(err)
}
}
}