Skip to content

Commit

Permalink
Move more of the videosource code to the videosourcewrappers file
Browse files Browse the repository at this point in the history
  • Loading branch information
randhid committed Sep 16, 2024
1 parent f2f1f74 commit ddf873a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 62 deletions.
62 changes: 0 additions & 62 deletions components/camera/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"github.com/pkg/errors"
pb "go.viam.com/api/component/camera/v1"

"go.viam.com/rdk/components/camera/rtppassthrough"
"go.viam.com/rdk/data"
"go.viam.com/rdk/gostream"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/rimage/transform"
Expand Down Expand Up @@ -147,66 +145,6 @@ type ImagesSource interface {
Images(ctx context.Context) ([]NamedImage, resource.ResponseMetadata, error)
}

type sourceBasedCamera struct {
resource.Named
resource.AlwaysRebuild
VideoSource
rtpPassthroughSource rtppassthrough.Source
logging.Logger
}

func (vs *sourceBasedCamera) SubscribeRTP(
ctx context.Context,
bufferSize int,
packetsCB rtppassthrough.PacketCallback,
) (rtppassthrough.Subscription, error) {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.SubscribeRTP(ctx, bufferSize, packetsCB)
}
return rtppassthrough.NilSubscription, errors.New("SubscribeRTP unimplemented")
}

func (vs *sourceBasedCamera) Unsubscribe(ctx context.Context, id rtppassthrough.SubscriptionID) error {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.Unsubscribe(ctx, id)
}
return errors.New("Unsubscribe unimplemented")
}

func (vs *videoSource) SubscribeRTP(
ctx context.Context,
bufferSize int,
packetsCB rtppassthrough.PacketCallback,
) (rtppassthrough.Subscription, error) {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.SubscribeRTP(ctx, bufferSize, packetsCB)
}
return rtppassthrough.NilSubscription, errors.New("SubscribeRTP unimplemented")
}

func (vs *videoSource) Unsubscribe(ctx context.Context, id rtppassthrough.SubscriptionID) error {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.Unsubscribe(ctx, id)
}
return errors.New("Unsubscribe unimplemented")
}

// NewPinholeModelWithBrownConradyDistortion creates a transform.PinholeCameraModel from
// a *transform.PinholeCameraIntrinsics and a *transform.BrownConrady.
// If *transform.BrownConrady is `nil`, transform.PinholeCameraModel.Distortion
// is not set & remains nil, to prevent https://go.dev/doc/faq#nil_error.
func NewPinholeModelWithBrownConradyDistortion(pinholeCameraIntrinsics *transform.PinholeCameraIntrinsics,
distortion *transform.BrownConrady,
) transform.PinholeCameraModel {
var cameraModel transform.PinholeCameraModel
cameraModel.PinholeCameraIntrinsics = pinholeCameraIntrinsics

if distortion != nil {
cameraModel.Distortion = distortion
}
return cameraModel
}

// NewPropertiesError returns an error specific to a failure in Properties.
func NewPropertiesError(cameraIdentifier string) error {
return errors.Errorf("failed to get properties from %s", cameraIdentifier)
Expand Down
60 changes: 60 additions & 0 deletions components/camera/videosourcewrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,66 @@ func FromVideoSource(name resource.Name, src VideoSource, logger logging.Logger)
}
}

type sourceBasedCamera struct {
resource.Named
resource.AlwaysRebuild
VideoSource
rtpPassthroughSource rtppassthrough.Source
logging.Logger
}

func (vs *sourceBasedCamera) SubscribeRTP(
ctx context.Context,
bufferSize int,
packetsCB rtppassthrough.PacketCallback,
) (rtppassthrough.Subscription, error) {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.SubscribeRTP(ctx, bufferSize, packetsCB)
}
return rtppassthrough.NilSubscription, errors.New("SubscribeRTP unimplemented")
}

func (vs *sourceBasedCamera) Unsubscribe(ctx context.Context, id rtppassthrough.SubscriptionID) error {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.Unsubscribe(ctx, id)
}
return errors.New("Unsubscribe unimplemented")
}

func (vs *videoSource) SubscribeRTP(
ctx context.Context,
bufferSize int,
packetsCB rtppassthrough.PacketCallback,
) (rtppassthrough.Subscription, error) {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.SubscribeRTP(ctx, bufferSize, packetsCB)
}
return rtppassthrough.NilSubscription, errors.New("SubscribeRTP unimplemented")
}

func (vs *videoSource) Unsubscribe(ctx context.Context, id rtppassthrough.SubscriptionID) error {
if vs.rtpPassthroughSource != nil {
return vs.rtpPassthroughSource.Unsubscribe(ctx, id)
}
return errors.New("Unsubscribe unimplemented")
}

// NewPinholeModelWithBrownConradyDistortion creates a transform.PinholeCameraModel from
// a *transform.PinholeCameraIntrinsics and a *transform.BrownConrady.
// If *transform.BrownConrady is `nil`, transform.PinholeCameraModel.Distortion
// is not set & remains nil, to prevent https://go.dev/doc/faq#nil_error.
func NewPinholeModelWithBrownConradyDistortion(pinholeCameraIntrinsics *transform.PinholeCameraIntrinsics,
distortion *transform.BrownConrady,
) transform.PinholeCameraModel {
var cameraModel transform.PinholeCameraModel
cameraModel.PinholeCameraIntrinsics = pinholeCameraIntrinsics

if distortion != nil {
cameraModel.Distortion = distortion
}
return cameraModel
}

// NewVideoSourceFromReader creates a VideoSource either with or without a projector. The stream type
// argument is for detecting whether or not the resulting camera supports return
// of pointcloud data in the absence of an implemented NextPointCloud function.
Expand Down

0 comments on commit ddf873a

Please sign in to comment.