forked from m1k1o/neko
-
Notifications
You must be signed in to change notification settings - Fork 1
Add Wayland screencopy video capture #14
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
Closed
Closed
Changes from all commits
Commits
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
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
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,136 @@ | ||
| package capture | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "os/exec" | ||
| "strconv" | ||
| "sync" | ||
|
|
||
| "github.com/rs/zerolog/log" | ||
|
|
||
| "github.com/m1k1o/neko/server/pkg/types" | ||
| ) | ||
|
|
||
| type frameSource interface { | ||
| Start(func([]byte)) error | ||
| Stop() | ||
| } | ||
|
|
||
| type waylandFrameSource struct { | ||
| recorder string | ||
| width int | ||
| height int | ||
| fps int | ||
|
|
||
| mu sync.Mutex | ||
| cancel context.CancelFunc | ||
| done chan struct{} | ||
| } | ||
|
|
||
| func newWaylandFrameSource(recorder string, screen types.ScreenSize) *waylandFrameSource { | ||
| fps := int(screen.Rate) | ||
| if fps <= 0 { | ||
| fps = 25 | ||
| } | ||
|
|
||
| return &waylandFrameSource{ | ||
| recorder: recorder, | ||
| width: screen.Width, | ||
| height: screen.Height, | ||
| fps: fps, | ||
| } | ||
| } | ||
|
|
||
| func (source *waylandFrameSource) frameSize() int { | ||
| return source.width * source.height * 4 | ||
| } | ||
|
|
||
| func (source *waylandFrameSource) args() []string { | ||
| return []string{ | ||
| "--no-damage", | ||
| "--no-dmabuf", | ||
| "--framerate", strconv.Itoa(source.fps), | ||
| "--muxer", "rawvideo", | ||
| "--codec", "rawvideo", | ||
| "--pixel-format", "bgr0", | ||
| "--file", "/dev/stdout", | ||
| "--overwrite", | ||
| } | ||
| } | ||
|
|
||
| func (source *waylandFrameSource) command() *exec.Cmd { | ||
| return exec.Command(source.recorder, source.args()...) | ||
| } | ||
|
|
||
| func (source *waylandFrameSource) Start(push func([]byte)) error { | ||
| if push == nil { | ||
| return fmt.Errorf("frame push callback is required") | ||
| } | ||
| if source.recorder == "" { | ||
| return fmt.Errorf("Wayland recorder executable is required") | ||
| } | ||
| if source.width <= 0 || source.height <= 0 { | ||
| return fmt.Errorf("invalid Wayland output size: %dx%d", source.width, source.height) | ||
| } | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cmd := exec.CommandContext(ctx, source.recorder, source.args()...) | ||
| cmd.Stderr = os.Stderr | ||
|
|
||
| stdout, err := cmd.StdoutPipe() | ||
| if err != nil { | ||
| cancel() | ||
| return fmt.Errorf("create Wayland recorder pipe: %w", err) | ||
| } | ||
| if err := cmd.Start(); err != nil { | ||
| cancel() | ||
| return fmt.Errorf("start Wayland recorder: %w", err) | ||
| } | ||
|
|
||
| done := make(chan struct{}) | ||
| source.mu.Lock() | ||
| source.cancel = cancel | ||
| source.done = done | ||
| source.mu.Unlock() | ||
|
|
||
| go func() { | ||
| defer close(done) | ||
| defer stdout.Close() | ||
|
|
||
| frame := make([]byte, source.frameSize()) | ||
| for { | ||
| if _, err := io.ReadFull(stdout, frame); err != nil { | ||
| if err != io.EOF && err != io.ErrUnexpectedEOF { | ||
| log.Warn().Err(err).Msg("Wayland recorder stopped while reading a frame") | ||
| } | ||
| break | ||
| } | ||
|
|
||
| push(frame) | ||
| } | ||
|
|
||
| if err := cmd.Wait(); err != nil && ctx.Err() == nil { | ||
| log.Warn().Err(err).Msg("Wayland recorder exited") | ||
| } | ||
| }() | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (source *waylandFrameSource) Stop() { | ||
| source.mu.Lock() | ||
| cancel := source.cancel | ||
| done := source.done | ||
| source.cancel = nil | ||
| source.done = nil | ||
| source.mu.Unlock() | ||
|
|
||
| if cancel == nil { | ||
| return | ||
| } | ||
| cancel() | ||
| <-done | ||
| } |
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,46 @@ | ||
| package capture | ||
|
|
||
| import ( | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/m1k1o/neko/server/pkg/types" | ||
| ) | ||
|
|
||
| func TestWaylandFrameSourceCommand(t *testing.T) { | ||
| source := newWaylandFrameSource("wf-recorder", types.ScreenSize{ | ||
| Width: 1920, | ||
| Height: 1080, | ||
| Rate: 25, | ||
| }) | ||
|
|
||
| got := source.command().Args | ||
| want := []string{ | ||
| "wf-recorder", | ||
| "--no-damage", | ||
| "--no-dmabuf", | ||
| "--framerate", "25", | ||
| "--muxer", "rawvideo", | ||
| "--codec", "rawvideo", | ||
| "--pixel-format", "bgr0", | ||
| "--file", "/dev/stdout", | ||
| "--overwrite", | ||
| } | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Fatalf("command args = %#v, want %#v", got, want) | ||
| } | ||
| } | ||
|
|
||
| func TestWaylandFrameSourceDefaultsFrameRate(t *testing.T) { | ||
| source := newWaylandFrameSource("wf-recorder", types.ScreenSize{ | ||
| Width: 10, | ||
| Height: 20, | ||
| }) | ||
|
|
||
| if source.fps != 25 { | ||
| t.Fatalf("fps = %d, want 25", source.fps) | ||
| } | ||
| if source.frameSize() != 800 { | ||
| t.Fatalf("frame size = %d, want 800", source.frameSize()) | ||
| } | ||
| } |
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
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.
Appsrc framerate conflicts with pipeline
High Severity
Wayland
appsrccaps andwf-recorderboth usescreen.Rate, while the encoding chain fromGetPipelineoften forces a different framerate viaVideoConfig.Fps(default"25"). Fixedappsrccaps cannot renegotiate against that capsfilter, so the default desktop rate (30) yields a not-negotiated pipeline and live video never starts.Reviewed by Cursor Bugbot for commit 0cd3a36. Configure here.