-
Notifications
You must be signed in to change notification settings - Fork 492
Uri media source http headers #3169
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
Merged
TheCodeTraveler
merged 41 commits into
CommunityToolkit:main
from
Kaaybi:uri-media-source-http-headers
Apr 2, 2026
Merged
Changes from 8 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
abdd329
feat: add HttpHeaders property to UriMediaSource and FromUri overload
Kaaybi 667e6ab
feat(android): support HTTP headers via DefaultHttpDataSource.Factory
Kaaybi ff201df
feat(ios): support HTTP headers via AVUrlAsset options
Kaaybi 4df6f10
feat(windows): support HTTP headers via AdaptiveMediaSource + HttpRan…
Kaaybi f438a19
test: add unit tests for UriMediaSource.HttpHeaders and MediaSource.F…
Kaaybi 7c676e3
sample: add custom HTTP headers demo to MediaElement sample page
Kaaybi 0b9f9ad
Merge branch 'main' into uri-media-source-http-headers
ne0rrmatrix ff62b10
fix(mediaelement): address PR review for HTTP headers support
Kaaybi c3e123b
chore: remove test files
Kaaybi d25acd0
fix(mediaelement): address copilot PR review for HTTP headers support
Kaaybi 1b90aa2
Merge remote-tracking branch 'upstream/main' into uri-media-source-ht…
Kaaybi 96cbd18
refactor(mediaelement): use ForceYielding
Kaaybi e4b7883
Merge branch 'main' into uri-media-source-http-headers
Kaaybi 9d30831
Merge branch 'main' into uri-media-source-http-headers
TheCodeTraveler 48515a8
Remove nullable MediaSource
TheCodeTraveler 1fed6f2
Make `UriMediaSource.HttpHeaders` init-only
TheCodeTraveler de31ca0
Update StreamingVideoUrls
TheCodeTraveler c32118b
Update for ObservableDictionary
TheCodeTraveler d19ca70
Update formatting
TheCodeTraveler 94e2142
Rename `StreamingVideoUrls` -> `StreamingUrls`
TheCodeTraveler c139f3a
Move `HttpRandomAccessStream` to `/Primatives/`
TheCodeTraveler cb81cbc
Use `Uri.UriSchemeFile`
TheCodeTraveler 7c5952e
Remove `Trace.WriteLine`
TheCodeTraveler e10837a
Remove `Trace.WriteLine`
TheCodeTraveler 21d093e
Update formatting
TheCodeTraveler c47d9d7
`dotnet format`
TheCodeTraveler 6a89037
Add `UriMediaSource` check
TheCodeTraveler 9f77085
Update src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSou…
TheCodeTraveler b7b0dcd
Update samples/CommunityToolkit.Maui.Sample/ViewModels/Views/MediaEle…
TheCodeTraveler 2db4505
Update `Remove(KeyValuePair<K, V> item)`
TheCodeTraveler 02a71ac
Update samples/CommunityToolkit.Maui.Sample/ViewModels/Views/MediaEle…
TheCodeTraveler ab3db0a
Update samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/…
TheCodeTraveler 7cc7dcd
Update src/CommunityToolkit.Maui.MediaElement/Views/MediaManager.maci…
TheCodeTraveler 53dc656
Merge branch 'uri-media-source-http-headers' of https://github.com/Ka…
TheCodeTraveler 44c76f4
Update UriMediaSourceTests.cs
TheCodeTraveler 979f89c
Update src/CommunityToolkit.Maui.MediaElement/Primitives/HttpRandomAc…
TheCodeTraveler e7b039e
Update src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSou…
TheCodeTraveler c78d845
Merge branch 'uri-media-source-http-headers' of https://github.com/Ka…
TheCodeTraveler 39fa22a
Update src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSou…
TheCodeTraveler 9591757
Update Test Names
TheCodeTraveler c9792c9
Merge branch 'uri-media-source-http-headers' of https://github.com/Ka…
TheCodeTraveler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/CommunityToolkit.Maui.MediaElement/Views/HttpRandomAccessStream.windows.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| using System.Runtime.InteropServices.WindowsRuntime; | ||
| using Windows.Foundation; | ||
| using Windows.Storage.Streams; | ||
| using HttpClient = Windows.Web.Http.HttpClient; | ||
| using HttpRequestMessage = Windows.Web.Http.HttpRequestMessage; | ||
| using HttpMethod = Windows.Web.Http.HttpMethod; | ||
| using HttpCompletionOption = Windows.Web.Http.HttpCompletionOption; | ||
|
|
||
| namespace CommunityToolkit.Maui.Core.Views; | ||
|
|
||
| /// <summary> | ||
| /// An <see cref="IRandomAccessStream"/> implementation backed by HTTP Range requests, | ||
| /// enabling progressive streaming of media content with custom HTTP headers without buffering the entire file in memory. | ||
| /// </summary> | ||
| sealed partial class HttpRandomAccessStream : IRandomAccessStream | ||
| { | ||
| readonly HttpClient httpClient; | ||
| readonly Uri requestUri; | ||
| readonly ulong size; | ||
|
|
||
| HttpRandomAccessStream(HttpClient httpClient, Uri requestUri, ulong size) | ||
| { | ||
| this.httpClient = httpClient; | ||
| this.requestUri = requestUri; | ||
| this.size = size; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public ulong Size | ||
| { | ||
| get => size; | ||
| set => throw new NotSupportedException("Cannot set size on a read-only HTTP stream."); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public ulong Position { get; private set; } | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool CanRead => true; | ||
|
|
||
| /// <inheritdoc/> | ||
| public bool CanWrite => false; | ||
|
|
||
| /// <summary> | ||
| /// Creates an <see cref="HttpRandomAccessStream"/> by issuing a HEAD request to determine the content length. | ||
| /// </summary> | ||
| /// <param name="httpClient">The <see cref="HttpClient"/> configured with the desired HTTP headers.</param> | ||
| /// <param name="uri">The URI of the media resource.</param> | ||
| /// <param name="cancellationToken"><see cref="CancellationToken"/>.</param> | ||
| /// <returns>A new <see cref="HttpRandomAccessStream"/> instance.</returns> | ||
| internal static async Task<HttpRandomAccessStream> CreateAsync(HttpClient httpClient, Uri uri, CancellationToken cancellationToken = default) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(httpClient); | ||
| ArgumentNullException.ThrowIfNull(uri); | ||
|
|
||
| using var request = new HttpRequestMessage(HttpMethod.Head, uri); | ||
| using var response = await httpClient.SendRequestAsync(request).AsTask(cancellationToken).ConfigureAwait(false); | ||
| response.EnsureSuccessStatusCode(); | ||
|
|
||
| var contentLength = response.Content.Headers.ContentLength ?? 0; | ||
| return new HttpRandomAccessStream(httpClient, uri, contentLength); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) | ||
| { | ||
| return AsyncInfo.Run<IBuffer, uint>(async (cancellationToken, _) => | ||
| { | ||
| using var request = new HttpRequestMessage(HttpMethod.Get, requestUri); | ||
| var rangeEnd = Position + count - 1; | ||
| request.Headers.TryAppendWithoutValidation("Range", $"bytes={Position}-{rangeEnd}"); | ||
|
|
||
| using var response = await httpClient.SendRequestAsync(request, HttpCompletionOption.ResponseHeadersRead).AsTask(cancellationToken).ConfigureAwait(false); | ||
|
Kaaybi marked this conversation as resolved.
Outdated
|
||
| response.EnsureSuccessStatusCode(); | ||
|
|
||
| var inputStream = await response.Content.ReadAsInputStreamAsync().AsTask(cancellationToken).ConfigureAwait(false); | ||
| var result = await inputStream.ReadAsync(buffer, count, options).AsTask(cancellationToken).ConfigureAwait(false); | ||
|
|
||
| Position += result.Length; | ||
| return result; | ||
| }); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Seek(ulong position) => Position = position; | ||
|
|
||
| /// <inheritdoc/> | ||
| public IRandomAccessStream CloneStream() => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public IInputStream GetInputStreamAt(ulong position) | ||
| { | ||
| Position = position; | ||
| return this; | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public IOutputStream GetOutputStreamAt(ulong position) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer) => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public IAsyncOperation<bool> FlushAsync() => throw new NotSupportedException(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public void Dispose() | ||
| { | ||
| // HttpClient is owned by the caller; do not dispose it here. | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.