-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathframe_tag_funnel.py
More file actions
249 lines (208 loc) · 7.99 KB
/
frame_tag_funnel.py
File metadata and controls
249 lines (208 loc) · 7.99 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from collections import deque
from threading import Lock
from typing import Deque, Optional, Tuple
from gst_plugins.python.frame_tag_filter_common import parse_stream_part_event
from savant.gstreamer import GLib, GObject, Gst
from savant.gstreamer.utils import on_pad_event
from savant.utils.log import LoggerMixin
DEFAULT_QUEUE_SIZE = 50
SINK_TAGGED_PAD_TEMPLATE = Gst.PadTemplate.new(
'sink_tagged',
Gst.PadDirection.SINK,
Gst.PadPresence.ALWAYS,
Gst.Caps.new_any(),
)
SINK_NOT_TAGGED_PAD_TEMPLATE = Gst.PadTemplate.new(
'sink_not_tagged',
Gst.PadDirection.SINK,
Gst.PadPresence.ALWAYS,
Gst.Caps.new_any(),
)
SRC_PAD_TEMPLATE = Gst.PadTemplate.new(
'src',
Gst.PadDirection.SRC,
Gst.PadPresence.ALWAYS,
Gst.Caps.new_any(),
)
class FrameTagFunnel(LoggerMixin, Gst.Element):
"""Frame tag funnel.
Funnels frames after "frame_tag_filter" element back to
a single stream with the original order.
"""
GST_PLUGIN_NAME = 'frame_tag_funnel'
__gstmetadata__ = (
'Frame tag funnel',
'Muxer',
'Funnels frames after "frame_tag_filter" element back to '
'a single stream with the original order.',
'Pavel Tomskikh <[email protected]>',
)
__gsttemplates__ = (
SINK_TAGGED_PAD_TEMPLATE,
SINK_NOT_TAGGED_PAD_TEMPLATE,
SRC_PAD_TEMPLATE,
)
__gproperties__ = {
'queue-size': (
GObject.TYPE_UINT,
'Max number of frames in a queue.',
'Max number of frames in a queue.',
1,
GLib.MAXUINT,
DEFAULT_QUEUE_SIZE,
GObject.ParamFlags.READWRITE,
),
}
def __init__(self):
super().__init__()
# properties
self.max_queue_size = DEFAULT_QUEUE_SIZE
# ((part_pad, part_id), part_source_pad)
self.pending_parts: Deque[Tuple[Tuple[Gst.Pad, int], Gst.Pad]] = deque()
self.buffers: Deque[Deque[Gst.Buffer]] = deque()
self.queue_size = 0
self.current_pad: Optional[Gst.Pad] = None
self.stream_lock = Lock()
self.sink_pad_tagged: Gst.Pad = Gst.Pad.new_from_template(
SINK_TAGGED_PAD_TEMPLATE, 'sink_tagged'
)
self.sink_pad_not_tagged: Gst.Pad = Gst.Pad.new_from_template(
SINK_NOT_TAGGED_PAD_TEMPLATE, 'sink_not_tagged'
)
self.received_eos = {
self.sink_pad_tagged: False,
self.sink_pad_not_tagged: False,
}
self.pending_events = {
self.sink_pad_tagged: 0,
self.sink_pad_not_tagged: 0,
}
self.src_pad: Gst.Pad = Gst.Pad.new_from_template(SRC_PAD_TEMPLATE, 'src')
self.add_pad(self.sink_pad_tagged)
self.add_pad(self.sink_pad_not_tagged)
self.add_pad(self.src_pad)
self.sink_pad_tagged.set_chain_function_full(self.handle_buffer)
self.sink_pad_not_tagged.set_chain_function_full(self.handle_buffer)
event_handlers = {
Gst.EventType.EOS: self.on_eos,
Gst.EventType.CUSTOM_DOWNSTREAM: self.on_custom_event,
}
self.sink_pad_not_tagged.add_probe(
Gst.PadProbeType.EVENT_DOWNSTREAM,
on_pad_event,
event_handlers,
)
self.sink_pad_tagged.add_probe(
Gst.PadProbeType.EVENT_DOWNSTREAM,
on_pad_event,
event_handlers,
)
def do_get_property(self, prop):
"""Get property callback."""
if prop.name == 'queue-size':
return self.max_queue_size
raise AttributeError(f'Unknown property {prop.name}')
def do_set_property(self, prop, value):
"""Set property callback."""
self.logger.debug('Setting property "%s" to "%s".', prop.name, value)
if prop.name == 'queue-size':
self.max_queue_size = value
else:
raise AttributeError(f'Unknown property {prop.name}')
def handle_buffer(
self,
sink_pad: Gst.Pad,
element: Gst.Element,
buffer: Gst.Buffer,
) -> Gst.FlowReturn:
"""Handle buffer from a sink pad.
Either pass the buffer to the src pad or store it in the buffer queue
for keeping the original order.
"""
self.logger.debug(
'Received buffer PTS=%s from %s', buffer.pts, sink_pad.get_name()
)
with self.stream_lock:
if sink_pad == self.current_pad:
return self.push_buffer(buffer)
else:
self.buffers[-1].append(buffer)
self.queue_size += 1
if self.queue_size < self.max_queue_size:
return Gst.FlowReturn.OK
for part_buffers in self.buffers:
if self.queue_size < self.max_queue_size:
return Gst.FlowReturn.OK
buffer = part_buffers.popleft()
self.queue_size -= 1
ret = self.push_buffer(buffer)
if ret != Gst.FlowReturn.OK:
self.logger.error(
'Failed to push buffer %s: %s.',
buffer.pts,
ret,
)
return ret
return Gst.FlowReturn.OK
def on_eos(self, pad: Gst.Pad, event: Gst.Event):
"""Handle EOS event from a sink pad.
Pass EOS to the src pad only when received from both sink pads.
"""
self.logger.info('Got EOS from %s', pad.get_name())
with self.stream_lock:
self.received_eos[pad] = True
if all(self.received_eos.values()):
self.logger.info('Pass EOS from %s', pad.get_name())
while self.buffers:
for buffer in self.buffers.popleft():
self.push_buffer(buffer)
self.queue_size -= 1
return Gst.PadProbeReturn.PASS
else:
self.logger.info('Drop EOS from %s', pad.get_name())
return Gst.PadProbeReturn.DROP
def on_custom_event(self, sink_pad: Gst.Pad, event: Gst.Event):
"""Handle stream-part event from a sink pad."""
self.logger.debug('Got CUSTOM_DOWNSTREAM event from %s', sink_pad.get_name())
parsed_event = parse_stream_part_event(event)
if parsed_event is None:
return Gst.PadProbeReturn.PASS
part_id, tagged = parsed_event
self.logger.debug(
'Got stream-part event from %s. Part ID: %s, tagged: %s.',
sink_pad.get_name(),
part_id,
tagged,
)
branch_pad = self.sink_pad_tagged if tagged else self.sink_pad_not_tagged
part = (branch_pad, part_id)
with self.stream_lock:
if self.pending_parts and self.pending_parts[0][0] == part:
(next_part_pad, _), part_source_pad = self.pending_parts.popleft()
self.pending_events[part_source_pad] -= 1
for buffer in self.buffers.popleft():
self.push_buffer(buffer)
self.queue_size -= 1
if self.pending_events[next_part_pad] == 0:
self.current_pad = next_part_pad
self.logger.debug(
'Switched current pad to %s', self.current_pad.get_name()
)
else:
if self.current_pad == sink_pad:
self.current_pad = None
self.pending_parts.append((part, sink_pad))
self.pending_events[sink_pad] += 1
self.buffers.append(deque())
return Gst.PadProbeReturn.DROP
def push_buffer(self, buffer: Gst.Buffer):
"""Push buffer to the src pad."""
self.logger.debug('Pushing buffer PTS=%s', buffer.pts)
return self.src_pad.push(buffer)
# register plugin
GObject.type_register(FrameTagFunnel)
__gstelementfactory__ = (
FrameTagFunnel.GST_PLUGIN_NAME,
Gst.Rank.NONE,
FrameTagFunnel,
)