Example #1
0
        public void TestBuildInput_NoIdentifier_Compression()
        {
            ICompressor compressor = new GZipCompressor();
            Image       img        = _createTestImage();
            JobInput    i          = new JobInput(img);

            i.Compressor = compressor;
            JobBuilderProcess p       = new JobBuilderProcess();
            XElement          element = p.BuildInput(i);

            Assert.AreEqual("input", element.Name);
            Assert.IsNull(element.Attribute("identifier"));

            Assert.IsNotNull(element.Attribute("compressor"));
            Assert.AreEqual("gzip", element.Attribute("compressor").Value.ToLower());

            XNode child = element.FirstNode;

            Assert.IsTrue(child.NodeType == System.Xml.XmlNodeType.CDATA);

            XCData data          = (XCData)child;
            string providedImage = data.Value;
            string expectedImage = System.Text.Encoding.Default.GetString(CompressionAssistant.Compress(img, compressor));

            Assert.AreEqual(expectedImage, providedImage);
        }
Example #2
0
        public void TestBuildInput_Identifier_NoCompression()
        {
            object   identifier = "Test";
            Image    img        = _createTestImage();
            JobInput i          = new JobInput(img);

            i.Identifier = identifier;
            JobBuilderProcess p       = new JobBuilderProcess();
            XElement          element = p.BuildInput(i);

            Assert.AreEqual("input", element.Name);

            Assert.IsNotNull(element.Attribute("identifier"));
            Assert.AreEqual(identifier, element.Attribute("identifier").Value);

            XNode child = element.FirstNode;

            Assert.IsTrue(child.NodeType == System.Xml.XmlNodeType.CDATA);

            XCData data          = (XCData)child;
            string providedImage = data.Value;
            string expectedImage = System.Text.Encoding.Default.GetString(CompressionAssistant.ImageToBytes(img));

            Assert.AreEqual(expectedImage, providedImage);
        }
Example #3
0
        /// <summary>
        /// Constructs Xml using the <see cref="JobInput"/>.
        /// </summary>
        /// <param name="input">The <see cref="JobInput"/> to be used as input
        /// in a job.</param>
        /// <returns>An <see cref="XElement"/> representing the
        /// <see cref="JobInput"/>.</returns>
        /// <exception cref="InvalidOperationException">this
        /// <see cref="IBuilderProcess"/> does not support building
        /// inputs.</exception>
        public XElement BuildInput(JobInput input)
        {
            ICollection <object> content = new List <object>();

            if (input.Identifier != null)
            {
                XAttribute id = new XAttribute("identifier", input.Identifier);
                content.Add(id);
            }

            byte[] imgBytes = null;
            if (input.Compressor != null)
            {
                string compressorId = CompressorFactory.ResolveIdentifier(input.Compressor);
                imgBytes = CompressionAssistant.Compress(input.Input, input.Compressor);
                XAttribute compressor = new XAttribute("compressor", compressorId);
                content.Add(compressor);
            }
            else
            {
                imgBytes = CompressionAssistant.ImageToBytes(input.Input);
            }

            string bytesAsString = System.Text.Encoding.Default.GetString(imgBytes);
            XCData imgNode       = new XCData(bytesAsString);

            content.Add(imgNode);

            return(new XElement("input", content));
        }
Example #4
0
        private IdentificationResponse streamJob(JobInput input)
        {
            // perform
            Console.WriteLine("Installing ffmpeg...");
            var ffmpegPath = InstallDependencies.Run();

            Console.WriteLine("Getting m3u8 link...");
            var m3u8Link = M3U8.GetAudioOnlyM3U8(input.Identifier);

            Console.WriteLine("Listening to channel...");
            var mp3 = ListenToChannel.Listen(m3u8Link);

            Console.WriteLine("Running recognition...");
            var result = Recognition.Run(mp3);

            Console.WriteLine(result);
            Console.WriteLine("Finished...");

            // cleanup
            Console.WriteLine("Cleaning up...");
            System.IO.File.Delete("/tmp/audio.mp3");
            System.IO.File.Delete("/tmp/audio.mp3.cli.lo");

            return(result);
        }
