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