[email protected] | 4d5f13bb | 2013-08-02 00:45:44 | [diff] [blame^] | 1 | # Copyright 2013 The Chromium Authors. All rights reserved. |
[email protected] | 8295ab5 | 2013-07-15 22:19:15 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Media Metrics class injects and calls JS responsible for recording metrics. |
| 6 | |
| 7 | Default media metrics are collected for every media element in the page, such as |
| 8 | decoded_frame_count, dropped_frame_count, decoded_video_bytes, and |
| 9 | decoded_audio_bytes. |
| 10 | """ |
| 11 | |
| 12 | import logging |
| 13 | import os |
| 14 | |
| 15 | |
| 16 | class MediaMetrics(object): |
| 17 | def __init__(self, tab): |
| 18 | with open( |
[email protected] | 4d5f13bb | 2013-08-02 00:45:44 | [diff] [blame^] | 19 | os.path.join(os.path.dirname(__file__), 'media.js')) as f: |
[email protected] | 8295ab5 | 2013-07-15 22:19:15 | [diff] [blame] | 20 | js = f.read() |
| 21 | tab.ExecuteJavaScript(js) |
| 22 | self.tab = tab |
| 23 | |
| 24 | def Start(self): |
| 25 | """Create the media metrics for all media elements in the document.""" |
| 26 | self.tab.ExecuteJavaScript('window.__createMediaMetricsForDocument()') |
| 27 | |
| 28 | def StopAndGetResults(self, results): |
| 29 | """Reports all recorded metrics as Telemetry perf results.""" |
| 30 | media_metrics = self.tab.EvaluateJavaScript('window.__getAllMetrics()') |
| 31 | for media_metric in media_metrics: |
[email protected] | 8e0a395 | 2013-07-23 23:43:21 | [diff] [blame] | 32 | self.AddResultsForMediaElement(media_metric, results) |
[email protected] | 8295ab5 | 2013-07-15 22:19:15 | [diff] [blame] | 33 | |
[email protected] | 8e0a395 | 2013-07-23 23:43:21 | [diff] [blame] | 34 | def AddResultsForMediaElement(self, media_metric, results): |
[email protected] | 8295ab5 | 2013-07-15 22:19:15 | [diff] [blame] | 35 | """Reports metrics for one media element. |
| 36 | |
| 37 | Media metrics contain an ID identifying the media element and values: |
| 38 | media_metric = { |
| 39 | 'id': 'video_1', |
| 40 | 'metrics': { |
| 41 | 'time_to_play': 120, |
| 42 | 'decoded_bytes': 13233, |
| 43 | ... |
| 44 | } |
| 45 | } |
| 46 | """ |
[email protected] | 8e0a395 | 2013-07-23 23:43:21 | [diff] [blame] | 47 | def AddResults(metric, unit): |
[email protected] | 8295ab5 | 2013-07-15 22:19:15 | [diff] [blame] | 48 | metrics = media_metric['metrics'] |
[email protected] | 8e0a395 | 2013-07-23 23:43:21 | [diff] [blame] | 49 | for m in metrics: |
| 50 | if m.startswith(metric): |
| 51 | special_label = m[len(metric):] |
| 52 | results.Add(trace + special_label, unit, str(metrics[m]), |
| 53 | chart_name=metric, data_type='default') |
[email protected] | 8295ab5 | 2013-07-15 22:19:15 | [diff] [blame] | 54 | |
| 55 | trace = media_metric['id'] |
| 56 | if not trace: |
| 57 | logging.error('Metrics ID is missing in results.') |
| 58 | return |
[email protected] | 8e0a395 | 2013-07-23 23:43:21 | [diff] [blame] | 59 | AddResults('decoded_audio_bytes', 'bytes') |
| 60 | AddResults('decoded_video_bytes', 'bytes') |
| 61 | AddResults('decoded_frame_count', 'frames') |
| 62 | AddResults('dropped_frame_count', 'frames') |
| 63 | AddResults('playback_time', 'sec') |
| 64 | AddResults('seek', 'sec') |
| 65 | AddResults('time_to_play', 'sec') |