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.
7 from telemetry
.value
import list_of_scalar_values
8 from telemetry
.value
import scalar
10 from metrics
import Metric
13 class MediaMetric(Metric
):
14 """MediaMetric class injects and calls JS responsible for recording metrics.
16 Default media metrics are collected for every media element in the page,
17 such as decoded_frame_count, dropped_frame_count, decoded_video_bytes, and
21 def __init__(self
, tab
):
22 super(MediaMetric
, self
).__init
__()
23 with
open(os
.path
.join(os
.path
.dirname(__file__
), 'media.js')) as f
:
25 tab
.ExecuteJavaScript(js
)
27 self
._skip
_basic
_metrics
= False
29 def Start(self
, page
, tab
):
30 """Create the media metrics for all media elements in the document."""
31 if hasattr(page
, 'skip_basic_metrics'):
32 self
._skip
_basic
_metrics
= page
.skip_basic_metrics
33 tab
.ExecuteJavaScript('window.__createMediaMetricsForDocument()')
35 def Stop(self
, page
, tab
):
36 self
._results
= tab
.EvaluateJavaScript('window.__getAllMetrics()')
38 # Optional |exclude_metrics| args are not in base class Metric.
39 # pylint: disable=W0221
40 def AddResults(self
, tab
, results
, exclude_metrics
=None):
41 """Reports all recorded metrics as Telemetry perf results."""
42 exclude_metrics
= exclude_metrics
or []
44 for media_metric
in self
._results
:
45 trace_names
.append(self
._AddResultsForMediaElement
(media_metric
, results
,
48 return '_'.join(trace_names
) or tab
.url
50 def _AddResultsForMediaElement(self
, media_metric
, results
, exclude_metrics
):
51 """Reports metrics for one media element.
53 Media metrics contain an ID identifying the media element and values:
58 'decoded_bytes': 13233,
63 def AddOneResult(metric
, unit
):
64 if metric
in exclude_metrics
:
67 metrics
= media_metric
['metrics']
69 if m
.startswith(metric
):
70 special_label
= m
[len(metric
):]
71 trace_name
= '%s.%s%s' % (metric
, trace
, special_label
)
72 if isinstance(metrics
[m
], list):
73 results
.AddValue(list_of_scalar_values
.ListOfScalarValues(
74 results
.current_page
, trace_name
, unit
,
75 values
=[float(v
) for v
in metrics
[m
]],
78 results
.AddValue(scalar
.ScalarValue(
79 results
.current_page
, trace_name
, unit
, value
=float(metrics
[m
]),
82 trace
= media_metric
['id']
84 logging
.error('Metrics ID is missing in results.')
87 if not self
._skip
_basic
_metrics
:
88 AddOneResult('buffering_time', 'ms')
89 AddOneResult('decoded_audio_bytes', 'bytes')
90 AddOneResult('decoded_video_bytes', 'bytes')
91 AddOneResult('decoded_frame_count', 'frames')
92 AddOneResult('dropped_frame_count', 'frames')
93 AddOneResult('time_to_play', 'ms')
95 AddOneResult('avg_loop_time', 'ms')
96 AddOneResult('seek', 'ms')