package gstreamer import ( "fmt" "github.com/rs/zerolog" "github.com/tinyzimmer/go-gst/gst" ) func (i *InputElement) create(pipeline *gst.Pipeline, log zerolog.Logger) error { elements := map[string]element{} e, err := gst.NewElementWithName(i.Type, i.Name) if err != nil { log.Error().Msgf("could not create element %s: %v\n", i.Name, err) return fmt.Errorf("could not create element %s", i.Name) } for prop, value := range i.Properties { setPropertyWrapper(e, prop, value) } err = pipeline.Add(e) if err != nil { log.Error().Msgf("could not add element %s to pipeline: %v\n", i.Name, err) return fmt.Errorf("could not add element %s to pipeline", i.Name) } input := element{element: e, name: i.Name, next: fmt.Sprintf("%s-decodebin", i.Name)} elements[i.Name] = input // ==================== //==================== // RAWVIDEOPARSE e, err = gst.NewElementWithName("rawvideoparse", fmt.Sprintf("%s-rawvideoparse", i.Name)) if err != nil { log.Error().Msgf("could not create rawvideoparse for %s: %v\n", i.Name, err) return fmt.Errorf("could not create rawvideoparse for %s", i.Name) } setPropertyWrapper(e, "format", "bgra") setPropertyWrapper(e, "width", 1280) setPropertyWrapper(e, "height", 720) setPropertyWrapper(e, "framerate", "50") err = pipeline.Add(e) if err != nil { log.Error().Msgf("could not add rawvideoparse for %s to pipeline: %v\n", i.Name, err) return fmt.Errorf("could not add rawvideoparse for %s to pipeline", i.Name) } rawvideoparse := element{element: e, name: fmt.Sprintf("%s-rawvideoparse", i.Name), prev: i.Name, next: fmt.Sprintf("%s-videoconvert", i.Name)} elements[fmt.Sprintf("%s-rawvideoparse", i.Name)] = rawvideoparse err = input.linkElementWrapper(&rawvideoparse, log) if err != nil { log.Error().Msgf("could not link %s to %s: %v", fmt.Sprintf("%s-rawvideoparse", i.Name), i.Name, err) return fmt.Errorf("could not link %s to %s", fmt.Sprintf("%s-rawvideoparse", i.Name), i.Name) } //==================== // ==================== // VIDEOCONVERT e, err = gst.NewElementWithName("videoconvert", fmt.Sprintf("%s-videoconvert", i.Name)) if err != nil { log.Error().Msgf("could not create videoconvert for %s: %v\n", i.Name, err) return fmt.Errorf("could not create videoconvert for %s", i.Name) } err = pipeline.Add(e) if err != nil { log.Error().Msgf("could not add videoconvert for %s to pipeline: %v\n", i.Name, err) return fmt.Errorf("could not add videoconvert for %s to pipeline", i.Name) } videoconvert := element{element: e, name: fmt.Sprintf("%s-videoconvert", i.Name), prev: fmt.Sprintf("%s-rawvideoparse", i.Name), next: fmt.Sprintf("%s-videoscale", i.Name)} elements[fmt.Sprintf("%s-videoconvert", i.Name)] = videoconvert err = rawvideoparse.linkElementWrapper(&videoconvert, log) if err != nil { log.Error().Msgf("could not link %s to %s: %v\n", fmt.Sprintf("%s-videoconvert", i.Name), fmt.Sprintf("%s-decodebin", i.Name), err) return fmt.Errorf("could not link %s to %s", fmt.Sprintf("%s-videoconvert", i.Name), fmt.Sprintf("%s-decodebin", i.Name)) } //===================== //===================== // VIDEOSCALE e, err = gst.NewElementWithName("videoscale", fmt.Sprintf("%s-videoscale", i.Name)) if err != nil { log.Error().Msgf("could not create videoscale for %s: %v\n", i.Name, err) return fmt.Errorf("could not create videoscale for %s", i.Name) } err = pipeline.Add(e) if err != nil { log.Error().Msgf("could not add videoscale for %s to pipeline: %v\n", i.Name, err) return fmt.Errorf("could not add videoscale for %s to pipeline", i.Name) } videoscale := element{element: e, name: fmt.Sprintf("%s-videoscale", i.Name), prev: fmt.Sprintf("%s-videoconvert", i.Name), next: fmt.Sprintf("%s-videorate", i.Name)} elements[fmt.Sprintf("%s-videoscale", i.Name)] = videoscale err = videoconvert.linkElementWrapper(&videoscale, log) if err != nil { log.Error().Msgf("could not link %s to %s: %v\n", fmt.Sprintf("%s-videoconvert", i.Name), fmt.Sprintf("%s-videoscale", i.Name), err) return fmt.Errorf("could not link %s to %s", fmt.Sprintf("%s-videoconvert", i.Name), fmt.Sprintf("%s-videoscale", i.Name)) } //===================== //===================== // VIDEORATE e, err = gst.NewElementWithName("videorate", fmt.Sprintf("%s-videorate", i.Name)) if err != nil { log.Error().Msgf("could not create videorate for %s: %v\n", i.Name, err) return fmt.Errorf("could not create videorate for %s", i.Name) } err = pipeline.Add(e) if err != nil { log.Error().Msgf("could not add videorate for %s to pipeline: %v\n", i.Name, err) return fmt.Errorf("could not add videorate for %s to pipeline", i.Name) } videorate := element{element: e, name: fmt.Sprintf("%s-videorate", i.Name), prev: fmt.Sprintf("%s-videoscale", i.Name), outputCaps: i.Caps} elements[fmt.Sprintf("%s-videorate", i.Name)] = videorate err = videoscale.linkElementWrapper(&videorate, log) if err != nil { log.Error().Msgf("could not link %s to %s: %v\n", fmt.Sprintf("%s-videoscale", i.Name), fmt.Sprintf("%s-videorate", i.Name), err) return fmt.Errorf("could not link %s to %s", fmt.Sprintf("%s-videoscale", i.Name), fmt.Sprintf("%s-videorate", i.Name)) } i.elements = elements return nil } func (i *InputElement) getLastElement() *element { e := i.elements[fmt.Sprintf("%s-videorate", i.Name)] return &e }