2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
22 #include "GUIDialogAudioSubtitleSettings.h"
23 #include "dialogs/GUIDialogFileBrowser.h"
24 #include "GUIPassword.h"
25 #include "utils/URIUtils.h"
26 #include "Application.h"
27 #include "video/VideoDatabase.h"
28 #include "dialogs/GUIDialogYesNo.h"
29 #include "filesystem/Directory.h"
30 #include "filesystem/File.h"
33 #include "addons/Skin.h"
34 #include "profiles/ProfilesManager.h"
35 #include "settings/AdvancedSettings.h"
36 #include "settings/MediaSettings.h"
37 #include "settings/MediaSourceSettings.h"
38 #include "settings/Settings.h"
39 #include "guilib/LocalizeStrings.h"
40 #include "pvr/PVRManager.h"
41 #include "cores/AudioEngine/Utils/AEUtil.h"
42 #include "cores/IPlayer.h"
43 #include "utils/LangCodeExpander.h"
46 using namespace XFILE;
49 #ifdef HAS_VIDEO_PLAYBACK
50 extern void xbox_audio_switch_channel(int iAudioStream, bool bAudioOnAllSpeakers); //lowlevel audio
53 CGUIDialogAudioSubtitleSettings::CGUIDialogAudioSubtitleSettings(void)
54 : CGUIDialogSettings(WINDOW_DIALOG_AUDIO_OSD_SETTINGS, "VideoOSDSettings.xml")
58 CGUIDialogAudioSubtitleSettings::~CGUIDialogAudioSubtitleSettings(void)
62 #define AUDIO_SETTINGS_VOLUME 1
63 #define AUDIO_SETTINGS_VOLUME_AMPLIFICATION 2
64 #define AUDIO_SETTINGS_DELAY 3
65 #define AUDIO_SETTINGS_STREAM 4
66 #define AUDIO_SETTINGS_OUTPUT_TO_ALL_SPEAKERS 5
67 #define AUDIO_SETTINGS_DIGITAL_ANALOG 6
70 #define SUBTITLE_SETTINGS_ENABLE 8
71 #define SUBTITLE_SETTINGS_DELAY 9
72 #define SUBTITLE_SETTINGS_STREAM 10
73 #define SUBTITLE_SETTINGS_BROWSER 11
74 #define AUDIO_SETTINGS_MAKE_DEFAULT 12
76 void CGUIDialogAudioSubtitleSettings::CreateSettings()
78 m_usePopupSliders = g_SkinInfo->HasSkinFile("DialogSlider.xml");
80 if (g_application.m_pPlayer)
82 g_application.m_pPlayer->GetAudioCapabilities(m_audioCaps);
83 g_application.m_pPlayer->GetSubtitleCapabilities(m_subCaps);
86 // clear out any old settings
88 // create our settings
89 m_volume = g_application.GetVolume(false);
90 AddSlider(AUDIO_SETTINGS_VOLUME, 13376, &m_volume, VOLUME_MINIMUM, VOLUME_MAXIMUM / 100.0f, VOLUME_MAXIMUM, PercentAsDecibel, false);
91 if (SupportsAudioFeature(IPC_AUD_AMP))
92 AddSlider(AUDIO_SETTINGS_VOLUME_AMPLIFICATION, 660, &CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification, VOLUME_DRC_MINIMUM * 0.01f, (VOLUME_DRC_MAXIMUM - VOLUME_DRC_MINIMUM) / 6000.0f, VOLUME_DRC_MAXIMUM * 0.01f, FormatDecibel, false);
93 if (g_application.m_pPlayer && g_application.m_pPlayer->IsPassthrough())
95 EnableSettings(AUDIO_SETTINGS_VOLUME,false);
96 EnableSettings(AUDIO_SETTINGS_VOLUME_AMPLIFICATION,false);
98 if (SupportsAudioFeature(IPC_AUD_OFFSET))
99 AddSlider(AUDIO_SETTINGS_DELAY, 297, &CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay, -g_advancedSettings.m_videoAudioDelayRange, .025f, g_advancedSettings.m_videoAudioDelayRange, FormatDelay);
100 if (SupportsAudioFeature(IPC_AUD_SELECT_STREAM))
101 AddAudioStreams(AUDIO_SETTINGS_STREAM);
103 // only show stuff available in digital mode if we have digital output
104 if (SupportsAudioFeature(IPC_AUD_OUTPUT_STEREO))
105 AddBool(AUDIO_SETTINGS_OUTPUT_TO_ALL_SPEAKERS, 252, &CMediaSettings::Get().GetCurrentVideoSettings().m_OutputToAllSpeakers, AUDIO_IS_BITSTREAM(CSettings::Get().GetInt("audiooutput.mode")));
107 int settings[3] = { 338, 339, 420 }; //ANALOG, IEC958, HDMI
108 m_outputmode = CSettings::Get().GetInt("audiooutput.mode");
109 if (SupportsAudioFeature(IPC_AUD_SELECT_OUTPUT))
110 AddSpin(AUDIO_SETTINGS_DIGITAL_ANALOG, 337, &m_outputmode, 3, settings);
113 m_subtitleVisible = g_application.m_pPlayer->GetSubtitleVisible();
114 AddBool(SUBTITLE_SETTINGS_ENABLE, 13397, &m_subtitleVisible);
115 if (SupportsSubtitleFeature(IPC_SUBS_OFFSET))
116 AddSlider(SUBTITLE_SETTINGS_DELAY, 22006, &CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay, -g_advancedSettings.m_videoSubsDelayRange, 0.1f, g_advancedSettings.m_videoSubsDelayRange, FormatDelay);
117 if (SupportsSubtitleFeature(IPC_SUBS_SELECT))
118 AddSubtitleStreams(SUBTITLE_SETTINGS_STREAM);
119 if (SupportsSubtitleFeature(IPC_SUBS_EXTERNAL))
120 AddButton(SUBTITLE_SETTINGS_BROWSER,13250);
121 AddButton(AUDIO_SETTINGS_MAKE_DEFAULT, 12376);
124 void CGUIDialogAudioSubtitleSettings::AddAudioStreams(unsigned int id)
128 setting.name = g_localizeStrings.Get(460);
129 setting.type = SettingInfo::SPIN;
131 setting.data = &m_audioStream;
133 // get the number of audio strams for the current movie
134 setting.max = (float)g_application.m_pPlayer->GetAudioStreamCount() - 1;
135 m_audioStream = g_application.m_pPlayer->GetAudioStream();
137 if( m_audioStream < 0 ) m_audioStream = 0;
139 // check if we have a single, stereo stream, and if so, allow us to split into
140 // left, right or both
143 CStdString strAudioInfo;
144 g_application.m_pPlayer->GetAudioInfo(strAudioInfo);
145 int iNumChannels = atoi(strAudioInfo.Right(strAudioInfo.size() - strAudioInfo.Find("chns:") - 5).c_str());
146 CStdString strAudioCodec = strAudioInfo.Mid(7, strAudioInfo.Find(") VBR") - 5);
147 bool bDTS = strstr(strAudioCodec.c_str(), "DTS") != 0;
148 bool bAC3 = strstr(strAudioCodec.c_str(), "AC3") != 0;
149 if (iNumChannels == 2 && !(bDTS || bAC3))
150 { // ok, enable these options
151 /* if (CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream == -1)
152 { // default to stereo stream
153 CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream = 0;
156 for (int i = 0; i <= setting.max; i++)
157 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(13320 + i)));
158 m_audioStream = -CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream - 1;
159 m_settings.push_back(setting);
164 // cycle through each audio stream and add it to our list control
165 for (int i = 0; i <= setting.max; ++i)
168 CStdString strLanguage;
170 SPlayerAudioStreamInfo info;
171 g_application.m_pPlayer->GetAudioStreamInfo(i, info);
173 if (!g_LangCodeExpander.Lookup(strLanguage, info.language))
174 strLanguage = g_localizeStrings.Get(13205); // Unknown
176 if (info.name.length() == 0)
177 strItem = strLanguage;
179 strItem.Format("%s - %s", strLanguage.c_str(), info.name.c_str());
181 strItem.AppendFormat(" (%i/%i)", i + 1, (int)setting.max + 1);
182 setting.entry.push_back(make_pair(setting.entry.size(), strItem));
185 if( setting.max < 0 )
188 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(231)));
191 m_settings.push_back(setting);
194 void CGUIDialogAudioSubtitleSettings::AddSubtitleStreams(unsigned int id)
199 setting.name = g_localizeStrings.Get(462);
200 setting.type = SettingInfo::SPIN;
202 setting.data = &m_subtitleStream;
203 m_subtitleStream = CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream;
205 if(m_subtitleStream < 0) m_subtitleStream = 0;
207 // get the number of audio strams for the current movie
208 setting.max = (float)g_application.m_pPlayer->GetSubtitleCount() - 1;
210 // cycle through each subtitle and add it to our entry list
211 for (int i = 0; i <= setting.max; ++i)
213 SPlayerSubtitleStreamInfo info;
214 g_application.m_pPlayer->GetSubtitleStreamInfo(i, info);
217 CStdString strLanguage;
219 if (!g_LangCodeExpander.Lookup(strLanguage, info.language))
220 strLanguage = g_localizeStrings.Get(13205); // Unknown
222 if (info.name.length() == 0)
223 strItem = strLanguage;
225 strItem.Format("%s - %s", strLanguage.c_str(), info.name.c_str());
227 strItem.AppendFormat(" (%i/%i)", i + 1, (int)setting.max + 1);
229 setting.entry.push_back(make_pair(setting.entry.size(), strItem));
233 { // no subtitle streams - just add a "None" entry
234 m_subtitleStream = 0;
236 setting.entry.push_back(make_pair(setting.entry.size(), g_localizeStrings.Get(231)));
238 m_settings.push_back(setting);
241 void CGUIDialogAudioSubtitleSettings::OnSettingChanged(SettingInfo &setting)
243 // check and update anything that needs it
244 if (setting.id == AUDIO_SETTINGS_VOLUME)
245 g_application.SetVolume(m_volume, false); //false - value is not in percent
246 else if (setting.id == AUDIO_SETTINGS_VOLUME_AMPLIFICATION)
248 if (g_application.m_pPlayer)
249 g_application.m_pPlayer->SetDynamicRangeCompression((long)(CMediaSettings::Get().GetCurrentVideoSettings().m_VolumeAmplification * 100));
251 else if (setting.id == AUDIO_SETTINGS_DELAY)
253 if (g_application.m_pPlayer)
254 g_application.m_pPlayer->SetAVDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_AudioDelay);
256 else if (setting.id == AUDIO_SETTINGS_STREAM)
258 // first check if it's a stereo track that we can change between stereo, left and right
259 if (g_application.m_pPlayer->GetAudioStreamCount() == 1)
261 if (setting.max == 2)
262 { // we're in the case we want - call the code to switch channels etc.
263 // update the screen setting...
264 CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream = -1 - m_audioStream;
265 // call monkeyh1's code here...
266 //bool bAudioOnAllSpeakers = (CSettings::Get().GetInt("audiooutput.mode") == AUDIO_IEC958) && CMediaSettings::Get().GetCurrentVideoSettings().m_OutputToAllSpeakers;
270 // only change the audio stream if a different one has been asked for
271 if (g_application.m_pPlayer->GetAudioStream() != m_audioStream)
273 CMediaSettings::Get().GetCurrentVideoSettings().m_AudioStream = m_audioStream;
274 g_application.m_pPlayer->SetAudioStream(m_audioStream); // Set the audio stream to the one selected
275 EnableSettings(AUDIO_SETTINGS_VOLUME, !g_application.m_pPlayer->IsPassthrough());
278 else if (setting.id == AUDIO_SETTINGS_OUTPUT_TO_ALL_SPEAKERS)
280 g_application.Restart();
282 else if (setting.id == AUDIO_SETTINGS_DIGITAL_ANALOG)
284 bool bitstream = false;
288 case 0: CSettings::Get().SetInt("audiooutput.mode", AUDIO_ANALOG ); break;
289 case 1: CSettings::Get().SetInt("audiooutput.mode", AUDIO_IEC958 ); bitstream = true; break;
290 case 2: CSettings::Get().SetInt("audiooutput.mode", AUDIO_HDMI ); bitstream = true; break;
293 EnableSettings(AUDIO_SETTINGS_OUTPUT_TO_ALL_SPEAKERS, bitstream);
294 g_application.Restart();
295 EnableSettings(AUDIO_SETTINGS_VOLUME, !g_application.m_pPlayer->IsPassthrough());
297 else if (setting.id == SUBTITLE_SETTINGS_ENABLE)
299 CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn = m_subtitleVisible;
300 g_application.m_pPlayer->SetSubtitleVisible(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleOn);
302 else if (setting.id == SUBTITLE_SETTINGS_DELAY)
304 g_application.m_pPlayer->SetSubTitleDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay);
306 else if (setting.id == SUBTITLE_SETTINGS_STREAM && setting.max > 0)
308 CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleStream = m_subtitleStream;
309 g_application.m_pPlayer->SetSubtitle(m_subtitleStream);
311 else if (setting.id == SUBTITLE_SETTINGS_BROWSER)
314 if (URIUtils::IsInRAR(g_application.CurrentFileItem().GetPath()) || URIUtils::IsInZIP(g_application.CurrentFileItem().GetPath()))
316 CURL url(g_application.CurrentFileItem().GetPath());
317 strPath = url.GetHostName();
320 strPath = g_application.CurrentFileItem().GetPath();
322 CStdString strMask = ".utf|.utf8|.utf-8|.sub|.srt|.smi|.rt|.txt|.ssa|.aqt|.jss|.ass|.idx|.rar|.zip";
323 if (g_application.GetCurrentPlayer() == EPC_DVDPLAYER)
324 strMask = ".srt|.rar|.zip|.ifo|.smi|.sub|.idx|.ass|.ssa|.txt";
325 VECSOURCES shares(*CMediaSourceSettings::Get().GetSources("video"));
326 if (CMediaSettings::Get().GetAdditionalSubtitleDirectoryChecked() != -1 && !CSettings::Get().GetString("subtitles.custompath").empty())
329 std::vector<CStdString> paths;
331 URIUtils::GetDirectory(strPath,strPath1);
332 paths.push_back(strPath1);
333 strPath1 = CSettings::Get().GetString("subtitles.custompath");
334 paths.push_back(CSettings::Get().GetString("subtitles.custompath"));
335 share.FromNameAndPaths("video",g_localizeStrings.Get(21367),paths);
336 shares.push_back(share);
337 strPath = share.strPath;
338 URIUtils::AddSlashAtEnd(strPath);
340 if (CGUIDialogFileBrowser::ShowAndGetFile(shares,strMask,g_localizeStrings.Get(293),strPath,false,true)) // "subtitles"
342 if (URIUtils::HasExtension(strPath, ".sub"))
343 if (CFile::Exists(URIUtils::ReplaceExtension(strPath, ".idx")))
344 strPath = URIUtils::ReplaceExtension(strPath, ".idx");
346 int id = g_application.m_pPlayer->AddSubtitle(strPath);
349 m_subtitleStream = id;
350 g_application.m_pPlayer->SetSubtitle(m_subtitleStream);
351 g_application.m_pPlayer->SetSubtitleVisible(true);
353 CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleCached = true;
357 else if (setting.id == AUDIO_SETTINGS_MAKE_DEFAULT)
359 if (CProfilesManager::Get().GetCurrentProfile().settingsLocked() &&
360 CProfilesManager::Get().GetMasterProfile().getLockMode() != ::LOCK_MODE_EVERYONE)
361 if (!g_passwordManager.IsMasterLockUnlocked(true))
364 // prompt user if they are sure
365 if (CGUIDialogYesNo::ShowAndGetInput(12376, 750, 0, 12377))
366 { // reset the settings
369 db.EraseVideoSettings();
371 CMediaSettings::Get().GetDefaultVideoSettings() = CMediaSettings::Get().GetCurrentVideoSettings();
372 CMediaSettings::Get().GetDefaultVideoSettings().m_SubtitleStream = -1;
373 CMediaSettings::Get().GetDefaultVideoSettings().m_AudioStream = -1;
374 CSettings::Get().Save();
378 if (g_PVRManager.IsPlayingRadio() || g_PVRManager.IsPlayingTV())
379 g_PVRManager.TriggerSaveChannelSettings();
382 void CGUIDialogAudioSubtitleSettings::FrameMove()
384 m_volume = g_application.GetVolume(false);
385 UpdateSetting(AUDIO_SETTINGS_VOLUME);
386 if (g_application.m_pPlayer)
388 // these settings can change on the fly
389 UpdateSetting(AUDIO_SETTINGS_DELAY);
390 UpdateSetting(SUBTITLE_SETTINGS_ENABLE);
391 UpdateSetting(SUBTITLE_SETTINGS_DELAY);
393 CGUIDialogSettings::FrameMove();
396 CStdString CGUIDialogAudioSubtitleSettings::PercentAsDecibel(float value, float interval)
399 text.Format("%2.1f dB", CAEUtil::PercentToGain(value));
403 CStdString CGUIDialogAudioSubtitleSettings::FormatDecibel(float value, float interval)
406 text.Format("%2.1f dB", value);
410 CStdString CGUIDialogAudioSubtitleSettings::FormatDelay(float value, float interval)
413 if (fabs(value) < 0.5f*interval)
414 text.Format(g_localizeStrings.Get(22003).c_str(), 0.0);
416 text.Format(g_localizeStrings.Get(22004).c_str(), fabs(value));
418 text.Format(g_localizeStrings.Get(22005).c_str(), value);
422 bool CGUIDialogAudioSubtitleSettings::SupportsAudioFeature(int feature)
424 for (Features::iterator itr = m_audioCaps.begin(); itr != m_audioCaps.end(); itr++)
426 if(*itr == feature || *itr == IPC_AUD_ALL)
432 bool CGUIDialogAudioSubtitleSettings::SupportsSubtitleFeature(int feature)
434 for (Features::iterator itr = m_subCaps.begin(); itr != m_subCaps.end(); itr++)
436 if(*itr == feature || *itr == IPC_SUBS_ALL)