2013-12-31 10:45:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Copyright (c) 2013,Thibault Saunier <thibault.saunier@collabora.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this program; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
# Boston, MA 02110-1301, USA.
|
|
|
|
""" Some utilies. """
|
|
|
|
|
|
|
|
import os
|
2014-01-09 08:14:27 +00:00
|
|
|
import urllib
|
2014-01-14 17:05:45 +00:00
|
|
|
import loggable
|
2014-01-09 08:14:27 +00:00
|
|
|
import urlparse
|
2014-01-09 14:23:38 +00:00
|
|
|
import subprocess
|
2014-01-09 08:14:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
GST_SECOND = 1000000000
|
2014-01-09 08:39:05 +00:00
|
|
|
DEFAULT_TIMEOUT = 10
|
|
|
|
DEFAULT_GST_QA_ASSETS = os.path.join(os.path.expanduser('~'), "Videos",
|
|
|
|
"gst-qa-assets")
|
2014-01-09 14:23:38 +00:00
|
|
|
DISCOVERER_COMMAND = "gst-discoverer-1.0"
|
|
|
|
DURATION_TOLERANCE = GST_SECOND / 2
|
2013-12-31 10:45:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Result(object):
|
|
|
|
NOT_RUN = "Not run"
|
|
|
|
FAILED = "Failed"
|
|
|
|
TIMEOUT = "Timeout"
|
|
|
|
PASSED = "Passed"
|
|
|
|
|
|
|
|
|
|
|
|
class Colors(object):
|
|
|
|
HEADER = '\033[95m'
|
|
|
|
OKBLUE = '\033[94m'
|
|
|
|
OKGREEN = '\033[92m'
|
|
|
|
WARNING = '\033[93m'
|
|
|
|
FAIL = '\033[91m'
|
|
|
|
ENDC = '\033[0m'
|
|
|
|
|
|
|
|
|
2014-01-10 09:27:25 +00:00
|
|
|
def desactivate_colors():
|
2013-12-31 10:45:07 +00:00
|
|
|
Colors.HEADER = ''
|
|
|
|
Colors.OKBLUE = ''
|
|
|
|
Colors.OKGREEN = ''
|
|
|
|
Colors.WARNING = ''
|
|
|
|
Colors.FAIL = ''
|
|
|
|
Colors.ENDC = ''
|
|
|
|
|
|
|
|
|
|
|
|
def mkdir(directory):
|
|
|
|
try:
|
|
|
|
os.makedirs(directory)
|
|
|
|
except os.error:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-01-09 14:17:53 +00:00
|
|
|
def which(name):
|
|
|
|
result = []
|
|
|
|
exts = filter(None, os.environ.get('PATHEXT', '').split(os.pathsep))
|
|
|
|
path = os.environ.get('PATH', None)
|
|
|
|
if path is None:
|
|
|
|
return []
|
|
|
|
for p in os.environ.get('PATH', '').split(os.pathsep):
|
|
|
|
p = os.path.join(p, name)
|
|
|
|
if os.access(p, os.X_OK):
|
|
|
|
result.append(p)
|
|
|
|
for e in exts:
|
|
|
|
pext = p + e
|
|
|
|
if os.access(pext, os.X_OK):
|
|
|
|
result.append(pext)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2014-01-15 15:11:39 +00:00
|
|
|
def get_color_for_result(result):
|
|
|
|
if result is Result.FAILED:
|
|
|
|
color = Colors.FAIL
|
|
|
|
elif result is Result.TIMEOUT:
|
|
|
|
color = Colors.WARNING
|
|
|
|
elif result is Result.PASSED:
|
|
|
|
color = Colors.OKGREEN
|
|
|
|
else:
|
|
|
|
color = Colors.OKBLUE
|
|
|
|
|
|
|
|
return color
|
|
|
|
|
|
|
|
|
2014-01-09 14:17:53 +00:00
|
|
|
def printc(message, color="", title=False):
|
2013-12-31 10:45:07 +00:00
|
|
|
if title:
|
2014-01-09 10:14:19 +00:00
|
|
|
length = 0
|
|
|
|
for l in message.split("\n"):
|
|
|
|
if len(l) > length:
|
|
|
|
length = len(l)
|
|
|
|
if length == 0:
|
|
|
|
length = len(message)
|
|
|
|
message = length * '=' + "\n" + str(message) + "\n" + length * '='
|
|
|
|
|
2013-12-31 10:45:07 +00:00
|
|
|
if hasattr(message, "result") and color == '':
|
2014-01-15 15:11:39 +00:00
|
|
|
color = get_color_for_result(message.result)
|
2013-12-31 10:45:07 +00:00
|
|
|
|
|
|
|
print color + str(message) + Colors.ENDC
|
|
|
|
|
|
|
|
|
|
|
|
def launch_command(command, color=None):
|
|
|
|
printc(command, Colors.OKGREEN, True)
|
2013-12-31 10:45:07 +00:00
|
|
|
os.system(command)
|
2014-01-09 08:14:27 +00:00
|
|
|
|
2014-01-09 14:24:52 +00:00
|
|
|
|
2014-01-09 08:14:27 +00:00
|
|
|
def path2url(path):
|
|
|
|
return urlparse.urljoin('file:', urllib.pathname2url(path))
|
|
|
|
|
|
|
|
|
2014-01-10 09:12:13 +00:00
|
|
|
def url2path(url):
|
|
|
|
return urlparse.urlparse(url).path
|
|
|
|
|
|
|
|
|
|
|
|
def isuri(string):
|
|
|
|
url = urlparse.urlparse(string)
|
|
|
|
if url.scheme != "" and url.scheme != "":
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2014-01-09 08:14:27 +00:00
|
|
|
##############################
|
|
|
|
# Encoding related utils #
|
|
|
|
##############################
|
|
|
|
class MediaFormatCombination(object):
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "%s and %s in %s" % (self.acodec, self.vcodec, self.container)
|
|
|
|
|
|
|
|
def __init__(self, container, acodec, vcodec):
|
|
|
|
self.container = container
|
|
|
|
self.acodec = acodec
|
|
|
|
self.vcodec = vcodec
|
|
|
|
|
|
|
|
|
|
|
|
FORMATS = {"aac": "audio/mpeg,mpegversion=4",
|
|
|
|
"ac3": "audio/x-ac3",
|
|
|
|
"vorbis": "audio/x-vorbis",
|
|
|
|
"mp3": "audio/mpeg,mpegversion=1,layer=3",
|
|
|
|
"h264": "video/x-h264",
|
|
|
|
"vp8": "video/x-vp8",
|
|
|
|
"theora": "video/x-theora",
|
|
|
|
"ogg": "application/ogg",
|
|
|
|
"mkv": "video/x-matroska",
|
|
|
|
"mp4": "video/quicktime,variant=iso;",
|
2014-01-09 14:24:52 +00:00
|
|
|
"webm": "video/webm"}
|
2014-01-09 08:14:27 +00:00
|
|
|
|
2014-01-10 14:30:38 +00:00
|
|
|
|
2014-01-09 08:14:27 +00:00
|
|
|
def get_profile_full(muxer, venc, aenc, video_restriction=None,
|
|
|
|
audio_restriction=None,
|
|
|
|
audio_presence=0, video_presence=0):
|
|
|
|
ret = "\""
|
|
|
|
if muxer:
|
|
|
|
ret += muxer
|
|
|
|
ret += ":"
|
|
|
|
if venc:
|
|
|
|
if video_restriction is not None:
|
|
|
|
ret = ret + video_restriction + '->'
|
|
|
|
ret += venc
|
|
|
|
if video_presence:
|
|
|
|
ret = ret + '|' + str(video_presence)
|
|
|
|
if aenc:
|
|
|
|
ret += ":"
|
|
|
|
if audio_restriction is not None:
|
|
|
|
ret = ret + audio_restriction + '->'
|
|
|
|
ret += aenc
|
|
|
|
if audio_presence:
|
|
|
|
ret = ret + '|' + str(audio_presence)
|
|
|
|
|
|
|
|
ret += "\""
|
|
|
|
return ret.replace("::", ":")
|
|
|
|
|
|
|
|
|
|
|
|
def get_profile(combination):
|
|
|
|
return get_profile_full(FORMATS[combination.container],
|
|
|
|
FORMATS[combination.vcodec],
|
|
|
|
FORMATS[combination.acodec],
|
|
|
|
video_restriction="video/x-raw,format=I420")
|
|
|
|
|
|
|
|
##################################################
|
|
|
|
# Some utilities to parse gst-validate output #
|
|
|
|
##################################################
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_position(p):
|
|
|
|
def parse_gsttimeargs(time):
|
|
|
|
return int(time.split(":")[0]) * 3600 + int(time.split(":")[1]) * 60 + int(time.split(":")[2].split(".")[0]) * 60
|
2014-01-09 14:24:52 +00:00
|
|
|
start_stop = p.replace("<position: ", '').replace("/>", "").split(" duration: ")
|
|
|
|
|
|
|
|
if len(start_stop) < 2:
|
|
|
|
loggable.warning("utils", "Got a unparsable value: %s" % p)
|
|
|
|
return 0, 0
|
|
|
|
|
|
|
|
if " speed: "in start_stop[1]:
|
|
|
|
start_stop[1] = start_stop[1].split("speed: ")[0]
|
2014-01-09 08:14:27 +00:00
|
|
|
|
|
|
|
return parse_gsttimeargs(start_stop[0]), parse_gsttimeargs(start_stop[1])
|
|
|
|
|
|
|
|
|
2014-01-24 10:29:50 +00:00
|
|
|
def _parse_buffering(b):
|
|
|
|
return b.split("buffering... ")[1].split("%")[0], 100
|
|
|
|
|
|
|
|
|
2014-01-09 08:14:27 +00:00
|
|
|
def _get_position(test):
|
2014-01-14 17:05:45 +00:00
|
|
|
position = duration = -1
|
2014-01-09 08:14:27 +00:00
|
|
|
|
|
|
|
test.reporter.out.seek(0)
|
|
|
|
m = None
|
2014-01-09 14:24:52 +00:00
|
|
|
for l in reversed(test.reporter.out.readlines()):
|
|
|
|
l = l.lower()
|
2014-01-24 10:29:50 +00:00
|
|
|
if "<position:" in l or "buffering" in l:
|
2014-01-09 08:14:27 +00:00
|
|
|
m = l
|
2014-01-09 14:24:52 +00:00
|
|
|
break
|
2014-01-09 08:14:27 +00:00
|
|
|
|
|
|
|
if m is None:
|
2014-01-14 17:05:45 +00:00
|
|
|
loggable.debug("utils", "Could not fine any positionning info")
|
2014-01-09 08:14:27 +00:00
|
|
|
return position, duration
|
|
|
|
|
|
|
|
for j in m.split("\r"):
|
2014-01-09 14:24:52 +00:00
|
|
|
if j.startswith("<position:") and j.endswith("/>"):
|
2014-01-09 08:14:27 +00:00
|
|
|
position, duration = _parse_position(j)
|
2014-01-24 10:29:50 +00:00
|
|
|
elif j.startswith("buffering") and j.endswith("%"):
|
|
|
|
position, duration = _parse_buffering(j)
|
2014-01-09 08:14:27 +00:00
|
|
|
|
|
|
|
return position, duration
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_position(test, max_passed_stop=0.5):
|
|
|
|
position, duration = _get_position(test)
|
|
|
|
|
|
|
|
if position > duration + max_passed_stop:
|
2014-01-14 17:05:45 +00:00
|
|
|
loggable.warning("utils", "Position > duration -> Returning -1")
|
|
|
|
return -1
|
2014-01-09 08:14:27 +00:00
|
|
|
|
|
|
|
return position
|
|
|
|
|
|
|
|
|
|
|
|
def get_current_size(test):
|
|
|
|
position = get_current_position(test)
|
|
|
|
|
2014-01-14 17:05:45 +00:00
|
|
|
if position is -1:
|
|
|
|
return -1
|
|
|
|
|
|
|
|
size = os.stat(urlparse.urlparse(test.dest_file).path).st_size
|
|
|
|
loggable.debug("utils", "Size: %s" % size)
|
|
|
|
return size
|
2014-01-09 08:14:27 +00:00
|
|
|
|
2014-01-09 14:23:38 +00:00
|
|
|
|
2014-01-09 14:24:52 +00:00
|
|
|
def get_duration(media_file):
|
|
|
|
duration = 0
|
2014-01-14 17:05:45 +00:00
|
|
|
|
2014-01-09 14:24:52 +00:00
|
|
|
def parse_gsttimeargs(time):
|
|
|
|
stime = time.split(":")
|
|
|
|
sns = stime[2].split(".")
|
|
|
|
stime[2] = sns[0]
|
|
|
|
stime.append(sns[1])
|
|
|
|
return (int(stime[0]) * 3600 + int(stime[1]) * 60 + int(stime[2]) * 60) * GST_SECOND + int(stime[3])
|
|
|
|
try:
|
|
|
|
res = subprocess.check_output([DISCOVERER_COMMAND, media_file])
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
# gst-media-check returns !0 if seeking is not possible, we do not care in that case.
|
|
|
|
pass
|
|
|
|
|
|
|
|
for l in res.split('\n'):
|
|
|
|
if "Duration: " in l:
|
|
|
|
duration = parse_gsttimeargs(l.replace("Duration: ", ""))
|
|
|
|
break
|
|
|
|
|
|
|
|
return duration
|
2014-01-09 14:23:38 +00:00
|
|
|
|
2014-01-14 17:05:45 +00:00
|
|
|
|
2014-01-09 14:23:38 +00:00
|
|
|
def compare_rendered_with_original(orig_duration, dest_file, tolerance=DURATION_TOLERANCE):
|
2014-01-09 14:24:52 +00:00
|
|
|
duration = get_duration(dest_file)
|
2014-01-09 14:23:38 +00:00
|
|
|
|
|
|
|
if orig_duration - tolerance >= duration >= orig_duration + tolerance:
|
|
|
|
return (Result.FAILED, "Duration of encoded file is "
|
|
|
|
" wrong (%s instead of %s)" %
|
|
|
|
(orig_duration / GST_SECOND,
|
|
|
|
duration / GST_SECOND),
|
|
|
|
"wrong-duration")
|
|
|
|
else:
|
|
|
|
return (Result.PASSED, "")
|