Example #5
0
 // PUT api/job/5
 public void Put(long id, [FromBody] JobInput job)
 {
     if (_jobService.JobExists(id))
     {
         _jobService.AddOrUpdate(job);
     }
 }
Example #6
0
        // </CreateOutputAsset>

        /// <summary>
        /// Submits a request to Media Services to apply the specified Transform to a given input video.
        /// </summary>
        /// <param name="client">The Media Services client.</param>
        /// <param name="resourceGroupName">The name of the resource group within the Azure subscription.</param>
        /// <param name="accountName"> The Media Services account name.</param>
        /// <param name="transformName">The name of the transform.</param>
        /// <param name="jobName">The (unique) name of the job.</param>
        /// <param name="jobInput"></param>
        /// <param name="outputAssetName">The (unique) name of the  output asset that will store the result of the encoding job. </param>
        // <SubmitJob>
        private static async Task <Job> SubmitJobAsync(IAzureMediaServicesClient client,
                                                       string resourceGroupName,
                                                       string accountName,
                                                       string transformName,
                                                       string jobName,
                                                       JobInput jobInput,
                                                       string outputAssetName)
        {
            JobOutput[] jobOutputs =
            {
                new JobOutputAsset(outputAssetName),
            };

            // In this example, we are assuming that the job name is unique.
            //
            // If you already have a job with the desired name, use the Jobs.Get method
            // to get the existing job. In Media Services v3, Get methods on entities returns null
            // if the entity doesn't exist (a case-insensitive check on the name).
            Job job = await client.Jobs.CreateAsync(
                resourceGroupName,
                accountName,
                transformName,
                jobName,
                new Job
            {
                Input   = jobInput,
                Outputs = jobOutputs,
            });

            return(job);
        }
        /// <summary>
        /// Submits a request to Media Services to apply the specified Transform to a given input video.
        /// </summary>
        /// <param name="client">The Media Services client.</param>
        /// <param name="resourceGroupName">The name of the resource group within the Azure subscription.</param>
        /// <param name="accountName"> The Media Services account name.</param>
        /// <param name="transformName">The name of the transform.</param>
        /// <param name="jobName">The (unique) name of the job.</param>
        /// <param name="jobInput"></param>
        /// <param name="outputAssetName">The (unique) name of the  output asset that will store the result of the encoding job. </param>
        private static Job SubmitJob(IAzureMediaServicesClient client,
                                     string resourceGroupName,
                                     string accountName,
                                     string transformName,
                                     string jobName,
                                     JobInput jobInput,
                                     string outputAssetName)
        {
            JobOutput[] jobOutputs =
            {
                new JobOutputAsset(outputAssetName),
            };

            Job job = client.Jobs.Create(
                resourceGroupName,
                accountName,
                transformName,
                jobName,
                new Job
            {
                Input   = jobInput,
                Outputs = jobOutputs,
            });

            return(job);
        }
        public JobDetailsOutput GetJobDetails([FromBody] JobInput input)
        {
            var result = new JobDetailsOutput();

            try
            {
                var detailsData = _schedulerDataProvider.GetJobDetailsData(input.Job, input.Group);

                result.JobDataMap = detailsData
                                    .JobDataMap
                                    .Select(pair => new Property(pair.Key.ToString(), pair.Value))
                                    .ToArray();

                result.JobProperties = detailsData
                                       .JobProperties
                                       .Select(pair => new Property(pair.Key, pair.Value))
                                       .ToArray();
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }
        public BaseResult AddJob([FromBody] JobInput input)
        {
            var result = new BaseResult();

            try
            {
                //先使用Assembly类载入DLL,再根据类的全路径获取类
                Type     typeofControl = null;
                Assembly tempAssembly;
                tempAssembly  = Assembly.Load(input.NameSpace);
                typeofControl = tempAssembly.GetType(input.ClassName);
                IJobDetail job = JobBuilder.Create(typeofControl)
                                 .WithIdentity(input.Job, input.Group)
                                 .WithDescription(input.Description)
                                 .UsingJobData("RequestUrl", input.RequestUrl)
                                 .UsingJobData("Parameters", input.Parameters)
                                 .StoreDurably(true)
                                 .Build();
                _schedulerProvider.Scheduler.AddJob(job, true);
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }
Example #10
0
        /// <summary>
        /// Performs the processing procedure against an input
        /// </summary>
        /// <param name="pipeline">The processing pipeline</param>
        /// <param name="input">The input to be processed</param>
        /// <returns>The output from the pipeline, or null if the process
        /// is cancelled</returns>
        private Image _processInput(Pipeline.Pipeline pipeline, JobInput input)
        {
            Image theInput = input.Input;

            foreach (PipelineEntry entry in pipeline)
            {
                lock ( _cancelPadlock )
                {
                    if (_cancel)
                    {
                        return(null);
                    }
                }

                AlgorithmPlugin plugin = entry.Process;
                plugin.Input = theInput;
                plugin.Run(entry.ProcessInput);
                theInput = plugin.Output ?? plugin.Input;
            }

            InputProcessedArgs e = new InputProcessedArgs(input.Identifier, (Image)theInput.Clone());

            _ticket.OnInputProcessed(e);
            return(theInput);
        }
Example #11
0
        public void AddOrUpdate(JobInput job)
        {
            var jobLocal = _context.Jobs.FirstOrDefault(x => x.Id == job.Id);

            jobLocal = Mapper.Map(job, jobLocal);

            _context.Jobs.AddOrUpdate(jobLocal);
            _context.SaveChanges();
        }
Example #12
0
        public InputViewModel(JobInput input)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            Input          = input.Input;
            Identifier     = input.Identifier;
            _outputPadlock = new object();
        }
Example #13
0
        /// <summary>
        /// Saves the output from the processor to the data-store.
        /// </summary>
        /// <param name="input">The input object provided to the processor.</param>
        /// <param name="output">The output from the processor.</param>
        protected override void Save(JobInput input, IProcessedImage output)
        {
            FileInfo  file   = (FileInfo)output.Identifier;
            readImage reader = new readImage();

            byte[] blob = reader.ImageToByteArray(output.Output);

            ProcessedImageRepository processed = new ProcessedImageRepository();

            processed.saveImage(file, blob);
        }
Example #14
0
 /// <summary>
 /// Performs the visiting logic against an <see cref="XNode"/> representing
 /// an input to the job.
 /// </summary>
 /// <param name="xml">The <see cref="XNode"/> representing an input to a
 /// job.</param>
 public void VisitInput(XNode xml)
 {
     try
     {
         JobInput i = _decompiler.DecompileInput(xml);
         Inputs.Add(i);
     }
     catch (Exception e)
     {
         throw new XmlDecompilationException(e.Message, e);
     }
 }
Example #15
0
 private void SetJobInputAssetArchive(JobInput jobInput, StandardBlobTier inputAssetStorageTier)
 {
     if (jobInput is JobInputAsset jobInputAsset && inputAssetStorageTier != StandardBlobTier.Unknown)
     {
         Asset             inputAsset = GetEntity <Asset>(MediaEntity.Asset, jobInputAsset.AssetName);
         StorageBlobClient blobClient = new StorageBlobClient(this.MediaAccount, inputAsset.StorageAccountName);
         CloudBlockBlob[]  blobs      = blobClient.ListBlobContainer(inputAsset.Container, null);
         foreach (CloudBlockBlob blob in blobs)
         {
             blob.SetStandardBlobTierAsync(inputAssetStorageTier);
         }
     }
 }
Example #16
0
        /// <summary>
        /// Saves the output from the processor to the data-store.
        /// </summary>
        /// <param name="input">The input object provided to the processor.</param>
        /// <param name="output">The output from the processor.</param>
        protected override void Save(JobInput input, IProcessedImage output)
        {
            _validateDirectories();

            string path = string.Format(@"{0}/Input/{1}", OutputDirectory, _coerceIdentifier(input.Identifier));

            using (Stream s = File.Create(path))
            {
                input.Input.Save(s, ImageFormat.Png);
            }

            _saveOutput(output);
        }
        public BaseResult DeleteJob([FromBody] JobInput input)
        {
            var result = new BaseResult();

            try
            {
                result.Success = _schedulerProvider.Scheduler.DeleteJob(new JobKey(input.Job, input.Group));
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }
        /// <summary>
        /// Submits a request to Media Services to apply the specified Transform to a given input video.
        /// </summary>
        /// <param name="client">The Media Services client.</param>
        /// <param name="log">Function logger.</param>
        /// <param name="resourceGroupName">The name of the resource group within the Azure subscription.</param>
        /// <param name="accountName"> The Media Services account name.</param>
        /// <param name="transformName">The name of the transform.</param>
        /// <param name="jobName">The (unique) name of the job.</param>
        /// <param name="jobInput">The input of the job</param>
        /// <param name="outputAssetName">The (unique) name of the  output asset that will store the result of the encoding job. </param>
        public static async Task <Job> SubmitJobAsync(
            IAzureMediaServicesClient client,
            ILogger log,
            string resourceGroupName,
            string accountName,
            string transformName,
            string jobName,
            JobInput jobInput,
            string outputAssetName
            )
        {
            JobOutput[] jobOutputs =
            {
                new JobOutputAsset(outputAssetName),
            };

            // In this example, we are assuming that the job name is unique.
            //
            // If you already have a job with the desired name, use the Jobs.Get method
            // to get the existing job. In Media Services v3, Get methods on entities returns null
            // if the entity doesn't exist (a case-insensitive check on the name).
            Job job;

            try
            {
                log.LogInformation("Creating a job...");
                job = await client.Jobs.CreateAsync(
                    resourceGroupName,
                    accountName,
                    transformName,
                    jobName,
                    new Job
                {
                    Input   = jobInput,
                    Outputs = jobOutputs,
                });
            }
            catch (Exception exception)
            {
                if (exception.GetBaseException() is ApiErrorException apiException)
                {
                    log.LogError(
                        $"ERROR: API call failed with error code '{apiException.Body.Error.Code}' and message '{apiException.Body.Error.Message}'.");
                }
                throw;
            }

            return(job);
        }
Example #19
0
        private static void VerifyJobInput(JobInput expectedInput, JobInput actualInput, bool jobInputsAcceptable = true)
        {
            Type expectedInputType = expectedInput.GetType();

            Assert.Equal(expectedInputType, actualInput.GetType());

            if (typeof(JobInputAsset) == expectedInputType)
            {
                JobInputAsset expected = (JobInputAsset)expectedInput;
                JobInputAsset actual   = (JobInputAsset)actualInput;

                Assert.Equal(expected.AssetName, actual.AssetName);
            }
            else if (typeof(JobInputHttp) == expectedInputType)
            {
                JobInputHttp expected = (JobInputHttp)expectedInput;
                JobInputHttp actual   = (JobInputHttp)actualInput;

                Assert.Equal(expected.BaseUri, actual.BaseUri);
                Assert.Equal(expected.Label, actual.Label);
                Assert.Equal(expected.Files.Count, actual.Files.Count);

                for (int i = 0; i < expected.Files.Count; i++)
                {
                    Assert.Equal(expected.Files[i], actual.Files[i]);
                }
            }
            else if (typeof(JobInputs) == expectedInputType)
            {
                if (!jobInputsAcceptable)
                {
                    throw new InvalidOperationException("Only top level JobInputs are supported.");
                }

                JobInputs expected = (JobInputs)expectedInput;
                JobInputs actual   = (JobInputs)actualInput;

                Assert.Equal(expected.Inputs.Count, actual.Inputs.Count);

                for (int i = 0; i < expected.Inputs.Count; i++)
                {
                    VerifyJobInput(expected.Inputs[i], actual.Inputs[i], false);
                }
            }
            else
            {
                throw new InvalidOperationException($"Unexpected input type {expectedInputType.Name}");
            }
        }
Example #20
0
        public void TestDecompileInput_NoCompressor_NoIdentifier()
        {
            Image rawImg = Image.FromFile("compression_test.bmp");

            byte[]           img         = CompressionAssistant.ImageToBytes(rawImg);
            string           imgAsString = System.Text.Encoding.Default.GetString(img);
            XCData           data        = new XCData(imgAsString);
            XElement         element     = new XElement("test", data);
            JobXmlDecompiler d           = new JobXmlDecompiler();
            JobInput         i           = d.DecompileInput(element);

            Assert.IsNotNull(i.Input);
            Assert.IsNotNull(i.Identifier);
            Assert.AreEqual(string.Empty, i.Identifier);
        }
        public BaseResult ExecuteNow([FromBody] JobInput input)
        {
            var result = new BaseResult();

            try
            {
                _schedulerProvider.Scheduler.TriggerJob(new JobKey(input.Job, input.Group));
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
            }
            return(result);
        }
Example #22
0
        /// <summary>
        /// Processes a single input from the job
        /// </summary>
        /// <param name="pipeline">The Pipeline to use in processing</param>
        /// <param name="input">The input to be processed</param>
        /// <returns>true if the process is completed successfully</returns>
        private bool _handleNextInput(Pipeline.Pipeline pipeline, JobInput input)
        {
            lock ( _cancelPadlock )
            {
                if (_cancel)
                {
                    _ticket.Result = JobResult.Cancelled;
                    _ticket.State  = JobState.Cancelled;
                    _currentArgs.Persister.Delete(_ticket.JobID);
                    return(false);
                }
            }

            return(_tryRunInput(pipeline, input));
        }
Example #23
0
        public IActionResult PutJob([FromRoute] long id, [FromBody] JobInput job)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != job.Id)
            {
                return(BadRequest());
            }

            _jobService.AddOrUpdate(job);
            return(NoContent());
        }
