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 signal |
6 import tempfile | 7 import tempfile |
7 | 8 |
8 from pylib import cmd_helper | 9 from pylib import cmd_helper |
9 | 10 |
10 # TODO(jbudorick) Remove once telemetry gets switched over. | 11 # TODO(jbudorick) Remove once telemetry gets switched over. |
11 import pylib.android_commands | 12 import pylib.android_commands |
12 import pylib.device.device_utils | 13 import pylib.device.device_utils |
13 | 14 |
14 | 15 |
15 class VideoRecorder(object): | 16 class VideoRecorder(object): |
(...skipping 10 matching lines...) Expand all Loading... |
26 """ | 27 """ |
27 def __init__(self, device, megabits_per_second=4, size=None, | 28 def __init__(self, device, megabits_per_second=4, size=None, |
28 rotate=False): | 29 rotate=False): |
29 # TODO(jbudorick) Remove once telemetry gets switched over. | 30 # TODO(jbudorick) Remove once telemetry gets switched over. |
30 if isinstance(device, pylib.android_commands.AndroidCommands): | 31 if isinstance(device, pylib.android_commands.AndroidCommands): |
31 device = pylib.device.device_utils.DeviceUtils(device) | 32 device = pylib.device.device_utils.DeviceUtils(device) |
32 self._device = device | 33 self._device = device |
33 self._device_file = ( | 34 self._device_file = ( |
34 '%s/screen-recording.mp4' % device.GetExternalStoragePath()) | 35 '%s/screen-recording.mp4' % device.GetExternalStoragePath()) |
35 self._recorder = None | 36 self._recorder = None |
36 self._recorder_pids = None | |
37 self._recorder_stdout = None | 37 self._recorder_stdout = None |
38 self._is_started = False | 38 self._is_started = False |
39 | 39 |
40 self._args = ['adb'] | 40 self._args = ['adb'] |
41 if self._device.old_interface.GetDevice(): | 41 if self._device.old_interface.GetDevice(): |
42 self._args += ['-s', self._device.old_interface.GetDevice()] | 42 self._args += ['-s', self._device.old_interface.GetDevice()] |
43 self._args += ['shell', 'screenrecord', '--verbose'] | 43 self._args += ['shell', 'screenrecord', '--verbose'] |
44 self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)] | 44 self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)] |
45 if size: | 45 if size: |
46 self._args += ['--size', '%dx%d' % size] | 46 self._args += ['--size', '%dx%d' % size] |
47 if rotate: | 47 if rotate: |
48 self._args += ['--rotate'] | 48 self._args += ['--rotate'] |
49 self._args += [self._device_file] | 49 self._args += [self._device_file] |
50 | 50 |
51 def Start(self): | 51 def Start(self): |
52 """Start recording video.""" | 52 """Start recording video.""" |
53 self._recorder_stdout = tempfile.mkstemp()[1] | 53 self._recorder_stdout = tempfile.mkstemp()[1] |
54 self._recorder = cmd_helper.Popen( | 54 self._recorder = cmd_helper.Popen( |
55 self._args, stdout=open(self._recorder_stdout, 'w')) | 55 self._args, stdout=open(self._recorder_stdout, 'w')) |
56 self._recorder_pids = self._device.old_interface.ExtractPid('screenrecord') | 56 if not self._device.GetPids('screenrecord'): |
57 if not self._recorder_pids: | |
58 raise RuntimeError('Recording failed. Is your device running Android ' | 57 raise RuntimeError('Recording failed. Is your device running Android ' |
59 'KitKat or later?') | 58 'KitKat or later?') |
60 | 59 |
61 def IsStarted(self): | 60 def IsStarted(self): |
62 if not self._is_started: | 61 if not self._is_started: |
63 for line in open(self._recorder_stdout): | 62 for line in open(self._recorder_stdout): |
64 self._is_started = line.startswith('Content area is ') | 63 self._is_started = line.startswith('Content area is ') |
65 if self._is_started: | 64 if self._is_started: |
66 break | 65 break |
67 return self._is_started | 66 return self._is_started |
68 | 67 |
69 def Stop(self): | 68 def Stop(self): |
70 """Stop recording video.""" | 69 """Stop recording video.""" |
71 os.remove(self._recorder_stdout) | 70 os.remove(self._recorder_stdout) |
72 self._is_started = False | 71 self._is_started = False |
73 if not self._recorder or not self._recorder_pids: | 72 if not self._recorder: |
74 return | 73 return |
75 self._device.RunShellCommand( | 74 self._device.KillAll('screenrecord', signum=signal.SIGINT) |
76 'kill -SIGINT ' + ' '.join(self._recorder_pids)) | |
77 self._recorder.wait() | 75 self._recorder.wait() |
78 | 76 |
79 def Pull(self, host_file): | 77 def Pull(self, host_file=None): |
80 """Pull resulting video file from the device. | 78 """Pull resulting video file from the device. |
81 | 79 |
82 Args: | 80 Args: |
83 host_file: Path to the video file to store on the host. | 81 host_file: Path to the video file to store on the host. |
| 82 Returns: |
| 83 Output video file name on the host. |
84 """ | 84 """ |
85 host_file_name = host_file or ('screen-recording-%s.mp4' % | 85 host_file_name = host_file or ('screen-recording-%s.mp4' % |
86 self._device.old_interface.GetTimestamp()) | 86 self._device.old_interface.GetTimestamp()) |
87 host_file_name = os.path.abspath(host_file_name) | 87 host_file_name = os.path.abspath(host_file_name) |
88 self._device.old_interface.EnsureHostDirectory(host_file_name) | 88 self._device.old_interface.EnsureHostDirectory(host_file_name) |
89 self._device.PullFile(self._device_file, host_file_name) | 89 self._device.PullFile(self._device_file, host_file_name) |
90 self._device.RunShellCommand('rm -f "%s"' % self._device_file) | 90 self._device.RunShellCommand('rm -f "%s"' % self._device_file) |
| 91 return host_file_name |
OLD | NEW |