import os
import tempfile
-import time
from pylib import cmd_helper
-
-def _GetTimestamp():
- return time.strftime('%Y-%m-%d-%H%M%S', time.localtime())
-
-
-def _EnsureHostDirectory(host_file):
- host_dir = os.path.dirname(os.path.abspath(host_file))
- if not os.path.exists(host_dir):
- os.makedirs(host_dir)
-
-
-def TakeScreenshot(adb, host_file):
- """Saves a screenshot image to |host_file| on the host.
-
- Args:
- adb: AndroidCommands instance.
- host_file: Path to the image file to store on the host.
- """
- host_file = os.path.abspath(host_file or
- 'screenshot-%s.png' % _GetTimestamp())
- _EnsureHostDirectory(host_file)
- device_file = '%s/screenshot.png' % adb.GetExternalStorage()
- adb.RunShellCommand('/system/bin/screencap -p %s' % device_file)
- adb.PullFileFromDevice(device_file, host_file)
- adb.RunShellCommand('rm -f "%s"' % device_file)
- return host_file
+# TODO(jbudorick) Remove once telemetry gets switched over.
+import pylib.android_commands
+import pylib.device.device_utils
class VideoRecorder(object):
"""Records a screen capture video from an Android Device (KitKat or newer).
Args:
- adb: AndroidCommands instance.
+ device: DeviceUtils instance.
host_file: Path to the video file to store on the host.
megabits_per_second: Video bitrate in megabits per second. Allowed range
from 0.1 to 100 mbps.
default.
rotate: If True, the video will be rotated 90 degrees.
"""
- def __init__(self, adb, host_file, megabits_per_second=4, size=None,
+ def __init__(self, device, host_file, megabits_per_second=4, size=None,
rotate=False):
- self._adb = adb
- self._device_file = '%s/screen-recording.mp4' % adb.GetExternalStorage()
- self._host_file = host_file or 'screen-recording-%s.mp4' % _GetTimestamp()
+ # TODO(jbudorick) Remove once telemetry gets switched over.
+ if isinstance(device, pylib.android_commands.AndroidCommands):
+ device = pylib.device.device_utils.DeviceUtils(device)
+ self._device = device
+ self._device_file = (
+ '%s/screen-recording.mp4' % device.old_interface.GetExternalStorage())
+ self._host_file = host_file or ('screen-recording-%s.mp4' %
+ device.old_interface.GetTimestamp())
self._host_file = os.path.abspath(self._host_file)
self._recorder = None
self._recorder_pids = None
self._is_started = False
self._args = ['adb']
- if self._adb.GetDevice():
- self._args += ['-s', self._adb.GetDevice()]
+ if self._device.old_interface.GetDevice():
+ self._args += ['-s', self._device.old_interface.GetDevice()]
self._args += ['shell', 'screenrecord', '--verbose']
self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)]
if size:
def Start(self):
"""Start recording video."""
- _EnsureHostDirectory(self._host_file)
+ self._device.old_interface.EnsureHostDirectory(self._host_file)
self._recorder_stdout = tempfile.mkstemp()[1]
self._recorder = cmd_helper.Popen(
self._args, stdout=open(self._recorder_stdout, 'w'))
- self._recorder_pids = self._adb.ExtractPid('screenrecord')
+ self._recorder_pids = self._device.old_interface.ExtractPid('screenrecord')
if not self._recorder_pids:
raise RuntimeError('Recording failed. Is your device running Android '
'KitKat or later?')
self._is_started = False
if not self._recorder or not self._recorder_pids:
return
- self._adb.RunShellCommand('kill -SIGINT ' + ' '.join(self._recorder_pids))
+ self._device.old_interface.RunShellCommand(
+ 'kill -SIGINT ' + ' '.join(self._recorder_pids))
self._recorder.wait()
def Pull(self):
Returns:
Output video file name on the host.
"""
- self._adb.PullFileFromDevice(self._device_file, self._host_file)
- self._adb.RunShellCommand('rm -f "%s"' % self._device_file)
+ self._device.old_interface.PullFileFromDevice(
+ self._device_file, self._host_file)
+ self._device.old_interface.RunShellCommand('rm -f "%s"' % self._device_file)
return self._host_file