Example #24
0
        /// <summary>
        /// Decompiles an Xml node back into a <see cref="JobInput"/>.
        /// </summary>
        /// <param name="inputNode">The <see cref="XNode"/> represnting the
        /// <see cref="JobInput."/></param>
        /// <returns>A <see cref="JobInput"/> object represented by the provided
        /// Xml.</returns>
        public JobInput DecompileInput(XNode inputNode)
        {
            if (inputNode.NodeType != XmlNodeType.Element)
            {
                throw new ArgumentException("An XElement is required.");
            }

            XElement element = (XElement)inputNode;
            Image    input   = _resolveInputImage(element);
            string   id      = _resolveInputID(element);

            JobInput theInput = new JobInput(input);

            theInput.Identifier = id;
            return(theInput);
        }
Example #25
0
        public IActionResult PostJob([FromBody] JobInput job)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_jobService.JobExists(job.Id))
            {
                return(new StatusCodeResult(StatusCodes.Status409Conflict));
            }

            _jobService.AddOrUpdate(job);

            return(CreatedAtAction("GetJob", new { id = job.Id }, job));
        }
Example #26
0
        private IdentificationResponse clipJob(JobInput input)
        {
            // perform
            Console.WriteLine("Installing ffmpeg...");
            var ffmpegPath = InstallDependencies.Run();
            var mp4        = DownloadClip.Run(input.Identifier);
            var result     = Recognition.Run(mp4);

            Console.WriteLine(result);
            Console.WriteLine("Finished...");
            // cleanup
            Console.WriteLine("Cleaning up...");
            System.IO.File.Delete("/tmp/audio.mp4");
            System.IO.File.Delete("/tmp/audio.mp4.cli.lo");

            return(result);
        }
