Skip to content

Fix reolink media playing on webkit browser#169184

Draft
mrtncode wants to merge 4 commits intohome-assistant:devfrom
mrtncode:reolink-media-webkit-fix
Draft

Fix reolink media playing on webkit browser#169184
mrtncode wants to merge 4 commits intohome-assistant:devfrom
mrtncode:reolink-media-webkit-fix

Conversation

@mrtncode
Copy link
Copy Markdown

@mrtncode mrtncode commented Apr 26, 2026

Breaking change

Proposed change

This PR fixes a bug where the reolink videos (from media_source) cannot be played on Webkit/ Apple Devices. This PR fixes that by using the ios/ webkit requirements for streaming the videos (especially request headers).

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)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • 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
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • [] Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

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 diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

PR is still WIP. The tests has not been updated yet

ToDos:

  • Add tests
  • Adjust the reolink aio package to passthrough the total file/ bytes length
  • Adjust the PR to use the total length from reolink aio to get better performance

Copilot AI review requested due to automatic review settings April 26, 2026 10:15
Copy link
Copy Markdown
Contributor

@home-assistant home-assistant Bot left a comment

Choose a reason for hiding this comment

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

Hi @mrtncode

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant Bot added bugfix cla-needed integration: reolink Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage labels Apr 26, 2026
@home-assistant
Copy link
Copy Markdown
Contributor

Hey there @starkillerOG, mind taking a look at this pull request as it has been labeled with an integration (reolink) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of reolink can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant mark-draft Mark the pull request as draft.
  • @home-assistant ready-for-review Remove the draft status from the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign reolink Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant update-branch Update the pull request branch with the base branch.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component, problem in config, problem in device, feature-request) on the pull request.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI review requested due to automatic review settings April 28, 2026 14:51
@mrtncode
Copy link
Copy Markdown
Author

mrtncode commented Apr 28, 2026

@starkillerOG Can you take a look? It works great on all apple devices (tested on iphone and mac) but it need much time to load (10-15 seconds). Any idea how to imporove this?

And the code is not ready/ good. Its rather a functional prototype 😅

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 8 comments.

Comment on lines +413 to 419
_mime_type, reolink_url, total_length_hint = await host.api.get_vod_source(
ch,
filename_decoded,
stream_res,
VodRequestType(vod_type),
include_total_length=True,
)
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

This changes the expected signature/return shape of host.api.get_vod_source (adds include_total_length kwarg and a third return value). If the host API and/or underlying reolink_aio dependency isn’t updated in the same PR, this will raise at runtime (unexpected kwarg and/or tuple unpacking error). Consider keeping backward compatibility by supporting both 2-tuple and 3-tuple returns (and only passing the kwarg when supported), or update the host API wrapper and pinned dependency together in this PR.

Copilot uses AI. Check for mistakes.

requires_auth = True
url = "/api/reolink/video/{config_entry_id}/{channel}/{stream_res}/{vod_type}/{filename}"
url = "/api/reolink/video/{config_entry_id}/{channel}/{stream_res}/{vod_type}/{filename}.mp4"
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The route is changed to require a literal .mp4 suffix. That can be a breaking change for any existing callers (bookmarked URLs, external clients, or older frontend code) that still use the previous endpoint without the suffix, while the PR is marked as a non-breaking bugfix. If backward compatibility is needed, add an additional route for the old path (or accept both patterns) and keep behavior consistent between them.

Copilot uses AI. Check for mistakes.
ssl_cipher=SSLCipherList.INSECURE,
)
self._vod_type: str | None = None
self._size_cache: dict[str, int] = {}
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The _size_cache is unbounded and keyed by reolink_url. If that URL contains per-request/session tokens (common for camera VOD URLs), this can grow without limit over time and also yield low cache hit rates. Consider bounding the cache (LRU/TTL) and/or using a stable cache key (e.g., config entry + channel + stream + filename) and clearing entries when sessions are expired/rotated.

Copilot uses AI. Check for mistakes.
Comment on lines +151 to +156
if (cached := self._size_cache.get(reolink_url)) is not None:
return cached

if total_length_hint is not None:
self._size_cache[reolink_url] = total_length_hint
return total_length_hint
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The _size_cache is unbounded and keyed by reolink_url. If that URL contains per-request/session tokens (common for camera VOD URLs), this can grow without limit over time and also yield low cache hit rates. Consider bounding the cache (LRU/TTL) and/or using a stable cache key (e.g., config entry + channel + stream + filename) and clearing entries when sessions are expired/rotated.

Copilot uses AI. Check for mistakes.
Comment on lines +181 to +182
self._size_cache[reolink_url] = length
return length
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

The _size_cache is unbounded and keyed by reolink_url. If that URL contains per-request/session tokens (common for camera VOD URLs), this can grow without limit over time and also yield low cache hit rates. Consider bounding the cache (LRU/TTL) and/or using a stable cache key (e.g., config entry + channel + stream + filename) and clearing entries when sessions are expired/rotated.

Copilot uses AI. Check for mistakes.
Comment on lines +140 to +149
async def _async_resolve_total_length(
self,
host: Any,
channel: int,
stream_res: str,
filename_decoded: str,
reolink_url: str,
reolink_response: ClientResponse,
total_length_hint: int | None = None,
) -> int | None:
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

_async_resolve_total_length currently accepts host, channel, stream_res, and filename_decoded but (in this diff) doesn’t use them. In Home Assistant’s Ruff configuration, unused arguments may be flagged, and it also makes the helper harder to understand. Either remove these parameters, prefix them with _ to indicate intentional unused args, or use them to build a stable cache key (which also helps avoid caching by a potentially tokenized URL).

Copilot uses AI. Check for mistakes.
Comment on lines +367 to +377
async def _async_stream_passthrough(
self,
request: web.Request,
host: Any,
reolink_response: ClientResponse,
) -> web.StreamResponse:
"""Stream the upstream response without any range synthesis."""
response_headers = dict(reolink_response.headers)
response_headers.pop("Content-Disposition", None)
response_headers.pop("content-disposition", None)
self._normalize_content_type(response_headers, reolink_response.content_type)
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

Hop-by-hop headers are stripped only in the WebKit branch (_HOP_BY_HOP_RESPONSE_HEADERS), but passthrough streaming can also propagate hop-by-hop headers like Connection or Transfer-Encoding directly from upstream, which is generally incorrect for a proxy response. Consider applying the same hop-by-hop header removal in _async_stream_passthrough as well.

Copilot uses AI. Check for mistakes.
Comment on lines +472 to +486
if not self._is_webkit_client(request):
return await self._async_stream_passthrough(request, host, reolink_response)

total_length = await self._async_resolve_total_length(
host,
ch,
stream_res,
filename_decoded,
reolink_url,
reolink_response,
total_length_hint,
)
force_range, content_range_total, error_response = self._plan_range_handling(
request, reolink_response, total_length
)
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

This adds substantial new behavior for WebKit clients (user-agent detection, range synthesis, 416 handling, and optional probing for total length) but the PR description notes tests are not updated yet. Add tests covering at least: (1) Safari/WebKit Range: bytes=0-1 probe resulting in 206 with correct Content-Range/Content-Length, (2) invalid range returning 416 with Content-Range: bytes */<total>, and (3) non-WebKit clients continuing to passthrough without synthesized range behavior.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix cla-signed integration: reolink Quality Scale: platinum Top 100 Integration is ranked within the top 100 by usage Top 200 Integration is ranked within the top 200 by usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reolink Home Hub does not play recording on iOS/MAC

3 participants