Skip to content

Uri media source http headers#3169

Merged
TheCodeTraveler merged 41 commits intoCommunityToolkit:mainfrom
Kaaybi:uri-media-source-http-headers
Apr 2, 2026
Merged

Uri media source http headers#3169
TheCodeTraveler merged 41 commits intoCommunityToolkit:mainfrom
Kaaybi:uri-media-source-http-headers

Conversation

@Kaaybi
Copy link
Copy Markdown
Contributor

@Kaaybi Kaaybi commented Mar 20, 2026

Description of Change

Add HTTP headers support to MediaElement

Linked Issues

PR Checklist

Additional 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.

screenshot2026-03-20 192916 screenshot2026-03-20 192949

@Kaaybi
Copy link
Copy Markdown
Contributor Author

Kaaybi commented Mar 20, 2026

@dotnet-policy-service agree

Copy link
Copy Markdown
Member

@pictos pictos left a comment

Choose a reason for hiding this comment

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

Good job, I've a few comments about your work

Comment thread src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/MediaSource/MediaSource.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/HttpRandomAccessStream.windows.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/HttpRandomAccessStream.windows.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.windows.cs Outdated
@Kaaybi
Copy link
Copy Markdown
Contributor Author

Kaaybi commented Mar 21, 2026

Thank you pictos for your detailed review, I will address your points asap!
edit: Done!

@Kaaybi Kaaybi requested a review from pictos March 21, 2026 08:49
@TheCodeTraveler TheCodeTraveler requested review from Copilot and removed request for pictos March 21, 2026 14:46
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

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.HttpHeaders and a new MediaSource.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.

Comment thread src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.macios.cs
Comment thread test-output.txt Outdated
Comment thread test-error.txt Outdated
Comment thread src/CommunityToolkit.Maui.UnitTests/Views/MediaElement/UriMediaSourceTests.cs Outdated
Comment thread test-error.txt Outdated
@Kaaybi Kaaybi requested a review from TheCodeTraveler March 21, 2026 17:59
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 9 out of 9 changed files in this pull request and generated 2 comments.

Comment thread src/CommunityToolkit.Maui.MediaElement/Views/HttpRandomAccessStream.windows.cs Outdated
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 14 out of 16 changed files in this pull request and generated 14 comments.

Comment thread src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.android.cs Outdated
Comment thread src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui.UnitTests/Views/MediaElement/UriMediaSourceTests.cs Outdated
TheCodeTraveler and others added 12 commits April 2, 2026 14:48
…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>
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 14 out of 16 changed files in this pull request and generated 8 comments.

Comment thread src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs Outdated
Comment on lines +132 to +138
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);
}
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

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

Thanks @Kaaybi!!

@TheCodeTraveler TheCodeTraveler merged commit 6a00a00 into CommunityToolkit:main Apr 2, 2026
10 checks passed
@Kaaybi Kaaybi deleted the uri-media-source-http-headers branch April 3, 2026 05:50
@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants