Example #1
0
 def set_location(self, value):        
     value = value.strip()
     if value != self.location:
         self.location = value
         self.index = 0
         if self.flow != None: #not the first time
             self.flow.stop()
         
         self.flow = FlowConsumerThread(Id=self.Id, name=self.location, size_fix=self.chunksize, size_offset=self.size_offset, window_fix=self.window_fix, rtt_fix=self.rtt_fix)
         self.flow.start()
         log.info("we start a new flow")
Example #2
0
class NDNSrc(threading.Thread):

    def __init__(self, chunksize=None,size_offset=0, window_fix=None, rtt_fix=None):
        threading.Thread.__init__(self)
        #gobject.GObjectMeta.__init__(self)
        
        #gst.Element.__init__(self)
        #gst.URIHandler.__init__(self)
        self.location = None#"/h243/chunksize/dir/c.mp3"
        
        
        self.flow = None
        self.chunksize = chunksize
        self.size_offset = size_offset
        self.window_fix =window_fix
        self.rtt_fix = rtt_fix
        self.Id = "win%s-chunksize%s-rtt%s" %(self.window_fix, self.chunksize, self.rtt_fix)
        #print self.ndn_name, type(self.ndn_name)
        
        
#         self.connect("need-data", self.need_data)
#         self.connect("enough-data", self.enough_data)
#         self.connect("seek-data", self.seed_data)
#         self.connect("end-of-stream", self.end_of_stream)

        #    def __init__(self, Id, ndn_name, fout=None, monitor_out_dir="output", enable_monitor=True, size_fix=None, window_fix=None, rtt_fix=None,
        #          packet_max_data_size=ETH_MTU-IP_HEADER_SIZE-UDP_HEADER_SIZE):
        
        self.index = 0
        self.first_run = True
        self.is_end = False

        
    def sample(self):
        if self.flow == None:
            return None, None, None, None, None
        OptimalChunkSizes, PacketLossRates, CongestionWindowSizes, Rtos, TimeCost = self.flow.summary()
        
        timecost = TimeCost[0]
        #assert type(timecost) == int, "wrong type: %s" %(type(timecost))
        
        if self.first_run:
            self.first_run = False
            if timecost == 0:
                temp = 0
            else:
                temp = self.flow.mydata.accept_bytes/float(timecost)
            self.bitrates = [temp]
            self.goodputs = [self.flow.mydata.accept_bytes]
            accept_raw_bytes, requested_raw_bytes, = self.flow.sample2()
            self.throughputs = [accept_raw_bytes]
            self.requested_sizes = [requested_raw_bytes] 
            
            
        else:
            temp2 = timecost - self.last_time_cost
            if temp2 != 0:
                temp = (self.flow.mydata.accept_bytes-self.last_accept_bytes)/temp2
                
            else:
                temp = 0
            assert temp>=0, "bitrate=%s, accept_byets:%s, last accept bytes:%s, temp2:%s, %s" \
                %(temp, self.flow.mydata.accept_bytes, self.last_accept_bytes, temp2, type(self.last_accept_bytes))
            
            self.bitrates.append(temp)
            self.goodputs.append(self.flow.mydata.accept_bytes)
            
            accept_raw_bytes, requested_raw_bytes, = self.flow.sample2()
            self.throughputs.append(accept_raw_bytes)
            self.requested_sizes.append(requested_raw_bytes)
            
            
        self.last_time_cost = timecost
        self.last_accept_bytes = self.flow.mydata.accept_bytes
        if self.flow.is_all:
            self.is_end = True
            
        return OptimalChunkSizes, PacketLossRates, CongestionWindowSizes, Rtos, self.bitrates, self.goodputs, \
            self.throughputs, self.requested_sizes
    
#     def run(self):
#         self.flow = FlowConsumerThread(Id=self.Id, name=self.location)
#         self.flow.start()
        
    def need_data(self, appsrc, need_bytes):   
        #self.emit("end-of-stream")
        if self.flow == None:
            return
        temp = "all" if self.flow.is_all else "requesting"
        if temp == "all":
            self.is_end = True
            
        name = appsrc.get_name()
        while self.flow.mydata.expected_chunkI <= self.index:
            if self.flow.is_all:
                appsrc.emit('end-of-stream')
                return
            
            time.sleep(1)
            
            
            log.debug("%s waiting data, player next need: %i, NDN expect:%i, total: %s, %s" 
              %(name, self.index, self.flow.mydata.expected_chunkI, self.flow.mydata.satisfied_chunkN, temp))
            
        
        chunkinfo = self.flow.chunkInfos[self.index]
        
        data = chunkinfo.content
        
        
        buffer = gst.Buffer(data)

        """gstream will showo critical message here, since caps is not valid (ANY)
        """
        #buffer.set_caps(caps)
        rst = appsrc.emit("push-buffer", buffer)
        
        
        if rst == gst.FLOW_OK:
            self.index += 1
            rst = ""
            log.debug("%s push-buffer back, player next need: %i, NDN expect:%i, total: %s, %s" 
                  %(name, self.index, self.flow.mydata.expected_chunkI, self.flow.mydata.satisfied_chunkN, temp))
        else:
            log.warn("%s fail to push-buffer back, %s, player next need: %i, NDN expect:%i, total: %s, %s" 
                  %(name, rst, self.index, self.flow.mydata.expected_chunkI, self.flow.mydata.satisfied_chunkN, temp))

    def set_location(self, value):        
        value = value.strip()
        if value != self.location:
            self.location = value
            self.index = 0
            if self.flow != None: #not the first time
                self.flow.stop()
            
            self.flow = FlowConsumerThread(Id=self.Id, name=self.location, size_fix=self.chunksize, size_offset=self.size_offset, window_fix=self.window_fix, rtt_fix=self.rtt_fix)
            self.flow.start()
            log.info("we start a new flow")
    
    def stop(self):
        if self.flow != None:
            self.flow.stop()
        #threading.Thread.__stop(self)
        return 0