Commit Graph

62 Commits (fdf1bfa1407308613814498d71e991ddb323455d)

Author SHA1 Message Date
uvjustin 3693b9bd04
Adjust frag_duration setting in stream (#59135) 2021-11-05 18:54:51 -07:00
uvjustin 9aaa92f366
Improve part metadata in stream (#58822) 2021-10-31 20:23:01 -07:00
uvjustin 35acca1063
Use PyAV fork and set hvc1 codec tag for H.265 (#58309) 2021-10-27 11:04:46 +02:00
uvjustin 06008bc343
Fix EXT-X-PROGRAM-DATE-TIME in stream (#58036)
* Fix EXT-X-PROGRAM-DATE-TIME in stream

* Update fragment duration comments in worker

* Update duration test in worker

* Augment test on low latency playlists

* Reset start_time on discontinuity
2021-10-25 11:33:41 +08:00
Marc Mueller d51487f82a
Import Callable from collections.abc (3) (#56777) 2021-09-29 16:19:06 +02:00
uvjustin 923158cfba
Add ll hls to stream (#49608) 2021-08-29 09:53:41 +08:00
Allen Porter 094f7d38ad
Use buffer at stream start with unsupported audio (#54672)
Add a test that reproduces the issue where resetting the iterator
drops the already read packets. Fix a bug in replace_underlying_iterator
because checking the self._next function turns out not to work since
it points to a bound method so the "is not" check fails.
2021-08-15 21:02:37 -07:00
uvjustin 7a200a5d3b
Fix non monotonic dts error in stream (#53712)
* Use defaultdict for TimestampValidator._last_dts

* Combine filters

* Allow PeekIterator to be updated while preserving buffer

* Fix peek edge case

* Readd is_valid filter to video only iterator
2021-07-30 08:02:33 -07:00
Allen Porter b3b5ee10b1
Fix mypy type for timestamp validator (#53598) 2021-07-27 22:04:59 -07:00
Allen Porter 022ba31999
Refactor the logic for peeking into the start of the stream (#52699)
* Reset dts validator when container is reset

* Reuse existing dts_validator when disabling audio stream

* Refactor peek logic at the start of a stream

Add a PeekingIterator to support rewinding an iterator so that the code
for adjusting audio streams and start pts can be inlined in the worker.

* Simplification and readability improvements

* Remove unnecessary verbiage from comments and pydoc

* Address pylint errors

* Remove rewind function and just mux the first packet separately

* More cleanup after removing rewind()

* Skip check to self._buffer on every iteration
2021-07-27 08:53:42 -07:00
Marc Mueller b5cec353cc
Fix pylint issue with stream component c-extension (#52847)
* Rename 'extension-pkg-whitelist' setting to 'extension-pkg-allow-list'
* Add 'av.stream' and 'av.audio.stream'
* Replace 'Any' type hint
2021-07-10 22:58:37 +02:00
Allen Porter e895b6cd42
Refactor decompression timestamp validation logic in stream component (#52462)
* Refactor dts validation logic into a separate function

Create a decompression timestamp validation function to move the logic out of
the worker into a separate class. This also uses the python itertools.chain
to chain together the initial packets with the remaining packets in the
container iterator, removing additional inline if statements.

* Reset dts validator when container is reset

* Fix typo in a comment

* Reuse existing dts_validator when disabling audio stream
2021-07-07 15:29:15 -07:00
uvjustin e8b5790846
Clean up stream refactor (#51951)
* Clean up target_duration method

* Consolidate Part creation in one place

* Use BytesIO.read instead of memoryview access

* Change flush() signature
2021-06-20 13:38:02 +08:00
uvjustin 97e77ab229
Improve type hints in stream (#51837)
* Improve type hints in stream

* Fix import locations

* Add stream to .strict-typing

Co-authored-by: Ruslan Sayfutdinov <ruslan@sayfutdinov.com>
2021-06-14 23:59:25 +08:00
uvjustin 123e8f01a1
Refactor stream to create partial segments (#51282) 2021-06-14 00:41:21 +08:00
uvjustin 3ca7eb9440
Update HLS playlist in stream (#51191)
* Enable gzip encoding for playlist responses
* Add EXT-X-PROGRAM-DATE-TIME to playlist
* Add EXT-X-START to playlist
* Change EXT-X-VERSION from 7 to 6
* Move idle timer call to recv
* Refactor recv to remove cursor and return bool
* Rename STREAM_TIMEOUT to SOURCE_TIMEOUT
2021-05-30 11:41:23 +08:00
uvjustin 38e0cbe964
Change stream sequence number to start from 0 (#51101)
* Use constants for provider strings

* Add last_sequence property
2021-05-27 11:22:31 +08:00
uvjustin c6f108f7c3
Refactor stream to use bytes (#51066)
* Refactor stream to use bytes
2021-05-26 16:19:09 +08:00
uvjustin 2eb87b8806
Combine StreamBuffer into SegmentBuffer in stream (#51041)
* Combine StreamBuffer into SegmentBuffer in stream

* Use new style type hint in comment
Remove unused member self._segment

* Change reset_av to static helper function

* Change make_new_av to only return OutputContainer
2021-05-24 22:57:07 -07:00
Ruslan Sayfutdinov 35f304450c
Enable type checks for stream component (#50527)
* Enable type checks for stream component

* Fix pylint
2021-05-13 23:26:11 +02:00
J. Nick Koston 3fa8ffa731
Enable mccabe complexity checks in flake8 (#49616)
Co-authored-by: Franck Nijhof <git@frenck.dev>
2021-04-25 12:38:40 +02:00
Erik Montnemery b0aa64d59c
Replace redacted stream recorder credentials with '****' (#48832) 2021-04-08 12:44:17 -07:00
uvjustin cd455e296e
Remove login details before logging stream source (#45398)
* Remove login details before logging stream source

* Convert to str before re

* Use compiled RE

* Add tests and filter log message in worker

* Update import

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* isort

Co-authored-by: Erik Montnemery <erik@montnemery.com>
2021-03-23 07:30:45 +01:00
uvjustin 14f85d8731
Disable audio stream when ADTS AAC detected (#47441)
* Disable audio stream when ADTS AAC detected

* Use context manager for memoryview

* Fix tests

* Add test

* Fix tests

* Change FakePacket bytearray size to 3
2021-03-06 16:40:49 +01:00
Allen Porter 97b59023d1
Improve handling for recording start of nest cam stream (#47144)
* Improve handling for start of nest cam stream

Add negative_cts_offsets to segment container options in order to better handle recording at the start of a stream. Nest streams start off with a negative offset, and if the segment container does not support it, then it adjusts the timestamps making it out of order with the next segment as described in issue #46968

* Update homeassistant/components/stream/__init__.py

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>
2021-02-27 11:20:58 -08:00
uvjustin f005c68630
Restore stream recorder functionality and add discontinuity support (#46772)
* Add discontinuity support to stream recorder

* Use same container options for both StreamOutputs

* Fix pts adjuster

* Remove redundant/incorrect duplicate hls segment check

* Use same StreamBuffer across outputs

* Remove keepalive check for recorder

* Set output video timescale explicitly

* Disable avoid_negative_ts
2021-02-23 10:37:19 +08:00
Allen Porter 4aa4f7e285
Rollback stream StreamOutput refactoring in PR#46610 (#46684)
* Rollback PR#46610

* Update stream tests post-merge
2021-02-20 06:49:39 -08:00
Allen Porter 88d143a644
Add discontinuity support to HLS streams and fix nest expiring stream urls (#46683)
* Support HLS stream discontinuity.

* Clarify discontinuity comments

* Signal a stream discontinuity on restart due to stream error

* Apply suggestions from code review

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>

* Simplify stream discontinuity logic
2021-02-18 20:26:02 +08:00
Allen Porter 08201d146b
Separate HLS logic out of core StreamOutput to prepare for discontinuity (#46610)
Separate the HLS stream view logic out of StreamOutput since the hls
stream view is about to get more complex to track discontinuities. This
makes the idle timeout, shutdown, and coupling between hls and record
more explicit.
2021-02-16 06:59:43 -08:00
Allen Porter 89aaeb3c35
Refactor stream worker responsibilities for segmenting into a separate class (#46563)
* Remove stream_worker dependencies on Stream

Removee stream_worker dependencies on Stream and split out the logic
for writing segments to a stream buffer.

* Stop calling internal stream methods

* Update homeassistant/components/stream/worker.py

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>

* Reuse self._outputs when creating new streams

Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>
2021-02-15 09:52:37 -08:00
Allen Porter dca6a93898
Centralize keepalive logic in Stream class (#45850)
* 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.
2021-02-08 07:19:41 -08:00
Allen Porter 65e3661f88
Repair flaky and broken stream tests in test_hls.py, and turn back on (#45025)
* Unmark tests as flaky (though still flaky)

This put tests into the broken state where they are flaky and do not yet pass

* Fix bug in test_hls_stream with incorrect path

* Enable and de-flake HLS stream tests

Background: Tests encode a fake video them start a stream to be decoded. Test
assert on the decoded segments, however there is a race with the stream worker
which can finish decoding first, and end the stream which ereases all buffers.

Breadown of fixes:
- Fix the race conditions by adding synchronization points right before the
  stream is finalized.
- Refactor StreamOutput.put so that a patch() can block the worker
  thread.  Previously, the put call would happen in the event loop which was
  not safe to block. This is a bit of a hack, but it is the simplist possible
  code change to add this synchronization and arguably provides slightly better
  separation of responsibilities from the worker anyway.
- Fix bugs in the tests that make them not pass, likely due to changes
  introduced while the tests were disabled
- Fix case where the HLS stream view recv() call returns None, indicating
  the worker finished while the request was waiting.

The tests were previously failing anywhere from 2-5% of the time on a lightly
loaded machine doing 1k iterations.  Now, have 0% flake rate.  Tested with:
$ py.test --count=1000 tests/components/strema/test_hls.py
2021-01-11 14:34:45 +01:00
uvjustin 414f167508
Remove pts adjustments in stream (#42399)
* Remove unnecessary pts adjustments

* Add comments

* Use -inf for initial last_dts to be more clear

* Use video first_pts as common adjuster in recorder

* Remove seek(0) before av.open
2020-11-16 15:13:33 -05:00
uvjustin 66dccd86bd
Modify wait timeout in stream (#42794)
* Set wait timeout to 40

* Use dynamic wait_timeout

* Catch error in container open

* Get restart times from const.py

* Fix test_stream_keepalive
2020-11-11 12:32:56 -05:00
springstan a967f689c7
Remove unnecessary instances of dict.keys() (#42518) 2020-10-28 20:43:48 +01:00
uvjustin f8d87bff16
Fix AVError and allow more missing DTS packets in stream (#42277)
* Fix AVError and allow more missing DTS in stream init

* Avoid recreating container demux iterator

* Relax missing dts requirement

* Fix spelling error

* Fix error message count

* Add timeout to av.open read

* Increase STREAM_TIMEOUT to 30
2020-10-24 22:55:12 -04:00
uvjustin 6a59e79bc6
Remove duplicated finalize_stream code in stream (#42171) 2020-10-21 23:36:54 +02:00
uvjustin ce0e08838f
Fix timestamp overflow error in stream (#41951) 2020-10-16 11:48:45 -04:00
uvjustin 1c6d0d138d
Adjust dts warning messages in stream (#41467)
* Adjust dts warning messages

* Wait for second packet with overflow dts condition

* Rename dts gap watch variable

* Remove out of order packet warning
2020-10-11 12:55:13 -04:00
uvjustin 6c18feb4ed
Allow only one packet with no dts in stream init (#41129)
* Allow only one packet with no dts in stream init

* Call finalize_stream() on exit
2020-10-11 12:52:51 -04:00
uvjustin 5658abfaca
Log timestamp overflow in stream (#39844) 2020-09-30 11:45:59 -04:00
uvjustin 8e0bb92c79
Disable audio in stream when audio stream profile is None (#40521) 2020-09-24 14:35:52 +02:00
uvjustin 8d3e4b6b3f
Ignore packets with missing dts in peek_first_pts (#40299) 2020-09-20 21:26:24 -04:00
uvjustin 9b29d09d45
Set output timescale to input timescale (#39946) 2020-09-11 14:07:45 -04:00
uvjustin 4ee5a29bc0
Disable audio for HLS or mpegts input (#39906) 2020-09-10 15:55:55 -04:00
Chris Talkington a0663d84d2
fix black for stream (#39622) 2020-09-03 11:54:04 -05:00
Eric Severance 9baa7c6c24
Restart keepalive streams (#38863) 2020-09-03 12:22:00 -04:00
Franck Nijhof 1c2ebdf307
Upgrade black to 20.8b1 (#39287) 2020-08-27 13:56:20 +02:00
uvjustin dc2d0b9297
Add audio to stream (#38846)
* Add audio to stream component

* Use container options to do most fmp4 formatting

* Add test for treatment of different audio inputs

* Add test for treatment of different audio inputs

* pcm_mulaw frames should be s16

* Use seek to get BytesIO length

* Remove unused utcnow

* Remove peek_next_audio_pts

* only demux audio and video packets - ignoring data and subtitle streams

Co-authored-by: Jason Hunter <hunterjm@gmail.com>
2020-08-19 23:18:54 -04:00
uvjustin 5355fcaba8
Add H.265 support to stream component (#38125)
* Add H.265 support to stream component

* Change find_box to generator

* Move fmp4 utilities to fmp4utils.py

* Add minimum segments and segment durations

* Remove MIN_SEGMENTS

* Fix when container_options is None

* Fix missing num_segments and update tests

* Remove unnecessary mock attribute

* Fix Segment construction in test_recorder_save

* fix recorder with lookback

Co-authored-by: Jason Hunter <hunterjm@gmail.com>
2020-08-11 17:12:41 -04:00