|
| 1 | +package capture |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "os/exec" |
| 9 | + "strconv" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/rs/zerolog/log" |
| 13 | + |
| 14 | + "github.com/m1k1o/neko/server/pkg/types" |
| 15 | +) |
| 16 | + |
| 17 | +type frameSource interface { |
| 18 | + Start(func([]byte)) error |
| 19 | + Stop() |
| 20 | +} |
| 21 | + |
| 22 | +type waylandFrameSource struct { |
| 23 | + recorder string |
| 24 | + width int |
| 25 | + height int |
| 26 | + fps int |
| 27 | + |
| 28 | + mu sync.Mutex |
| 29 | + cancel context.CancelFunc |
| 30 | + done chan struct{} |
| 31 | +} |
| 32 | + |
| 33 | +func newWaylandFrameSource(recorder string, screen types.ScreenSize) *waylandFrameSource { |
| 34 | + fps := int(screen.Rate) |
| 35 | + if fps <= 0 { |
| 36 | + fps = 25 |
| 37 | + } |
| 38 | + |
| 39 | + return &waylandFrameSource{ |
| 40 | + recorder: recorder, |
| 41 | + width: screen.Width, |
| 42 | + height: screen.Height, |
| 43 | + fps: fps, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (source *waylandFrameSource) frameSize() int { |
| 48 | + return source.width * source.height * 4 |
| 49 | +} |
| 50 | + |
| 51 | +func (source *waylandFrameSource) args() []string { |
| 52 | + return []string{ |
| 53 | + "--no-damage", |
| 54 | + "--no-dmabuf", |
| 55 | + "--framerate", strconv.Itoa(source.fps), |
| 56 | + "--muxer", "rawvideo", |
| 57 | + "--codec", "rawvideo", |
| 58 | + "--pixel-format", "bgr0", |
| 59 | + "--file", "/dev/stdout", |
| 60 | + "--overwrite", |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (source *waylandFrameSource) command() *exec.Cmd { |
| 65 | + return exec.Command(source.recorder, source.args()...) |
| 66 | +} |
| 67 | + |
| 68 | +func (source *waylandFrameSource) Start(push func([]byte)) error { |
| 69 | + if push == nil { |
| 70 | + return fmt.Errorf("frame push callback is required") |
| 71 | + } |
| 72 | + if source.recorder == "" { |
| 73 | + return fmt.Errorf("Wayland recorder executable is required") |
| 74 | + } |
| 75 | + if source.width <= 0 || source.height <= 0 { |
| 76 | + return fmt.Errorf("invalid Wayland output size: %dx%d", source.width, source.height) |
| 77 | + } |
| 78 | + |
| 79 | + ctx, cancel := context.WithCancel(context.Background()) |
| 80 | + cmd := exec.CommandContext(ctx, source.recorder, source.args()...) |
| 81 | + cmd.Stderr = os.Stderr |
| 82 | + |
| 83 | + stdout, err := cmd.StdoutPipe() |
| 84 | + if err != nil { |
| 85 | + cancel() |
| 86 | + return fmt.Errorf("create Wayland recorder pipe: %w", err) |
| 87 | + } |
| 88 | + if err := cmd.Start(); err != nil { |
| 89 | + cancel() |
| 90 | + return fmt.Errorf("start Wayland recorder: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + done := make(chan struct{}) |
| 94 | + source.mu.Lock() |
| 95 | + source.cancel = cancel |
| 96 | + source.done = done |
| 97 | + source.mu.Unlock() |
| 98 | + |
| 99 | + go func() { |
| 100 | + defer close(done) |
| 101 | + defer stdout.Close() |
| 102 | + |
| 103 | + frame := make([]byte, source.frameSize()) |
| 104 | + for { |
| 105 | + if _, err := io.ReadFull(stdout, frame); err != nil { |
| 106 | + if err != io.EOF && err != io.ErrUnexpectedEOF { |
| 107 | + log.Warn().Err(err).Msg("Wayland recorder stopped while reading a frame") |
| 108 | + } |
| 109 | + break |
| 110 | + } |
| 111 | + |
| 112 | + push(frame) |
| 113 | + } |
| 114 | + |
| 115 | + if err := cmd.Wait(); err != nil && ctx.Err() == nil { |
| 116 | + log.Warn().Err(err).Msg("Wayland recorder exited") |
| 117 | + } |
| 118 | + }() |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func (source *waylandFrameSource) Stop() { |
| 124 | + source.mu.Lock() |
| 125 | + cancel := source.cancel |
| 126 | + done := source.done |
| 127 | + source.cancel = nil |
| 128 | + source.done = nil |
| 129 | + source.mu.Unlock() |
| 130 | + |
| 131 | + if cancel == nil { |
| 132 | + return |
| 133 | + } |
| 134 | + cancel() |
| 135 | + <-done |
| 136 | +} |
0 commit comments