├── .gitignore ├── README.md ├── appsink └── main.go ├── appsink2 └── main.go ├── appsrc └── main.go ├── dynamicpipeline └── main.go ├── go.mod ├── go.sum └── webrtc └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | 17 | .idea/ 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gst-go-demo 2 | demos for https://github.com/notedit/gst 3 | -------------------------------------------------------------------------------- /appsink/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/notedit/gst" 6 | ) 7 | 8 | func main() { 9 | 10 | pipeline, err := gst.ParseLaunch("videotestsrc num-buffers=10 ! appsink name=sink") 11 | 12 | if err != nil { 13 | panic("pipe error") 14 | } 15 | 16 | element := pipeline.GetByName("sink") 17 | 18 | pipeline.SetState(gst.StatePlaying) 19 | 20 | for { 21 | 22 | sample, err := element.PullSample() 23 | if err != nil { 24 | if element.IsEOS() == true { 25 | fmt.Println("eos") 26 | return 27 | } else { 28 | fmt.Println(err) 29 | continue 30 | } 31 | } 32 | fmt.Println("got sample", sample.Duration) 33 | } 34 | 35 | pipeline.SetState(gst.StateNull) 36 | 37 | pipeline = nil 38 | element = nil 39 | } 40 | -------------------------------------------------------------------------------- /appsink2/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "github.com/notedit/gst" 6 | ) 7 | 8 | func main() { 9 | 10 | 11 | pipelineStr := "videotestsrc is-live=true ! video/x-raw,format=I420,framerate=15/1 ! x264enc aud=false bframes=0 speed-preset=veryfast key-int-max=15 ! video/x-h264,stream-format=byte-stream,profile=baseline ! h264parse ! rtph264pay config-interval=-1 pt=%d ! appsink name=appsink" 12 | pipelineStr = fmt.Sprintf(pipelineStr, 100) 13 | 14 | err := gst.CheckPlugins([]string{"x264","rtp","videoparsersbad"}) 15 | 16 | if err != nil { 17 | fmt.Println(err) 18 | } 19 | 20 | pipeline, err := gst.ParseLaunch(pipelineStr) 21 | 22 | if err != nil { 23 | panic(err) 24 | } 25 | 26 | element := pipeline.GetByName("appsink") 27 | 28 | pipeline.SetState(gst.StatePlaying) 29 | 30 | for { 31 | sample, err := element.PullSample() 32 | if err != nil { 33 | if element.IsEOS() == true { 34 | fmt.Println("eos") 35 | return 36 | } else { 37 | fmt.Println(err) 38 | continue 39 | } 40 | } 41 | fmt.Println("got sample", sample.Duration) 42 | } 43 | 44 | pipeline.SetState(gst.StateNull) 45 | 46 | pipeline = nil 47 | element = nil 48 | } 49 | 50 | -------------------------------------------------------------------------------- /appsrc/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | 7 | "github.com/notedit/gst" 8 | ) 9 | 10 | func main() { 11 | 12 | pipeline, err := gst.ParseLaunch("appsrc name=mysource format=time is-live=true do-timestamp=true ! videoconvert ! autovideosink") 13 | 14 | if err != nil { 15 | panic("pipeline error") 16 | } 17 | 18 | videoCap := gst.CapsFromString("video/x-raw,format=RGB,width=320,height=240,bpp=24,depth=24") 19 | 20 | element := pipeline.GetByName("mysource") 21 | 22 | element.SetObject("caps", videoCap) 23 | 24 | pipeline.SetState(gst.StatePlaying) 25 | 26 | i := 0 27 | for { 28 | 29 | if i > 100 { 30 | break 31 | } 32 | 33 | data := make([]byte, 320*240*3) 34 | 35 | err := element.PushBuffer(data) 36 | 37 | if err != nil { 38 | fmt.Println("push buffer error") 39 | break 40 | } 41 | 42 | fmt.Println("push one") 43 | i++ 44 | time.Sleep(50000000) 45 | } 46 | 47 | pipeline.SetState(gst.StateNull) 48 | 49 | pipeline = nil 50 | element = nil 51 | videoCap = nil 52 | } 53 | -------------------------------------------------------------------------------- /dynamicpipeline/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | 7 | "github.com/notedit/gst" 8 | ) 9 | 10 | func main() { 11 | 12 | pipeline, err := gst.PipelineNew("test-pipeline") 13 | 14 | if err != nil { 15 | panic(err) 16 | } 17 | 18 | source, _ := gst.ElementFactoryMake("uridecodebin", "source") 19 | convert, _ := gst.ElementFactoryMake("audioconvert", "convert") 20 | sink, _ := gst.ElementFactoryMake("autoaudiosink", "sink") 21 | 22 | pipeline.Add(source) 23 | pipeline.Add(convert) 24 | pipeline.Add(sink) 25 | 26 | convert.Link(sink) 27 | 28 | source.SetObject("uri", "https://www.freedesktop.org/software/gstreamer-sdk/data/media/sintel_trailer-480p.webm") 29 | 30 | source.SetPadAddedCallback(func(element *gst.Element, pad *gst.Pad) { 31 | capstr := pad.GetCurrentCaps().ToString() 32 | 33 | if strings.HasPrefix(capstr, "audio") { 34 | sinkpad := convert.GetStaticPad("sink") 35 | pad.Link(sinkpad) 36 | } 37 | 38 | }) 39 | 40 | pipeline.SetState(gst.StatePlaying) 41 | 42 | bus := pipeline.GetBus() 43 | 44 | for { 45 | message := bus.Pull(gst.MessageError | gst.MessageEos) 46 | fmt.Println("message:", message.GetName()) 47 | if message.GetType() == gst.MessageEos { 48 | break 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/notedit/gst-go-demo 2 | 3 | go 1.12 4 | 5 | require github.com/notedit/gst v0.1.3 // indirect 6 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/notedit/gst v0.1.2 h1:WJXtIrLWeiFjwODhc3hwzikNjqazPO3mgoVkRSS35gY= 2 | github.com/notedit/gst v0.1.2/go.mod h1:99yZ0oIrCg0xd2a1rn/1cjcUAHLAGTDxXLD1FQ5NpoM= 3 | github.com/notedit/gst v0.1.3 h1:Z5q274M3TJst3w+iAdZLIoIhzp7QiwYdH98Vik+f89g= 4 | github.com/notedit/gst v0.1.3/go.mod h1:99yZ0oIrCg0xd2a1rn/1cjcUAHLAGTDxXLD1FQ5NpoM= 5 | -------------------------------------------------------------------------------- /webrtc/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | "github.com/notedit/gst" 7 | ) 8 | 9 | const pipelineStr = ` 10 | webrtcbin bundle-policy=max-bundle name=pusher 11 | videotestsrc is-live=true pattern=ball ! videoconvert ! queue ! vp8enc deadline=1 ! rtpvp8pay ! application/x-rtp,media=video,encoding-name=VP8,payload=96 ! pusher. 12 | audiotestsrc is-live=true wave=red-noise ! audioconvert ! audioresample ! queue ! opusenc ! rtpopuspay ! queue ! application/x-rtp,media=audio,encoding-name=OPUS,payload=97 ! pusher. 13 | ` 14 | 15 | func main() { 16 | 17 | pipeline, err := gst.ParseLaunch(pipelineStr) 18 | 19 | if err != nil { 20 | panic(err) 21 | } 22 | 23 | pipeline.SetState(gst.StatePlaying) 24 | 25 | bus := pipeline.GetBus() 26 | 27 | for { 28 | message := bus.Pull(gst.MessageAny) 29 | fmt.Println("message:", message.GetName()) 30 | if message.GetType() == gst.MessageEos { 31 | break 32 | } 33 | } 34 | 35 | pipeline.SetState(gst.StateNull) 36 | 37 | pipeline = nil 38 | } 39 | --------------------------------------------------------------------------------