add: run video in pipe

This commit is contained in:
Charles Hathaway
2023-10-01 20:38:38 -07:00
parent 19bb6c49b4
commit a07a993bab
8 changed files with 570 additions and 141 deletions
+31 -137
View File
@@ -5,57 +5,37 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"sync"
"time"
"connectrpc.com/connect"
pb "github.com/chathaway-codes/home-sensors/v2/gen"
servicepb "github.com/chathaway-codes/home-sensors/v2/gen/genconnect"
"github.com/google/uuid"
"github.com/chathaway-codes/home-sensors/v2/internal/video"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
"github.com/pion/webrtc/v3/pkg/media/ivfreader"
"google.golang.org/protobuf/encoding/prototext"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"google.golang.org/protobuf/proto"
)
var (
videoFileName = flag.String("in", "/home/charles/Downloads/simpsons_movie_1080p_hddvd_trailer/output.ivf", "Where to load data from; if set to -, stdin will be used")
)
func withAuth[T any](token string, v *T) *connect.Request[T] {
req := connect.NewRequest[T](v)
req.Header().Add("Authorization", "Bearer "+token)
return req
}
func main() { //nolint
func main() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
flag.Parse()
ctx := context.Background()
/*httpClient := &http.Client{
Transport: &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, _ *tls.Config) (net.Conn, error) {
// If you're also using this client for non-h2c traffic, you may want
// to delegate to tls.Dial if the network isn't TCP or the addr isn't
// in an allowlist.
log.Printf("Connecting to %s : %s", network, addr)
return net.Dial(network, addr)
},
// Don't forget timeouts!
},
}*/
vid, err := newVideo(ctx)
vid, err := video.Default.Get()
if err != nil {
log.Fatalf("Failed to start video: %v", err)
log.Fatal().Err(err).Msg("failed to get default video")
}
client := servicepb.NewSignalerServiceClient(http.DefaultClient, "http://192.168.0.65:8080/")
authToken, err := client.CreateAuthToken(ctx, connect.NewRequest(&pb.CreateAuthTokenRequest{
@@ -67,115 +47,29 @@ func main() { //nolint
},
}))
if err != nil {
log.Fatalf("Failed to get auth token: %v", err)
log.Fatal().Err(err).Msg("failed to get auth token")
}
token := authToken.Msg.GetToken()
log.Printf("Got token %s", prototext.Format(authToken.Msg))
go vid.Run()
defer vid.Done()
// Create a new RTCPeerConnection
log.Printf("Waiting for connections")
log.Info().Msg("waiting for connections")
for {
// Wait for a session request
session, err := client.PopSession(ctx, withAuth(token, &pb.PopSessionRequest{}))
if err != nil {
log.Fatalf("error creating session: %v", err)
log.Fatal().Err(err).Msg("error creating session")
}
go handleSession(ctx, client, token, session, vid)
}
}
type video struct {
mu sync.Mutex
listeners map[string]chan<- []byte
codec string
}
func newVideo(ctx context.Context) (*video, error) {
func handleSession(ctx context.Context, client servicepb.SignalerServiceClient, token string, session *connect.Response[pb.Session], vid *video.Video) {
var err error
// Assert that we have an audio or video file
videoFileName := *videoFileName
var videoIn io.Reader
if videoFileName == "-" {
videoIn = os.Stdin
} else {
videoIn, err = os.Open(videoFileName)
if err != nil {
return nil, fmt.Errorf("failed to open %q: %v", videoFileName, err)
}
}
ivf, header, err := ivfreader.NewWith(videoIn)
if err != nil {
return nil, fmt.Errorf("failed to read video: %v", err)
}
// Determine video codec
var trackCodec string
switch header.FourCC {
case "AV01":
trackCodec = webrtc.MimeTypeAV1
case "VP90":
trackCodec = webrtc.MimeTypeVP9
case "VP80":
trackCodec = webrtc.MimeTypeVP8
default:
return nil, fmt.Errorf("unable to handle FourCC %s", header.FourCC)
}
vid := &video{
listeners: make(map[string]chan<- []byte),
codec: trackCodec,
}
go func() {
// Send our video file frame at a time. Pace our sending so we send it at the same speed it should be played back as.
// This isn't required since the video is timestamped, but we will such much higher loss if we send all at once.
//
// It is important to use a time.Ticker instead of time.Sleep because
// * avoids accumulating skew, just calling time.Sleep didn't compensate for the time spent parsing the data
// * works around latency issues with Sleep (see https://github.com/golang/go/issues/44343)
ticker := time.NewTicker(time.Millisecond * time.Duration((float32(header.TimebaseNumerator)/float32(header.TimebaseDenominator))*1000))
for ; true; <-ticker.C {
frame, _, ivfErr := ivf.ParseNextFrame()
if errors.Is(ivfErr, io.EOF) {
fmt.Printf("All video frames parsed and sent")
}
if ivfErr != nil {
panic(ivfErr)
}
vid.mu.Lock()
for _, lis := range vid.listeners {
lis <- frame
}
vid.mu.Unlock()
}
}()
return vid, nil
}
func (v *video) Join() (<-chan []byte, string, func()) {
v.mu.Lock()
defer v.mu.Unlock()
myID := uuid.New().String()
ch := make(chan []byte)
v.listeners[myID] = ch
return ch, v.codec, func() {
v.mu.Lock()
defer v.mu.Unlock()
delete(v.listeners, myID)
}
}
func handleSession(ctx context.Context, client servicepb.SignalerServiceClient, token string, session *connect.Response[pb.Session], vid *video) {
var err error
log.Printf("New session")
log.Debug().Msg("new session")
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
@@ -189,7 +83,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(context.Background())
defer func() {
if err := peerConnection.Close(); err != nil {
fmt.Printf("cannot close peerConnection: %v\n", err)
log.Debug().Err(err).Msg("cannot close peerConnection")
}
}()
@@ -199,12 +93,12 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
// Create a video track
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: trackCodec}, "video", "pion")
if videoTrackErr != nil {
log.Printf("Failed to create video track: %v", err)
log.Info().Err(err).Msg("Failed to create video track")
}
rtpSender, err := peerConnection.AddTrack(videoTrack)
if err != nil {
log.Printf("Failed to add track to connection: %v", err)
log.Info().Err(err).Msg("Failed to add track to connection")
}
// Read incoming RTCP packets
@@ -241,7 +135,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
fmt.Printf("Connection State has changed %s \n", connectionState.String())
log.Debug().Msgf("Connection State has changed %s \n", connectionState.String())
if connectionState == webrtc.ICEConnectionStateConnected {
iceConnectedCtxCancel()
}
@@ -250,7 +144,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
fmt.Printf("Peer Connection State has changed: %s\n", s.String())
log.Debug().Msgf("Peer Connection State has changed: %s\n", s.String())
if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
@@ -269,7 +163,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
Type: &pb.IceMessage_NoMoreCandidates{},
},
})); err != nil {
log.Fatalf("Error sending done w/ candidates: %v", err)
log.Warn().Err(err).Msg("error sending done w/ candidates")
}
return
}
@@ -292,7 +186,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
},
}))
})
log.Printf("Spawning helper")
log.Info().Msg("Spawning helper")
// helper which sends answers, waits for
@@ -302,9 +196,9 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
SessionIdentifier: session.Msg.GetId(),
}))
if err != nil {
log.Printf("failed to pop ice message: %v", err)
log.Info().Err(err).Msg("failed to pop ice message")
continue
}
//log.Printf("Got ice message: %v", prototext.Format(msg.Msg))
switch msg.Msg.Type.(type) {
case *pb.IceMessage_Candidate:
candidate := msg.Msg.GetCandidate()
@@ -318,18 +212,18 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
SDPMid: candidate.SdpMid,
SDPMLineIndex: sdpMLine,
}); err != nil {
log.Fatalf("Failed to add ice candidate: %v", err)
log.Warn().Err(err).Msg("failed to add ice candidate")
}
// Send back an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
log.Printf("Candidate failed")
log.Debug().Msg("Candidate failed")
continue
}
if err := peerConnection.SetLocalDescription(answer); err != nil {
log.Printf("Failed to set local description: %v", err)
log.Info().Err(err).Msg("Failed to set local description")
}
_, err = client.CreateIceMessage(ctx, withAuth(token, &pb.CreateIceMessageRequest{
@@ -344,7 +238,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
},
}))
if err != nil {
log.Printf("Failed to send answer: %v", err)
log.Info().Err(err).Msg("Failed to send answer")
}
case *pb.IceMessage_Session:
iceSession := msg.Msg.GetSession()
@@ -357,12 +251,12 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
}
if err := peerConnection.SetRemoteDescription(offer); err != nil {
log.Fatalf("Failed to set remote description: %v", err)
log.Warn().Err(err).Msg("failed to set remote description")
}
default:
log.Printf("unexpected sdp type: %v", webrtc.SDPType(iceSession.SdpType).String())
log.Info().Msgf("unexpected sdp type: %v", webrtc.SDPType(iceSession.SdpType).String())
}
log.Printf("Accepted promise!")
log.Info().Msg("Accepted promise!")
case *pb.IceMessage_NoMoreCandidates:
// do nothing
}