Skip to content

Commit

Permalink
remove screenshotr and fix screenshot service
Browse files Browse the repository at this point in the history
  • Loading branch information
shamanec authored and danielpaulus committed Jul 11, 2024
1 parent dd33eed commit afe9077
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 250 deletions.
49 changes: 41 additions & 8 deletions ios/screenshotr/mjpeg_server.go → ios/instruments/screenshot.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
package screenshotr
package instruments

import (
"bufio"
"bytes"
"fmt"
"github.com/danielpaulus/go-ios/ios"
dtx "github.com/danielpaulus/go-ios/ios/dtx_codec"
log "github.com/sirupsen/logrus"
"image/jpeg"
"image/png"
"io"
"net/http"
"sync"
"time"

"github.com/danielpaulus/go-ios/ios"
log "github.com/sirupsen/logrus"
)

const screenshotServiceName string = "com.apple.instruments.server.services.screenshot"

type ScreenshotService struct {
channel *dtx.Channel
conn *dtx.Connection
}

func NewScreenshotService(device ios.DeviceEntry) (*ScreenshotService, error) {
dtxConn, err := connectInstruments(device)
if err != nil {
return nil, err
}
processControlChannel := dtxConn.RequestChannelIdentifier(screenshotServiceName, loggingDispatcher{dtxConn})
return &ScreenshotService{channel: processControlChannel, conn: dtxConn}, nil
}

func (d *ScreenshotService) Close() {
d.conn.Close()
}

func (d *ScreenshotService) TakeScreenshot() ([]byte, error) {
msg, err := d.channel.MethodCall("takeScreenshot")
if err != nil {
return nil, fmt.Errorf("TakeScreenshot: %s", err)
}
imageBytes := msg.Payload[0].([]byte)

return imageBytes, nil
}

// MJPEG server code
var (
consumers sync.Map
conversionQueue = make(chan []byte, 20)
)

func StartStreamingServer(device ios.DeviceEntry, port string) error {
conn, err := New(device)
func StartMJPEGStreamingServer(device ios.DeviceEntry, port string) error {
conn, err := NewScreenshotService(device)
if err != nil {
return err
}
defer conn.Close()

go startScreenshotting(conn)
go startConversionQueue()
http.HandleFunc("/", mjpegHandler)
Expand Down Expand Up @@ -62,12 +95,12 @@ func startConversionQueue() {
}
}

func startScreenshotting(conn *Connection) {
func startScreenshotting(conn *ScreenshotService) {
for {
start := time.Now()
pngBytes, err := conn.TakeScreenshot()
if err != nil {
log.Fatal("screenshotr failed", err)
log.Fatal("Screenshot failed", err)
}
elapsed := time.Since(start)
log.Debugf("shot took %fs", elapsed.Seconds())
Expand Down
74 changes: 0 additions & 74 deletions ios/screenshotr/messages.go

This file was deleted.

128 changes: 0 additions & 128 deletions ios/screenshotr/screenshot.go

This file was deleted.

33 changes: 0 additions & 33 deletions ios/screenshotr/screenshot_integration_test.go

This file was deleted.

14 changes: 7 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"github.com/danielpaulus/go-ios/ios/mcinstall"
"github.com/danielpaulus/go-ios/ios/notificationproxy"
"github.com/danielpaulus/go-ios/ios/pcap"
"github.com/danielpaulus/go-ios/ios/screenshotr"
syslog "github.com/danielpaulus/go-ios/ios/syslog"
"github.com/docopt/docopt-go"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -595,7 +594,7 @@ The commands work as following:
if port == "" {
port = "3333"
}
err := screenshotr.StartStreamingServer(device, port)
err := instruments.StartMJPEGStreamingServer(device, port)
exitIfError("failed starting mjpeg", err)
return
}
Expand Down Expand Up @@ -1722,18 +1721,19 @@ func printDeviceName(device ios.DeviceEntry) {
}

func saveScreenshot(device ios.DeviceEntry, outputPath string) {
log.Debug("take screenshot")
screenshotrService, err := screenshotr.New(device)
exitIfError("Starting Screenshotr failed with", err)
screenshotService, err := instruments.NewScreenshotService(device)
exitIfError("Starting screenshot service failed", err)
defer screenshotService.Close()

imageBytes, err := screenshotrService.TakeScreenshot()
exitIfError("screenshotr failed", err)
imageBytes, err := screenshotService.TakeScreenshot()
exitIfError("Taking screenshot failed", err)

if outputPath == "" {
timestamp := time.Now().Format("20060102150405")
outputPath, err = filepath.Abs("./screenshot" + timestamp + ".png")
exitIfError("getting filepath failed", err)
}

err = os.WriteFile(outputPath, imageBytes, 0o777)
exitIfError("write file failed", err)

Expand Down

0 comments on commit afe9077

Please sign in to comment.