Example #27
0
        public void AddOrUpdate(JobInput job)
        {
            var jobLocal = _context.Jobs.FirstOrDefault(x => x.Id == job.Id);

            var add = jobLocal == null;

            jobLocal = Mapper.Map(job, jobLocal);

            if (add)
            {
                _context.Jobs.Add(jobLocal);
            }
            else
            {
                _context.Jobs.Update(jobLocal);
            }
            _context.SaveChanges();
        }
Example #28
0
 /// <summary>
 /// Attempts to run an input
 /// </summary>
 /// <param name="pipeline">The pipeline to use in processing</param>
 /// <param name="input">The input to process</param>
 /// <returns>true if the input is processed without error;
 /// false otherwise</returns>
 private bool _tryRunInput(Pipeline.Pipeline pipeline, JobInput input)
 {
     try
     {
         _ticket.OnBeganInput();
         Image theInput = _processInput(pipeline, input);
         _currentArgs.Persister.Persist(
             _ticket.JobID, theInput, input.Identifier);
         return(true);
     }
     catch (Exception e)
     {
         JobResult r = new JobResult(e);
         _ticket.Result = r;
         _ticket.State  = JobState.Error;
         _ticket.OnJobError(e);
         return(false);
     }
 }
Example #29
0
    public void SendJob(JobInput j, EndPoint i)
    {
        TcpClient client = null;

        lock (connectedClients)
        {
            if (!connectedClients.TryGetValue(i, out client))
            {
                if (OnErrorLog != null)
                {
                    OnErrorLog("Tried to send job to not present external client " + i);
                }
                return;
            }
        }
        try
        {
            lock (client)
            {
                var wrapped = new PlatformIndependentWrapper(client);

                wrapped.WriteInt((int)ServerOpcodes.NEW_JOB);
                wrapped.WriteString(j.Guid);
                wrapped.WriteString(j.Src);
                wrapped.WriteInt(j.Key.Length);
                wrapped.WriteBytes(j.Key);
                wrapped.WriteInt(j.LargerThen ? 1 : 0);
                wrapped.WriteInt(j.Size);
                wrapped.WriteInt(j.ResultSize);
            }
        }
        catch (Exception e)
        {
            if (OnErrorLog != null)
            {
                OnErrorLog("Got " + e.GetType() + " while sending job to " + i);
            }
            client.Close();
            // nothing more to do here. HandleClient will remove this from connectedClients
        }
    }
