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

Add stream preview element to generic camera #21463

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from

Conversation

davet2001
Copy link

@davet2001 davet2001 commented Jul 24, 2024

Proposed change

As part of a PR to enable a more user-friendly experience when configuring video stream sources, a preview element is added for previewing the still image and stream from a generic camera during configuration.

This is done by building on the FlowPreviewGeneric element, which had to be slightly modified to allow it to be subclassed.
Also included is an svg spinner displayed by the video element while the stream is loading, from https://github.com/n3r4zzurr0/svg-spinners

Type of change

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

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

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

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced error handling for playlist URLs in the media player, improving robustness.
    • Added support for a new preview type, "generic_camera," enhancing the preview system.
    • Introduced a new component for rendering camera feeds, allowing display of still images and live streams.
  • Improvements

    • Increased modularity and extensibility of the preview components, facilitating better integration and functionality.

Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Walkthrough

The recent updates improve URL handling in the HaHLSPlayer, expand preview type support in the preview system, enhance class visibility in FlowPreviewGeneric, and introduce the FlowPreviewGenericCamera component for rendering camera feeds. These changes collectively enhance error resilience, extend functionality, and improve the modular architecture of the codebase.

Changes

Files Change Summary
src/components/ha-hls-player.ts Improved error handling for playlist URL construction, adding a fallback to window.location.href if the URL is invalid.
src/data/preview.ts Expanded the HAS_CUSTOM_PREVIEW array to include "generic_camera" alongside "template", increasing the types of supported previews.
src/dialogs/config-flow/previews/flow-preview-generic.ts, src/dialogs/config-flow/previews/flow-preview-generic_camera.ts Made FlowPreviewGeneric class exportable and changed _preview and _error properties from private to protected, enhancing modularity.
src/dialogs/config-flow/previews/flow-preview-generic_camera.ts Introduced the FlowPreviewGenericCamera component for rendering camera feeds, featuring logic to display still images or live streams based on available URLs.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant FlowPreviewGeneric
    participant FlowPreviewGenericCamera
    participant HaHLSPlayer

    User->>FlowPreviewGeneric: Request camera preview
    FlowPreviewGeneric->>FlowPreviewGenericCamera: Render camera preview
    FlowPreviewGenericCamera->>FlowPreviewGenericCamera: Check if _preview is available
    alt _preview exists
        FlowPreviewGenericCamera->>FlowPreviewGenericCamera: Extract stillUrl and streamUrl
        alt stillUrl exists
            FlowPreviewGenericCamera->>User: Display still image
        else streamUrl exists
            FlowPreviewGenericCamera->>HaHLSPlayer: Initialize video player
            HaHLSPlayer->>User: Display video stream
        end
    else
        FlowPreviewGenericCamera->>User: Display error alert
    end
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@davet2001 davet2001 changed the title Add tream preview element to generic camera Add stream preview element to generic camera Jul 24, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
src/components/ha-hls-player.ts (1)

153-154: Enhance the comment for clarity.

Clarify the comment to explicitly mention the use of a try-catch block for error handling.

-  // In case we arrive here with a relative URL, we need to provide a valid
-  // base/absolute URL to avoid the URL() constructor throwing an error.
+  // Use a try-catch block to handle cases where this.url is a relative URL.
+  // Provide a valid base/absolute URL to avoid the URL() constructor throwing an error.

@davet2001
Copy link
Author

@balloob is this in line with what you meant in your comment on #21246?

autoplay
playsinline
.hass=${this.hass}
.controls=${false}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
.controls=${false}

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @bramkragten!
I assume this is because the default is false?
(sorry I am relatively new to frontend things)

Copy link
Member

Choose a reason for hiding this comment

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

yes, the default is false, but also because it is a boolean, you can set it to true by just adding the attribute like you did for playsinline for example.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks!

@davet2001
Copy link
Author

@bramkragten
Thanks for the review, it is working ok but I get this client side console error whenever I tick/untick the box to accept the preview:

Calling service system_log write 
Object { logger: "frontend.js_dev.modern.202407190", message: 'Unhandled promise rejection from Firefox 128.0 on Ubuntu\n{\n  "code": "not_found",\n  "message": "Subscription not found."\n}', level: "debug" }
 undefined [connection-mixin.ts:94](webpack://home-assistant-frontend/src/state/connection-mixin.ts?5cdf)
Uncaught (in promise) 
Object { code: "not_found", message: "Subscription not found." }
[connection.js:313](webpack://home-assistant-frontend/node_modules/home-assistant-js-websocket/dist/connection.js?f1aa)
    unsubscribe connection.js:313
    AsyncFunctionThrow self-hosted:808
    (Async: async)
    subscribeMessage connection.js:327
    _subscribePreview flow-preview-generic.ts:74
    InterpretGeneratorResume self-hosted:1417
    AsyncFunctionNext self-hosted:804
    (Async: async)

Any ideas on how to deal with this?

@davet2001 davet2001 force-pushed the generic_camera_stream_preview_ui_3 branch from 7329103 to 81d9510 Compare October 8, 2024 11:53
@davet2001
Copy link
Author

Any ideas on how to deal with this?

The client side log messages are still present, but it seems to work ok

@davet2001
Copy link
Author

I have tried rebasing this but it is no longer working correctly. Will take a little longer to figure out. Setting to draft for now.

@davet2001 davet2001 marked this pull request as draft November 13, 2024 22:23
@davet2001 davet2001 force-pushed the generic_camera_stream_preview_ui_3 branch from 81d9510 to 5ad17d2 Compare November 20, 2024 22:19
@davet2001
Copy link
Author

Now working again, after url was re-added to <ha-hls-player>. Ready for review.

@davet2001 davet2001 marked this pull request as ready for review November 20, 2024 22:35
@davet2001
Copy link
Author

I am not 100% sure that this PR should have the 'wait for backend' tag. The frontend changes can be made safely without the backend, as they just create lit elements that are not used until the backend is changed.

It is more the backend changes that can only be made once the frontend is changed.

@bramkragten
Copy link
Member

I am not 100% sure that this PR should have the 'wait for backend' tag. The frontend changes can be made safely without the backend, as they just create lit elements that are not used until the backend is changed.

It is more the backend changes that can only be made once the frontend is changed.

We don't merge code until both frontend and backend are approved to avoid the backend changing after the frontend is merged or the other way around. We also dont want unused code in the project.

@davet2001
Copy link
Author

Ok, understood. Both are now ready from a test coverage point of view.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants