fix: switch to h264 to benefit from hardware

This commit is contained in:
Charles
2024-01-10 21:39:06 -08:00
parent d16d1e0ac5
commit ccafa34f33
9 changed files with 274 additions and 105 deletions
+25 -19
View File
@@ -13,8 +13,8 @@ import (
"connectrpc.com/connect"
pb "github.com/chathaway-codes/home-sensors/v2/gen"
servicepb "github.com/chathaway-codes/home-sensors/v2/gen/genconnect"
"github.com/chathaway-codes/home-sensors/v2/internal/h264video"
"github.com/chathaway-codes/home-sensors/v2/internal/sensors"
"github.com/chathaway-codes/home-sensors/v2/internal/video"
"github.com/chathaway-codes/home-sensors/v2/internal/watcher/config"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
@@ -59,7 +59,7 @@ func main() {
}
token := authToken.Msg.GetToken()
vid, err := video.Default.Get()
vid, err := h264video.Default.Get()
if err != nil {
log.Fatal().Err(err).Msg("failed to get default video")
}
@@ -111,7 +111,7 @@ func handleSensor(ctx context.Context, client servicepb.SignalerServiceClient, t
}
}
func handleSession(ctx context.Context, client servicepb.SignalerServiceClient, token string, session *connect.Response[pb.Session], vid *video.Video) {
func handleSession(ctx context.Context, client servicepb.SignalerServiceClient, token string, session *connect.Response[pb.Session], vid *h264video.Video) {
var err error
log.Debug().Msg("new session")
@@ -122,6 +122,25 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
},
},
})
if err != nil {
log.Info().Err(err).Msg("failed to get a connection")
return
}
// connect to the video stream; the cleanup is done in the goroutine which
// consumes the framess
ch, trackCodec, cleanUp := vid.Join()
// Create a video track
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: trackCodec}, "video", "pion")
if videoTrackErr != nil {
log.Info().Err(videoTrackErr).Msg("Failed to create video track")
return
}
rtpSender, err := peerConnection.AddTrack(videoTrack)
if err != nil {
log.Info().Err(err).Msg("Failed to add track to connection")
}
// We use the cancel func to signal that the stream is ready
iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(context.Background())
@@ -131,20 +150,6 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
}
}()
// connect to the video stream; the cleanup is done in the goroutine which
// consumes the framess
ch, trackCodec, cleanUp := vid.Join()
// Create a video track
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: trackCodec}, "video", "pion")
if videoTrackErr != nil {
log.Info().Err(err).Msg("Failed to create video track")
}
rtpSender, err := peerConnection.AddTrack(videoTrack)
if err != nil {
log.Info().Err(err).Msg("Failed to add track to connection")
}
// Read incoming RTCP packets
// Before these packets are returned they are processed by interceptors. For things
// like NACK this needs to be called.
@@ -170,8 +175,9 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
if !readyToSend {
continue
}
if err := videoTrack.WriteSample(media.Sample{Data: frame, Duration: time.Second}); err != nil {
panic(err)
if err := videoTrack.WriteSample(media.Sample{Data: frame, Duration: time.Millisecond * 33}); err != nil {
log.Err(err).Msg("failed to write sample")
return
}
}
}()