Example #30
0
        private Job CreateJob(string transformName, MediaJob mediaJob)
        {
            Job job = null;

            try
            {
                MediaTransformPreset[] transformPresets = GetTransformPresets(transformName);
                JobInput                     jobInput   = GetJobInput(mediaJob);
                JobOutputAsset[]             jobOutputs = GetJobOutputs(transformPresets, mediaJob);
                IDictionary <string, string> jobPublish = GetJobPublish(mediaJob.OutputPublish);
                job = new Job()
                {
                    Description     = mediaJob.Description,
                    Priority        = mediaJob.Priority,
                    Input           = jobInput,
                    Outputs         = jobOutputs,
                    CorrelationData = jobPublish
                };
                job = _media.Jobs.Create(MediaAccount.ResourceGroupName, MediaAccount.Name, transformName, mediaJob.Name, job);
                SetOutputAssetLink(transformPresets, jobOutputs, mediaJob.OutputInsight);
            }
            catch (Exception ex)
            {
                Dictionary <string, string> exProperties = new Dictionary <string, string>
                {
                    { "Media Account", JsonConvert.SerializeObject(MediaAccount) },
                    { "Transform Name", transformName },
                    { "Media Job", JsonConvert.SerializeObject(mediaJob) }
                };
                if (job != null)
                {
                    exProperties.Add("Job", JsonConvert.SerializeObject(job));
                }
                TelemetryClient telemetryClient = new TelemetryClient();
                telemetryClient.TrackException(ex, exProperties);
                throw;
            }
            return(job);
        }