-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainers.py
198 lines (152 loc) · 5.91 KB
/
containers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# -*- coding: utf-8 -*-
import ctypes
from abc import ABC, abstractmethod
from collections import deque
import callbacks as cb
from constants import CONFIG_BUFFER_SIZE, CONFIG_BUFFER_STREAM
from config import cfg
class StreamContainer(ABC):
"""An abstract class representing stream data. This class exposes the
neccesary functions for libVLC playback.
Args:
vlc_instance (libvlc_instance_t*): The vlc instance.
stream_info (dict): Holds information about stream url and stream quality.
Attributes:
media (libvlc_media_t*): The media object that vlc uses, includes callbacks.
"""
def __init__(self, vlc_instance):
# Cast this container to a c pointer to use in the callbacks
self._opaque = ctypes.cast(ctypes.pointer(
ctypes.py_object(self)), ctypes.c_void_p)
# Create the vlc callbacks, these will in turn call the methods defined
# in this container
self.media = vlc_instance.media_new_callbacks(
cb.CALLBACKS["read"],
cb.CALLBACKS["open"],
cb.CALLBACKS["seek"],
cb.CALLBACKS["close"],
self._opaque
)
@abstractmethod
def open(self):
"""The open event is triggered after media_open_cb in libVLC."""
pass
@abstractmethod
def read(self, buf, length):
"""Read reads from the stream and writes this to the buf according to
the length parameter.
Args:
buf (LP_c_char): char pointer to the video buffer.
length: amount that should be read from the buffer.
"""
pass
@abstractmethod
def seek(self, offset):
"""Seeks in the stream according to the offset.
Args:
offset: absolute byte offset to seek to in the media.
"""
pass
@abstractmethod
def close(self):
"""Close calls all neccesary functions to close down the container."""
pass
class LiveStreamContainer(StreamContainer):
"""This class representas a **live** stream and contains all information
regarding it's media.
The LiveStreamContainer pulls all of it's video data directly from the
livestream itself, while at the same time caching away previous data in a
buffer.
Add attribute on_stream_end() to bind a callback for when the stream has ended.
Note: Do not try to remove this Container in that callback, as it will not work.
"""
def __init__(self, vlc_instance, url, streams, quality, buffer_length=cfg[CONFIG_BUFFER_SIZE]):
super().__init__(vlc_instance)
# Use default value for buffer_length if none specified
if not buffer_length:
buffer_length = cfg[CONFIG_BUFFER_SIZE]
self.streams = streams
self.stream = self.streams[quality].open()
self.buffer = deque(maxlen=buffer_length)
self.update_info(url, quality)
def open(self):
"""Called by libVLC upon opening the media. Not currently used."""
return 0
def read(self, buf, length):
"""Called by libVLC upon requesting more data.
Reads 'length' video data directly from the stream, as well as caches
it away in the buffer accordingly.
"""
data = self.stream.read(length)
if cfg[CONFIG_BUFFER_STREAM]:
self.buffer.append(data)
data_len = len(data)
# if the stream has ended invoke on_stream_end
if data_len == 0:
try:
self.on_stream_end()
except AttributeError:
pass
for i, val in enumerate(data):
buf[i] = val
return len(data)
def seek(self, offset):
"""Called by libVLC upon seeking in the media."""
return 0
def close(self):
"""Called by libVLC upon closing the media."""
self.stream.close()
return 0
@staticmethod
def quality_options(streams):
return sorted(streams.keys())
def update_info(self, url, quality):
"""Updates the current quality as well as available qualities of
the stream.
"""
self.url = url
self.quality = quality
self.all_qualities = LiveStreamContainer.quality_options(self.streams)
def change_stream_quality(self, quality):
"""Changes the streams quality."""
self.stream.close()
self.stream = self.streams[quality].open()
self.buffer.clear()
self.quality = quality
def refresh(self):
self.change_stream_quality(self.quality)
class RewoundStreamContainer(StreamContainer):
"""This class represents a **rewound** stream and contains all information
regarding it's media.
The RewoundStreamContainer takes another streams buffer, copies that and
pulls all of it's video data directly from the copied buffer.
"""
def __init__(self, vlc_instance, stream_buffer):
super().__init__(vlc_instance)
self.buffer = list(stream_buffer)
self.curr = 0
def open(self):
"""Called by libVLC upon opening the media. Not currently used."""
return 0
def read(self, buf, length):
"""Called by libVLC upon requesting more data.
Reads 'length' video data directly from the copied buffer.
"""
# If we have no data to read HACK:
if self.curr >= len(self.buffer):
# Returning an empty buffer somehow forces vlc to loop
return length
data = self.buffer[self.curr]
for i, val in enumerate(data):
if i < length:
buf[i] = val
self.curr = (self.curr + 1)
return min(len(data), length)
def seek(self, offset):
"""Called by libVLC upon seeking in the media."""
# Set the current pointer to the correct location
self.curr = int((offset / 2 ** 62) * self.curr)
return 0
def close(self):
"""Called by libVLC upon closing the media."""
return 0