Skip to content
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

Fix screenshot bug #214

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions clashroyalebuildabot/emulator/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import platform
import subprocess
import sys
import threading
import time
import zipfile
Expand Down Expand Up @@ -40,16 +39,8 @@ def __init__(self, device_serial, ip):

def _start_recording(self):
cmd = f"""#!/bin/bash
record() {{
screenrecord --output-format=h264 --time-limit "$1" --size "$2" --bit-rate "$3" -
}}

time_interval=179
size="{self.width}x{self.height}"
bitrate="20M"
#screenrecord --output-format=h264 --time-limit 1 --size "$size" --bit-rate "$bitrate" -
while true; do
record $time_interval "$size" "$bitrate"
screenrecord --output-format=h264 --time-limit "179" --size "{self.width}x{self.height}" --bit-rate "5M" -
done\n"""
cmd = base64.standard_b64encode(cmd.encode("utf-8")).decode("utf-8")
cmd = ["echo", cmd, "|", "base64", "-d", "|", "sh"]
Expand Down Expand Up @@ -109,9 +100,9 @@ def _run_command(self, command):
f"Command executed in {end_time - start_time} seconds"
)
except subprocess.CalledProcessError as e:
logger.error(f"Error executing command: {e}")
logger.error(f"Output: {e.stdout}")
logger.error(f"Error output: {e.stderr}")
logger.error(str(e))
logger.error(f"stdout: {e.stdout}")
logger.error(f"stderr: {e.stderr}")
raise

if result.returncode != 0:
Expand All @@ -129,31 +120,34 @@ def _restart_server(self):
self._run_command(["start-server"])

def _update_frame(self):
lines = []
try:
for line in iter(self.video_thread.stdout.readline, b""):
for line in iter(self.video_thread.stdout.readline, b""):
try:
if not line:
continue

if self.os_name == "windows":
line = line.replace(b"\r\n", b"\n")
if line:
lines.append(line)
if len(lines) == 0:

packets = self.codec.parse(line)
if len(packets) == 0:
continue

try:
packets = self.codec.parse(b"".join(lines))
if len(packets) == 0:
continue
frames = self.codec.decode(packets[-1])
if len(frames) == 0:
continue

frames = self.codec.decode(packets[-1])
if len(frames) == 0:
continue
self.frame = (
frames[-1]
.reformat(
width=SCREENSHOT_WIDTH,
height=SCREENSHOT_HEIGHT,
format="rgb24",
)
.to_image()
)

self.frame = frames[-1]
lines.clear()
except Exception as e:
sys.stderr.write(f"{e}\n")
except Exception as e:
sys.stderr.write(f"{e}\n")
except Exception as e:
logger.error(str(e))

def _start_updating_frame(self):
self.frame_thread = threading.Thread(target=self._update_frame)
Expand Down Expand Up @@ -188,10 +182,9 @@ def click(self, x, y):
def take_screenshot(self):
logger.debug("Starting to take screenshot...")
while self.frame is None:
time.sleep(0.001)
frame, self.frame = self.frame, None
screenshot = frame.reformat(
width=SCREENSHOT_WIDTH, height=SCREENSHOT_HEIGHT, format="rgb24"
).to_image()
time.sleep(0.01)
continue

screenshot, self.frame = self.frame, None

return screenshot