Create a channel for live streaming from Azure Media Services SDK for .NET

Yesterday I talked about the new preview for Live Streaming in Azure Media Services. As we saw is quite easy to create channels from the portal, but there are situations where we need to make this process automatic and configurable through our code. The following example shows how to create a channel using Azure Media Services SDK for NET:

using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace LiveStreaming
{
    class Program
    {
        static void Main(string[] args)
        {
            //0. Constants
            const string AccountName = "[YOUR_ACCOUNT_NAME]";
            const string AccountKey = "[YOUR_ACCOUNT_KEY]";

            //1. Install Nuget packages
            //1.1 Nuget: Install-Package windowsazure.mediaservices

            //2. Get AMS context
            var context = new CloudMediaContext(AccountName, AccountKey);

            //3. Create a channel
            //3.1 Create Channel Input
            var input = new ChannelInput
            {
                StreamingProtocol = StreamingProtocol.RTMP,
                AccessControl = new ChannelAccessControl
                {
                    IPAllowList = new List<IPRange>
                    {
                        new IPRange{
                            Name="MyInput",
                            Address=IPAddress.Parse("0.0.0.0"),
                            SubnetPrefixLength = 0
                        }
                    }
                }
            };

            //3.2 Create Channel Preview
            var preview = new ChannelPreview
            {
                AccessControl = new ChannelAccessControl
                {
                    IPAllowList = new List<IPRange>()
                    {
                        new IPRange{
                            Name= "MyPreview",
                            Address = IPAddress.Parse("0.0.0.0"),
                            SubnetPrefixLength = 0
                        }
                    }
                }
            };

            //3.3 Create Channel Output
            var output = new ChannelOutput { Hls = new ChannelOutputHls { FragmentsPerSegment = 1 } };

            var channel = context.Channels.Create(new ChannelCreationOptions
            {
                Name = "Channel-Returngis-1",
                Input = input,
                Preview = preview,
                Output = output

            });

            channel.Start();

            var previewEndpoint = channel.Preview.Endpoints.FirstOrDefault().Url.ToString();
            var ingestEndpoint = channel.Input.Endpoints.FirstOrDefault().Url.ToString();

            Console.WriteLine("The channel {0} is {1}", channel.Name, channel.State);
            Console.WriteLine("Ingest endpoint: {0}",ingestEndpoint);
            Console.WriteLine("Preview endpoint: {0}",previewEndpoint);

            Console.ReadLine();

        }
    }
}
  1. Azure Media Services credentials are needed.
  2. Install the windowsazure.mediaservices via Nuget
  3. Get the context of the account.
  4. To generate the channel we need:
    1. Create the ingest endpoint through ChannelInput, selecting the protocol to use and the range of IPs from which you can perform ingest. If we use 0.0.0.0 we are giving public access to it.
    2. Endpoint to access the preview channel, where we only add the IP range allowed.
    3. Finally, it is necessary to create the output through ChannelOutput.
  5. Once we have the ingest and the preview endpoints we can create the channel through context.Channels.Create and start it through the Start() method. When the process finishes we can recover the ingest and preview URLs to begin testing the new channel.

Happy live streaming!