Skip to content

Commit d7a6229

Browse files
authored
feat: support scale and quality for android mjpeg
1 parent b7759fe commit d7a6229

File tree

8 files changed

+33
-11
lines changed

8 files changed

+33
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ mobilecli io text --device <device-id> 'hello world'
150150
- `HOME` - Home button
151151
- `BACK` - Back button (Android only)
152152
- `POWER` - Power button
153-
- `VOLUME_UP` - Volume up
154-
- `VOLUME_DOWN` - Volume down
153+
- `VOLUME_UP`, `VOLUME_DOWN` - Volume up and down
154+
- `DPAD_UP`, `DPAD_DOWN`, `DPAD_LEFT`, `DPAD_RIGHT`, `DPAD_CENTER` - D-pad controls (Android only)
155155

156156
## Platform-Specific Notes
157157

cli/screenshot.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77

88
"github.com/mobile-next/mobilecli/commands"
9+
"github.com/mobile-next/mobilecli/devices"
910
"github.com/spf13/cobra"
1011
)
1112

@@ -77,7 +78,7 @@ var screencaptureCmd = &cobra.Command{
7778
}
7879

7980
// Start screen capture and stream to stdout
80-
err = targetDevice.StartScreenCapture(screencaptureFormat, func(data []byte) bool {
81+
err = targetDevice.StartScreenCapture(screencaptureFormat, devices.DefaultMJPEGQuality, devices.DefaultMJPEGScale, func(data []byte) bool {
8182
_, writeErr := os.Stdout.Write(data)
8283
if writeErr != nil {
8384
fmt.Fprintf(os.Stderr, "Error writing data: %v\n", writeErr)

commands/screencapture.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package commands
22

33
type ScreenCaptureRequest struct {
4-
DeviceID string `json:"deviceId"`
5-
Format string `json:"format"`
4+
DeviceID string `json:"deviceId"`
5+
Format string `json:"format"`
6+
Quality int `json:"quality,omitempty"`
7+
Scale float64 `json:"scale,omitempty"`
68
}

devices/android.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (d AndroidDevice) GetAppPath(packageName string) (string, error) {
325325
return appPath, nil
326326
}
327327

328-
func (d AndroidDevice) StartScreenCapture(format string, callback func([]byte) bool) error {
328+
func (d AndroidDevice) StartScreenCapture(format string, quality int, scale float64, callback func([]byte) bool) error {
329329
if format != "mjpeg" {
330330
return fmt.Errorf("unsupported format: %s, only 'mjpeg' is supported", format)
331331
}
@@ -342,7 +342,7 @@ func (d AndroidDevice) StartScreenCapture(format string, callback func([]byte) b
342342
}
343343

344344
utils.Verbose("Starting MJPEG server with app path: %s", appPath)
345-
cmdArgs := append([]string{"-s", d.id}, "shell", fmt.Sprintf("CLASSPATH=%s", appPath), "app_process", "/system/bin", "com.mobilenext.devicekit.MjpegServer")
345+
cmdArgs := append([]string{"-s", d.id}, "shell", fmt.Sprintf("CLASSPATH=%s", appPath), "app_process", "/system/bin", "com.mobilenext.devicekit.MjpegServer", "--quality", fmt.Sprintf("%d", quality), "--scale", fmt.Sprintf("%.2f", scale))
346346
cmd := exec.Command(getAdbPath(), cmdArgs...)
347347

348348
stdout, err := cmd.StdoutPipe()

devices/common.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ import (
77
"github.com/mobile-next/mobilecli/utils"
88
)
99

10+
const (
11+
// Default MJPEG streaming quality (1-100)
12+
DefaultMJPEGQuality = 80
13+
// Default MJPEG streaming scale (0.1-1.0)
14+
DefaultMJPEGScale = 1.0
15+
)
16+
1017
type ControllableDevice interface {
1118
ID() string
1219
Name() string
@@ -25,7 +32,7 @@ type ControllableDevice interface {
2532
OpenURL(url string) error
2633
ListApps() ([]InstalledAppInfo, error)
2734
Info() (*FullDeviceInfo, error)
28-
StartScreenCapture(format string, callback func([]byte) bool) error
35+
StartScreenCapture(format string, quality int, scale float64, callback func([]byte) bool) error
2936
}
3037

3138
// GetAllControllableDevices aggregates all known devices (iOS, Android, Simulators)

devices/ios.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ func (d IOSDevice) Info() (*FullDeviceInfo, error) {
546546
}, nil
547547
}
548548

549-
func (d IOSDevice) StartScreenCapture(format string, callback func([]byte) bool) error {
549+
func (d IOSDevice) StartScreenCapture(format string, quality int, scale float64, callback func([]byte) bool) error {
550550
return d.mjpegClient.StartScreenCapture(format, callback)
551551
}
552552

devices/simulator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func (s Simulator) Info() (*FullDeviceInfo, error) {
403403
}, nil
404404
}
405405

406-
func (s Simulator) StartScreenCapture(format string, callback func([]byte) bool) error {
406+
func (s Simulator) StartScreenCapture(format string, quality int, scale float64, callback func([]byte) bool) error {
407407
mjpegClient := mjpeg.NewWdaMjpegClient("http://localhost:9100")
408408
return mjpegClient.StartScreenCapture(format, callback)
409409
}

server/server.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/mobile-next/mobilecli/commands"
13+
"github.com/mobile-next/mobilecli/devices"
1314
)
1415

1516
const (
@@ -429,6 +430,17 @@ func handleScreenCapture(w http.ResponseWriter, params json.RawMessage) error {
429430
return fmt.Errorf("format must be 'mjpeg' for screen capture")
430431
}
431432

433+
// Set defaults if not provided
434+
quality := screenCaptureParams.Quality
435+
if quality == 0 {
436+
quality = devices.DefaultMJPEGQuality
437+
}
438+
439+
scale := screenCaptureParams.Scale
440+
if scale == 0.0 {
441+
scale = devices.DefaultMJPEGScale
442+
}
443+
432444
err = targetDevice.StartAgent()
433445
if err != nil {
434446
return fmt.Errorf("error starting agent: %v", err)
@@ -441,7 +453,7 @@ func handleScreenCapture(w http.ResponseWriter, params json.RawMessage) error {
441453
w.Header().Set("Transfer-Encoding", "chunked")
442454

443455
// Start screen capture and stream to the response writer
444-
err = targetDevice.StartScreenCapture(screenCaptureParams.Format, func(data []byte) bool {
456+
err = targetDevice.StartScreenCapture(screenCaptureParams.Format, quality, scale, func(data []byte) bool {
445457
_, writeErr := w.Write(data)
446458
if writeErr != nil {
447459
fmt.Println("Error writing data:", writeErr)

0 commit comments

Comments
 (0)