Changes between Initial Version and Version 1 of GEC16Agenda/WiMAX-Tutorial/Streamload/01


Ignore:
Timestamp:
03/17/13 20:13:00 (11 years ago)
Author:
Fraida Fund
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • GEC16Agenda/WiMAX-Tutorial/Streamload/01

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