/// <summary>
        /// Asynchronously create a new channel.
        /// </summary>
        /// <param name="options"> Channel creation options </param>
        /// <returns>The channel creation task.</returns>
        public Task <IChannel> CreateAsync(ChannelCreationOptions options)
        {
            var response = CreateChannelAsync(options);

            return(response.ContinueWith <IChannel>(t =>
            {
                t.ThrowIfFaulted();
                string operationId = t.Result.Single().Headers[StreamingConstants.OperationIdHeader];

                IOperation operation = AsyncHelper.WaitOperationCompletion(
                    MediaContext,
                    operationId,
                    StreamingConstants.CreateChannelPollInterval);

                string messageFormat = Resources.ErrorCreateChannelFailedFormat;
                string message;

                switch (operation.State)
                {
                case OperationState.Succeeded:
                    var result = (ChannelData)t.Result.AsyncState;
                    result.Refresh();
                    return result;

                case OperationState.Failed:
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.Failed, operationId, operation.ErrorMessage);
                    throw new InvalidOperationException(message);

                default:     // can never happen unless state enum is extended
                    message = string.Format(CultureInfo.CurrentCulture, messageFormat, Resources.InInvalidState, operationId, operation.State);
                    throw new InvalidOperationException(message);
                }
            }));
        }
