Skip to content

Commit

Permalink
Screen
Browse files Browse the repository at this point in the history
  • Loading branch information
aBozowski committed Oct 20, 2023
1 parent 6352581 commit c5d668c
Showing 1 changed file with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#

import asyncio
import multiprocessing
import os

from capture.utils import log
from mobly.utils import stop_standing_subprocess
from capture.utils.artifact import create_standard_log_name
from typing import TYPE_CHECKING
from ..base import AndroidStream
Expand All @@ -34,25 +35,21 @@
class ScreenRecorder(AndroidStream):

def __init__(self, platform: "Android"):
# TODO need to continuously start new streams to workaround limits
self.screen_command = None
self.screen_proc = None
self.logger = logger
self.platform = platform
self.screen_artifact = create_standard_log_name("screencast", "mp4", parent=platform.artifact_dir)
self.screen_check_command = "shell dumpsys deviceidle | grep mScreenOn"
self.screen_phone_out_path = f"/sdcard/Movies/{os.path.basename(self.screen_artifact)}"
self.screen_command = f"shell screenrecord --bugreport {self.screen_phone_out_path}"
self.screen_proc = platform.get_adb_background_command(self.screen_command)
self.screen_pull = False
self.screen_pull_command = f"pull {self.screen_phone_out_path} {self.screen_artifact}"
self.file_counter = 0
self.pull_commands: [str] = []

def check_screen(self) -> bool:
screen_cmd_output = self.platform.run_adb_command(
self.screen_check_command, capture_output=True)
return "true" in screen_cmd_output.get_captured_output()

async def prepare_screen_recording(self) -> None:
if self.screen_proc.command_is_running():
return
try:
async with asyncio.timeout_at(asyncio.get_running_loop().time() + 20.0):
screen_on = self.check_screen()
Expand All @@ -66,20 +63,39 @@ async def prepare_screen_recording(self) -> None:
self.logger.error("Screen recording timeout")
return

def update_commands(self) -> None:
screen_artifact = create_standard_log_name("screencast"+self.file_counter,
"mp4",
parent=self.platform.artifact_dir)
screen_phone_out_path = f"/sdcard/Movies/{os.path.basename(self.screen_artifact)}"
self.screen_command = f"shell screenrecord --bugreport {self.screen_phone_out_path}"
screen_pull_command = f"pull {screen_phone_out_path} {screen_artifact}"
self.pull_commands.append(screen_pull_command)
self.file_counter += 1

def run_recorder(self) -> None:
while True:
self.update_commands()
self.logger.info(f"New screen recording file started {self.screen_phone_out_path} {self.screen_artifact}")
self.platform.run_adb_command(self.screen_command)

async def start(self):
await self.prepare_screen_recording()
if self.check_screen():
self.screen_pull = True
self.screen_proc.start_command()
self.screen_proc = multiprocessing.Process(target=self.run_recorder)

async def pull_screen_recording(self) -> None:
if self.screen_pull:
self.logger.info("Attempting to pull screen recording")
await asyncio.sleep(3)
self.platform.run_adb_command(self.screen_pull_command)
self.screen_pull = False
for command in self.pull_commands:
self.logger.info("Attempting to pull screen recording")
await asyncio.sleep(3)
self.platform.run_adb_command(command)
self.screen_pull = False

async def stop(self):
self.logger.info("Stopping screen proc")
self.screen_proc.stop_command()
if self.screen_proc is not None:
self.screen_proc.kill()
await self.pull_screen_recording()
# TODO stitch together

0 comments on commit c5d668c

Please sign in to comment.