video_stream.py
Go to the documentation of this file.
1 import threading
2 from . protocol import *
3 
4 
5 class VideoStream(object):
6  def __init__(self, drone):
7  self.drone = drone
8  self.log = drone.log
9  self.cond = threading.Condition()
10  self.queue = []
11  self.closed = False
12  self.prev_video_data = None
14  self.ignore_packets = 0
15  self.name = 'VideoStream'
16  drone.subscribe(drone.EVENT_CONNECTED, self.__handle_event)
17  drone.subscribe(drone.EVENT_DISCONNECTED, self.__handle_event)
18  drone.subscribe(drone.EVENT_VIDEO_DATA, self.__handle_event)
19 
20  def read(self, size):
21  self.cond.acquire()
22  try:
23  if len(self.queue) == 0 and not self.closed:
24  self.cond.wait(5.0)
25  data = bytes()
26  while 0 < len(self.queue) and len(data) + len(self.queue[0]) < size:
27  data = data + self.queue[0]
28  del self.queue[0]
29  finally:
30  self.cond.release()
31  # returning data of zero length indicates end of stream
32  self.log.debug('%s.read(size=%d) = %d' % (self.name, size, len(data)))
33  return data
34 
35  def seek(self, offset, whence):
36  self.log.info('%s.seek(%d, %d)' % (str(self.name), offset, whence))
37  return -1
38 
39  def __handle_event(self, event, sender, data):
40  if event is self.drone.EVENT_CONNECTED:
41  self.log.info('%s.handle_event(CONNECTED)' % (self.name))
42  elif event is self.drone.EVENT_DISCONNECTED:
43  self.log.info('%s.handle_event(DISCONNECTED)' % (self.name))
44  self.cond.acquire()
45  self.queue = []
46  self.closed = True
47  self.cond.notifyAll()
48  self.cond.release()
49  elif event is self.drone.EVENT_VIDEO_DATA:
50  self.log.debug('%s.handle_event(VIDEO_DATA, size=%d)' % (self.name, len(data)))
51  video_data = VideoData(data)
52  if 0 < video_data.gap(self.prev_video_data):
53  self.wait_first_packet_in_frame = True
54 
55  self.prev_video_data = video_data
56  if self.wait_first_packet_in_frame and byte(data[1]) != 0:
57  self.ignore_packets += 1
58  return
60  self.log.debug('%s.handle_event(VIDEO_DATA): ignore %d packets' %
61  (self.name, self.ignore_packets))
62  self.ignore_packets = 0
63  self.wait_first_packet_in_frame = False
64 
65  self.cond.acquire()
66  self.queue.append(data[2:])
67  self.cond.notifyAll()
68  self.cond.release()
def __handle_event(self, event, sender, data)
Definition: video_stream.py:39


tello_driver
Author(s): Jordy van Appeven
autogenerated on Wed May 13 2020 03:34:54