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