-
Notifications
You must be signed in to change notification settings - Fork 52
feat: gracefully stop and restart recordings during display resize #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1a8e693
feat: gracefully stop and restart recordings during display resize
hiroTamada 3d62c2a
fix: address PR review feedback for recording resize
hiroTamada 0820c4a
fix: skip restart when deregister fails to avoid ID conflict
hiroTamada c4fd4c7
refactor: use segment-based recording restart on display resize
hiroTamada d2fc5a4
fix: make recording duration and size limits cumulative across segments
hiroTamada f4d3313
fix: only restart recording segments after successful resize
hiroTamada 3e47691
fix: always restart stopped recordings to prevent data loss
hiroTamada File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/onkernel/kernel-images/server/lib/recorder" | ||
| "github.com/onkernel/kernel-images/server/lib/scaletozero" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| var testMockFFmpegBin = filepath.Join("..", "..", "..", "lib", "recorder", "testdata", "mock_ffmpeg.sh") | ||
|
|
||
| func testFFmpegFactory(t *testing.T, tempDir string) recorder.FFmpegRecorderFactory { | ||
| t.Helper() | ||
| fr := 5 | ||
| disp := 0 | ||
| size := 1 | ||
| config := recorder.FFmpegRecordingParams{ | ||
| FrameRate: &fr, | ||
| DisplayNum: &disp, | ||
| MaxSizeInMB: &size, | ||
| OutputDir: &tempDir, | ||
| } | ||
| return recorder.NewFFmpegRecorderFactory(testMockFFmpegBin, config, scaletozero.NewNoopController()) | ||
| } | ||
|
|
||
| func newTestServiceWithFactory(t *testing.T, mgr recorder.RecordManager, factory recorder.FFmpegRecorderFactory) *ApiService { | ||
| t.Helper() | ||
| svc, err := New(mgr, factory, newTestUpstreamManager(), scaletozero.NewNoopController(), newMockNekoClient(t)) | ||
| require.NoError(t, err) | ||
| return svc | ||
| } | ||
|
|
||
| func TestStopActiveRecordings(t *testing.T) { | ||
| t.Run("stops and deregisters active recording", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| rec, err := factory("test-rec", recorder.FFmpegRecordingParams{}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, mgr.RegisterRecorder(ctx, rec)) | ||
| require.NoError(t, rec.Start(ctx)) | ||
| time.Sleep(50 * time.Millisecond) | ||
| require.True(t, rec.IsRecording(ctx)) | ||
|
|
||
| stopped, err := svc.stopActiveRecordings(ctx) | ||
| require.NoError(t, err) | ||
| require.Len(t, stopped, 1) | ||
| assert.Equal(t, "test-rec", stopped[0].id) | ||
| assert.NotNil(t, stopped[0].params.FrameRate) | ||
| assert.Equal(t, filepath.Join(tempDir, "test-rec.mp4"), stopped[0].outputPath) | ||
|
|
||
| _, exists := mgr.GetRecorder("test-rec") | ||
| assert.False(t, exists, "recorder should be deregistered after stop") | ||
| }) | ||
|
|
||
| t.Run("stops multiple active recordings", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| ids := []string{"rec-a", "rec-b"} | ||
| for _, id := range ids { | ||
| rec, err := factory(id, recorder.FFmpegRecordingParams{}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, mgr.RegisterRecorder(ctx, rec)) | ||
| require.NoError(t, rec.Start(ctx)) | ||
| } | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| stopped, err := svc.stopActiveRecordings(ctx) | ||
| require.NoError(t, err) | ||
| assert.Len(t, stopped, 2) | ||
|
|
||
| stoppedIDs := map[string]bool{} | ||
| for _, s := range stopped { | ||
| stoppedIDs[s.id] = true | ||
| } | ||
| for _, id := range ids { | ||
| assert.True(t, stoppedIDs[id], "recording %s should have been stopped", id) | ||
| _, exists := mgr.GetRecorder(id) | ||
| assert.False(t, exists, "recorder %s should be deregistered", id) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("skips non-recording recorders", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| mock := &mockRecorder{id: "idle-rec", isRecordingFlag: false} | ||
| require.NoError(t, mgr.RegisterRecorder(ctx, mock)) | ||
|
|
||
| stopped, err := svc.stopActiveRecordings(ctx) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, stopped) | ||
|
|
||
| _, exists := mgr.GetRecorder("idle-rec") | ||
| assert.True(t, exists, "non-recording recorder should remain registered") | ||
| }) | ||
|
|
||
| t.Run("returns empty when no recorders exist", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| stopped, err := svc.stopActiveRecordings(ctx) | ||
| require.NoError(t, err) | ||
| assert.Empty(t, stopped) | ||
| }) | ||
| } | ||
|
|
||
| func TestRestartRecordings(t *testing.T) { | ||
| t.Run("renames old file and starts new recording", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| outputPath := filepath.Join(tempDir, "test-rec.mp4") | ||
| require.NoError(t, os.WriteFile(outputPath, []byte("fake video data"), 0644)) | ||
|
|
||
| fr := 5 | ||
| disp := 0 | ||
| size := 1 | ||
| info := stoppedRecordingInfo{ | ||
| id: "test-rec", | ||
| params: recorder.FFmpegRecordingParams{ | ||
| FrameRate: &fr, | ||
| DisplayNum: &disp, | ||
| MaxSizeInMB: &size, | ||
| OutputDir: &tempDir, | ||
| }, | ||
| outputPath: outputPath, | ||
| } | ||
|
|
||
| svc.restartRecordings(ctx, []stoppedRecordingInfo{info}) | ||
|
|
||
| entries, err := os.ReadDir(tempDir) | ||
| require.NoError(t, err) | ||
|
|
||
| foundRenamed := false | ||
| for _, e := range entries { | ||
| if strings.Contains(e.Name(), "before-resize") { | ||
| foundRenamed = true | ||
| data, readErr := os.ReadFile(filepath.Join(tempDir, e.Name())) | ||
| require.NoError(t, readErr) | ||
| assert.Equal(t, []byte("fake video data"), data) | ||
| } | ||
| } | ||
| assert.True(t, foundRenamed, "pre-resize recording should be preserved with renamed file") | ||
|
|
||
| rec, exists := mgr.GetRecorder("test-rec") | ||
| require.True(t, exists, "restarted recorder should be registered") | ||
| assert.True(t, rec.IsRecording(ctx), "restarted recorder should be recording") | ||
|
|
||
| _ = rec.Stop(ctx) | ||
| }) | ||
|
|
||
| t.Run("starts recording even when no old file exists", func(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| fr := 5 | ||
| disp := 0 | ||
| size := 1 | ||
| info := stoppedRecordingInfo{ | ||
| id: "fresh-rec", | ||
| params: recorder.FFmpegRecordingParams{ | ||
| FrameRate: &fr, | ||
| DisplayNum: &disp, | ||
| MaxSizeInMB: &size, | ||
| OutputDir: &tempDir, | ||
| }, | ||
| outputPath: filepath.Join(tempDir, "fresh-rec.mp4"), | ||
| } | ||
|
|
||
| svc.restartRecordings(ctx, []stoppedRecordingInfo{info}) | ||
|
|
||
| rec, exists := mgr.GetRecorder("fresh-rec") | ||
| require.True(t, exists, "recorder should be registered") | ||
| assert.True(t, rec.IsRecording(ctx)) | ||
|
|
||
| _ = rec.Stop(ctx) | ||
| }) | ||
| } | ||
|
|
||
| func TestStopAndRestartRecordings_RoundTrip(t *testing.T) { | ||
| ctx := context.Background() | ||
| tempDir := t.TempDir() | ||
| factory := testFFmpegFactory(t, tempDir) | ||
| mgr := recorder.NewFFmpegManager() | ||
| svc := newTestServiceWithFactory(t, mgr, factory) | ||
|
|
||
| // Start a recording | ||
| rec, err := factory("round-trip", recorder.FFmpegRecordingParams{}) | ||
| require.NoError(t, err) | ||
| require.NoError(t, mgr.RegisterRecorder(ctx, rec)) | ||
| require.NoError(t, rec.Start(ctx)) | ||
| time.Sleep(50 * time.Millisecond) | ||
| require.True(t, rec.IsRecording(ctx)) | ||
|
|
||
| // Stop all active recordings | ||
| stopped, err := svc.stopActiveRecordings(ctx) | ||
| require.NoError(t, err) | ||
| require.Len(t, stopped, 1) | ||
| assert.Equal(t, "round-trip", stopped[0].id) | ||
|
|
||
| // Verify the recorder was deregistered | ||
| _, exists := mgr.GetRecorder("round-trip") | ||
| require.False(t, exists) | ||
|
|
||
| // Restart recordings | ||
| svc.restartRecordings(ctx, stopped) | ||
|
|
||
| // Verify the recording resumed with the same ID | ||
| newRec, exists := mgr.GetRecorder("round-trip") | ||
| require.True(t, exists, "recorder should be re-registered after restart") | ||
| assert.True(t, newRec.IsRecording(ctx), "recorder should be actively recording") | ||
|
|
||
| _ = newRec.Stop(ctx) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is another place where we're breaking the record manager interface. I wonder if it's simpler in thinking of these operations as
stop and clonebut nbdalso
segmentsis a new concept. you could encode it into the record manager interface and treat the replays as disjoint and perhaps join them back together at the end. unclear