Exemple #1
0
 public JsonResult Create(string transformName, string jobName, string jobDescription, Priority jobPriority, string jobData,
                          string inputFileUrl, string inputAssetName, MediaJobOutputMode outputAssetMode, string streamingPolicyName)
 {
     try
     {
         Job    job       = null;
         string authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             bool      videoAnalyzerPreset = false;
             bool      audioAnalyzerPreset = false;
             Transform transform           = mediaClient.GetEntity <Transform>(MediaEntity.Transform, transformName);
             foreach (TransformOutput transformOutput in transform.Outputs)
             {
                 if (transformOutput.Preset is VideoAnalyzerPreset)
                 {
                     videoAnalyzerPreset = true;
                 }
                 else if (transformOutput.Preset is AudioAnalyzerPreset)
                 {
                     audioAnalyzerPreset = true;
                 }
             }
             string insightId        = null;
             Asset  inputAsset       = null;
             string assetDescription = null;
             if (!string.IsNullOrEmpty(inputAssetName))
             {
                 inputAsset       = mediaClient.GetEntity <Asset>(MediaEntity.Asset, inputAssetName);
                 assetDescription = inputAsset.Description;
             }
             if (mediaClient.IndexerEnabled() && (videoAnalyzerPreset || audioAnalyzerPreset))
             {
                 bool audioOnly = !videoAnalyzerPreset && audioAnalyzerPreset;
                 insightId = mediaClient.IndexerUploadVideo(mediaClient.MediaAccount, inputAsset, null, jobPriority, true, audioOnly);
             }
             if (!string.IsNullOrEmpty(transformName))
             {
                 if (string.IsNullOrEmpty(inputFileUrl) && inputAsset != null)
                 {
                     StorageBlobClient blobClient = new StorageBlobClient(mediaClient.MediaAccount, inputAsset.StorageAccountName);
                     MediaAsset        mediaAsset = new MediaAsset(mediaClient, inputAsset);
                     string            fileName   = mediaAsset.Files[0].Name;
                     inputFileUrl = blobClient.GetDownloadUrl(inputAsset.Container, fileName, false);
                 }
                 string[] assetDescriptions = new string[] { assetDescription };
                 string[] assetAlternateIds = new string[] { insightId };
                 job = mediaClient.CreateJob(authToken, transformName, jobName, jobDescription, jobPriority, jobData, inputFileUrl, inputAssetName, outputAssetMode, assetDescriptions, assetAlternateIds, streamingPolicyName);
             }
         }
         return(Json(job));
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #2
0
        public IActionResult Index(string transformName, string jobName)
        {
            string authToken = HomeController.GetAuthToken(Request, Response);

            using (MediaClient mediaClient = new MediaClient(authToken))
            {
                Job[] jobs = new Job[] { };
                if (!string.IsNullOrEmpty(jobName))
                {
                    Job job = mediaClient.GetEntity <Job>(MediaEntity.TransformJob, jobName, transformName);
                    if (job != null)
                    {
                        jobs = new Job[] { job };
                    }
                }
                else
                {
                    jobs = mediaClient.GetAllEntities <Job, Transform>(MediaEntity.TransformJob, MediaEntity.Transform);
                }
                ViewData["transformJobs"]     = jobs;
                ViewData["transforms"]        = GetTransforms(mediaClient);
                ViewData["streamingPolicies"] = GetStreamingPolicies(mediaClient);
            }
            return(View());
        }
Exemple #3
0
 public JsonResult Publish(string entityName, bool unpublish)
 {
     try
     {
         string message;
         string authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             if (unpublish)
             {
                 mediaClient.DeleteLocators(entityName);
                 message = string.Format(Constant.Message.AssetUnpublished, entityName);
             }
             else
             {
                 Asset            asset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, entityName);
                 string           streamingPolicyName = PredefinedStreamingPolicy.DownloadAndClearStreaming;
                 StreamingLocator streamingLocator    = mediaClient.GetStreamingLocator(asset.Name, asset.Name, streamingPolicyName, null);
                 message = mediaClient.GetStreamingUrl(streamingLocator, null);
             }
         }
         return(Json(message));
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #4
0
        public IActionResult Index(string assetName)
        {
            List <MediaAsset> mediaAssets = new List <MediaAsset>();
            string            authToken   = HomeController.GetAuthToken(Request, Response);

            using (MediaClient mediaClient = new MediaClient(authToken))
            {
                if (!string.IsNullOrEmpty(assetName))
                {
                    Asset asset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetName);
                    if (asset != null)
                    {
                        MediaAsset mediaAsset = new MediaAsset(mediaClient, asset);
                        mediaAssets.Add(mediaAsset);
                    }
                }
                else
                {
                    IPage <Asset> assets = mediaClient.GetEntities <Asset>(MediaEntity.Asset);
                    foreach (Asset asset in assets)
                    {
                        MediaAsset mediaAsset = new MediaAsset(mediaClient, asset);
                        mediaAssets.Add(mediaAsset);
                    }
                }
            }
            ViewData["assets"] = mediaAssets.ToArray();
            return(View());
        }
