示例#1
0
        private void OnEncounteredError()
        {
            var exception = new MediaException("A VLC exception occurred");

            Trace.TraceError(exception.ToString());
            Error.Exception = exception;
        }
示例#2
0
 private void HandleError(Exception exception)
 {
     if (exception != null)
     {
         var ex = new MediaException("Could not open audio", exception);
         Trace.TraceWarning(ex.ToString());
         Error.Exception = ex;
     }
     else
     {
         Error.Exception = null;
     }
 }
示例#3
0
        public async Task <Tuple <bool, MediaJob, MediaException> > EncodeMediaAsync(
            string title,
            string assetName,
            byte[] assetData,
            Action <string> progess,
            bool downloadAssets,
            CancellationToken token)
        {
            _logger.LogInformation($"EncodeMediaAsync: {assetName} with file: {assetData.Length}");
            bool           isSuccess = false;
            MediaJob       result    = null;
            MediaException exception = null;

            IAzureMediaServicesClient client = await GetClient();

            // create unique name to prevent collisions with dup file names
            string uniqueness      = Guid.NewGuid().ToString().Substring(0, 13);
            string jobName         = $"job-{uniqueness}";
            string locatorName     = $"locator-{uniqueness}";
            string inputAssetName  = $"input-{assetName}{uniqueness}";
            string outputAssetName = $"output-{assetName}{uniqueness}";
            bool   stopEndpoint    = false;



            try
            {
                // Ensure that you have customized encoding Transform.  This is really a one time setup operation.
                progess("Creating Transform...");
                // var transform = await CreateCustomTransform(client, _settings.ResourceGroup, _settings.AccountName, CustomTransform);
                var transform = await CreateBuiltinTransform(client, _settings.ResourceGroup, _settings.AccountName, DefaultTransform);

                _logger.LogInformation($"Transform created...{transform.Description}");

                // Create a new input Asset and upload the specified local video file into it.
                progess("Creating input Asset...");
                var inputAsset = await CreateInputAssetAsync(client, _settings.ResourceGroup, _settings.AccountName,
                                                             inputAssetName, assetName, assetData, token);

                _logger.LogInformation($"Input Asset created...{inputAsset.AssetId}");

                // Output from the Job must be written to an Asset, so let's create one
                progess("Creating output Asset...");
                var outputAsset = await CreateOutputAssetAsync(client, _settings.ResourceGroup, _settings.AccountName, outputAssetName, token);

                _logger.LogInformation($"Output Asset created...{outputAsset.AssetId}");

                // create job
                progess("Creating and Submitting Job...");
                var job = await SubmitJobAsync(client, _settings.ResourceGroup, _settings.AccountName, DefaultTransform, jobName, inputAsset.Name, outputAsset.Name, token);

                _logger.LogInformation($"Job created...{job.Name}");

                DateTime startedTime = DateTime.Now;

                //TODO: event hub
                // Polling is not a recommended best practice for production applications because of the latency it introduces.
                // Overuse of this API may trigger throttling. Developers should instead use Event Grid and listen for the status events on the jobs
                _logger.LogInformation("Polling job status...");
                progess("Polling job status...");
                job = await WaitForJobToFinishAsync(client, _settings.ResourceGroup, _settings.AccountName, DefaultTransform, jobName, progess, token);

                TimeSpan elapsed = DateTime.Now - startedTime;
                if (job.State == JobState.Finished)
                {
                    _logger.LogInformation($"Job finished: {elapsed}");
                    progess($"Job finished: {elapsed}");
                    var thumbnailList = await GenerateResults(client, _settings.ResourceGroup, _settings.AccountName, outputAsset.Name, OutputFolder, progess, downloadAssets, token);

                    //Streaming
                    progess("Creating Stream endpoints...");
                    var locator = await CreateStreamingLocatorAsync(client, _settings.ResourceGroup, _settings.AccountName, outputAssetName, locatorName, token);

                    var streamingEndpoint = await client.StreamingEndpoints.GetAsync(_settings.ResourceGroup, _settings.AccountName, DefaultStreamingEndpointName, token);

                    if (streamingEndpoint != null)
                    {
                        if (streamingEndpoint.ResourceState != StreamingEndpointResourceState.Running)
                        {
                            _logger.LogInformation("Streaming Endpoint was Stopped, restarting now..");
                            progess("Streaming Endpoint was Stopped, restarting now..");
                            await client.StreamingEndpoints.StartAsync(_settings.ResourceGroup, _settings.AccountName, DefaultStreamingEndpointName, token);

                            // Since we started the endpoint, we should stop it in cleanup.
                            stopEndpoint = true;
                        }
                    }
                    _logger.LogInformation("Getting the Streaming manifest URLs for HLS and DASH:");
                    progess("Getting the Streaming manifest URLs for HLS and DASH:");
                    var streamUrls = await GetStreamingUrlsAsync(client, _settings.ResourceGroup, _settings.AccountName, locator.Name, streamingEndpoint, token);

                    _logger.LogInformation($"Retuning {streamUrls.Count} URLs");

                    isSuccess = true;
                    result    = new MediaJob(jobName, locatorName, inputAssetName, outputAssetName,
                                             stopEndpoint, streamUrls, thumbnailList.FirstOrDefault(), title);
                }
                else if (job.State == JobState.Error)
                {
                    _logger.LogInformation($"ERROR: Job finished with error message: {job.Outputs[0].Error.Message}");
                    _logger.LogInformation($"ERROR: error details: {job.Outputs[0].Error.Details[0].Message}");
                    exception = new MediaException(job.Outputs[0].Error.Message);
                    _logger.LogInformation("Cleaning up...");
                    await CleanUpAsync(client, _settings.ResourceGroup, _settings.AccountName, DefaultTransform, jobName,
                                       inputAssetName, outputAssetName, locatorName, stopEndpoint, DefaultStreamingEndpointName);
                }
            }
            catch (ApiErrorException e)
            {
                _logger.LogError(e, e.Message);
                exception = new MediaException(e.Message, e);
                _logger.LogInformation("Cleaning up...");
                await CleanUpAsync(client, _settings.ResourceGroup, _settings.AccountName, DefaultTransform, jobName,
                                   inputAssetName, outputAssetName, locatorName, stopEndpoint, DefaultStreamingEndpointName);
            }
            return(new Tuple <bool, MediaJob, MediaException>(isSuccess, result, exception));
        }