Uri media source http headers#3169
Conversation
|
@dotnet-policy-service agree |
pictos
left a comment
There was a problem hiding this comment.
Good job, I've a few comments about your work
|
Thank you pictos for your detailed review, I will address your points asap! |
There was a problem hiding this comment.
Pull request overview
Adds support for supplying custom HTTP request headers when MediaElement loads media from a UriMediaSource, enabling scenarios like authenticated streaming (Fixes #1889, #3154).
Changes:
- Introduces
UriMediaSource.HttpHeadersand a newMediaSource.FromUri(Uri?, IDictionary<string,string>?)overload. - Implements header propagation on Android (Media3), iOS/macCatalyst (AVUrlAsset options), and Windows (AdaptiveMediaSource + stream fallback).
- Updates the sample page UI to let users define headers, and adds unit tests for the new API surface.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| test-output.txt | Adds captured test runner output (appears to be a local artifact). |
| test-error.txt | Adds captured test crash output (appears to be a local artifact). |
| src/CommunityToolkit.Maui.UnitTests/Views/MediaElement/UriMediaSourceTests.cs | Adds unit tests for HttpHeaders and the new FromUri overload. |
| src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs | Applies headers on Windows using AdaptiveMediaSource and a stream fallback. |
| src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs | Applies headers for iOS/macCatalyst via AVUrlAsset options. |
| src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.android.cs | Applies headers on Android by configuring a DefaultHttpDataSource.Factory. |
| src/CommunityToolkit.Maui.MediaElement/Views/HttpRandomAccessStream.windows.cs | New Windows stream implementation used for header-enabled URI fallback. |
| src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs | Adds the HttpHeaders property to UriMediaSource. |
| src/CommunityToolkit.Maui.MediaElement/MediaSource/MediaSource.shared.cs | Adds FromUri(Uri?, IDictionary<string,string>?) overload. |
| samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementPage.xaml.cs | Adds logic to capture/apply custom headers in the sample. |
| samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementPage.xaml | Adds UI controls for entering custom headers in the sample. |
…rce.shared.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ment/MediaElementCollectionViewViewModel.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ment/MediaElementCarouselViewViewModel.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…MediaElementPage.xaml.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…os.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…cessStream.windows.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…rce.shared.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| using var request = new HttpRequestMessage(HttpMethod.Head, uri); | ||
| using var response = await httpClient.SendRequestAsync(request).AsTask(cancellationToken).ConfigureAwait(ConfigureAwaitOptions.ForceYielding); | ||
| response.EnsureSuccessStatusCode(); | ||
|
|
||
| var contentLength = response.Content.Headers.ContentLength ?? 0; | ||
| return new HttpRandomAccessStream(httpClient, uri, contentLength); | ||
| } |
There was a problem hiding this comment.
CreateAsync relies on a successful HTTP HEAD request (HttpMethod.Head) before any reads. Many servers/CDNs either don’t implement HEAD or block it (405/403), which would make header-based playback fail even if GET would succeed. Consider falling back to GET when HEAD fails (or otherwise handling non-HEAD servers) so the headers feature works with a wider range of hosts.
…rce.shared.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
TheCodeTraveler
left a comment
There was a problem hiding this comment.
Thanks @Kaaybi!!
Description of Change
Add HTTP headers support to MediaElement
Linked Issues
PR Checklist
approved(bug) orChampioned(feature/proposal)mainat time of PRAdditional information
Thank you for reading through!
As documented, Tizen does not support custom HTTP headers.
I tested this on Windows with Mockoon. On HLS for instance, custom headers are propagated to both manifest and segments.
Feel free to test this by using the sample and setting custom headers.