Exemple #5
0
 public JsonResult Refresh(string[] transformNames, string[] jobNames)
 {
     try
     {
         List <Job> jobs      = new List <Job>();
         string     authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             for (int i = 0; i < jobNames.Length; i++)
             {
                 string transformName = transformNames[i];
                 string jobName       = jobNames[i];
                 Job    job           = mediaClient.GetEntity <Job>(MediaEntity.TransformJob, jobName, transformName);
                 jobs.Add(job);
             }
         }
         return(Json(jobs.ToArray()));
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
 public JsonResult Data(string assetName, string fileName, string insightId)
 {
     try
     {
         JContainer insights  = null;
         string     authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             if (!string.IsNullOrEmpty(insightId))
             {
                 if (mediaClient.IndexerInsightExists(insightId, out JObject insight))
                 {
                     insights = insight;
                 }
             }
             else
             {
                 Asset             asset      = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetName);
                 StorageBlobClient blobClient = new StorageBlobClient(mediaClient.MediaAccount, asset.StorageAccountName);
                 CloudBlockBlob    fileBlob   = blobClient.GetBlockBlob(asset.Container, null, fileName);
                 using (Stream fileStream = fileBlob.OpenReadAsync().Result)
                 {
                     StreamReader fileReader = new StreamReader(fileStream);
                     string       fileData   = fileReader.ReadToEnd().TrimStart();
                     if (fileData.StartsWith("["))
                     {
                         insights = JArray.Parse(fileData);
                     }
                     else
                     {
                         insights = JObject.Parse(fileData);
                     }
                 }
             }
         }
         return(Json(insights));
     }
     catch (ValidationException ex)
     {
         Error error = new Error()
         {
             Type    = HttpStatusCode.BadRequest,
             Message = ex.Message
         };
         return(new JsonResult(error)
         {
             StatusCode = (int)error.Type
         });
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #7
0
        private static void SyncMediaJobs()
        {
            DatabaseClient databaseClient = new DatabaseClient();
            string         collectionId   = Constant.Database.Collection.MediaJobAccount;

            MediaJobAccount[] jobAccounts = databaseClient.GetDocuments <MediaJobAccount>(collectionId);
            foreach (MediaJobAccount jobAccount in jobAccounts)
            {
                using (MediaClient mediaClient = new MediaClient(null, jobAccount.MediaAccount))
                {
                    Job job = mediaClient.GetEntity <Job>(MediaEntity.TransformJob, jobAccount.JobName);
                    if (job == null)
                    {
                        databaseClient.DeleteDocument(collectionId, jobAccount.JobName);
                    }
                }
            }
        }
Exemple #8
0
 public JsonResult StartEvent(string eventName)
 {
     try
     {
         string authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             LiveEvent      liveEvent      = mediaClient.GetEntity <LiveEvent>(MediaEntity.LiveEvent, eventName);
             MediaLiveEvent mediaLiveEvent = new MediaLiveEvent(mediaClient, liveEvent);
             if (mediaLiveEvent.Outputs.Length == 0)
             {
                 string eventOutputName      = string.Concat(eventName, Constant.Media.LiveEvent.Output.NameSuffix);
                 string eventOutputAssetName = Guid.NewGuid().ToString();
                 string streamingPolicyName  = PredefinedStreamingPolicy.ClearStreamingOnly;
                 int    archiveWindowMinutes = Constant.Media.LiveEvent.Output.DefaultArchiveMinutes;
                 CreateOutput(eventName, eventOutputName, null, eventOutputAssetName, streamingPolicyName, archiveWindowMinutes);
             }
             mediaClient.StartLiveEvent(eventName);
         }
         return(Json(eventName));
     }
     catch (ValidationException ex)
     {
         Error error = new Error()
         {
             Type    = HttpStatusCode.BadRequest,
             Message = ex.Message
         };
         return(new JsonResult(error)
         {
             StatusCode = (int)error.Type
         });
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #9
0
 public JsonResult Publish(string entityName, string parentName, string insightId, bool unpublish)
 {
     try
     {
         MediaJobPublish jobPublish;
         if (unpublish)
         {
             string authToken = HomeController.GetAuthToken(Request, Response);
             using (MediaClient mediaClient = new MediaClient(authToken))
             {
                 Job job = mediaClient.GetEntity <Job>(MediaEntity.TransformJob, entityName, parentName);
                 foreach (JobOutputAsset outputAsset in job.Outputs)
                 {
                     mediaClient.DeleteLocators(outputAsset.AssetName);
                 }
             }
             jobPublish = new MediaJobPublish()
             {
                 UserNotification = new UserNotification()
                 {
                     JobOutputMessage = string.Format(Constant.Message.JobOutputUnpublished, entityName)
                 }
             };
         }
         else
         {
             jobPublish = MediaClient.PublishJobOutput(entityName, insightId);
         }
         return(Json(jobPublish));
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #10
0
 public JsonResult Find(string assetName)
 {
     try
     {
         Asset asset = null;
         if (!string.IsNullOrEmpty(assetName))
         {
             string authToken = HomeController.GetAuthToken(Request, Response);
             using (MediaClient mediaClient = new MediaClient(authToken))
             {
                 asset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetName);
             }
         }
         return(Json(asset));
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #11
0
 public JsonResult StopEvent(string eventName)
 {
     try
     {
         string authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             mediaClient.StopLiveEvent(eventName);
             LiveEvent      liveEvent      = mediaClient.GetEntity <LiveEvent>(MediaEntity.LiveEvent, eventName);
             MediaLiveEvent mediaLiveEvent = new MediaLiveEvent(mediaClient, liveEvent);
             foreach (LiveOutput liveOutput in mediaLiveEvent.Outputs)
             {
                 mediaClient.DeleteEntity(MediaEntity.LiveEventOutput, liveOutput.Name, mediaLiveEvent.Name);
             }
         }
         return(Json(eventName));
     }
     catch (ValidationException ex)
     {
         Error error = new Error()
         {
             Type    = HttpStatusCode.BadRequest,
             Message = ex.Message
         };
         return(new JsonResult(error)
         {
             StatusCode = (int)error.Type
         });
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #12
0
 public JsonResult Create(string transformName, string jobName, string jobDescription, string jobPriority, string inputFileUrl,
                          string inputAssetName, StandardBlobTier inputAssetStorageTier, string streamingPolicyName, ContentProtection contentProtection)
 {
     try
     {
         Job    job       = null;
         string authToken = HomeController.GetAuthToken(Request, Response);
         using (MediaClient mediaClient = new MediaClient(authToken))
         {
             bool      videoAnalyzerPreset = false;
             bool      audioAnalyzerPreset = false;
             Transform transform           = mediaClient.GetEntity <Transform>(MediaEntity.Transform, transformName);
             foreach (TransformOutput transformOutput in transform.Outputs)
             {
                 if (transformOutput.Preset is VideoAnalyzerPreset)
                 {
                     videoAnalyzerPreset = true;
                 }
                 else if (transformOutput.Preset is AudioAnalyzerPreset)
                 {
                     audioAnalyzerPreset = true;
                 }
             }
             MediaJobOutputPublish outputAssetPublish = new MediaJobOutputPublish()
             {
                 InputAssetStorageTier = inputAssetStorageTier,
                 StreamingPolicyName   = streamingPolicyName,
                 ContentProtection     = contentProtection,
             };
             string insightId  = null;
             Asset  inputAsset = string.IsNullOrEmpty(inputAssetName) ? null : mediaClient.GetEntity <Asset>(MediaEntity.Asset, inputAssetName);
             if (mediaClient.IndexerEnabled() && (videoAnalyzerPreset || audioAnalyzerPreset))
             {
                 insightId = mediaClient.IndexerUploadVideo(inputFileUrl, inputAsset, jobPriority, videoAnalyzerPreset, audioAnalyzerPreset);
             }
             if (!string.IsNullOrEmpty(transformName))
             {
                 MediaJobOutputInsight outputInsight = new MediaJobOutputInsight()
                 {
                     Id           = insightId,
                     VideoIndexer = videoAnalyzerPreset,
                     AudioIndexer = audioAnalyzerPreset
                 };
                 job = mediaClient.CreateJob(transformName, jobName, jobDescription, jobPriority, inputFileUrl, inputAsset, null, outputAssetPublish, outputInsight, true);
             }
         }
         return(Json(job));
     }
     catch (ValidationException ex)
     {
         Error error = new Error()
         {
             Type    = HttpStatusCode.BadRequest,
             Message = ex.Message
         };
         return(new JsonResult(error)
         {
             StatusCode = (int)error.Type
         });
     }
     catch (ApiErrorException ex)
     {
         return(new JsonResult(ex.Response.Content)
         {
             StatusCode = (int)ex.Response.StatusCode
         });
     }
 }
Exemple #13
0
        private static void SyncMediaAssets(DatabaseClient databaseClient, ILogger logger)
        {
            string collectionId = Constant.Database.Collection.MediaAssets;

            MediaAssetLink[] assetLinks = databaseClient.GetDocuments <MediaAssetLink>(collectionId);
            foreach (MediaAssetLink assetLink in assetLinks)
            {
                using (MediaClient mediaClient = new MediaClient(assetLink.MediaAccount, assetLink.UserAccount))
                {
                    Asset parentAsset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetLink.AssetName);
                    if (parentAsset == null)
                    {
                        foreach (KeyValuePair <MediaTransformPreset, string> jobOutput in assetLink.JobOutputs)
                        {
                            if (jobOutput.Key == MediaTransformPreset.VideoIndexer || jobOutput.Key == MediaTransformPreset.AudioIndexer)
                            {
                                string insightId = jobOutput.Value;
                                mediaClient.IndexerDeleteVideo(insightId);
                            }
                            else
                            {
                                string assetName = jobOutput.Value;
                                mediaClient.DeleteEntity(MediaEntity.Asset, assetName);
                            }
                        }
                        string documentId = assetLink.AssetName;
                        databaseClient.DeleteDocument(collectionId, documentId);
                        logger.LogInformation("Asset Link Deleted: {0}", documentId);
                    }
                    else
                    {
                        List <MediaTransformPreset> missingJobOutputs = new List <MediaTransformPreset>();
                        foreach (KeyValuePair <MediaTransformPreset, string> jobOutput in assetLink.JobOutputs)
                        {
                            if (jobOutput.Key == MediaTransformPreset.VideoIndexer || jobOutput.Key == MediaTransformPreset.AudioIndexer)
                            {
                                string insightId     = jobOutput.Value;
                                bool   insightExists = mediaClient.IndexerInsightExists(insightId, out JObject insight);
                                if (!insightExists)
                                {
                                    missingJobOutputs.Add(jobOutput.Key);
                                    logger.LogInformation("Missing Indexer Insight: {0}", insightId);
                                }
                            }
                            else
                            {
                                string assetName  = jobOutput.Value;
                                Asset  childAsset = mediaClient.GetEntity <Asset>(MediaEntity.Asset, assetName);
                                if (childAsset == null)
                                {
                                    missingJobOutputs.Add(jobOutput.Key);
                                    logger.LogInformation("Missing Output Asset: {0}", assetName);
                                }
                            }
                        }
                        if (missingJobOutputs.Count > 0)
                        {
                            foreach (MediaTransformPreset missingJobOutput in missingJobOutputs)
                            {
                                assetLink.JobOutputs.Remove(missingJobOutput);
                            }
                            databaseClient.UpsertDocument(collectionId, assetLink);
                            logger.LogInformation("Asset Link Upserted: {0}", assetLink);
                        }
                    }
                }
            }
        }