OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 import os |
| 6 import time |
| 7 |
| 8 import android_commands |
| 9 import cmd_helper |
| 10 |
| 11 |
| 12 def _GetTimestamp(): |
| 13 return time.strftime('%Y-%m-%d-%H%M%S', time.localtime()) |
| 14 |
| 15 |
| 16 def _EnsureHostDirectory(host_file): |
| 17 host_dir = os.path.dirname(os.path.abspath(host_file)) |
| 18 if not os.path.exists(host_dir): |
| 19 os.makedirs(host_dir) |
| 20 |
| 21 |
| 22 def TakeScreenshot(adb, host_file): |
| 23 """Saves a screenshot image to |host_file| on the host. |
| 24 |
| 25 Args: |
| 26 adb: AndroidCommands instance. |
| 27 host_file: Path to the image file to store on the host. |
| 28 """ |
| 29 host_file = os.path.abspath(host_file or |
| 30 'screenshot-%s.png' % _GetTimestamp()) |
| 31 _EnsureHostDirectory(host_file) |
| 32 device_file = '%s/screenshot.png' % adb.GetExternalStorage() |
| 33 adb.RunShellCommand('/system/bin/screencap -p %s' % device_file) |
| 34 adb.PullFileFromDevice(device_file, host_file) |
| 35 adb.RunShellCommand('rm -f "%s"' % device_file) |
| 36 return host_file |
| 37 |
| 38 |
| 39 class VideoRecorder(object): |
| 40 """Records a screen capture video from an Android Device (KitKat or newer). |
| 41 |
| 42 Args: |
| 43 adb: AndroidCommands instance. |
| 44 host_file: Path to the video file to store on the host. |
| 45 megabits_per_second: Video bitrate in megabits per second. Allowed range |
| 46 from 0.1 to 100 mbps. |
| 47 size: Video frame size tuple (width, height) or None to use the device |
| 48 default. |
| 49 rotate: If True, the video will be rotated 90 degrees. |
| 50 """ |
| 51 def __init__(self, adb, host_file, megabits_per_second=4, size=None, |
| 52 rotate=False): |
| 53 self._adb = adb |
| 54 self._device_file = '%s/screen-recording.mp4' % adb.GetExternalStorage() |
| 55 self._host_file = host_file or 'screen-recording-%s.mp4' % _GetTimestamp() |
| 56 self._host_file = os.path.abspath(self._host_file) |
| 57 self._recorder = None |
| 58 self._recorder_pids = None |
| 59 |
| 60 self._args = ['adb'] |
| 61 if self._adb.GetDevice(): |
| 62 self._args += ['-s', self._adb.GetDevice()] |
| 63 self._args += ['shell', 'screenrecord'] |
| 64 self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)] |
| 65 if size: |
| 66 self._args += ['--size', '%dx%d' % size] |
| 67 if rotate: |
| 68 self._args += ['--rotate'] |
| 69 self._args += [self._device_file] |
| 70 |
| 71 def Start(self): |
| 72 """Start recording video.""" |
| 73 _EnsureHostDirectory(self._host_file) |
| 74 self._recorder = cmd_helper.Popen(self._args) |
| 75 self._recorder_pids = self._adb.ExtractPid('screenrecord') |
| 76 if not self._recorder_pids: |
| 77 raise RuntimeError('Recording failed. Is your device running Android ' |
| 78 'KitKat or later?') |
| 79 |
| 80 def Stop(self): |
| 81 """Stop recording video.""" |
| 82 if not self._recorder or not self._recorder_pids: |
| 83 return |
| 84 self._adb.RunShellCommand('kill -SIGINT ' + ' '.join(self._recorder_pids)) |
| 85 self._recorder.wait() |
| 86 |
| 87 def Pull(self): |
| 88 """Pull resulting video file from the device. |
| 89 |
| 90 Returns: |
| 91 Output video file name on the host. |
| 92 """ |
| 93 self._adb.PullFileFromDevice(self._device_file, self._host_file) |
| 94 self._adb.RunShellCommand('rm -f "%s"' % self._device_file) |
| 95 return self._host_file |
OLD | NEW |