Index: build/android/pylib/screenshot.py |
diff --git a/build/android/pylib/screenshot.py b/build/android/pylib/screenshot.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..580470c5aca6233c825f49ccb8ff2dd2e30d2bd1 |
--- /dev/null |
+++ b/build/android/pylib/screenshot.py |
@@ -0,0 +1,95 @@ |
+# Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import os |
+import time |
+ |
+import android_commands |
+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 |
+ |
+ |
+class VideoRecorder(object): |
+ """Records a screen capture video from an Android Device (KitKat or newer). |
+ |
+ Args: |
+ adb: AndroidCommands 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. |
+ size: Video frame size tuple (width, height) or None to use the device |
+ default. |
+ rotate: If True, the video will be rotated 90 degrees. |
+ """ |
+ def __init__(self, adb, 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() |
+ self._host_file = os.path.abspath(self._host_file) |
+ self._recorder = None |
+ self._recorder_pids = None |
+ |
+ self._args = ['adb'] |
+ if self._adb.GetDevice(): |
+ self._args += ['-s', self._adb.GetDevice()] |
+ self._args += ['shell', 'screenrecord'] |
+ self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)] |
+ if size: |
+ self._args += ['--size', '%dx%d' % size] |
+ if rotate: |
+ self._args += ['--rotate'] |
+ self._args += [self._device_file] |
+ |
+ def Start(self): |
+ """Start recording video.""" |
+ _EnsureHostDirectory(self._host_file) |
+ self._recorder = cmd_helper.Popen(self._args) |
+ self._recorder_pids = self._adb.ExtractPid('screenrecord') |
+ if not self._recorder_pids: |
+ raise RuntimeError('Recording failed. Is your device running Android ' |
+ 'KitKat or later?') |
+ |
+ def Stop(self): |
+ """Stop recording video.""" |
+ if not self._recorder or not self._recorder_pids: |
+ return |
+ self._adb.RunShellCommand('kill -SIGINT ' + ' '.join(self._recorder_pids)) |
+ self._recorder.wait() |
+ |
+ def Pull(self): |
+ """Pull resulting video file from the device. |
+ |
+ 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) |
+ return self._host_file |