|
| 1 | +// This example uses gstreamer's device provider api. |
| 2 | +// |
| 3 | +// https://gstreamer.freedesktop.org/documentation/gstreamer/gstdeviceprovider.html |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + |
| 10 | + "github.com/go-gst/go-glib/glib" |
| 11 | + "github.com/go-gst/go-gst/examples" |
| 12 | + "github.com/go-gst/go-gst/gst" |
| 13 | +) |
| 14 | + |
| 15 | +func runPipeline(loop *glib.MainLoop) error { |
| 16 | + |
| 17 | + gst.Init(nil) |
| 18 | + fmt.Println("Running device provider") |
| 19 | + // if len(os.Args) < 2 { |
| 20 | + // fmt.Printf("USAGE: %s <uri>\n", os.Args[0]) |
| 21 | + // os.Exit(1) |
| 22 | + // } |
| 23 | + |
| 24 | + // uri := os.Args[1] |
| 25 | + fmt.Println("Creating device monitor") |
| 26 | + |
| 27 | + // provider := gst.FindDeviceProviderByName("foo") |
| 28 | + // fmt.Println("Created device provider", provider) |
| 29 | + |
| 30 | + provider := gst.FindDeviceProviderByName("avfdeviceprovider") |
| 31 | + fmt.Println("Created device provider", provider) |
| 32 | + |
| 33 | + if provider == nil { |
| 34 | + fmt.Println("No provider found") |
| 35 | + os.Exit(2) |
| 36 | + } |
| 37 | + |
| 38 | + fmt.Println("Getting device provider bus") |
| 39 | + bus := provider.GetBus() |
| 40 | + fmt.Println("Got device provider bus", bus) |
| 41 | + |
| 42 | + bus.AddWatch(func(msg *gst.Message) bool { |
| 43 | + switch msg.Type() { |
| 44 | + case gst.MessageDeviceAdded: |
| 45 | + message := msg.ParseDeviceAdded().GetDisplayName() |
| 46 | + fmt.Println("Added: ", message) |
| 47 | + case gst.MessageDeviceRemoved: |
| 48 | + message := msg.ParseDeviceRemoved().GetDisplayName() |
| 49 | + fmt.Println("Removed: ", message) |
| 50 | + default: |
| 51 | + // All messages implement a Stringer. However, this is |
| 52 | + // typically an expensive thing to do and should be avoided. |
| 53 | + fmt.Println("Type: ", msg.Type()) |
| 54 | + fmt.Println("Message: ", msg) |
| 55 | + } |
| 56 | + return true |
| 57 | + }) |
| 58 | + |
| 59 | + fmt.Println("Starting device monitor") |
| 60 | + provider.Start() |
| 61 | + fmt.Println("Started device monitor") |
| 62 | + |
| 63 | + fmt.Println("listing devices from provider") |
| 64 | + devices := provider.GetDevices() |
| 65 | + for i, v := range devices { |
| 66 | + fmt.Printf("Device: %d %s\n", i, v.GetDisplayName()) |
| 67 | + } |
| 68 | + |
| 69 | + loop.Run() |
| 70 | + |
| 71 | + return nil |
| 72 | +} |
| 73 | + |
| 74 | +func main() { |
| 75 | + examples.RunLoop(func(loop *glib.MainLoop) error { |
| 76 | + return runPipeline(loop) |
| 77 | + }) |
| 78 | +} |
0 commit comments