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. 4 import logging 5 import os 6 7 from metrics import Metric 8 9 class MediaMetric(Metric): 10 """MediaMetric class injects and calls JS responsible for recording metrics. 11 12 Default media metrics are collected for every media element in the page, 13 such as decoded_frame_count, dropped_frame_count, decoded_video_bytes, and 14 decoded_audio_bytes. 15 """ 16 def __init__(self, tab): 17 super(MediaMetric, self).__init__() 18 with open(os.path.join(os.path.dirname(__file__), 'media.js')) as f: 19 js = f.read() 20 tab.ExecuteJavaScript(js) 21 self._results = None 22 23 def Start(self, page, tab): 24 """Create the media metrics for all media elements in the document.""" 25 tab.ExecuteJavaScript('window.__createMediaMetricsForDocument()') 26 27 def Stop(self, page, tab): 28 self._results = tab.EvaluateJavaScript('window.__getAllMetrics()') 29 30 def AddResults(self, tab, results): 31 """Reports all recorded metrics as Telemetry perf results.""" 32 assert self._results 33 for media_metric in self._results: 34 self._AddResultsForMediaElement(media_metric, results) 35 36 def _AddResultsForMediaElement(self, media_metric, results): 37 """Reports metrics for one media element. 38 39 Media metrics contain an ID identifying the media element and values: 40 media_metric = { 41 'id': 'video_1', 42 'metrics': { 43 'time_to_play': 120, 44 'decoded_bytes': 13233, 45 ... 46 } 47 } 48 """ 49 def AddOneResult(metric, unit): 50 metrics = media_metric['metrics'] 51 for m in metrics: 52 if m.startswith(metric): 53 special_label = m[len(metric):] 54 results.Add(trace + special_label, unit, str(metrics[m]), 55 chart_name=metric, data_type='default') 56 57 trace = media_metric['id'] 58 if not trace: 59 logging.error('Metrics ID is missing in results.') 60 return 61 AddOneResult('decoded_audio_bytes', 'bytes') 62 AddOneResult('decoded_video_bytes', 'bytes') 63 AddOneResult('decoded_frame_count', 'frames') 64 AddOneResult('dropped_frame_count', 'frames') 65 AddOneResult('playback_time', 'sec') 66 AddOneResult('seek', 'sec') 67 AddOneResult('time_to_play', 'sec') 68 69