Improve stream robustness by always retrying in the worker on failure, rather than only when
keepalive is enabled.
This will make cloud cameras like nest more robust, since they have a tendency to be flaky. This
is also needed to improve client side retry behavior because when the client attempts to retry,
the stream token is already revoked because the worker stopped.
The worker will still idle timeout if no streams are present, so it won't go on forever if no
frontend is viewing the stream.
This refactoring was pulled out of https://github.com/home-assistant/core/pull/53676 as an
initial step towards reverting the addition of the SegmentBuffer class, which will be
unrolled back into a for loop.
The StreamState class holds the persistent state in stream that is used across stream worker
instantiations, e.g. state across a retry or url expiration, which primarily handles
discontinuities. By itself, this PR is not a large win until follow up PRs further simplify
the SegmentBuffer class.
* Add timeouts on recving packets
Add a timeout when recving packets from the worker thread in case it hangs.
Add an exit condition just in case the while loop goes on forever.
* Add a timeout to recorder thread join.
* Wait for recorder thread to be invoked in tests
Remove the while loop and instead wait for segments to be produced by the background worker thread.
* Allow worker to resume before stopping to fix timeouts
* Lower test timeout further
* Remove test_stream_ended since it is flaky
This test doesn't really add additional value on top of other tests.
* Remove dependencies on keepalive from StremaOutput and stream_worker
Pull logic from StreamOutput and stream_worker into the Stream
class, unifying keepalive and idle timeout logic. This prepares
for future changes to preserve hls state across stream url changes.