clipper/backend/server/server.go

186 lines
5.2 KiB
Go
Raw Normal View History

2021-09-25 17:00:19 +00:00
package server
import (
2021-10-22 19:30:09 +00:00
"context"
2021-11-01 05:28:40 +00:00
"fmt"
2021-10-29 12:52:31 +00:00
"io"
2021-10-22 19:30:09 +00:00
"net/http"
2021-09-25 17:00:19 +00:00
"time"
2021-10-22 19:30:09 +00:00
pbMediaSet "git.netflux.io/rob/clipper/generated/pb/media_set"
"git.netflux.io/rob/clipper/media"
2021-11-01 05:28:40 +00:00
"github.com/google/uuid"
2021-11-16 06:48:30 +00:00
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
2021-10-22 19:30:09 +00:00
"github.com/improbable-eng/grpc-web/go/grpcweb"
2021-11-16 06:48:30 +00:00
"go.uber.org/zap"
2021-10-22 19:30:09 +00:00
"google.golang.org/grpc"
2021-11-01 05:28:40 +00:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
2021-10-22 19:30:09 +00:00
"google.golang.org/protobuf/types/known/durationpb"
2021-09-25 17:00:19 +00:00
)
const (
// ts-proto generates code that automatically retries for a subset of gRPC
// response codes. To avoid invoking this behaviour, default to returning a
2021-11-13 18:52:49 +00:00
// Cancelled code for now.
// See https://github.com/stephenh/ts-proto/blob/459b94f5b2988d58d186461332e888c3e511603a/src/generate-grpc-web.ts#L293
// and https://github.com/stephenh/ts-proto/pull/131.
defaultResponseCode = codes.Canceled
defaultResponseMessage = "An unexpected error occurred"
)
2021-11-01 05:28:40 +00:00
2021-11-16 06:48:30 +00:00
const (
getAudioTimeout = time.Minute * 5
getAudioSegmentTimeout = time.Second * 10
)
type ResponseError struct {
err error
s string
2021-11-01 05:28:40 +00:00
}
func (r *ResponseError) Error() string {
return fmt.Sprintf("unexpected error: %v", r.err.Error())
2021-11-01 05:28:40 +00:00
}
func (r *ResponseError) Unwrap() error {
return r.err
2021-11-01 05:28:40 +00:00
}
func (r *ResponseError) GRPCStatus() *status.Status {
return status.New(defaultResponseCode, r.s)
}
func newResponseError(err error) *ResponseError {
return &ResponseError{err: err, s: defaultResponseMessage}
2021-11-01 05:28:40 +00:00
}
2021-09-25 17:00:19 +00:00
type Options struct {
2021-10-29 12:52:31 +00:00
BindAddr string
Timeout time.Duration
2021-11-01 05:28:40 +00:00
Store media.Store
2021-11-12 12:41:59 +00:00
YoutubeClient media.YoutubeClient
2021-10-29 12:52:31 +00:00
S3Client media.S3Client
2021-09-25 17:00:19 +00:00
}
2021-11-01 05:28:40 +00:00
// mediaSetServiceController implements gRPC controller for MediaSetService
type mediaSetServiceController struct {
pbMediaSet.UnimplementedMediaSetServiceServer
2021-10-22 19:30:09 +00:00
2021-11-01 05:28:40 +00:00
mediaSetService *media.MediaSetService
2021-10-22 19:30:09 +00:00
}
2021-11-01 05:28:40 +00:00
// Get returns a pbMediaSet.MediaSet
func (c *mediaSetServiceController) Get(ctx context.Context, request *pbMediaSet.GetRequest) (*pbMediaSet.MediaSet, error) {
mediaSet, err := c.mediaSetService.Get(ctx, request.GetYoutubeId())
2021-10-22 19:30:09 +00:00
if err != nil {
return nil, newResponseError(err)
2021-10-22 19:30:09 +00:00
}
result := pbMediaSet.MediaSet{
2021-11-02 18:03:26 +00:00
Id: mediaSet.ID.String(),
2021-11-02 16:20:47 +00:00
YoutubeId: mediaSet.YoutubeID,
AudioChannels: int32(mediaSet.Audio.Channels),
AudioFrames: mediaSet.Audio.Frames,
AudioApproxFrames: mediaSet.Audio.ApproxFrames,
AudioSampleRate: int32(mediaSet.Audio.SampleRate),
AudioYoutubeItag: int32(mediaSet.Audio.YoutubeItag),
AudioMimeType: mediaSet.Audio.MimeType,
VideoDuration: durationpb.New(mediaSet.Video.Duration),
VideoYoutubeItag: int32(mediaSet.Video.YoutubeItag),
VideoMimeType: mediaSet.Video.MimeType,
2021-10-22 19:30:09 +00:00
}
return &result, nil
}
2021-11-16 06:48:30 +00:00
// GetAudio returns a stream of GetAudioProgress relating to the entire audio
// part of the MediaSet.
2021-11-01 05:28:40 +00:00
func (c *mediaSetServiceController) GetAudio(request *pbMediaSet.GetAudioRequest, stream pbMediaSet.MediaSetService_GetAudioServer) error {
2021-11-16 06:48:30 +00:00
// TODO: reduce timeout when fetching from S3
2021-11-01 05:28:40 +00:00
ctx, cancel := context.WithTimeout(context.Background(), getAudioTimeout)
2021-10-29 12:52:31 +00:00
defer cancel()
2021-11-01 05:28:40 +00:00
id, err := uuid.Parse(request.GetId())
if err != nil {
return newResponseError(err)
2021-11-01 05:28:40 +00:00
}
reader, err := c.mediaSetService.GetAudio(ctx, id, int(request.GetNumBins()))
2021-10-29 12:52:31 +00:00
if err != nil {
return newResponseError(err)
2021-10-29 12:52:31 +00:00
}
for {
progress, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
return newResponseError(err)
2021-10-29 12:52:31 +00:00
}
// TODO: consider using int32 throughout the backend flow to avoid this.
peaks := make([]int32, len(progress.Peaks))
for i, p := range progress.Peaks {
peaks[i] = int32(p)
}
2021-11-01 05:28:40 +00:00
progressPb := pbMediaSet.GetAudioProgress{
2021-10-29 12:52:31 +00:00
PercentCompleted: progress.PercentComplete,
Peaks: peaks,
}
stream.Send(&progressPb)
}
2021-10-22 19:30:09 +00:00
return nil
}
2021-11-16 06:48:30 +00:00
// GetAudioSegment returns a set of peaks for a segment of an audio part of a
// MediaSet.
func (c *mediaSetServiceController) GetAudioSegment(ctx context.Context, request *pbMediaSet.GetAudioSegmentRequest) (*pbMediaSet.GetAudioSegmentResponse, error) {
ctx, cancel := context.WithTimeout(ctx, getAudioSegmentTimeout)
defer cancel()
id, err := uuid.Parse(request.GetId())
if err != nil {
return nil, newResponseError(err)
}
_, err = c.mediaSetService.GetAudioSegment(ctx, id, request.StartFrame, request.EndFrame, int(request.GetNumBins()))
if err != nil {
return nil, newResponseError(err)
}
return nil, nil
}
2021-10-22 19:30:09 +00:00
func Start(options Options) error {
2021-11-16 06:48:30 +00:00
logger, _ := zap.NewDevelopment()
defer logger.Sync()
2021-10-22 19:30:09 +00:00
2021-11-16 06:48:30 +00:00
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(grpczap.UnaryServerInterceptor(logger)),
)
2021-10-29 12:52:31 +00:00
2021-11-16 06:48:30 +00:00
fetchMediaSetService := media.NewMediaSetService(options.Store, options.YoutubeClient, options.S3Client, logger)
2021-11-01 05:28:40 +00:00
pbMediaSet.RegisterMediaSetServiceServer(grpcServer, &mediaSetServiceController{mediaSetService: fetchMediaSetService})
2021-10-22 19:30:09 +00:00
2021-11-13 18:52:49 +00:00
// TODO: configure CORS
2021-10-22 19:30:09 +00:00
grpcWebServer := grpcweb.WrapServer(grpcServer, grpcweb.WithOriginFunc(func(string) bool { return true }))
2021-11-16 06:48:30 +00:00
2021-10-22 19:30:09 +00:00
handler := func(w http.ResponseWriter, r *http.Request) {
grpcWebServer.ServeHTTP(w, r)
}
httpServer := http.Server{
Addr: options.BindAddr,
ReadTimeout: options.Timeout,
WriteTimeout: options.Timeout,
Handler: http.HandlerFunc(handler),
}
return httpServer.ListenAndServe()
2021-09-25 17:00:19 +00:00
}