Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 | |
| 3 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 | |
| 7 import csv | |
| 8 import os | |
| 9 import random | |
| 10 | |
| 11 NOT_AVAILABLE_STRING = 'not available' | |
| 12 | |
| 13 | |
| 14 class MediaTestMatrix: | |
| 15 """This class reads video test matrix file and stores them. | |
| 16 | |
| 17 Video test matrix contains different video titles as column and different | |
| 18 video codec (such as "webm" as columns. The matrix entry contains video | |
| 19 specification such as "320x240 50 fps VP8 / no audio 1 MBit/s PSNR 36.70" | |
| 20 or "not available". It also provides methods to generate information based | |
| 21 on the client's needs. For example, "video" tag or "audio" tag is returned | |
| 22 based on what kinds of media it is. This matrix is for managing different | |
| 23 kinds of media files in media testing. | |
| 24 | |
| 25 TODO(imasaki@chromium.org): remove the code duplication in Generate methods. | |
| 26 """ | |
| 27 # Video extensions such as "webm". | |
| 28 __exts = [] | |
|
Nirnimesh
2011/03/20 09:14:06
nit: why double underscores?
(Repeat for all occur
imasaki1
2011/03/20 16:17:16
Changed them into single underscore.
(I thought t
| |
| 29 # Video specification dictionary of list of video descriptions | |
| 30 # (the key is video title). | |
| 31 __specs = {} | |
| 32 # Video titles such as "bear". | |
| 33 __video_titles = [] | |
| 34 # Tag dictionary (the key is extension). | |
| 35 __tags = {} | |
| 36 | |
| 37 def ReadData(self, csv_file): | |
| 38 """Reads CSV file and stores in list specs and extensions. | |
| 39 | |
| 40 CSV file should have the following format: | |
| 41 "ext","tag","title1","title2",.... | |
| 42 "0.webm", "video","description1","descrption2", "not available" | |
| 43 (when it is not available). | |
| 44 | |
| 45 Args: | |
| 46 csv_file : CSV file name contains video matrix information described | |
| 47 above. | |
| 48 | |
| 49 Raises: | |
| 50 csv.Error if the number of columns is not the same as the number of | |
| 51 tiles. | |
| 52 """ | |
| 53 # Clean up all data. | |
| 54 self.__exts = [] | |
| 55 self.__specs = {} | |
| 56 self.__video_titles = [] | |
| 57 self.__tags = {} | |
| 58 file = open(csv_file, 'rb') | |
| 59 reader = csv.reader(file) | |
| 60 for counter, row in enumerate(reader): | |
| 61 if counter == 0: | |
| 62 # First row is comment. So, skip it. | |
| 63 pass | |
| 64 elif counter == 1: | |
| 65 # Second row is for header (video titles). | |
| 66 for title in row[1:]: | |
| 67 self.__video_titles.append(title) | |
| 68 self.__specs[title] = [] | |
| 69 else: | |
| 70 # Error checking is done here based on the number of titles | |
| 71 if len(self.__video_titles) != len(row) - 1: | |
| 72 print "Row %d should have %d columns but has %d columns" % (counter, | |
| 73 len(self.__video_titles), len(row)) | |
| 74 raise csv.Error | |
| 75 # First column should contain extension. | |
| 76 self.__exts.append(row[0]) | |
| 77 # Second column should contain tag (audio or video). | |
| 78 self.__tags[row[0]] = row[1] | |
| 79 for i in range(len(row) - 2): | |
| 80 self.__specs[self.__video_titles[i]].append(row[i + 2]) | |
| 81 file.close() | |
| 82 | |
| 83 def __GenerateMediaInfo(self, sub_type, name, media_test_matrix_home_url): | |
|
Nirnimesh
2011/03/20 09:14:06
nit: why double underscores?
imasaki1
2011/03/20 16:17:16
I thought it is for private methods when I read so
| |
| 84 """Generate media information from matrix generated by CSV file. | |
| 85 | |
| 86 Args: | |
| 87 sub_type: index of the extensions (rows in the CSV file). | |
| 88 name: the name of the video file (column name in the CSV file). | |
| 89 media_test_matrix_home_url: url of the matrix home that used | |
| 90 to generate link. | |
| 91 | |
| 92 Returns: | |
| 93 a tuple of (info, url, link, tag, is_video, nickname). | |
| 94 info: description of the video (an entry in the matrix). | |
| 95 url: URL of the video or audio. | |
| 96 link: href tag for video or audio. | |
| 97 tag: a tag string can be used to display video or audio. | |
| 98 is_video: True if the file is video; False otherwise. | |
| 99 nickname: nickname of the video for presentation. | |
| 100 """ | |
| 101 if name is 'none': | |
| 102 return | |
| 103 cur_video = name | |
| 104 is_video = self.__tags[self.__exts[sub_type]] == 'video' | |
| 105 file = os.path.join(cur_video, cur_video + self.__exts[sub_type]) | |
| 106 # Specs were generated from CSV file in readFile(). | |
| 107 info = self.__specs[cur_video][sub_type] | |
| 108 url = media_test_matrix_home_url + file | |
| 109 link = '<b><a href="%s">%s</a> %s</b>' % (url, file, info) | |
| 110 type = ['audio', 'video'][is_video] | |
| 111 tag_attr = 'id="v" controls autoplay playbackRate=1 loop valign=top' | |
| 112 tag = '<%s %s src="%s">' % (type, tag_attr, file) | |
| 113 nickname = cur_video + self.__exts[sub_type] | |
| 114 return (info, url, link, tag, is_video, nickname) | |
| 115 | |
| 116 def GenerateRandomMediaInfo(self, number_of_tries=10, | |
| 117 video_matrix_home_url=''): | |
| 118 """Generate random video info that can be used for playing this media. | |
| 119 | |
| 120 Args: | |
| 121 video_only: True if generate random video only. | |
| 122 media_test_matrix_home_url: url of the matrix home that used | |
| 123 to generate link. | |
| 124 | |
| 125 Returns: | |
| 126 a list of a tuples (info, url, link, tag, is_video, nickname). | |
| 127 info: description of the video (an entry in the matrix). | |
| 128 url: URL of the video/audio. | |
| 129 link: href tag for video or audio. | |
| 130 tag: a tag string can be used to display video or audio. | |
| 131 is_video: True if the file is video; False otherwise. | |
| 132 nickname: nickname of the video for presentation. | |
| 133 """ | |
| 134 # Try number_of_tries times to find available video/audio. | |
| 135 for i in range(number_of_tries): | |
| 136 sub_type = random.randint(0, len(self.__exts)) | |
| 137 name = self.__video_titles[random.randint(0, len(self.__video_titles)-1)] | |
| 138 (info, url, link, tag, is_video, nickname) = ( | |
| 139 self.__GenerateMediaInfo(sub_type, name, video_matrix_home_url)) | |
| 140 if NOT_AVAILABLE_STRING not in info: | |
| 141 return (info, url, link, tag, is_video, nickname) | |
| 142 # Gives up after that (very small chance). | |
| 143 return None | |
| 144 | |
| 145 def GenerateAllMediaInfos(self, video_only, media_matrix_home_url=''): | |
| 146 """Generate all video infos that can be used for playing this media. | |
| 147 | |
| 148 Args: | |
| 149 video_only: True if generate random video only (not audio). | |
| 150 media_test_matrix_home_url: url of the matrix home that used | |
| 151 to generate link. | |
| 152 Returns: | |
| 153 a list of a tuples (info, url, link, tag, is_video, nickname). | |
| 154 info: description of the video (an entry in the matrix). | |
| 155 url: URL of the video/audio. | |
| 156 link: href tag for video or audio. | |
| 157 tag: a tag string can be used to display video or audio. | |
| 158 is_video: True if the file is video; False otherwise. | |
| 159 nickname: nickname of the video for presentation (such as bear.webm). | |
| 160 """ | |
| 161 media_infos = [] | |
| 162 | |
| 163 for i in range(0, len(self.__video_titles)-1): | |
| 164 name = self.__video_titles[i] | |
| 165 for sub_type in range(len(self.__exts)): | |
| 166 (info, url, link, tag, is_video, nickname) = ( | |
| 167 self.__GenerateMediaInfo(sub_type, name, media_matrix_home_url)) | |
| 168 if ((NOT_AVAILABLE_STRING not in info) and | |
| 169 ((not video_only) or (video_only and is_video))): | |
| 170 media_infos.append([info, url, link, tag, is_video, nickname]) | |
| 171 return media_infos | |
| 172 | |
| 173 def GenerateAllMediaInfosInCompactForm(self, video_only, | |
| 174 media_matrix_home_url=''): | |
| 175 """Generate all media information in compact form. | |
| 176 | |
| 177 Compact form contains only url, nickname and tag. | |
| 178 | |
| 179 Args: | |
| 180 video_only: True if generate random video only. | |
| 181 media_test_matrix_home_url: url of the matrix home that used | |
| 182 to generate link. | |
| 183 | |
| 184 Returns: | |
| 185 a list of a tuples (url, nickname, tag). | |
| 186 url: URL of the video/audio. | |
| 187 nickname: nickname of the video/audio for presentation | |
| 188 (such as bear.webm). | |
| 189 tag: HTML5 tag for the video/audio. | |
| 190 """ | |
| 191 media_infos = [] | |
| 192 for i in range(0, len(self.__video_titles)-1): | |
| 193 name = self.__video_titles[i] | |
| 194 for sub_type in range(len(self.__exts)): | |
| 195 (info, url, link, tag, is_video, nickname) = ( | |
| 196 self.__GenerateMediaInfo(sub_type, name, media_matrix_home_url)) | |
| 197 tag = ['audio', 'video'][is_video] | |
| 198 if ((not NOT_AVAILABLE_STRING in info) and | |
| 199 ((not video_only) or (video_only and is_video))): | |
| 200 media_infos.append([url, nickname, tag]) | |
| 201 return media_infos | |
| 202 | |
| 203 @staticmethod | |
| 204 def LookForMediaInfoInCompactFormByNickName(compact_list, target): | |
| 205 """Look for video by its nickname in the compact_list. | |
| 206 | |
| 207 Args: | |
| 208 compact_list: a list generated by GenerateAllMediaInfosInCompactForm. | |
| 209 target: a target nickname string to look for. | |
| 210 | |
| 211 Returns: | |
| 212 A tuple (url, nickname, tag) where nickname is a target string. | |
| 213 url: URL of the video/audio. | |
| 214 nickname: nickname of the video/audio for presentation | |
| 215 (such as bear.webm). | |
| 216 tag: HTML5 tag for the video/audio. | |
| 217 """ | |
| 218 for url, nickname, tag in compact_list: | |
| 219 if target == nickname: | |
| 220 return (url, nickname, tag) | |
| OLD | NEW |