// <SnippetAMSDownloadRequested> private async void DownloadRequested(AdaptiveMediaSource sender, AdaptiveMediaSourceDownloadRequestedEventArgs args) { // rewrite key URIs to replace http:// with https:// if (args.ResourceType == AdaptiveMediaSourceResourceType.Key) { string originalUri = args.ResourceUri.ToString(); string secureUri = originalUri.Replace("http:", "https:"); // override the URI by setting property on the result sub object args.Result.ResourceUri = new Uri(secureUri); } if (args.ResourceType == AdaptiveMediaSourceResourceType.Manifest) { AdaptiveMediaSourceDownloadRequestedDeferral deferral = args.GetDeferral(); args.Result.Buffer = await CreateMyCustomManifest(args.ResourceUri); deferral.Complete(); } if (args.ResourceType == AdaptiveMediaSourceResourceType.MediaSegment) { var resourceUri = args.ResourceUri.ToString() + "?range=" + args.ResourceByteRangeOffset + "-" + (args.ResourceByteRangeLength - 1); // override the URI by setting a property on the result sub object args.Result.ResourceUri = new Uri(resourceUri); // clear the byte range properties on the result sub object args.Result.ResourceByteRangeOffset = null; args.Result.ResourceByteRangeLength = null; } }
private async void DownloadRequested(AdaptiveMediaSource sender, AdaptiveMediaSourceDownloadRequestedEventArgs args) { // rewrite key URIs to replace http:// with https:// if (args.ResourceType == AdaptiveMediaSourceResourceType.Key) { string originalUri = args.ResourceUri.ToString(); string secureUri = originalUri.Replace("http:", "https:"); // override the URI by setting property on the result sub object args.Result.ResourceUri = new Uri(secureUri); } if (args.ResourceType == AdaptiveMediaSourceResourceType.Manifest) { AdaptiveMediaSourceDownloadRequestedDeferral deferral = args.GetDeferral(); // args.Result.Buffer = await CreateMyCustomManifest(args.ResourceUri); deferral.Complete(); } }
private async Task AppDownloadedKeyRequest(AdaptiveMediaSourceDownloadRequestedEventArgs args) { if (adaptiveContentModel == null) { return; } // For AzureKeyAcquisitionMethod.ApplicationDownloaded we do the following: // Call .GetDeferral() to allow asynchronous work to be performed // Get the requested network resource using app code // Set .Result.InputStream or .Result.Buffer with the response data // Complete the deferral to indicate that we are done. // With this pattern, the app has complete control over any downloaded content. // Obtain a deferral so we can perform asynchronous operations. var deferral = args.GetDeferral(); try { var appHttpClientForKeys = new HttpClient(); appHttpClientForKeys.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", adaptiveContentModel.AesToken); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, args.ResourceUri); HttpResponseMessage response = await appHttpClientForKeys.SendRequestAsync( request, HttpCompletionOption.ResponseHeadersRead).AsTask(ctsForAppHttpClientForKeys.Token); if (response.IsSuccessStatusCode) { args.Result.InputStream = await response.Content.ReadAsInputStreamAsync(); // Alternatively, we could use: // args.Result.Buffer = await response.Content.ReadAsBufferAsync(); } else { // The app code failed. Report this by setting the args.Result.ExtendedStatus. // This will ensure that the AdaptiveMediaSource does not attempt to download the resource // itself, and instead treats the download as having failed. switch (response.StatusCode) { case HttpStatusCode.Unauthorized: // HTTP_E_STATUS_DENIED args.Result.ExtendedStatus = 0x80190191; break; case HttpStatusCode.NotFound: // HTTP_E_STATUS_NOT_FOUND args.Result.ExtendedStatus = 0x80190194; break; default: // HTTP_E_STATUS_UNEXPECTED args.Result.ExtendedStatus = 0x80190001; break; } Log($"Key Download Failed: {response.StatusCode} {args.ResourceUri}"); } } catch (TaskCanceledException) { Log($"Request canceled: {args.ResourceUri}"); } catch (Exception e) { Log($"Key Download Failed: {e.Message} {args.ResourceUri}"); } finally { // Complete the deferral when done. deferral.Complete(); } }