Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make stream recorder work concurrently #73478

Merged
merged 5 commits into from
Jun 17, 2022

Conversation

uvjustin
Copy link
Contributor

@uvjustin uvjustin commented Jun 14, 2022

Proposed change

The current stream recorder works by spawning a recorder worker thread from the RecorderOutput.cleanup() function once the intended duration is reached and the RecorderOutput is removed. Since the worker is not called until the end of the recording, the video data is retained in the RecorderOutput until this time, and the work to remux the video is done all at this time.
This PR changes the async_record call to use a new RecorderOutput.async_record method which processes each segment in real time (farming the actual muxing work out with executor thread tasks). This may reduce the memory usage for longer recordings of several minutes or more, and it may also allow for recovery of a partial file in case there is an interruption to HA during the recording.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link

Hey there @hunterjm, @allenporter, mind taking a look at this pull request as it has been labeled with an integration (stream) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@uvjustin uvjustin force-pushed the async-record-stream branch 2 times, most recently from 77c996c to 3f1d98e Compare June 15, 2022 19:35
Copy link
Contributor

@allenporter allenporter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great improvement! Really like the idea of writing the stream as soon as possible rather than buffering the entire recording.
I have hust a couple questions about the core of the worker loop.

_LOGGER.error("Recording failed to capture anything")
else:
output.close()
os.rename(self.video_path + ".tmp", self.video_path)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put this part in (writing to a file with a different name first) because I believe some users currently use the existence of the output file to check whether the recording is done yet.

@uvjustin uvjustin force-pushed the async-record-stream branch from 4b6d663 to 775bfd4 Compare June 17, 2022 01:48
@uvjustin
Copy link
Contributor Author

Rebased on dev

@uvjustin uvjustin merged commit 7a3f632 into home-assistant:dev Jun 17, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jun 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants