PageRenderTime 66ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/xbmc/interfaces/legacy/Player.cpp

https://github.com/alanwww1/xbmc
C++ | 502 lines | 389 code | 78 blank | 35 comment | 47 complexity | 24c1cdac31e623e4e67455fb57280f5c MD5 | raw file
  1. /*
  2. * Copyright (C) 2005-2013 Team XBMC
  3. * http://xbmc.org
  4. *
  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)
  8. * any later version.
  9. *
  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.
  14. *
  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/>.
  18. *
  19. */
  20. #include "Player.h"
  21. #include "ListItem.h"
  22. #include "PlayList.h"
  23. #include "PlayListPlayer.h"
  24. #include "settings/MediaSettings.h"
  25. #include "Application.h"
  26. #include "ApplicationMessenger.h"
  27. #include "GUIInfoManager.h"
  28. #include "AddonUtils.h"
  29. #include "utils/log.h"
  30. #include "cores/IPlayer.h"
  31. #include "settings/MediaSettings.h"
  32. namespace XBMCAddon
  33. {
  34. namespace xbmc
  35. {
  36. PlayParameter Player::defaultPlayParameter;
  37. Player::Player(int _playerCore)
  38. {
  39. iPlayList = PLAYLIST_MUSIC;
  40. if (_playerCore == EPC_DVDPLAYER ||
  41. _playerCore == EPC_MPLAYER ||
  42. _playerCore == EPC_PAPLAYER)
  43. playerCore = (EPLAYERCORES)_playerCore;
  44. else
  45. playerCore = EPC_NONE;
  46. // now that we're done, register hook me into the system
  47. if (languageHook)
  48. {
  49. DelayedCallGuard dc(languageHook);
  50. languageHook->RegisterPlayerCallback(this);
  51. }
  52. }
  53. Player::~Player()
  54. {
  55. deallocating();
  56. // we're shutting down so unregister me.
  57. if (languageHook)
  58. {
  59. DelayedCallGuard dc(languageHook);
  60. languageHook->UnregisterPlayerCallback(this);
  61. }
  62. }
  63. void Player::play(const Alternative<String, const PlayList* > & item,
  64. const XBMCAddon::xbmcgui::ListItem* listitem, bool windowed, int startpos)
  65. {
  66. XBMC_TRACE;
  67. if (&item == &defaultPlayParameter)
  68. playCurrent(windowed);
  69. else if (item.which() == XBMCAddon::first)
  70. playStream(item.former(), listitem, windowed);
  71. else // item is a PlayListItem
  72. playPlaylist(item.later(),windowed,startpos);
  73. }
  74. void Player::playStream(const String& item, const xbmcgui::ListItem* plistitem, bool windowed)
  75. {
  76. XBMC_TRACE;
  77. DelayedCallGuard dc(languageHook);
  78. if (!item.empty())
  79. {
  80. // set fullscreen or windowed
  81. CMediaSettings::Get().SetVideoStartWindowed(windowed);
  82. // force a playercore before playing
  83. g_application.m_eForcedNextPlayer = playerCore;
  84. const AddonClass::Ref<xbmcgui::ListItem> listitem(plistitem);
  85. if (listitem.isSet())
  86. {
  87. // set m_strPath to the passed url
  88. listitem->item->SetPath(item.c_str());
  89. CApplicationMessenger::Get().PlayFile((const CFileItem)(*(listitem->item)), false);
  90. }
  91. else
  92. {
  93. CFileItem nextitem(item, false);
  94. CApplicationMessenger::Get().MediaPlay(nextitem.GetPath());
  95. }
  96. }
  97. else
  98. playCurrent(windowed);
  99. }
  100. void Player::playCurrent(bool windowed)
  101. {
  102. XBMC_TRACE;
  103. DelayedCallGuard dc(languageHook);
  104. // set fullscreen or windowed
  105. CMediaSettings::Get().SetVideoStartWindowed(windowed);
  106. // force a playercore before playing
  107. g_application.m_eForcedNextPlayer = playerCore;
  108. // play current file in playlist
  109. if (g_playlistPlayer.GetCurrentPlaylist() != iPlayList)
  110. g_playlistPlayer.SetCurrentPlaylist(iPlayList);
  111. CApplicationMessenger::Get().PlayListPlayerPlay(g_playlistPlayer.GetCurrentSong());
  112. }
  113. void Player::playPlaylist(const PlayList* playlist, bool windowed, int startpos)
  114. {
  115. XBMC_TRACE;
  116. DelayedCallGuard dc(languageHook);
  117. if (playlist != NULL)
  118. {
  119. // set fullscreen or windowed
  120. CMediaSettings::Get().SetVideoStartWindowed(windowed);
  121. // force a playercore before playing
  122. g_application.m_eForcedNextPlayer = playerCore;
  123. // play a python playlist (a playlist from playlistplayer.cpp)
  124. iPlayList = playlist->getPlayListId();
  125. g_playlistPlayer.SetCurrentPlaylist(iPlayList);
  126. if (startpos > -1)
  127. g_playlistPlayer.SetCurrentSong(startpos);
  128. CApplicationMessenger::Get().PlayListPlayerPlay(startpos);
  129. }
  130. else
  131. playCurrent(windowed);
  132. }
  133. void Player::stop()
  134. {
  135. XBMC_TRACE;
  136. CApplicationMessenger::Get().MediaStop();
  137. }
  138. void Player::pause()
  139. {
  140. XBMC_TRACE;
  141. CApplicationMessenger::Get().MediaPause();
  142. }
  143. void Player::playnext()
  144. {
  145. XBMC_TRACE;
  146. DelayedCallGuard dc(languageHook);
  147. // force a playercore before playing
  148. g_application.m_eForcedNextPlayer = playerCore;
  149. CApplicationMessenger::Get().PlayListPlayerNext();
  150. }
  151. void Player::playprevious()
  152. {
  153. XBMC_TRACE;
  154. DelayedCallGuard dc(languageHook);
  155. // force a playercore before playing
  156. g_application.m_eForcedNextPlayer = playerCore;
  157. CApplicationMessenger::Get().PlayListPlayerPrevious();
  158. }
  159. void Player::playselected(int selected)
  160. {
  161. XBMC_TRACE;
  162. DelayedCallGuard dc(languageHook);
  163. // force a playercore before playing
  164. g_application.m_eForcedNextPlayer = playerCore;
  165. if (g_playlistPlayer.GetCurrentPlaylist() != iPlayList)
  166. {
  167. g_playlistPlayer.SetCurrentPlaylist(iPlayList);
  168. }
  169. g_playlistPlayer.SetCurrentSong(selected);
  170. CApplicationMessenger::Get().PlayListPlayerPlay(selected);
  171. //g_playlistPlayer.Play(selected);
  172. //CLog::Log(LOGNOTICE, "Current Song After Play: %i", g_playlistPlayer.GetCurrentSong());
  173. }
  174. void Player::OnPlayBackStarted()
  175. {
  176. XBMC_TRACE;
  177. invokeCallback(new CallbackFunction<Player>(this,&Player::onPlayBackStarted));
  178. }
  179. void Player::OnPlayBackEnded()
  180. {
  181. XBMC_TRACE;
  182. invokeCallback(new CallbackFunction<Player>(this,&Player::onPlayBackEnded));
  183. }
  184. void Player::OnPlayBackStopped()
  185. {
  186. XBMC_TRACE;
  187. invokeCallback(new CallbackFunction<Player>(this,&Player::onPlayBackStopped));
  188. }
  189. void Player::OnPlayBackPaused()
  190. {
  191. XBMC_TRACE;
  192. invokeCallback(new CallbackFunction<Player>(this,&Player::onPlayBackPaused));
  193. }
  194. void Player::OnPlayBackResumed()
  195. {
  196. XBMC_TRACE;
  197. invokeCallback(new CallbackFunction<Player>(this,&Player::onPlayBackResumed));
  198. }
  199. void Player::OnQueueNextItem()
  200. {
  201. XBMC_TRACE;
  202. invokeCallback(new CallbackFunction<Player>(this,&Player::onQueueNextItem));
  203. }
  204. void Player::OnPlayBackSpeedChanged(int speed)
  205. {
  206. XBMC_TRACE;
  207. invokeCallback(new CallbackFunction<Player,int>(this,&Player::onPlayBackSpeedChanged,speed));
  208. }
  209. void Player::OnPlayBackSeek(int time, int seekOffset)
  210. {
  211. XBMC_TRACE;
  212. invokeCallback(new CallbackFunction<Player,int,int>(this,&Player::onPlayBackSeek,time,seekOffset));
  213. }
  214. void Player::OnPlayBackSeekChapter(int chapter)
  215. {
  216. XBMC_TRACE;
  217. invokeCallback(new CallbackFunction<Player,int>(this,&Player::onPlayBackSeekChapter,chapter));
  218. }
  219. void Player::onPlayBackStarted()
  220. {
  221. XBMC_TRACE;
  222. }
  223. void Player::onPlayBackEnded()
  224. {
  225. XBMC_TRACE;
  226. }
  227. void Player::onPlayBackStopped()
  228. {
  229. XBMC_TRACE;
  230. }
  231. void Player::onPlayBackPaused()
  232. {
  233. XBMC_TRACE;
  234. }
  235. void Player::onPlayBackResumed()
  236. {
  237. XBMC_TRACE;
  238. }
  239. void Player::onQueueNextItem()
  240. {
  241. XBMC_TRACE;
  242. }
  243. void Player::onPlayBackSpeedChanged(int speed)
  244. {
  245. XBMC_TRACE;
  246. }
  247. void Player::onPlayBackSeek(int time, int seekOffset)
  248. {
  249. XBMC_TRACE;
  250. }
  251. void Player::onPlayBackSeekChapter(int chapter)
  252. {
  253. XBMC_TRACE;
  254. }
  255. bool Player::isPlaying()
  256. {
  257. XBMC_TRACE;
  258. return g_application.m_pPlayer->IsPlaying();
  259. }
  260. bool Player::isPlayingAudio()
  261. {
  262. XBMC_TRACE;
  263. return g_application.m_pPlayer->IsPlayingAudio();
  264. }
  265. bool Player::isPlayingVideo()
  266. {
  267. XBMC_TRACE;
  268. return g_application.m_pPlayer->IsPlayingVideo();
  269. }
  270. String Player::getPlayingFile() throw (PlayerException)
  271. {
  272. XBMC_TRACE;
  273. if (!g_application.m_pPlayer->IsPlaying())
  274. throw PlayerException("XBMC is not playing any file");
  275. return g_application.CurrentFile();
  276. }
  277. InfoTagVideo* Player::getVideoInfoTag() throw (PlayerException)
  278. {
  279. XBMC_TRACE;
  280. if (!g_application.m_pPlayer->IsPlayingVideo())
  281. throw PlayerException("XBMC is not playing any videofile");
  282. const CVideoInfoTag* movie = g_infoManager.GetCurrentMovieTag();
  283. if (movie)
  284. return new InfoTagVideo(*movie);
  285. return new InfoTagVideo();
  286. }
  287. InfoTagMusic* Player::getMusicInfoTag() throw (PlayerException)
  288. {
  289. XBMC_TRACE;
  290. if (g_application.m_pPlayer->IsPlayingVideo() || !g_application.m_pPlayer->IsPlayingAudio())
  291. throw PlayerException("XBMC is not playing any music file");
  292. const MUSIC_INFO::CMusicInfoTag* tag = g_infoManager.GetCurrentSongTag();
  293. if (tag)
  294. return new InfoTagMusic(*tag);
  295. return new InfoTagMusic();
  296. }
  297. double Player::getTotalTime() throw (PlayerException)
  298. {
  299. XBMC_TRACE;
  300. if (!g_application.m_pPlayer->IsPlaying())
  301. throw PlayerException("XBMC is not playing any media file");
  302. return g_application.GetTotalTime();
  303. }
  304. double Player::getTime() throw (PlayerException)
  305. {
  306. XBMC_TRACE;
  307. if (!g_application.m_pPlayer->IsPlaying())
  308. throw PlayerException("XBMC is not playing any media file");
  309. return g_application.GetTime();
  310. }
  311. void Player::seekTime(double pTime) throw (PlayerException)
  312. {
  313. XBMC_TRACE;
  314. if (!g_application.m_pPlayer->IsPlaying())
  315. throw PlayerException("XBMC is not playing any media file");
  316. g_application.SeekTime( pTime );
  317. }
  318. void Player::setSubtitles(const char* cLine)
  319. {
  320. XBMC_TRACE;
  321. if (g_application.m_pPlayer->HasPlayer())
  322. {
  323. int nStream = g_application.m_pPlayer->AddSubtitle(cLine);
  324. if(nStream >= 0)
  325. {
  326. g_application.m_pPlayer->SetSubtitle(nStream);
  327. g_application.m_pPlayer->SetSubtitleVisible(true);
  328. CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay = 0.0f;
  329. g_application.m_pPlayer->SetSubTitleDelay(CMediaSettings::Get().GetCurrentVideoSettings().m_SubtitleDelay);
  330. }
  331. }
  332. }
  333. void Player::showSubtitles(bool bVisible)
  334. {
  335. XBMC_TRACE;
  336. if (g_application.m_pPlayer->HasPlayer())
  337. {
  338. g_application.m_pPlayer->SetSubtitleVisible(bVisible != 0);
  339. }
  340. }
  341. String Player::getSubtitles()
  342. {
  343. XBMC_TRACE;
  344. if (g_application.m_pPlayer->HasPlayer())
  345. {
  346. SPlayerSubtitleStreamInfo info;
  347. g_application.m_pPlayer->GetSubtitleStreamInfo(g_application.m_pPlayer->GetSubtitle(), info);
  348. if (info.language.length() > 0)
  349. return info.language;
  350. else
  351. return info.name;
  352. }
  353. return NULL;
  354. }
  355. void Player::disableSubtitles()
  356. {
  357. XBMC_TRACE;
  358. CLog::Log(LOGWARNING,"'xbmc.Player().disableSubtitles()' is deprecated and will be removed in future releases, please use 'xbmc.Player().showSubtitles(false)' instead");
  359. if (g_application.m_pPlayer->HasPlayer())
  360. {
  361. g_application.m_pPlayer->SetSubtitleVisible(false);
  362. }
  363. }
  364. std::vector<String>* Player::getAvailableSubtitleStreams()
  365. {
  366. if (g_application.m_pPlayer->HasPlayer())
  367. {
  368. int subtitleCount = g_application.m_pPlayer->GetSubtitleCount();
  369. std::vector<String>* ret = new std::vector<String>(subtitleCount);
  370. for (int iStream=0; iStream < subtitleCount; iStream++)
  371. {
  372. SPlayerSubtitleStreamInfo info;
  373. g_application.m_pPlayer->GetSubtitleStreamInfo(iStream, info);
  374. if (info.language.length() > 0)
  375. (*ret)[iStream] = info.language;
  376. else
  377. (*ret)[iStream] = info.name;
  378. }
  379. return ret;
  380. }
  381. return NULL;
  382. }
  383. void Player::setSubtitleStream(int iStream)
  384. {
  385. if (g_application.m_pPlayer->HasPlayer())
  386. {
  387. int streamCount = g_application.m_pPlayer->GetSubtitleCount();
  388. if(iStream < streamCount)
  389. {
  390. g_application.m_pPlayer->SetSubtitle(iStream);
  391. g_application.m_pPlayer->SetSubtitleVisible(true);
  392. }
  393. }
  394. }
  395. std::vector<String>* Player::getAvailableAudioStreams()
  396. {
  397. if (g_application.m_pPlayer->HasPlayer())
  398. {
  399. int streamCount = g_application.m_pPlayer->GetAudioStreamCount();
  400. std::vector<String>* ret = new std::vector<String>(streamCount);
  401. for (int iStream=0; iStream < streamCount; iStream++)
  402. {
  403. SPlayerAudioStreamInfo info;
  404. g_application.m_pPlayer->GetAudioStreamInfo(iStream, info);
  405. if (info.language.length() > 0)
  406. (*ret)[iStream] = info.language;
  407. else
  408. (*ret)[iStream] = info.name;
  409. }
  410. return ret;
  411. }
  412. return NULL;
  413. }
  414. void Player::setAudioStream(int iStream)
  415. {
  416. if (g_application.m_pPlayer->HasPlayer())
  417. {
  418. int streamCount = g_application.m_pPlayer->GetAudioStreamCount();
  419. if(iStream < streamCount)
  420. g_application.m_pPlayer->SetAudioStream(iStream);
  421. }
  422. }
  423. }
  424. }