Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(822)

Side by Side Diff: build/android/pylib/screenshot.py

Issue 221823011: [Android] Change object types from AndroidCommands to DeviceUtils in build/android/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Frank's comments. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 7 import time
8 8
9 from pylib import cmd_helper 9 from pylib import cmd_helper
10 10
11 # TODO(jbudorick) Remove once telemetry gets switched over.
12 import pylib.android_commands
13 import pylib.device.device_utils
14
11 15
12 def _GetTimestamp(): 16 def _GetTimestamp():
13 return time.strftime('%Y-%m-%d-%H%M%S', time.localtime()) 17 return time.strftime('%Y-%m-%d-%H%M%S', time.localtime())
14 18
15 19
16 def _EnsureHostDirectory(host_file): 20 def _EnsureHostDirectory(host_file):
17 host_dir = os.path.dirname(os.path.abspath(host_file)) 21 host_dir = os.path.dirname(os.path.abspath(host_file))
18 if not os.path.exists(host_dir): 22 if not os.path.exists(host_dir):
19 os.makedirs(host_dir) 23 os.makedirs(host_dir)
20 24
21 25
22 def TakeScreenshot(adb, host_file): 26 def TakeScreenshot(device, host_file):
23 """Saves a screenshot image to |host_file| on the host. 27 """Saves a screenshot image to |host_file| on the host.
24 28
25 Args: 29 Args:
26 adb: AndroidCommands instance. 30 device: DeviceUtils instance.
27 host_file: Path to the image file to store on the host. 31 host_file: Path to the image file to store on the host.
28 """ 32 """
29 host_file = os.path.abspath(host_file or 33 host_file = os.path.abspath(host_file or
30 'screenshot-%s.png' % _GetTimestamp()) 34 'screenshot-%s.png' % _GetTimestamp())
31 _EnsureHostDirectory(host_file) 35 _EnsureHostDirectory(host_file)
32 device_file = '%s/screenshot.png' % adb.GetExternalStorage() 36 device_file = '%s/screenshot.png' % device.old_interface.GetExternalStorage()
33 adb.RunShellCommand('/system/bin/screencap -p %s' % device_file) 37 device.old_interface.RunShellCommand(
34 adb.PullFileFromDevice(device_file, host_file) 38 '/system/bin/screencap -p %s' % device_file)
35 adb.RunShellCommand('rm -f "%s"' % device_file) 39 device.old_interface.PullFileFromDevice(device_file, host_file)
40 device.old_interface.RunShellCommand('rm -f "%s"' % device_file)
36 return host_file 41 return host_file
37 42
38 43
39 class VideoRecorder(object): 44 class VideoRecorder(object):
40 """Records a screen capture video from an Android Device (KitKat or newer). 45 """Records a screen capture video from an Android Device (KitKat or newer).
41 46
42 Args: 47 Args:
43 adb: AndroidCommands instance. 48 device: DeviceUtils instance.
44 host_file: Path to the video file to store on the host. 49 host_file: Path to the video file to store on the host.
45 megabits_per_second: Video bitrate in megabits per second. Allowed range 50 megabits_per_second: Video bitrate in megabits per second. Allowed range
46 from 0.1 to 100 mbps. 51 from 0.1 to 100 mbps.
47 size: Video frame size tuple (width, height) or None to use the device 52 size: Video frame size tuple (width, height) or None to use the device
48 default. 53 default.
49 rotate: If True, the video will be rotated 90 degrees. 54 rotate: If True, the video will be rotated 90 degrees.
50 """ 55 """
51 def __init__(self, adb, host_file, megabits_per_second=4, size=None, 56 def __init__(self, device, host_file, megabits_per_second=4, size=None,
52 rotate=False): 57 rotate=False):
53 self._adb = adb 58 # TODO(jbudorick) Remove once telemetry gets switched over.
54 self._device_file = '%s/screen-recording.mp4' % adb.GetExternalStorage() 59 if isinstance(device, pylib.android_commands.AndroidCommands):
60 device = pylib.device.device_utils.DeviceUtils(device)
61 self._device = device
62 self._device_file = (
63 '%s/screen-recording.mp4' % device.old_interface.GetExternalStorage())
55 self._host_file = host_file or 'screen-recording-%s.mp4' % _GetTimestamp() 64 self._host_file = host_file or 'screen-recording-%s.mp4' % _GetTimestamp()
56 self._host_file = os.path.abspath(self._host_file) 65 self._host_file = os.path.abspath(self._host_file)
57 self._recorder = None 66 self._recorder = None
58 self._recorder_pids = None 67 self._recorder_pids = None
59 self._recorder_stdout = None 68 self._recorder_stdout = None
60 self._is_started = False 69 self._is_started = False
61 70
62 self._args = ['adb'] 71 self._args = ['adb']
63 if self._adb.GetDevice(): 72 if self._device.old_interface.GetDevice():
64 self._args += ['-s', self._adb.GetDevice()] 73 self._args += ['-s', self._device.old_interface.GetDevice()]
65 self._args += ['shell', 'screenrecord', '--verbose'] 74 self._args += ['shell', 'screenrecord', '--verbose']
66 self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)] 75 self._args += ['--bit-rate', str(megabits_per_second * 1000 * 1000)]
67 if size: 76 if size:
68 self._args += ['--size', '%dx%d' % size] 77 self._args += ['--size', '%dx%d' % size]
69 if rotate: 78 if rotate:
70 self._args += ['--rotate'] 79 self._args += ['--rotate']
71 self._args += [self._device_file] 80 self._args += [self._device_file]
72 81
73 def Start(self): 82 def Start(self):
74 """Start recording video.""" 83 """Start recording video."""
75 _EnsureHostDirectory(self._host_file) 84 _EnsureHostDirectory(self._host_file)
76 self._recorder_stdout = tempfile.mkstemp()[1] 85 self._recorder_stdout = tempfile.mkstemp()[1]
77 self._recorder = cmd_helper.Popen( 86 self._recorder = cmd_helper.Popen(
78 self._args, stdout=open(self._recorder_stdout, 'w')) 87 self._args, stdout=open(self._recorder_stdout, 'w'))
79 self._recorder_pids = self._adb.ExtractPid('screenrecord') 88 self._recorder_pids = self._device.old_interface.ExtractPid('screenrecord')
80 if not self._recorder_pids: 89 if not self._recorder_pids:
81 raise RuntimeError('Recording failed. Is your device running Android ' 90 raise RuntimeError('Recording failed. Is your device running Android '
82 'KitKat or later?') 91 'KitKat or later?')
83 92
84 def IsStarted(self): 93 def IsStarted(self):
85 if not self._is_started: 94 if not self._is_started:
86 for line in open(self._recorder_stdout): 95 for line in open(self._recorder_stdout):
87 self._is_started = line.startswith('Content area is ') 96 self._is_started = line.startswith('Content area is ')
88 if self._is_started: 97 if self._is_started:
89 break 98 break
90 return self._is_started 99 return self._is_started
91 100
92 def Stop(self): 101 def Stop(self):
93 """Stop recording video.""" 102 """Stop recording video."""
94 os.remove(self._recorder_stdout) 103 os.remove(self._recorder_stdout)
95 self._is_started = False 104 self._is_started = False
96 if not self._recorder or not self._recorder_pids: 105 if not self._recorder or not self._recorder_pids:
97 return 106 return
98 self._adb.RunShellCommand('kill -SIGINT ' + ' '.join(self._recorder_pids)) 107 self._device.old_interface.RunShellCommand(
108 'kill -SIGINT ' + ' '.join(self._recorder_pids))
99 self._recorder.wait() 109 self._recorder.wait()
100 110
101 def Pull(self): 111 def Pull(self):
102 """Pull resulting video file from the device. 112 """Pull resulting video file from the device.
103 113
104 Returns: 114 Returns:
105 Output video file name on the host. 115 Output video file name on the host.
106 """ 116 """
107 self._adb.PullFileFromDevice(self._device_file, self._host_file) 117 self._device.old_interface.PullFileFromDevice(
108 self._adb.RunShellCommand('rm -f "%s"' % self._device_file) 118 self._device_file, self._host_file)
119 self._device.old_interface.RunShellCommand('rm -f "%s"' % self._device_file)
109 return self._host_file 120 return self._host_file
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698