93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"git.entr0py.de/garionion/gstreamer-graphix/api"
|
|
"git.entr0py.de/garionion/gstreamer-graphix/gstreamer"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"time"
|
|
)
|
|
|
|
type APIClient struct {
|
|
client api.PipelineServiceClient
|
|
log zerolog.Logger
|
|
}
|
|
|
|
func Init(address string) *APIClient {
|
|
log := log.With().Str("gRPC", address).Logger()
|
|
conn, err := grpc.Dial(address, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
if err != nil {
|
|
log.Error().Msgf("Failed to connect to gRPC server: %s", err)
|
|
}
|
|
return &APIClient{
|
|
client: api.NewPipelineServiceClient(conn),
|
|
log: log,
|
|
}
|
|
}
|
|
|
|
func (a *APIClient) CreatePipeline(pipeline *api.Pipeline) (string, error) {
|
|
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*5))
|
|
resp, err := a.client.CreatePipeline(ctx, pipeline)
|
|
return resp.PipelineId.GetId(), err
|
|
}
|
|
|
|
func (a *APIClient) DeletePipeline(id string) error {
|
|
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*5))
|
|
_, err := a.client.DeletePipeline(ctx, &api.PipelineID{Id: id})
|
|
return err
|
|
}
|
|
|
|
func (a *APIClient) UpdatePipeline(id string, overlays []*api.Overlay) error {
|
|
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(time.Second*5))
|
|
_, err := a.client.UpdatePipeline(ctx, &api.PipelineUpdates{
|
|
Id: &api.PipelineID{Id: id},
|
|
Overlays: overlays,
|
|
})
|
|
return err
|
|
}
|
|
|
|
func convertGstreamerOverlayToAPIOverlay(overlay gstreamer.Overlay) *api.Overlay {
|
|
|
|
switch overlay.(type) {
|
|
case *gstreamer.TextOverlay:
|
|
o := overlay.(*gstreamer.TextOverlay)
|
|
return &api.Overlay{
|
|
Name: o.Name,
|
|
X: int64(o.X),
|
|
Y: int64(o.Y),
|
|
Display: o.Display,
|
|
BlendIn: int64(o.BlendIn),
|
|
BlendOut: int64(o.BlendOut),
|
|
OverlayData: &api.Overlay_TextOverlay{
|
|
TextOverlay: &api.TextOverlay{
|
|
Text: o.Value,
|
|
FontName: o.Font,
|
|
FontSize: uint64(o.FontSize),
|
|
FontWeight: o.FontWeight,
|
|
FontColor: o.Color,
|
|
MaxWidth: uint64(o.MaxWidth),
|
|
},
|
|
},
|
|
}
|
|
case *gstreamer.ImageOverlay:
|
|
o := overlay.(*gstreamer.ImageOverlay)
|
|
return &api.Overlay{
|
|
Name: o.Name,
|
|
X: int64(o.X),
|
|
Y: int64(o.Y),
|
|
Display: o.Display,
|
|
BlendIn: int64(o.BlendIn),
|
|
BlendOut: int64(o.BlendOut),
|
|
OverlayData: &api.Overlay_ImageOverlay{
|
|
ImageOverlay: &api.ImageOverlay{
|
|
Path: o.Path,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|