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 = [] | |
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): | |
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 # All videos are placed up to the rows (below 15th row). | |
dennis_jeffrey
2011/03/18 23:51:41
I think we can remove this comment now that videos
imasaki1
2011/03/19 01:00:38
Done.
| |
105 is_video = self.__tags[self.__exts[sub_type]] == "video" | |
dennis_jeffrey
2011/03/18 23:51:41
Use single quotes for strings:
'video'
imasaki1
2011/03/19 01:00:38
Done.
| |
106 file = os.path.join(cur_video, cur_video + self.__exts[sub_type]) | |
107 # Specs were generated from CSV file in readFile(). | |
108 info = self.__specs[cur_video][sub_type] | |
109 url = media_test_matrix_home_url + file | |
110 link = '<b><a href="%s">%s</a> %s</b>' % (url, file, info) | |
111 type = ['audio', 'video'][is_video] | |
112 tag_attr = 'id="v" controls autoplay playbackRate=1 loop valign=top' | |
113 tag = '<%s %s src="%s">' % (type, tag_attr, file) | |
114 nickname = cur_video + self.__exts[sub_type] | |
115 return (info, url, link, tag, is_video, nickname) | |
116 | |
117 def GenerateRandomMediaInfo(self, number_of_tries=10, | |
118 video_matrix_home_url=''): | |
119 """Generate random video info that can be used for playing this media. | |
120 | |
121 Args: | |
122 video_only: True if generate random video only. | |
123 media_test_matrix_home_url: url of the matrix home that used | |
124 to generate link. | |
125 | |
126 Returns: | |
127 a list of a tuples (info, url, link, tag, is_video, nickname). | |
128 info: description of the video (an entry in the matrix). | |
129 url: URL of the video/audio. | |
130 link: href tag for video or audio. | |
131 tag: a tag string can be used to display video or audio. | |
132 is_video: True if the file is video; False otherwise. | |
133 nickname: nickname of the video for presentation. | |
134 """ | |
135 # Try number_of_tries times to find available video/audio. | |
136 for i in range(number_of_tries): | |
137 sub_type = random.randint(0, len(self.__exts)) | |
138 name = self.__video_titles[random.randint(0, len(self.__video_titles)-1)] | |
139 (info, url, link, tag, is_video, nickname) = ( | |
140 self.__GenerateMediaInfo(sub_type, name, video_matrix_home_url)) | |
141 if NOT_AVAILABLE_STRING not in info: | |
142 return (info, url, link, tag, is_video, nickname) | |
143 # Gives up after that (very small chance). | |
144 return None | |
145 | |
146 def GenerateAllMediaInfos(self, video_only, media_matrix_home_url=''): | |
147 """Generate all video infos that can be used for playing this media. | |
148 | |
149 Args: | |
150 video_only: True if generate random video only (not audio). | |
dennis_jeffrey
2011/03/18 23:51:41
Add a line here for the "media_matrix_home_url" ar
imasaki1
2011/03/19 01:00:38
Done.
| |
151 | |
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 video_infos = [] | |
dennis_jeffrey
2011/03/18 23:51:41
Since it looks like this function processes both v
imasaki1
2011/03/19 01:00:38
Done.
| |
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 video_infos.append([info, url, link, tag, is_video, nickname]) | |
171 return video_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. | |
dennis_jeffrey
2011/03/18 23:51:41
Need a line for the "media_matrix_home_url" arg.
imasaki1
2011/03/19 01:00:38
Done.
| |
181 | |
182 Returns: | |
183 a list of a tuples (url, nickname, tag). | |
184 url: URL of the video/audio. | |
185 nickname: nickname of the video/audio for presentation | |
186 (such as bear.webm). | |
187 tag: HTML5 tag for the video/audio. | |
188 """ | |
189 video_infos = [] | |
dennis_jeffrey
2011/03/18 23:51:41
Similar comment as line 161 above.
imasaki1
2011/03/19 01:00:38
Done.
| |
190 for i in range(0, len(self.__video_titles)-1): | |
191 name = self.__video_titles[i] | |
192 for sub_type in range(len(self.__exts)): | |
193 (info, url, link, tag, is_video, nickname) = ( | |
194 self.__GenerateMediaInfo(sub_type, name, media_matrix_home_url)) | |
195 tag = ['audio', 'video'][is_video] | |
196 if ((not NOT_AVAILABLE_STRING in info) and | |
197 ((not video_only) or (video_only and is_video))): | |
198 video_infos.append([url, nickname, tag]) | |
199 return video_infos | |
200 | |
201 @staticmethod | |
202 def LookForMediaInfoInCompactFormByNickName(compact_list, target): | |
203 """Look for video by its nickname in the compact_list. | |
204 | |
205 Args: | |
206 compact_list: a list generated by GenerateAllMediaInfosInCompactForm. | |
207 target: a target nickname string to look for. | |
208 | |
209 Returns: | |
210 A tuple (info, url, link, tag, is_video, nickname) where nickname is | |
dennis_jeffrey
2011/03/18 23:51:41
Since this function only returns (url, nickname, t
imasaki1
2011/03/19 01:00:38
Done.
| |
211 a target string. | |
212 url: URL of the video/audio. | |
213 tag: HTML5 tag for the video/audio. | |
214 nickname: nickname of the video/audio for presentation | |
dennis_jeffrey
2011/03/18 23:51:41
Swap the "nickname" and "tag" lines to match the o
imasaki1
2011/03/19 01:00:38
Done.
| |
215 (such as bear.webm). | |
216 """ | |
217 for url, nickname, tag in compact_list: | |
218 if target == nickname: | |
219 return (url, nickname, tag) | |
OLD | NEW |