Search code examples
azureazure-media-services

How To Specify Different Streaming Endpoint When Creating An OnDemand Streaming Locator


I have two Azure Media Streaming Endpoints defined: the default endpoint that was created before September 11 2014 and does not support https, and a new streaming endpoint that was created after September 11 2014 and does support https.

I have removed all streaming units from the default streaming endpoint and have turned the default streaming endpoint off. The new streaming endpoint is enabled and has a single streaming unit.

When I create a Locator for my asset I need the locator to return the base uri of the new streaming endpoint, however it returns the base uri of the default streaming endpoint. For example:

var locator = mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, policy, DateTime.UtcNow.AddMinutes(-5));

// locator.BaseUri == http://example.origin.mediaservices.windows.net
// This uri points to the default streaming endpoint

How do I specify which streaming endpoint to use when creating a new locator for my asset?


Solution

  • It turns out I was using an older version of the Azure Media Services Client SDK (v3.0.0.5). In newer versions the MediaContext class has a collection of Streaming Endpoints that make it trivial to find the endpoint you are after. The way I ended up solving this is as follows:

    public void Example(CloudMediaContext mediaContext, IAsset asset)
    {
        var policy = mediaContext.AccessPolicies.Create("Streaming policy", MaxMediaAccessPeriod, AccessPermissions.Read);
        var locator = mediaContext.Locators.CreateLocator(LocatorType.OnDemandOrigin, asset, policy, DateTime.UtcNow.AddMinutes(-5));
        var template = new UriTemplate("{contentAccessComponent}/");
        var result = mediaContext.StreamingEndpoints
                                 .AsEnumerable()
                                 .Where(x => x.State == StreamingEndpointState.Running)
                                 .Select(x => template.BindByPosition(new Uri("https://" + x.HostName), locator.ContentAccessComponent))
                                 .First();
    }
    

    I found the information I needed in this blog post.