|
| 1 | +from threading import Event |
| 2 | + |
| 3 | +from .output import Output |
| 4 | + |
| 5 | + |
| 6 | +class SplittableOutput(Output): |
| 7 | + """ |
| 8 | + The SplittableOutput passes the encoded bitstream to another output (or drops them if there isn't one). |
| 9 | +
|
| 10 | + It can be told to "split" the current output that it's been writing to, to another one. This means |
| 11 | + it switches seamlessly from the current output, which is closed, to a new one, without dropping |
| 12 | + any frames. By default, it performs the switch at a video keyframem though it can be told not to |
| 13 | + wait for one (by setting wait_for_keyframe to False). |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, output=None, *args, **kwargs): |
| 17 | + super().__init__(*args, **kwargs) |
| 18 | + self._output = output |
| 19 | + self._new_output = None |
| 20 | + self._split_done = Event() |
| 21 | + self._streams = [] |
| 22 | + |
| 23 | + def split_output(self, new_output, wait_for_keyframe=True): |
| 24 | + """ |
| 25 | + Terminate the current output, switching seamlessly to the new one. |
| 26 | +
|
| 27 | + If wait_for_keyframe is True, the switch only occurs when the next video keyframe is |
| 28 | + seen. Otherwise the switch happens immediately. |
| 29 | +
|
| 30 | + THe function returns only when the switch has happened, which may entail waiting for a |
| 31 | + video keyframe, and the old output has been closed. |
| 32 | + """ |
| 33 | + old_output = self._output |
| 34 | + # Start the new outoput in this thread, then schedule outputframe to make the switch. |
| 35 | + new_output.start() |
| 36 | + for encoder_stream, codec, kwargs in self._streams: |
| 37 | + new_output._add_stream(encoder_stream, codec, **kwargs) |
| 38 | + self._wait_for_keyframe = wait_for_keyframe |
| 39 | + self._new_output = new_output |
| 40 | + # Wait for the switch-over to happen, and close the old output in this thread too. |
| 41 | + self._split_done.wait() |
| 42 | + self._split_done.clear() |
| 43 | + if old_output: |
| 44 | + old_output.stop() |
| 45 | + |
| 46 | + def outputframe(self, frame, keyframe=True, timestamp=None, packet=None, audio=False): |
| 47 | + # Audio frames probably always say they're keyframes, but we must wait for a video one. |
| 48 | + if self._new_output and (not self._wait_for_keyframe or (not audio and keyframe)): |
| 49 | + self._split_done.set() |
| 50 | + # split_output will close the old output. |
| 51 | + self._output = self._new_output |
| 52 | + self._new_output = None |
| 53 | + if self._output: |
| 54 | + self._output.outputframe(frame, keyframe, timestamp, packet, audio) |
| 55 | + |
| 56 | + def start(self): |
| 57 | + super().start() |
| 58 | + if self._output: |
| 59 | + self._output.start() |
| 60 | + |
| 61 | + def stop(self): |
| 62 | + super().stop() |
| 63 | + if self._output: |
| 64 | + self._output.stop() |
| 65 | + |
| 66 | + def _add_stream(self, encoder_stream, codec_name, **kwargs): |
| 67 | + # The underlying output may need to know what streams it's dealing with, so we must |
| 68 | + # remember them. |
| 69 | + self._streams.append((encoder_stream, codec_name, kwargs)) |
| 70 | + # Forward immediately to the output if we were given one initially. |
| 71 | + if self._output: |
| 72 | + self._output._add_stream(encoder_stream, codec_name, **kwargs) |
0 commit comments