| 1 | After verifying that the simple streamload client works as expected, the experimenter uses the [https://pypi.python.org/pypi/oml4py/ oml4py] library to instrument the script. The experimenter set up three ''measurement points'' in the script: |
| 2 | |
| 3 | * ''conf'' - at initialization, the client reports its own configuration settings |
| 4 | * ''dl'' - the client reports the chunk number and layer number each time a subchunk is downloaded |
| 5 | * ''play'' - the client reports the chunk number and the cumulative number of layers that are played each time a chunk is played |
| 6 | |
| 7 | |
| 8 | {{{ |
| 9 | #!python |
| 10 | #!/usr/bin/python |
| 11 | |
| 12 | import sys, time, httplib2, math, thread, oml4py |
| 13 | from xml.dom.minidom import parse, parseString |
| 14 | from optparse import OptionParser |
| 15 | |
| 16 | class Finished(Exception): |
| 17 | def __init__(self, value): |
| 18 | self.value = value |
| 19 | def __str__(self): |
| 20 | return repr(self.value) |
| 21 | |
| 22 | class Wait(Exception): |
| 23 | def __init__(self, value): |
| 24 | self.value = value |
| 25 | def __str__(self): |
| 26 | return repr(self.value) |
| 27 | |
| 28 | class StreamLoad: |
| 29 | |
| 30 | def __init__(self): |
| 31 | self.oml = oml4py.OMLBase("streamload") |
| 32 | self.oml.addmp("dl", "chunk:long layer:long") |
| 33 | self.oml.addmp("conf", "key:string value:string") |
| 34 | self.oml.addmp("play", "chunk:long layers:long") |
| 35 | |
| 36 | def getConfig(self, baseurl, video): |
| 37 | self.video = video |
| 38 | resp, content = httplib2.Http().request(baseurl + video + ".xml") |
| 39 | doc = parseString(content) |
| 40 | for node in doc.getElementsByTagName('video'): |
| 41 | self.buf = int(node.attributes["buffer"].value) |
| 42 | self.chunks = int(node.attributes["chunks"].value) |
| 43 | self.layers = int(node.attributes["layers"].value) |
| 44 | self.urlbase = str(baseurl) + str(node.attributes["url"].value) |
| 45 | self.chunktime = float(node.attributes["chunktime"].value) |
| 46 | |
| 47 | def player(self): |
| 48 | startbuf = 0 |
| 49 | self.played = 0 |
| 50 | start = self.starttime |
| 51 | while self.played<self.chunks: |
| 52 | thistime = time.time() |
| 53 | if thistime-start >= self.chunktime and self.downloaded[0]>self.played: |
| 54 | maxlayer = -1 |
| 55 | for i in range(self.layers): |
| 56 | if self.downloaded[i]>=self.played+1: |
| 57 | maxlayer = i |
| 58 | self.oml.inject("play", [self.played+1, maxlayer]) |
| 59 | self.played+=1 |
| 60 | start = thistime |
| 61 | time.sleep(0.001) |
| 62 | |
| 63 | |
| 64 | def start(self, window, sl): |
| 65 | self.sl=sl |
| 66 | if self.sl: |
| 67 | self.window = int(window) |
| 68 | else: |
| 69 | self.window = int(self.buf) |
| 70 | |
| 71 | self.oml.start() |
| 72 | self.oml.inject("conf", ["video", str(self.video)]) |
| 73 | self.oml.inject("conf", ["chunks", str(self.chunks)]) |
| 74 | self.oml.inject("conf", ["layers", str(self.layers)]) |
| 75 | self.oml.inject("conf", ["chunktime", str(self.chunktime)]) |
| 76 | self.oml.inject("conf", ["buffer", str(self.buf)]) |
| 77 | self.oml.inject("conf", ["window", str(self.window)]) |
| 78 | self.oml.inject("conf", ["url", str(self.urlbase)]) |
| 79 | self.oml.inject("conf", ["sl", str(sl)]) |
| 80 | |
| 81 | self.downloaded = {} |
| 82 | for i in range(self.layers): |
| 83 | self.downloaded[i] = 0 |
| 84 | self.played = 0 |
| 85 | self.starttime = 0 |
| 86 | self.getNextPiece() |
| 87 | self.starttime = time.time() |
| 88 | self.t = thread.start_new_thread(self.player,()) |
| 89 | |
| 90 | while True: |
| 91 | try: |
| 92 | self.getNextPiece() |
| 93 | except Wait: |
| 94 | pass |
| 95 | |
| 96 | def nextUp(self): |
| 97 | # Return chunk 1, layer 0 if we haven't downloaded anything yet |
| 98 | if self.starttime == 0: |
| 99 | return(1,0) |
| 100 | base_chunks_played = self.played |
| 101 | if base_chunks_played == self.chunks: |
| 102 | raise Finished("all done here") |
| 103 | # Download base layer if we are allowed to |
| 104 | if (self.downloaded[0] - base_chunks_played) < self.buf and (self.downloaded[0] + 1) <= self.chunks: |
| 105 | return (self.downloaded[0]+1,0) |
| 106 | else: |
| 107 | for i in range(self.layers-1): |
| 108 | i = i+1 |
| 109 | tmp = max(self.downloaded[i],base_chunks_played+1) |
| 110 | # If it's in the window and not past the end of the video, download it |
| 111 | if tmp - base_chunks_played <= self.window and tmp + 1 <= self.chunks: |
| 112 | return (tmp + 1,i) |
| 113 | # After all subchunks in the quality windows are downloaded, request one chunk at a time, lower layers first |
| 114 | for i in range(self.layers-1): |
| 115 | i = i + 1 |
| 116 | # If I am the highest layer and I have less than the layer beneath me, get the next chunk in my layer |
| 117 | if self.sl and i == self.layers-1 and self.downloaded[i] <= self.downloaded[i-1] and self.downloaded[i]+1<=self.chunks: |
| 118 | return (self.downloaded[i]+1,i) |
| 119 | # Otherwise, if I have less than or the same as the layer above me, download the next chunk |
| 120 | if self.sl and i < self.layers-1 and self.downloaded[i] <= self.downloaded[i+1] and self.downloaded[i]+1<=self.chunks: |
| 121 | return (self.downloaded[i]+1,i) |
| 122 | raise Wait("Nothing to do") |
| 123 | |
| 124 | |
| 125 | |
| 126 | def getNextPiece(self): |
| 127 | try: |
| 128 | (chunk, layer) = self.nextUp() |
| 129 | except Wait: |
| 130 | return |
| 131 | nxt = "chunk" + str(chunk) + "_" + str(layer) + ".264" |
| 132 | try: |
| 133 | resp, content = httplib2.Http().request(self.urlbase + nxt) |
| 134 | except: |
| 135 | print "Error downloading file" |
| 136 | if chunk > self.played or chunk==self.chunks: |
| 137 | try: |
| 138 | self.lastpiecetime = time.time() |
| 139 | self.oml.inject("dl", [chunk, layer]) |
| 140 | except: |
| 141 | return |
| 142 | self.downloaded[layer] = chunk |
| 143 | |
| 144 | |
| 145 | if __name__ == "__main__": |
| 146 | |
| 147 | parser = OptionParser() |
| 148 | parser.add_option("-u", "--url", dest="url", |
| 149 | help="URL of video source") |
| 150 | parser.add_option("-v", "--video", dest="video", |
| 151 | help="Name of video") |
| 152 | parser.add_option("-w", "--window", dest="window", default="5", |
| 153 | help="Download window for enhancement layers") |
| 154 | parser.add_option("-s", "--streamload", dest = "sl", default="True", |
| 155 | help="Download in streamload mode") |
| 156 | |
| 157 | (options, args) = parser.parse_args() |
| 158 | |
| 159 | x = StreamLoad() |
| 160 | x.getConfig(options.url,options.video) |
| 161 | try: |
| 162 | if options.sl == "True": |
| 163 | x.start(int(options.window),True) |
| 164 | else: |
| 165 | x.start(int(options.window),False) |
| 166 | except Finished: |
| 167 | sys.exit(0) |
| 168 | |
| 169 | }}} |