private async void Load_Click(object sender, RoutedEventArgs e) { adaptiveContentModel = (AdaptiveContentModel)SelectedContent.SelectedItem; if (tokenMethod == AzureKeyAcquisitionMethod.AuthorizationHeader) { // Use an IHttpFilter to identify key request URIs and insert an Authorization header with the Bearer token. var baseProtocolFilter = new HttpBaseProtocolFilter(); httpClientFilter = new AddAuthorizationHeaderFilter(baseProtocolFilter); httpClientFilter.AuthorizationHeader = new HttpCredentialsHeaderValue("Bearer", adaptiveContentModel.AesToken); var httpClient = new HttpClient(httpClientFilter); // Here is where you can add any required custom CDN headers. httpClient.DefaultRequestHeaders.Append("X-HeaderKey", "HeaderValue"); // NOTE: It is not recommended to set Authorization headers needed for key request on the // default headers of the HttpClient, as these will also be used on non-HTTPS calls // for media segments and manifests -- and thus will be easily visible. await LoadSourceFromUriAsync(adaptiveContentModel.ManifestUri, httpClient); } else { await LoadSourceFromUriAsync(adaptiveContentModel.ManifestUri); } // On small screens, hide the description text to make room for the video. DescriptionText.Visibility = (ActualHeight < 650) ? Visibility.Collapsed : Visibility.Visible; }
private async Task <MediaPlaybackItem> LoadSourceFromUriAsync(Uri uri, HttpClient httpClient = null) { UnregisterHandlers(mediaPlayerElement.MediaPlayer); if (tokenMethod == AzureKeyAcquisitionMethod.AuthorizationHeader) { if (ContentSelectorControl.SelectedModel == null) { return(null); } // Use an IHttpFilter to identify key request URIs and insert an Authorization header with the Bearer token. var baseProtocolFilter = new HttpBaseProtocolFilter(); baseProtocolFilter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.NoCache; // Always set WriteBehavior = NoCache httpClientFilter = new AddAuthorizationHeaderFilter(baseProtocolFilter); httpClientFilter.AuthorizationHeader = new HttpCredentialsHeaderValue("Bearer", ContentSelectorControl.SelectedModel.AesToken); httpClient = new HttpClient(httpClientFilter); // Here is where you can add any required custom CDN headers. httpClient.DefaultRequestHeaders.Append("X-HeaderKey", "HeaderValue"); // NOTE: It is not recommended to set Authorization headers needed for key request on the // default headers of the HttpClient, as these will also be used on non-HTTPS calls // for media segments and manifests -- and thus will be easily visible. } AdaptiveMediaSourceCreationResult result = null; if (httpClient != null) { result = await AdaptiveMediaSource.CreateFromUriAsync(uri, httpClient); } else { result = await AdaptiveMediaSource.CreateFromUriAsync(uri); } MediaSource source; if (result.Status == AdaptiveMediaSourceCreationStatus.Success) { adaptiveMediaSource = result.MediaSource; // We use a helper class that logs all the events for the AdaptiveMediaSource: adaptiveMediaSourceLogger = new AdaptiveMediaSourceLogger(LoggerControl, adaptiveMediaSource); // In addition to logging, we use the callbacks to update some UI elements in this scenario: RegisterForAdaptiveMediaSourceEvents(adaptiveMediaSource); // At this point, we have read the manifest of the media source, and all bitrates are known. // Now that we have bitrates, attempt to cap them based on HdcpProtection. HdcpProtection?protection = hdcpSession.GetEffectiveProtection(); SetMaxBitrateForProtectionLevel(protection, adaptiveMediaSource); source = MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSource); } else { Log($"Error creating the AdaptiveMediaSource. Status: {result.Status}, ExtendedError.Message: {result.ExtendedError.Message}, ExtendedError.HResult: {result.ExtendedError.HResult.ToString("X8")}"); return(null); } // We use a helper class that logs all the events for the MediaSource: mediaSourceLogger = new MediaSourceLogger(LoggerControl, source); // Save the original Uri. source.CustomProperties["uri"] = uri.ToString(); // You're likely to put a content tracking id into the CustomProperties. source.CustomProperties["contentId"] = Guid.NewGuid().ToString(); var mpItem = new MediaPlaybackItem(source); // We use a helper class that logs all the events for the MediaPlaybackItem: mediaPlaybackItemLogger = new MediaPlaybackItemLogger(LoggerControl, mpItem); HideDescriptionOnSmallScreen(); return(mpItem); }