Example #1
0
        private async Task <string> CreateStreamingUrl(string locatorName,
                                                       StreamingPolicyStreamingProtocol protocol)
        {
            var streamingEndpoint = await _client.StreamingEndpoints.GetAsync(_option.ResourceGroupName, _option.AccountName,
                                                                              _option.StreamingEndpointName);

            if (streamingEndpoint == null)
            {
                throw new StreamingEndpointMissingException();
            }

            if (streamingEndpoint.ResourceState != StreamingEndpointResourceState.Running)
            {
                await _client.StreamingEndpoints.StartAsync(_option.ResourceGroupName, _option.AccountName,
                                                            _option.StreamingEndpointName);
            }

            var paths = await _client.StreamingLocators.ListPathsAsync(_option.ResourceGroupName, _option.AccountName,
                                                                       locatorName);

            return(paths.StreamingPaths
                   .Where(x => x.StreamingProtocol == protocol)
                   .Select(p =>
            {
                var urlBuilder = new UriBuilder
                {
                    Scheme = "https", Host = streamingEndpoint.HostName, Path = p.Paths[0]
                };
                return urlBuilder.ToString();
            })
                   .FirstOrDefault());
        }
        /// <summary>
        /// This function returns a Dash or HLS Uri from the pathResponse.
        /// It also adds .mpd or .m3u8 at the end of the URL for a better compatibility with media players.
        /// </summary>
        /// <param name="pathResponse">Response from AMS on list paths.</param>
        /// <param name="selectedStreamingEndpoint">The selected streaming endpoint.</param>
        /// <param name="protocol">The protocol selected.</param>
        /// <returns>The streaming Uri, if available, or null.</returns>
        private static Uri GetStreamingUri(ListPathsResponse pathResponse, StreamingEndpoint selectedStreamingEndpoint, StreamingPolicyStreamingProtocol protocol)
        {
            string uristr = pathResponse.StreamingPaths.Where(p => p.StreamingProtocol == protocol).FirstOrDefault()?.Paths.FirstOrDefault();

            if (uristr != null)
            {
                uristr = "https://" + selectedStreamingEndpoint.HostName + uristr;
                if (!uristr.EndsWith(ExtensionPerProtocol[protocol]))
                {
                    uristr += ExtensionPerProtocol[protocol];
                }
            }

            return(uristr != null ? new Uri(uristr) : null);
        }