blob: 823fb35396dc4b8428e0b148ded42d44707e0810 [file] [log] [blame]
harpreet6ffd1e42017-01-24 18:24:36 -08001# Copyright (c) 2017 The Chromium OS 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
5import enum, json
6
7from autotest_lib.server.cros import cfm_jmidata_helper_base
8
9# Start index in the JSON object that contains Audio/Video streams related info.
10AV_INDEX = 4
11
12SSRC = u'ssrc'
13GLOBAL = u'global'
14
15AUDIO_INPUT = u'audioInputLevel'
16AUDIO_OUTPUT = u'audioOutputLevel'
17BYTES_RECEIVED = u'bytesReceived'
18BYTES_SENT = u'bytesSent'
19ADAPTATION_CHANGES = u'googAdaptationChanges'
20AVERAGE_ENCODE_TIME = u'googAvgEncodeMs'
21BANDWIDTH_LIMITED_RESOLUTION = u'googBandwidthLimitedResolution'
22CPU_LIMITED_RESOLUTION = u'googCpuLimitedResolution'
23VIDEO_ENCODE_CPU_USAGE = u'googEncodeUsagePercent'
24VIDEO_RECEIVED_FRAME_HEIGHT = u'googFrameHeightReceived'
25VIDEO_RECEIVED_FRAME_WIDTH = u'googFrameWidthReceived'
26FRAMERATE_OUTGOING = u'googFrameRateInput'
27FRAMERATE_RECEIVED = u'googFrameRateReceived'
28FRAMERATE_SENT = u'googFrameRateSent'
29FRAMERATE_DECODED = u'googFrameRateDecoded'
30FRAMERATE_OUTPUT = u'googFrameRateOutput'
31FRAME_WIDTH_SENT = u'googFrameWidthSent'
32FRAME_HEIGHT_SENT = u'googFrameHeightSent'
33FRAMES_DECODED = u'framesDecoded'
34FRAMES_ENCODED = u'framesEncoded'
35VIDEO_PACKETS_LOST = u'packetsLost'
36VIDEO_PACKETS_SENT = u'packetsSent'
37
38
39class CpuUsageType(enum.Enum):
40 BROWSER_CPU = u'browserCpuUsage'
41 GPU_CPU = u'gpuCpuUsage'
42 NUM_PROCESSORS = u'numOfProcessors'
43 NACL_EFFECTS_CPU = u'pluginCpuUsage'
44 RENDERER_CPU = u'tabCpuUsage'
45 TOTAL_CPU = u'totalCpuUsage'
46
47
48class JMIDataV3Helper(cfm_jmidata_helper_base.JMIDataHelperBase):
49 """Helper class for JMI data v3 parsing. This class helps in extracting
50 relevant JMI data from javascript log.
51
52 The class takes javascript file as input and extracts jmidata elements from
53 the file that is internally stored as a list. Whenever we need to extract
54 data i.e. audio received bytes we call a method such as
55 getAudioReceivedDataList. This method converts each string element in the
56 internal list to a json object and retrieves the relevant info from it which
57 is then stored and returned as a list.
58 """
59
60 def __init__(self, log_file_content):
61 super(JMIDataV3Helper, self).__init__(log_file_content, 'jmidatav3')
62
63 def _ExtractAllJMIDataPointsWithKey(self, jmi_type, is_audio, key):
64 """Extracts all values from the data points with the given key."""
65 data_list = []
66 for jmi_data_point in self._jmi_list:
67 json_arr = json.loads(jmi_data_point)
68 for i in range(AV_INDEX, len(json_arr)):
69 if json_arr[i] and jmi_type in json_arr[i]:
70 jmi_obj = json_arr[i][jmi_type]
71 #print jmi_obj
72 this_is_audio = (AUDIO_INPUT in jmi_obj or
73 AUDIO_OUTPUT in jmi_obj)
74 if this_is_audio == is_audio and key in jmi_obj:
75 if (not isinstance(jmi_obj[key], int) and
76 (jmi_obj[key] == 'false' or
77 jmi_obj[key] == 'true')):
78 jmi_obj[key] = 1 if jmi_obj[key] == 'true' else 0
79 data_list.append(jmi_obj[key])
80 return data_list
81
82 def GetAudioReceivedBytesList(self):
83 return self._ExtractAllJMIDataPointsWithKey(
84 jmi_type=SSRC, is_audio=True, key=BYTES_RECEIVED)
85
86 def GetAudioSentBytesList(self):
87 return self._ExtractAllJMIDataPointsWithKey(
88 jmi_type=SSRC, is_audio=True, key=BYTES_SENT)
89
90 def GetAudioReceivedEnergyList(self):
91 return self._ExtractAllJMIDataPointsWithKey(
92 jmi_type=SSRC, is_audio=True, key=AUDIO_OUTPUT)
93
94 def GetAudioSentEnergyList(self):
95 return self._ExtractAllJMIDataPointsWithKey(
96 jmi_type=SSRC, is_audio=True, key=AUDIO_INPUT)
97
98 def GetVideoSentBytesList(self):
99 return self._ExtractAllJMIDataPointsWithKey(
100 jmi_type=SSRC, is_audio=False, key=BYTES_SENT)
101
102 def GetVideoReceivedBytesList(self):
103 return self._ExtractAllJMIDataPointsWithKey(
104 jmi_type=SSRC, is_audio=False, key=BYTES_RECEIVED)
105
106 def GetVideoIncomingFramerateReceivedList(self):
107 return self._ExtractAllJMIDataPointsWithKey(
108 jmi_type=SSRC, is_audio=False, key=FRAMERATE_RECEIVED)
109
110 def GetVideoOutgoingFramerateSentList(self):
111 return self._ExtractAllJMIDataPointsWithKey(
112 jmi_type=SSRC, is_audio=False, key=FRAMERATE_SENT)
113
114 def GetVideoIncomingFramerateDecodedList(self):
115 return self._ExtractAllJMIDataPointsWithKey(
116 jmi_type=SSRC, is_audio=False, key=FRAMERATE_DECODED)
117
118 def GetVideoIncomingFramerateList(self):
119 return self._ExtractAllJMIDataPointsWithKey(
120 jmi_type=SSRC, is_audio=False, key=FRAMERATE_OUTPUT)
121
122 def GetVideoSentFrameWidthList(self):
123 return self._ExtractAllJMIDataPointsWithKey(
124 jmi_type=SSRC, is_audio=False, key=FRAME_WIDTH_SENT)
125
126 def GetVideoSentFrameHeightList(self):
127 return self._ExtractAllJMIDataPointsWithKey(
128 jmi_type=SSRC, is_audio=False, key=FRAME_HEIGHT_SENT)
129
130 def GetCPULimitedResolutionList(self):
131 return self._ExtractAllJMIDataPointsWithKey(
132 jmi_type=SSRC, is_audio=False, key=CPU_LIMITED_RESOLUTION)
133
134 def GetVideoPacketsSentList(self):
135 return self._ExtractAllJMIDataPointsWithKey(
136 jmi_type=SSRC, is_audio=False, key=VIDEO_PACKETS_SENT)
137
138 def GetVideoPacketsLostList(self):
139 return self._ExtractAllJMIDataPointsWithKey(
140 jmi_type=SSRC, is_audio=False, key=VIDEO_PACKETS_LOST)
141
142 def GetVideoIncomingFramesDecodedList(self):
143 return self._ExtractAllJMIDataPointsWithKey(
144 jmi_type=SSRC, is_audio=False, key=FRAMES_DECODED)
145
146 def GetVideoOutgoingFramesEncodedList(self):
147 return self._ExtractAllJMIDataPointsWithKey(
148 jmi_type=SSRC, is_audio=False, key=FRAMES_ENCODED)
149
150 def GetVideoAdaptationChangeList(self):
151 return self._ExtractAllJMIDataPointsWithKey(
152 jmi_type=SSRC, is_audio=False, key=ADAPTATION_CHANGES)
153
154 def GetVideoEncodeTimeList(self):
155 return self._ExtractAllJMIDataPointsWithKey(
156 jmi_type=SSRC, is_audio=False, key=AVERAGE_ENCODE_TIME)
157
158 def GetBandwidthLimitedResolutionList(self):
159 return self._ExtractAllJMIDataPointsWithKey(
160 jmi_type=SSRC, is_audio=False, key=BANDWIDTH_LIMITED_RESOLUTION)
161
162 def GetVideoReceivedFrameHeightList(self):
163 return self._ExtractAllJMIDataPointsWithKey(
164 jmi_type=SSRC, is_audio=False, key=VIDEO_RECEIVED_FRAME_HEIGHT)
165
166 def GetVideoOutgoingFramerateInputList(self):
167 return self._ExtractAllJMIDataPointsWithKey(
168 jmi_type=SSRC, is_audio=False, key=FRAMERATE_OUTGOING)
169
170 def GetVideoReceivedFrameWidthList(self):
171 return self._ExtractAllJMIDataPointsWithKey(
172 jmi_type=SSRC, is_audio=False, key=VIDEO_RECEIVED_FRAME_WIDTH)
173
174 def GetVideoEncodeCpuUsagePercentList(self):
175 return self._ExtractAllJMIDataPointsWithKey(
176 jmi_type=SSRC, is_audio=False, key=VIDEO_ENCODE_CPU_USAGE)
177
178 def GetNumberOfActiveIncomingVideoStreams(self):
179 """Retrieve number of active incoming video streams."""
180 if not self._jmi_list:
181 # JMI data hasn't started populating yet.
182 return 0
183
184 num_video_streams = []
185
186 # If JMI data has started getting populated and has video stream data.
187 for jmi_data_point in self._jmi_list:
188 json_arr = json.loads(jmi_data_point)
189 video_streams = 0
190 for i in range(AV_INDEX, len(json_arr)):
191 if json_arr[i] and SSRC in json_arr[i]:
192 ssrc_obj = json_arr[i][SSRC]
193 is_audio = ('audioInputLevel' in ssrc_obj or
194 'audioOutputLevel' in ssrc_obj)
195 is_incoming = 'bytesReceived' in ssrc_obj
196 frame_rate_received = 'googFrameRateReceived' in ssrc_obj
197 if ssrc_obj['mediaType'] == 'video' and \
198 frame_rate_received:
199 frame_rate = ssrc_obj['googFrameRateReceived']
200 if (is_incoming and not is_audio) and \
201 frame_rate != 0:
202 video_streams += 1
203 num_video_streams.append(video_streams)
204 return num_video_streams
205
206 def GetCpuUsageList(self, cpu_type):
207 """Retrieves cpu usage data from JMI data.
208
209 @param cpu_type: Enum of type CpuUsageType.
210 @returns List containing CPU usage data.
211 """
212 data_list = []
213 for jmi_data_point in self._jmi_list:
214 json_arr = json.loads(jmi_data_point)
215 for i in range(AV_INDEX, len(json_arr)):
216 if json_arr[i] and GLOBAL in json_arr[i]:
217 global_obj = json_arr[i][GLOBAL]
218 # Some values in JMIDataV3 are set to 'null'.
219 if cpu_type.value == u'numOfProcessors':
220 return global_obj[cpu_type.value]
221 elif (cpu_type.value in global_obj and
222 self.IsFloat(global_obj[cpu_type.value])):
223 data_list.append(float(global_obj[cpu_type.value]))
224 return data_list
225
226 def GetCpuUsageType(self):
227 return CpuUsageType
228
229 def IsFloat(self, value):
230 try:
231 float(value)
232 return True
233 except TypeError:
234 return False