| OLD | NEW |
| (Empty) |
| 1 # Copyright 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 logging | |
| 6 import os | |
| 7 import tempfile | |
| 8 import time | |
| 9 | |
| 10 from pylib import cmd_helper | |
| 11 from pylib import device_signal | |
| 12 from pylib.device import device_errors | |
| 13 | |
| 14 # TODO(jbudorick) Remove once telemetry gets switched over. | |
| 15 import pylib.android_commands | |
| 16 import pylib.device.device_utils | |
| 17 | |
| 18 | |
| 19 class VideoRecorder(object): | |
| 20 """Records a screen capture video from an Android Device (KitKat or newer). | |
| 21 | |
| 22 Args: | |
| 23 device: DeviceUtils instance. | |
| 24 host_file: Path to the video file to store on the host. | |
| 25 megabits_per_second: Video bitrate in megabits per second. Allowed range | |
| 26 from 0.1 to 100 mbps. | |
| 27 size: Video frame size tuple (width, height) or None to use the device | |
| 28 default. | |
| 29 rotate: If True, the video will be rotated 90 degrees. | |
| 30 """ | |
| 31 def __init__(self, device, megabits_per_second=4, size=None, | |
| 32 rotate=False): | |
| 33 # TODO(jbudorick) Remove once telemetry gets switched over. | |
| 34 if isinstance(device, pylib.android_commands.AndroidCommands): | |
| 35 device = pylib.device.device_utils.DeviceUtils(device) | |
| 36 self._device = device | |
| 37 self._device_file = ( | |
| 38 '%s/screen-recording.mp4' % device.GetExternalStoragePath()) | |
| 39 self._recorder = None | |
| 40 self._recorder_stdout = None | |
| 41 self._is_started = False | |
| 42 | |
| 43 self._args = ['adb'] | |
| 44 if str(self._device): | |
| 45 self._args += ['-s', str(self._device)] | |
| 46 self._args += ['shell', 'screenrecord', '--verbose'] | |
| 47 self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)] | |
| 48 if size: | |
| 49 self._args += ['--size', '%dx%d' % size] | |
| 50 if rotate: | |
| 51 self._args += ['--rotate'] | |
| 52 self._args += [self._device_file] | |
| 53 | |
| 54 def Start(self): | |
| 55 """Start recording video.""" | |
| 56 self._recorder_stdout = tempfile.mkstemp()[1] | |
| 57 self._recorder = cmd_helper.Popen( | |
| 58 self._args, stdout=open(self._recorder_stdout, 'w')) | |
| 59 if not self._device.GetPids('screenrecord'): | |
| 60 raise RuntimeError('Recording failed. Is your device running Android ' | |
| 61 'KitKat or later?') | |
| 62 | |
| 63 def IsStarted(self): | |
| 64 if not self._is_started: | |
| 65 for line in open(self._recorder_stdout): | |
| 66 self._is_started = line.startswith('Content area is ') | |
| 67 if self._is_started: | |
| 68 break | |
| 69 return self._is_started | |
| 70 | |
| 71 def Stop(self): | |
| 72 """Stop recording video.""" | |
| 73 os.remove(self._recorder_stdout) | |
| 74 self._is_started = False | |
| 75 if not self._recorder: | |
| 76 return | |
| 77 if not self._device.KillAll('screenrecord', signum=device_signal.SIGINT, | |
| 78 quiet=True): | |
| 79 logging.warning('Nothing to kill: screenrecord was not running') | |
| 80 self._recorder.wait() | |
| 81 | |
| 82 def Pull(self, host_file=None): | |
| 83 """Pull resulting video file from the device. | |
| 84 | |
| 85 Args: | |
| 86 host_file: Path to the video file to store on the host. | |
| 87 Returns: | |
| 88 Output video file name on the host. | |
| 89 """ | |
| 90 # TODO(jbudorick): Merge filename generation with the logic for doing so in | |
| 91 # DeviceUtils. | |
| 92 host_file_name = ( | |
| 93 host_file | |
| 94 or 'screen-recording-%s.mp4' % time.strftime('%Y%m%dT%H%M%S', | |
| 95 time.localtime())) | |
| 96 host_file_name = os.path.abspath(host_file_name) | |
| 97 self._device.PullFile(self._device_file, host_file_name) | |
| 98 self._device.RunShellCommand('rm -f "%s"' % self._device_file) | |
| 99 return host_file_name | |
| OLD | NEW |