// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// The Audio View Model
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.ViewModels
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Caliburn.Micro;
using HandBrake.Interop.Interop;
using HandBrake.Interop.Interop.Interfaces.Model.Encoders;
using HandBrake.Interop.Utilities;
using HandBrakeWPF.EventArgs;
using HandBrakeWPF.Model.Audio;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Services.Presets.Model;
using HandBrakeWPF.Services.Scan.Model;
using HandBrakeWPF.Utilities;
using HandBrakeWPF.ViewModels.Interfaces;
using AudioEncoder = Services.Encode.Model.Models.AudioEncoder;
using AudioTrack = Services.Encode.Model.Models.AudioTrack;
using EncodeTask = Services.Encode.Model.EncodeTask;
using OutputFormat = Services.Encode.Model.Models.OutputFormat;
///
/// The Audio View Model
///
public class AudioViewModel : ViewModelBase, IAudioViewModel
{
private readonly IWindowManager windowManager;
private IEnumerable sourceTracks;
private Preset currentPreset;
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
///
/// The window manager.
///
///
/// The user Setting Service.
///
public AudioViewModel(IWindowManager windowManager, IUserSettingService userSettingService)
{
this.windowManager = windowManager;
this.Task = new EncodeTask();
this.AudioDefaultsViewModel = new AudioDefaultsViewModel(this.Task);
this.SampleRates = new ObservableCollection { "Auto" };
foreach (var item in HandBrakeEncoderHelpers.AudioSampleRates)
{
this.SampleRates.Add(item.Name);
}
this.AudioEncoders = EnumHelper.GetEnumList();
this.SourceTracks = new List();
}
#endregion
public event EventHandler TabStatusChanged;
#region Properties
///
/// Gets or sets the audio defaults view model.
///
public IAudioDefaultsViewModel AudioDefaultsViewModel { get; set; }
///
/// Gets or sets AudioEncoders.
///
public IEnumerable AudioEncoders { get; set; }
///
/// Gets or sets SampleRates.
///
public IList SampleRates { get; set; }
///
/// Gets or sets SourceTracks.
///
public IEnumerable SourceTracks
{
get
{
return this.sourceTracks;
}
set
{
this.sourceTracks = value;
this.NotifyOfPropertyChange(() => this.SourceTracks);
}
}
///
/// Gets or sets the EncodeTask.
///
public EncodeTask Task { get; set; }
///
/// Gets the panel title.
///
public string PanelTitle
{
get
{
return Resources.AudioViewModel_AudioTracks;
}
}
///
/// Gets the switch display title.
///
public string SwitchDisplayTitle
{
get
{
return Resources.AudioViewModel_ConfigureDefaults;
}
}
///
/// Gets the default audio behaviours.
///
public AudioBehaviours AudioBehaviours
{
get
{
return this.AudioDefaultsViewModel.AudioBehaviours;
}
}
#endregion
#region Public Methods
///
/// Add an Audio Track
///
public void Add()
{
// Add the first track if available.
this.Add(null, false);
}
///
/// The add all remaining.
///
public void AddAllRemaining()
{
this.AddAllRemainingTracks();
}
///
/// Remove the Selected Track
///
///
/// The track.
///
public void Remove(AudioTrack track)
{
this.Task.AudioTracks.Remove(track);
}
///
/// Clear out the Audio Tracks
///
public void Clear()
{
this.Task.AudioTracks.Clear();
}
///
/// Reload the audio tracks based on the defaults.
///
public void ReloadDefaults()
{
this.SetupTracks();
}
///
/// Trigger a Notify Property Changed on the Task to force various UI elements to update.
///
public void RefreshTask()
{
this.NotifyOfPropertyChange(() => this.Task);
if (this.Task.OutputFormat == OutputFormat.Mp4)
{
foreach (AudioTrack track in this.Task.AudioTracks.Where(track => track.Encoder == AudioEncoder.ffflac || track.Encoder == AudioEncoder.Vorbis))
{
track.Encoder = AudioEncoder.ffaac;
}
}
if (this.Task.OutputFormat == OutputFormat.WebM)
{
foreach (AudioTrack track in this.Task.AudioTracks.Where(track => track.Encoder != AudioEncoder.Vorbis && track.Encoder != AudioEncoder.Opus))
{
track.Encoder = AudioEncoder.Vorbis;
}
}
this.AudioDefaultsViewModel.RefreshTask();
}
///
/// The show audio defaults.
///
public void ShowAudioDefaults()
{
if (this.windowManager.ShowDialogAsync(this.AudioDefaultsViewModel).Result == true)
{
this.OnTabStatusChanged(null);
}
}
#endregion
#region Implemented Interfaces
///
/// Setup this tab for the specified preset.
///
///
/// The preset.
///
///
/// The task.
///
public void SetPreset(Preset preset, EncodeTask task)
{
this.Task = task;
this.currentPreset = preset;
// Audio Behaviours
this.AudioDefaultsViewModel.Setup(preset, task);
if (preset != null && preset.Task != null)
{
this.SetupTracks();
}
this.NotifyOfPropertyChange(() => this.Task);
}
///
/// Update all the UI controls based on the encode task passed in.
///
///
/// The task.
///
public void UpdateTask(EncodeTask task)
{
this.Task = task;
this.NotifyOfPropertyChange(() => this.Task.AudioTracks);
this.NotifyOfPropertyChange(() => this.Task);
}
public bool MatchesPreset(Preset preset)
{
// Check the default behaviours still match the preset.
if (preset.AudioTrackBehaviours.SelectedBehaviour != this.AudioBehaviours.SelectedBehaviour)
{
return false;
}
if (preset.AudioTrackBehaviours.SelectedTrackDefaultBehaviour
!= this.AudioBehaviours.SelectedTrackDefaultBehaviour)
{
return false;
}
foreach (var item in this.AudioBehaviours.SelectedLangauges)
{
if (!preset.AudioTrackBehaviours.SelectedLangauges.Contains(item))
{
return false;
}
}
if (preset.Task.AllowedPassthruOptions.AudioAllowMP2Pass != this.Task.AllowedPassthruOptions.AudioAllowMP2Pass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowMP3Pass != this.Task.AllowedPassthruOptions.AudioAllowMP3Pass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowAACPass != this.Task.AllowedPassthruOptions.AudioAllowAACPass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowAC3Pass != this.Task.AllowedPassthruOptions.AudioAllowAC3Pass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowEAC3Pass != this.Task.AllowedPassthruOptions.AudioAllowEAC3Pass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowDTSPass != this.Task.AllowedPassthruOptions.AudioAllowDTSPass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowDTSHDPass != this.Task.AllowedPassthruOptions.AudioAllowDTSHDPass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowTrueHDPass != this.Task.AllowedPassthruOptions.AudioAllowTrueHDPass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioAllowFlacPass != this.Task.AllowedPassthruOptions.AudioAllowFlacPass)
{
return false;
}
if (preset.Task.AllowedPassthruOptions.AudioEncoderFallback != this.Task.AllowedPassthruOptions.AudioEncoderFallback)
{
return false;
}
foreach (var language in preset.AudioTrackBehaviours.SelectedLangauges)
{
if (!this.AudioBehaviours.SelectedLangauges.Contains(language))
{
return false;
}
}
if (preset.AudioTrackBehaviours.SelectedLangauges.Count != this.AudioBehaviours.SelectedLangauges.Count)
{
return false;
}
if (preset.AudioTrackBehaviours.BehaviourTracks.Count != this.AudioBehaviours.BehaviourTracks.Count)
{
return false;
}
return true;
}
///
/// Set the Source Title
///
///
/// The source.
///
///
/// The title.
///
///
/// The preset.
///
///
/// The task.
///
public void SetSource(Source source, Title title, Preset preset, EncodeTask task)
{
this.SourceTracks = title.AudioTracks;
// Only reset the audio tracks if we have none, or if the task is null.
if (this.Task == null)
{
this.SetPreset(preset, task);
}
// If there are no source tracks, clear the list, otherwise try to Auto-Select the correct tracks
if (this.SourceTracks == null || !this.SourceTracks.Any())
{
this.Task.AudioTracks.Clear();
}
else
{
this.SetupTracks();
}
// Force UI Updates
this.NotifyOfPropertyChange(() => this.Task);
}
#endregion
#region Methods
protected virtual void OnTabStatusChanged(TabStatusEventArgs e)
{
this.TabStatusChanged?.Invoke(this, e);
}
///
/// Add the specified source track, or the first track in the SourceTracks collection if available.
///
///
/// The source track.
///
///
/// The use Behaviour Template Mode.
///
private void Add(Audio sourceTrack, bool useBehaviourTemplateMode)
{
if (this.SourceTracks != null)
{
Audio track = sourceTrack ?? this.GetPreferredAudioTrack();
if (track != null)
{
if (!useBehaviourTemplateMode)
{
this.Task.AudioTracks.Add(new AudioTrack { ScannedTrack = track });
return;
}
switch (this.AudioBehaviours.SelectedTrackDefaultBehaviour)
{
case AudioTrackDefaultsMode.FirstTrack:
AudioBehaviourTrack template = this.AudioBehaviours.BehaviourTracks.FirstOrDefault();
if (this.CanAddTrack(template, track, this.Task.AllowedPassthruOptions.AudioEncoderFallback))
{
this.Task.AudioTracks.Add( template != null ? new AudioTrack(template, track, this.Task.AllowedPassthruOptions, this.Task.OutputFormat) : new AudioTrack { ScannedTrack = track });
}
break;
case AudioTrackDefaultsMode.AllTracks:
foreach (AudioBehaviourTrack tmpl in this.AudioBehaviours.BehaviourTracks)
{
if (this.CanAddTrack(tmpl, track, this.Task.AllowedPassthruOptions.AudioEncoderFallback))
{
this.Task.AudioTracks.Add(tmpl != null ? new AudioTrack(tmpl, track, this.Task.AllowedPassthruOptions, this.Task.OutputFormat) : new AudioTrack { ScannedTrack = track });
}
}
break;
}
}
}
}
private bool CanAddTrack(AudioBehaviourTrack track, Audio sourceTrack, AudioEncoder fallback)
{
if (fallback == AudioEncoder.None && track != null)
{
HBAudioEncoder encoderInfo = HandBrakeEncoderHelpers.GetAudioEncoder(EnumHelper.GetShortName(track.Encoder));
if (track.IsPassthru && (sourceTrack.Codec & encoderInfo.Id) == 0)
{
return false;
}
}
return true;
}
///
/// Attempt to automatically select the correct audio tracks based on the users settings.
///
private void SetupTracks()
{
if (!this.SourceTracks.Any())
{
// Clear out the old tracks
this.Task.AudioTracks.Clear();
return;
}
// Step 1, Cleanup Previous Tracks
this.Task.AudioTracks.Clear();
// Step 2, Sanity Check
if (this.SourceTracks == null || !this.SourceTracks.Any())
{
return;
}
// Step 3, Setup the tracks from the preset
foreach (AudioBehaviourTrack track in this.AudioBehaviours.BehaviourTracks)
{
Audio sourceTrack = this.GetPreferredAudioTrack();
if (this.CanAddTrack(track, sourceTrack, this.Task.AllowedPassthruOptions.AudioEncoderFallback))
{
this.Task.AudioTracks.Add(new AudioTrack(track, sourceTrack, this.Task.AllowedPassthruOptions, this.Task.OutputFormat));
}
}
// Step 4, Handle the default selection behaviour.
switch (this.AudioBehaviours.SelectedBehaviour)
{
case AudioBehaviourModes.None:
this.Task.AudioTracks.Clear();
break;
case AudioBehaviourModes.FirstMatch: // Adding all remaining audio tracks
this.AddFirstForSelectedLanguages();
break;
case AudioBehaviourModes.AllMatching: // Add Langauges tracks for the additional languages selected, in-order.
this.AddAllRemainingForSelectedLanguages();
break;
}
}
///
/// The add first for selected languages.
///
private void AddFirstForSelectedLanguages()
{
bool anyLanguageSelected = this.AudioBehaviours.SelectedLangauges.Contains(Constants.Any);
if (anyLanguageSelected && this.Task.AudioTracks.Count >= 1)
{
return;
}
foreach (Audio sourceTrack in this.GetSelectedLanguagesTracks())
{
// Step 2: Check if the track list already contains this track
bool found = this.Task.AudioTracks.Any(audioTrack => Equals(audioTrack.ScannedTrack, sourceTrack));
if (!found)
{
// Check if we are already using this language
bool foundLanguage = false;
foreach (var item in this.Task.AudioTracks.Where(item => item.ScannedTrack != null && sourceTrack.LanguageCode.Contains(item.ScannedTrack.LanguageCode)))
{
foundLanguage = true;
}
if (foundLanguage)
{
continue;
}
// If it doesn't, add it.
this.Add(sourceTrack, true);
// If we are using "(Any)" then break here. We only add the first track in this instance.
if (anyLanguageSelected)
{
break;
}
}
}
}
///
/// Add all source tracks that don't currently exist on the list.
///
private void AddAllRemainingTracks()
{
// For all the source audio tracks
foreach (Audio sourceTrack in this.SourceTracks)
{
// Step 2: Check if the track list already contrains this track
bool found = this.Task.AudioTracks.Any(audioTrack => Equals(audioTrack.ScannedTrack, sourceTrack));
if (!found)
{
// If it doesn't, add it.
this.Add(sourceTrack, true);
}
}
}
///
/// Add all remaining for selected languages.
///
public void AddAllRemainingForSelectedLanguages()
{
// Add them if they are not already added.
foreach (Audio sourceTrack in this.GetSelectedLanguagesTracks())
{
// Step 2: Check if the track list already contrains this track
bool found = this.Task.AudioTracks.Any(audioTrack => Equals(audioTrack.ScannedTrack, sourceTrack));
if (!found)
{
// If it doesn't, add it.
this.Add(sourceTrack, true);
}
}
}
///
/// The get preferred audio track, or the first if none available.
///
///
/// The users preferred language, or the first if none available.
///
private Audio GetPreferredAudioTrack()
{
// The first track in the selected languages list is considered the preferred language.
// So, try match tracks on this.
IEnumerable preferredAudioTracks = new List();
if (this.AudioBehaviours.SelectedLangauges.Count > 0)
{
string langName = this.AudioBehaviours.SelectedLangauges.FirstOrDefault(w => !w.Equals(Constants.Any));
string langCode = LanguageUtilities.GetLanguageCode(langName);
if (!string.IsNullOrEmpty(langCode))
{
preferredAudioTracks = this.SourceTracks.Where(item => item.LanguageCode.Contains(langCode));
}
}
return preferredAudioTracks.FirstOrDefault() ?? this.SourceTracks.FirstOrDefault();
}
///
/// Gets a list of source tracks for the users selected languages.
///
///
/// A list of source audio tracks.
///
private IEnumerable GetSelectedLanguagesTracks()
{
// Translate to Iso Codes
List iso6392Codes = new List();
if (this.AudioBehaviours.SelectedLangauges.Contains(Constants.Any))
{
iso6392Codes = LanguageUtilities.GetIsoCodes();
iso6392Codes = LanguageUtilities.OrderIsoCodes(iso6392Codes, this.AudioBehaviours.SelectedLangauges);
}
else
{
iso6392Codes = LanguageUtilities.GetLanguageCodes(this.AudioBehaviours.SelectedLangauges.ToArray());
}
List orderedTracks = new List();
foreach (string code in iso6392Codes)
{
orderedTracks.AddRange(this.SourceTracks.Where(audio => audio.LanguageCode == code));
}
return orderedTracks;
}
#endregion
}
}