Beispiel #2
0
        private Task <IMediaDataServiceResponse> CreateChannelAsync(ChannelCreationOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            if (string.IsNullOrEmpty(options.Name))
            {
                throw new ArgumentException(Resources.ErrorEmptyChannelName);
            }

            if (options.Input == null ||
                options.Input.AccessControl == null ||
                options.Input.AccessControl.IPAllowList == null)
            {
                throw new ArgumentException(Resources.ErrorEmptyChannelInputIPAllowList);
            }

            var channelData = new ChannelData
            {
                Name                    = options.Name,
                Description             = options.Description,
                CrossSiteAccessPolicies = options.CrossSiteAccessPolicies,
                Slate                   = options.Slate,
            };

            IChannel channel = channelData;

            channel.Input        = options.Input;
            channel.Preview      = options.Preview;
            channel.Output       = options.Output;
            channel.EncodingType = options.EncodingType;
            channel.Encoding     = options.Encoding;

            channelData.ValidateSettings();

            channelData.SetMediaContext(MediaContext);

            IMediaDataServiceContext dataContext = MediaContext.MediaServicesClassFactory.CreateDataServiceContext();

            dataContext.AddObject(ChannelSet, channel);

            MediaRetryPolicy retryPolicy = MediaContext.MediaServicesClassFactory.GetSaveChangesRetryPolicy(dataContext as IRetryPolicyAdapter);

            return(retryPolicy.ExecuteAsync(() => dataContext.SaveChangesAsync(channel)));
        }
        /// <summary>
        /// Sends create channel operation to the service asynchronously. Use Operations collection to get operation's status.
        /// </summary>
        /// <param name="options"> Channel creation options </param>
        /// <returns>Task to wait on for operation sending completion.</returns>
        public Task <IOperation> SendCreateOperationAsync(ChannelCreationOptions options)
        {
            var response = CreateChannelAsync(options);

            return(response.ContinueWith(t =>
            {
                t.ThrowIfFaulted();

                string operationId = t.Result.Single().Headers[StreamingConstants.OperationIdHeader];

                IOperation result = new OperationData
                {
                    ErrorCode = null,
                    ErrorMessage = null,
                    Id = operationId,
                    State = OperationState.InProgress.ToString(),
                };

                return result;
            }));
        }
        public static IChannel CreateAndStartChannel()
        {
            var channelInput = CreateChannelInput();
            var channePreview = CreateChannelPreview();
            var channelEncoding = CreateChannelEncoding();

            ChannelCreationOptions options = new ChannelCreationOptions
            {
               //     EncodingType = ChannelEncodingType.Standard,
                Name = ChannelName,
                Input = channelInput,
                Preview = channePreview,
                Encoding = channelEncoding
            };

            Log("Creating channel");
            IOperation channelCreateOperation = _context.Channels.SendCreateOperation(options);
            string channelId = TrackOperation(channelCreateOperation, "Channel create");

            IChannel channel = _context.Channels.Where(c => c.Id == channelId).FirstOrDefault();

            Log("Starting channel");
            var channelStartOperation = channel.SendStartOperation();
            TrackOperation(channelStartOperation, "Channel start");

            return channel;
        }
 /// <summary>
 /// Create a new channel.
 /// </summary>
 /// <param name="options"> Channel creation options </param>
 /// <returns>The created channel.</returns>
 public IChannel Create(ChannelCreationOptions options)
 {
     return(AsyncHelper.Wait(CreateAsync(options)));
 }
 /// <summary>
 /// Sends create channel operation to the service and returns. Use Operations collection to get operation's status.
 /// </summary>
 /// <param name="options"> Channel creation options </param>
 /// <returns>Operation info that can be used to track the operation.</returns>
 public IOperation SendCreateOperation(ChannelCreationOptions options)
 {
     return(AsyncHelper.Wait(SendCreateOperationAsync(options)));
 }
        private async void DoCreateChannel()
        {
            CreateLiveChannel form = new CreateLiveChannel(_context)
            {
                KeyframeInterval = Properties.Settings.Default.LiveKeyFrameInterval,
                HLSFragmentPerSegment = Properties.Settings.Default.LiveHLSFragmentsPerSegment,
                StartChannelNow = true
            };
            if (form.ShowDialog() == DialogResult.OK)
            {
                TextBoxLogWriteLine("Channel '{0}' : creating...", form.ChannelName);

                bool Error = false;
                ChannelCreationOptions options = new ChannelCreationOptions();
                try
                {
                    options = new ChannelCreationOptions()
                    {
                        Name = form.ChannelName,
                        Description = form.ChannelDescription,
                        EncodingType = form.EncodingType,
                        Input = new ChannelInput()
                        {
                            StreamingProtocol = form.Protocol,
                            AccessControl = new ChannelAccessControl()
                            {
                                IPAllowList = form.inputIPAllow
                            },
                            KeyFrameInterval = form.KeyframeInterval
                        },
                        Output = new ChannelOutput() { Hls = new ChannelOutputHls() { FragmentsPerSegment = form.HLSFragmentPerSegment } },
                        Preview = new ChannelPreview()
                        {
                            AccessControl = new ChannelAccessControl()
                            {
                                IPAllowList = form.previewIPAllow
                            },
                        }
                    };

                    if (form.EncodingType != ChannelEncodingType.None)
                    {
                        options.Encoding = form.EncodingOptions;
                        options.Slate = form.Slate;
                    }
                }

                catch (Exception ex)
                {
                    Error = true;
                    TextBoxLogWriteLine("Error with channel settings.", true);
                    TextBoxLogWriteLine(ex);
                }

                if (!Error)
                {
                    await Task.Run(() => IObjectExecuteOperationAsync(
                     () =>
                         _context.Channels.SendCreateOperationAsync(
                         options),
                         form.ChannelName,
                         "Channel",
                         "created",
                         _context));

                    DoRefreshGridChannelV(false);
                    IChannel channel = GetChannelFromName(form.ChannelName);
                    if (channel != null)
                    {
                        if (form.StartChannelNow)
                        {
                            Task.Run(async () =>
                            {
                                // let's start the channel now
                                await StartChannelAsync(GetChannelFromName(form.ChannelName));
                            }
                );
                        }
                    }
                }
            }
        }
        private async void ProcessCloneChannelToAnotherAMSAccount(CredentialsEntry DestinationCredentialsEntry, string DestinationStorageAccount, IChannel sourceChannel)
        {
            TextBoxLogWriteLine("Starting the channel cloning process...");
            CloudMediaContext DestinationContext = Program.ConnectAndGetNewContext(DestinationCredentialsEntry);

            var options = new ChannelCreationOptions()
            {
                Name = sourceChannel.Name,
                Description = sourceChannel.Description,
                EncodingType = sourceChannel.EncodingType,
                Input = sourceChannel.Input,
                Output = sourceChannel.Output,
                Preview = sourceChannel.Preview
            };

            if (sourceChannel.EncodingType != ChannelEncodingType.None)
            {
                options.Encoding = sourceChannel.Encoding;
                options.Slate = sourceChannel.Slate;
            }

            await Task.Run(() => IObjectExecuteOperationAsync(
                 () =>
                     DestinationContext.Channels.SendCreateOperationAsync(
                     options),
                     sourceChannel.Name,
                     "Cloned channel",
                     "created",
                     DestinationContext));

        }