예제 #1
0
		public void StartEncode(EncodeJob job, bool preview, int previewNumber, int previewSeconds, double overallSelectedLengthSeconds, int verbosity, int previewCount, bool useDvdNav)
		{
			CurrentEncoder = this;
			this.callback = OperationContext.Current.GetCallbackChannel<IHandBrakeEncoderCallback>();

			try
			{
				if (this.callback == null)
				{
					throw new ArgumentException("Could not get callback channel.");
				}

				HandBrakeUtils.MessageLogged += (o, e) =>
					{
						this.StopOnException(() =>
							{
								this.callback.OnMessageLogged(e.Message);
							});
					};

				HandBrakeUtils.ErrorLogged += (o, e) =>
					{
						this.StopOnException(() =>
							{
								this.callback.OnErrorLogged(e.Message);
							});
					};

				HandBrakeUtils.SetDvdNav(useDvdNav);

				this.instance = new HandBrakeInstance();
				this.instance.Initialize(verbosity);

				this.instance.ScanCompleted += (o, e) =>
					{
						try
						{
							Title encodeTitle = this.instance.Titles.FirstOrDefault(title => title.TitleNumber == job.Title);

							if (encodeTitle != null)
							{
								lock (this.encodeLock)
								{
									this.instance.StartEncode(job, preview, previewNumber, previewSeconds, overallSelectedLengthSeconds, previewCount);
									this.callback.OnEncodeStarted();
									this.state = EncodeState.Encoding;
								}
							}
							else
							{
								this.callback.OnEncodeComplete(true);
								this.CleanUpAndSignalCompletion();
							}
						}
						catch (Exception exception)
						{
							this.callback.OnException(exception.ToString());
							this.CleanUpAndSignalCompletion();
						}
					};

				this.instance.EncodeProgress += (o, e) =>
					{
						this.StopOnException(() =>
							{
								this.callback.OnEncodeProgress(e.AverageFrameRate, e.CurrentFrameRate, e.EstimatedTimeLeft, e.FractionComplete, e.Pass);
							});
					};

				this.instance.EncodeCompleted += (o, e) =>
					{
						this.state = EncodeState.Finished;

						try
						{
							this.callback.OnEncodeComplete(e.Error);
						}
						catch (CommunicationException exception)
						{
							WorkerLogger.Log("Got exception when reporting completion: " + exception, isError: true);
						}
						finally
						{
							this.CleanUpAndSignalCompletion();
						}
					};

				this.instance.StartScan(job.SourcePath, previewCount, job.Title);
				this.state = EncodeState.Scanning;
			}
			catch (Exception exception)
			{
				this.callback.OnException(exception.ToString());
				throw;
			}
		}
예제 #2
0
        public void StartEncode(EncodeJob job, bool preview, int previewNumber, int previewSeconds, double overallSelectedLengthSeconds, int verbosity, int previewCount, bool useDvdNav)
        {
            CurrentEncoder = this;
            this.callback  = OperationContext.Current.GetCallbackChannel <IHandBrakeEncoderCallback>();

            try
            {
                if (this.callback == null)
                {
                    throw new ArgumentException("Could not get callback channel.");
                }

                HandBrakeUtils.MessageLogged += (o, e) =>
                {
                    this.StopOnException(() =>
                    {
                        this.callback.OnMessageLogged(e.Message);
                    });
                };

                HandBrakeUtils.ErrorLogged += (o, e) =>
                {
                    this.StopOnException(() =>
                    {
                        this.callback.OnErrorLogged(e.Message);
                    });
                };

                HandBrakeUtils.SetDvdNav(useDvdNav);

                this.instance = new HandBrakeInstance();
                this.instance.Initialize(verbosity);

                this.instance.ScanCompleted += (o, e) =>
                {
                    try
                    {
                        Title encodeTitle = this.instance.Titles.FirstOrDefault(title => title.TitleNumber == job.Title);

                        if (encodeTitle != null)
                        {
                            lock (this.encodeLock)
                            {
                                this.instance.StartEncode(job, preview, previewNumber, previewSeconds, overallSelectedLengthSeconds, previewCount);
                                this.callback.OnEncodeStarted();
                                this.state = EncodeState.Encoding;
                            }
                        }
                        else
                        {
                            this.callback.OnEncodeComplete(true);
                            this.CleanUpAndSignalCompletion();
                        }
                    }
                    catch (Exception exception)
                    {
                        this.callback.OnException(exception.ToString());
                        this.CleanUpAndSignalCompletion();
                    }
                };

                this.instance.EncodeProgress += (o, e) =>
                {
                    this.StopOnException(() =>
                    {
                        this.callback.OnEncodeProgress(e.AverageFrameRate, e.CurrentFrameRate, e.EstimatedTimeLeft, e.FractionComplete, e.Pass);
                    });
                };

                this.instance.EncodeCompleted += (o, e) =>
                {
                    this.state = EncodeState.Finished;

                    try
                    {
                        this.callback.OnEncodeComplete(e.Error);
                    }
                    catch (CommunicationException exception)
                    {
                        WorkerLogger.Log("Got exception when reporting completion: " + exception, isError: true);
                    }
                    finally
                    {
                        this.CleanUpAndSignalCompletion();
                    }
                };

                this.instance.StartScan(job.SourcePath, previewCount, job.Title);
                this.state = EncodeState.Scanning;
            }
            catch (Exception exception)
            {
                this.callback.OnException(exception.ToString());
                throw;
            }
        }