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 "CodecFactory.h"
24 #include "settings/AdvancedSettings.h"
25 #include "settings/Settings.h"
26 #include "music/tags/MusicInfoTag.h"
27 #include "utils/TimeUtils.h"
28 #include "utils/log.h"
29 #include "utils/MathUtils.h"
30 #include "utils/JobManager.h"
32 #include "threads/SingleLock.h"
33 #include "cores/AudioEngine/AEFactory.h"
34 #include "cores/AudioEngine/Utils/AEUtil.h"
35 #include "cores/AudioEngine/Interfaces/AEStream.h"
37 #define TIME_TO_CACHE_NEXT_FILE 5000 /* 5 seconds before end of song, start caching the next song */
38 #define FAST_XFADE_TIME 80 /* 80 milliseconds */
39 #define MAX_SKIP_XFADE_TIME 2000 /* max 2 seconds crossfade on track skip */
41 CAEChannelInfo ICodec::GetChannelInfo()
43 return CAEUtil::GuessChLayout(m_Channels);
46 class CQueueNextFileJob : public CJob
52 CQueueNextFileJob(const CFileItem& item, PAPlayer &player)
53 : m_item(item), m_player(player) {}
54 virtual ~CQueueNextFileJob() {}
57 return m_player.QueueNextFileEx(m_item, true, true);
61 // PAP: Psycho-acoustic Audio Player
62 // Supporting all open audio codec standards.
63 // First one being nullsoft's nsv audio decoder format
65 PAPlayer::PAPlayer(IPlayerCallback& callback) :
68 m_signalSpeedChange (false),
73 m_defaultCrossfadeMS (0),
74 m_upcomingCrossfadeMS(0),
75 m_currentStream (NULL ),
76 m_audioCallback (NULL ),
77 m_FileItem (new CFileItem()),
79 m_continueStream (false)
81 memset(&m_playerGUIData, 0, sizeof(m_playerGUIData));
90 bool PAPlayer::HandlesType(const CStdString &type)
92 ICodec* codec = CodecFactory::CreateCodec(type);
93 if (codec && codec->CanInit())
102 void PAPlayer::SoftStart(bool wait/* = false */)
104 CSharedLock lock(m_streamsLock);
105 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
107 StreamInfo* si = *itt;
108 if (si->m_fadeOutTriggered)
111 si->m_stream->Resume();
112 si->m_stream->FadeVolume(0.0f, 1.0f, FAST_XFADE_TIME);
117 /* wait for them to fade in */
119 Sleep(FAST_XFADE_TIME);
122 /* be sure they have faded in */
126 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
128 StreamInfo* si = *itt;
129 if (si->m_stream->IsFading())
142 void PAPlayer::SoftStop(bool wait/* = false */, bool close/* = true */)
144 /* fade all the streams out fast for a nice soft stop */
145 CSharedLock lock(m_streamsLock);
146 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
148 StreamInfo* si = *itt;
150 si->m_stream->FadeVolume(1.0f, 0.0f, FAST_XFADE_TIME);
154 si->m_prepareTriggered = true;
155 si->m_playNextTriggered = true;
156 si->m_fadeOutTriggered = true;
160 /* if we are going to wait for them to finish fading */
163 /* wait for them to fade out */
165 Sleep(FAST_XFADE_TIME);
168 /* be sure they have faded out */
169 while(wait && !CAEFactory::IsSuspended())
172 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
174 StreamInfo* si = *itt;
175 if (si->m_stream && si->m_stream->IsFading())
186 /* if we are not closing the streams, pause them */
189 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
191 StreamInfo* si = *itt;
192 si->m_stream->Pause();
198 void PAPlayer::CloseAllStreams(bool fade/* = true */)
202 CExclusiveLock lock(m_streamsLock);
203 while(!m_streams.empty())
205 StreamInfo* si = m_streams.front();
206 m_streams.pop_front();
210 CAEFactory::FreeStream(si->m_stream);
214 si->m_decoder.Destroy();
218 while(!m_finishing.empty())
220 StreamInfo* si = m_finishing.front();
221 m_finishing.pop_front();
225 CAEFactory::FreeStream(si->m_stream);
229 si->m_decoder.Destroy();
232 m_currentStream = NULL;
236 SoftStop(false, true);
237 CExclusiveLock lock(m_streamsLock);
238 m_currentStream = NULL;
242 bool PAPlayer::OpenFile(const CFileItem& file, const CPlayerOptions &options)
244 m_defaultCrossfadeMS = CSettings::Get().GetInt("musicplayer.crossfade") * 1000;
246 if (m_streams.size() > 1 || !m_defaultCrossfadeMS || m_isPaused)
248 CloseAllStreams(!m_isPaused);
250 m_isPaused = false; // Make sure to reset the pause state
253 if (!QueueNextFileEx(file, false))
256 CSharedLock lock(m_streamsLock);
257 if (m_streams.size() == 2)
259 //do a short crossfade on trackskip, set to max 2 seconds for these prev/next transitions
260 m_upcomingCrossfadeMS = std::min(m_defaultCrossfadeMS, (unsigned int)MAX_SKIP_XFADE_TIME);
262 //start transition to next track
263 StreamInfo* si = m_streams.front();
264 si->m_playNextAtFrame = si->m_framesSent; //start next track at current frame
265 si->m_prepareTriggered = true; //next track is ready to go
272 /* trigger playback start */
278 void PAPlayer::UpdateCrossfadeTime(const CFileItem& file)
280 // we explicitely disable crossfading for audio cds
282 m_upcomingCrossfadeMS = 0;
284 m_upcomingCrossfadeMS = m_defaultCrossfadeMS = CSettings::Get().GetInt("musicplayer.crossfade") * 1000;
285 if (m_upcomingCrossfadeMS)
287 if (m_streams.size() == 0 ||
289 file.HasMusicInfoTag() && !CSettings::Get().GetBool("musicplayer.crossfadealbumtracks") &&
290 m_FileItem->HasMusicInfoTag() &&
291 (m_FileItem->GetMusicInfoTag()->GetAlbum() != "") &&
292 (m_FileItem->GetMusicInfoTag()->GetAlbum() == file.GetMusicInfoTag()->GetAlbum()) &&
293 (m_FileItem->GetMusicInfoTag()->GetDiscNumber() == file.GetMusicInfoTag()->GetDiscNumber()) &&
294 (m_FileItem->GetMusicInfoTag()->GetTrackNumber() == file.GetMusicInfoTag()->GetTrackNumber() - 1)
298 //do not crossfade when playing consecutive albumtracks
299 m_upcomingCrossfadeMS = 0;
304 bool PAPlayer::QueueNextFile(const CFileItem &file)
307 CExclusiveLock lock(m_streamsLock);
310 CJobManager::GetInstance().AddJob(new CQueueNextFileJob(file, *this), this, CJob::PRIORITY_NORMAL);
314 bool PAPlayer::QueueNextFileEx(const CFileItem &file, bool fadeIn/* = true */, bool job /* = false */)
316 StreamInfo *si = new StreamInfo();
318 // check if we advance a track of a CUE sheet
319 // if this is the case we don't need to open a new stream
320 std::string newURL = file.GetMusicInfoTag() ? file.GetMusicInfoTag()->GetURL() : file.GetPath();
321 std::string oldURL = m_FileItem->GetMusicInfoTag() ? m_FileItem->GetMusicInfoTag()->GetURL() : m_FileItem->GetPath();
322 if (newURL.compare(oldURL) == 0 &&
323 file.m_lStartOffset &&
324 file.m_lStartOffset == m_FileItem->m_lEndOffset &&
325 m_currentStream && m_currentStream->m_prepareTriggered)
327 m_continueStream = true;
328 m_upcomingCrossfadeMS = 0;
334 m_continueStream = false;
337 if (!si->m_decoder.Create(file, (file.m_lStartOffset * 1000) / 75))
339 CLog::Log(LOGWARNING, "PAPlayer::QueueNextFileEx - Failed to create the decoder");
344 m_callback.OnPlayBackStarted();
345 m_callback.OnQueueNextItem();
349 /* decode until there is data-available */
350 si->m_decoder.Start();
351 while(si->m_decoder.GetDataSize() == 0)
353 int status = si->m_decoder.GetStatus();
354 if (status == STATUS_ENDED ||
355 status == STATUS_NO_FILE ||
356 si->m_decoder.ReadSamples(PACKET_SIZE) == RET_ERROR)
358 CLog::Log(LOGINFO, "PAPlayer::QueueNextFileEx - Error reading samples");
360 si->m_decoder.Destroy();
364 m_callback.OnPlayBackStarted();
365 m_callback.OnQueueNextItem();
369 /* yield our time so that the main PAP thread doesnt stall */
373 /* init the streaminfo struct */
374 si->m_decoder.GetDataFormat(&si->m_channelInfo, &si->m_sampleRate, &si->m_encodedSampleRate, &si->m_dataFormat);
375 si->m_startOffset = file.m_lStartOffset * 1000 / 75;
376 si->m_endOffset = file.m_lEndOffset * 1000 / 75;
377 si->m_bytesPerSample = CAEUtil::DataFormatToBits(si->m_dataFormat) >> 3;
378 si->m_bytesPerFrame = si->m_bytesPerSample * si->m_channelInfo.Count();
379 si->m_started = false;
380 si->m_finishing = false;
381 si->m_framesSent = 0;
382 si->m_seekNextAtFrame = 0;
383 si->m_seekFrame = -1;
385 si->m_volume = (fadeIn && m_upcomingCrossfadeMS) ? 0.0f : 1.0f;
386 si->m_fadeOutTriggered = false;
387 si->m_isSlaved = false;
389 int64_t streamTotalTime = si->m_decoder.TotalTime();
391 streamTotalTime = si->m_endOffset - si->m_startOffset;
393 si->m_prepareNextAtFrame = 0;
394 // cd drives don't really like it to be crossfaded or prepared
397 if (streamTotalTime >= TIME_TO_CACHE_NEXT_FILE + m_defaultCrossfadeMS)
398 si->m_prepareNextAtFrame = (int)((streamTotalTime - TIME_TO_CACHE_NEXT_FILE - m_defaultCrossfadeMS) * si->m_sampleRate / 1000.0f);
401 if (m_currentStream && (AE_IS_RAW(m_currentStream->m_dataFormat) || AE_IS_RAW(si->m_dataFormat)))
403 m_currentStream->m_prepareTriggered = false;
404 m_currentStream->m_waitOnDrain = true;
405 m_currentStream->m_prepareNextAtFrame = 0;
406 si->m_decoder.Destroy();
411 UpdateCrossfadeTime(file);
413 si->m_prepareTriggered = false;
414 si->m_playNextAtFrame = 0;
415 si->m_playNextTriggered = false;
416 si->m_waitOnDrain = false;
418 if (!PrepareStream(si))
420 CLog::Log(LOGINFO, "PAPlayer::QueueNextFileEx - Error preparing stream");
422 si->m_decoder.Destroy();
426 m_callback.OnPlayBackStarted();
427 m_callback.OnQueueNextItem();
431 /* add the stream to the list */
432 CExclusiveLock lock(m_streamsLock);
433 m_streams.push_back(si);
434 //update the current stream to start playing the next track at the correct frame.
435 UpdateStreamInfoPlayNextAtFrame(m_currentStream, m_upcomingCrossfadeMS);
442 void PAPlayer::UpdateStreamInfoPlayNextAtFrame(StreamInfo *si, unsigned int crossFadingTime)
446 int64_t streamTotalTime = si->m_decoder.TotalTime();
448 streamTotalTime = si->m_endOffset - si->m_startOffset;
449 if (streamTotalTime < crossFadingTime)
450 si->m_playNextAtFrame = (int)((streamTotalTime / 2) * si->m_sampleRate / 1000.0f);
452 si->m_playNextAtFrame = (int)((streamTotalTime - crossFadingTime) * si->m_sampleRate / 1000.0f);
456 inline bool PAPlayer::PrepareStream(StreamInfo *si)
458 /* if we have a stream we are already prepared */
462 /* get a paused stream */
463 si->m_stream = CAEFactory::MakeStream(
466 si->m_encodedSampleRate,
473 CLog::Log(LOGDEBUG, "PAPlayer::PrepareStream - Failed to get IAEStream");
477 si->m_stream->SetVolume (si->m_volume);
478 si->m_stream->SetReplayGain(si->m_decoder.GetReplayGain());
480 /* if its not the first stream and crossfade is not enabled */
481 if (m_currentStream && m_currentStream != si && !m_upcomingCrossfadeMS)
483 /* slave the stream for gapless */
484 si->m_isSlaved = true;
485 m_currentStream->m_stream->RegisterSlave(si->m_stream);
488 /* fill the stream's buffer */
489 while(si->m_stream->IsBuffering())
491 int status = si->m_decoder.GetStatus();
492 if (status == STATUS_ENDED ||
493 status == STATUS_NO_FILE ||
494 si->m_decoder.ReadSamples(PACKET_SIZE) == RET_ERROR)
496 CLog::Log(LOGINFO, "PAPlayer::PrepareStream - Stream Finished");
503 /* yield our time so that the main PAP thread doesnt stall */
507 CLog::Log(LOGINFO, "PAPlayer::PrepareStream - Ready");
512 bool PAPlayer::CloseFile(bool reopen)
515 CAEFactory::KeepConfiguration(3000);
518 SoftStop(true, true);
519 CloseAllStreams(false);
521 /* wait for the thread to terminate */
522 StopThread(true);//true - wait for end of thread
524 // wait for any pending jobs to complete
526 CSharedLock lock(m_streamsLock);
527 while (m_jobCounter > 0)
530 m_jobEvent.WaitMSec(100);
538 void PAPlayer::Process()
540 if (!m_startEvent.WaitMSec(100))
542 CLog::Log(LOGDEBUG, "PAPlayer::Process - Failed to receive start event");
546 CLog::Log(LOGDEBUG, "PAPlayer::Process - Playback started");
547 while(m_isPlaying && !m_bStop)
549 /* this needs to happen outside of any locks to prevent deadlocks */
550 if (m_signalSpeedChange)
552 m_callback.OnPlayBackSpeedChanged(m_playbackSpeed);
553 m_signalSpeedChange = false;
556 double freeBufferTime = 0.0;
557 ProcessStreams(freeBufferTime);
559 // if none of our streams wants at least 10ms of data, we sleep
560 if (freeBufferTime < 0.01)
565 GetTimeInternal(); //update for GUI
568 if(m_isFinished && !m_bStop)
569 m_callback.OnPlayBackEnded();
571 m_callback.OnPlayBackStopped();
574 inline void PAPlayer::ProcessStreams(double &freeBufferTime)
576 CSharedLock sharedLock(m_streamsLock);
577 if (m_isFinished && m_streams.empty() && m_finishing.empty())
580 freeBufferTime = 1.0;
584 /* destroy any drained streams */
585 for(StreamList::iterator itt = m_finishing.begin(); itt != m_finishing.end();)
587 StreamInfo* si = *itt;
588 if (si->m_stream->IsDrained())
590 itt = m_finishing.erase(itt);
591 CAEFactory::FreeStream(si->m_stream);
593 CLog::Log(LOGDEBUG, "PAPlayer::ProcessStreams - Stream Freed");
600 CExclusiveLock lock(m_streamsLock);
602 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
604 StreamInfo* si = *itt;
605 if (!m_currentStream && !si->m_started)
607 m_currentStream = si;
608 UpdateGUIData(si); //update for GUI
610 /* if the stream is finishing */
611 if ((si->m_playNextTriggered && si->m_stream && !si->m_stream->IsFading()) || !ProcessStream(si, freeBufferTime))
613 if (!si->m_prepareTriggered)
615 if (si->m_waitOnDrain)
617 si->m_stream->Drain(true);
618 si->m_waitOnDrain = false;
620 si->m_prepareTriggered = true;
621 m_callback.OnQueueNextItem();
624 /* remove the stream */
625 itt = m_streams.erase(itt);
626 /* if its the current stream */
627 if (si == m_currentStream)
629 /* if it was the last stream */
630 if (itt == m_streams.end())
632 /* if it didnt trigger the next queue item */
633 if (!si->m_prepareTriggered)
635 if (si->m_waitOnDrain)
637 si->m_stream->Drain(true);
638 si->m_waitOnDrain = false;
640 m_callback.OnQueueNextItem();
641 si->m_prepareTriggered = true;
643 m_currentStream = NULL;
647 m_currentStream = *itt;
648 UpdateGUIData(*itt); //update for GUI
652 /* unregister the audio callback */
653 si->m_stream->UnRegisterAudioCallback();
654 si->m_decoder.Destroy();
655 si->m_stream->Drain(false);
656 m_finishing.push_back(si);
663 /* is it time to prepare the next stream? */
664 if (si->m_prepareNextAtFrame > 0 && !si->m_prepareTriggered && si->m_framesSent >= si->m_prepareNextAtFrame)
666 si->m_prepareTriggered = true;
667 m_callback.OnQueueNextItem();
670 /* it is time to start playing the next stream? */
671 if (si->m_playNextAtFrame > 0 && !si->m_playNextTriggered && !m_continueStream && si->m_framesSent >= si->m_playNextAtFrame)
673 if (!si->m_prepareTriggered)
675 si->m_prepareTriggered = true;
676 m_callback.OnQueueNextItem();
681 if (m_upcomingCrossfadeMS)
683 si->m_stream->FadeVolume(1.0f, 0.0f, m_upcomingCrossfadeMS);
684 si->m_fadeOutTriggered = true;
686 m_currentStream = NULL;
688 /* unregister the audio callback */
689 si->m_stream->UnRegisterAudioCallback();
692 si->m_playNextTriggered = true;
697 inline bool PAPlayer::ProcessStream(StreamInfo *si, double &freeBufferTime)
699 /* if playback needs to start on this stream, do it */
700 if (si == m_currentStream && !si->m_started)
702 si->m_started = true;
703 si->m_stream->RegisterAudioCallback(m_audioCallback);
705 si->m_stream->Resume();
706 si->m_stream->FadeVolume(0.0f, 1.0f, m_upcomingCrossfadeMS);
707 m_callback.OnPlayBackStarted();
710 /* if we have not started yet and the stream has been primed */
711 unsigned int space = si->m_stream->GetSpace();
712 if (!si->m_started && !space)
715 /* see if it is time yet to FF/RW or a direct seek */
716 if (!si->m_playNextTriggered && ((m_playbackSpeed != 1 && si->m_framesSent >= si->m_seekNextAtFrame) || si->m_seekFrame > -1))
718 int64_t time = (int64_t)0;
719 /* if its a direct seek */
720 if (si->m_seekFrame > -1)
722 time = (int64_t)((float)si->m_seekFrame / (float)si->m_sampleRate * 1000.0f);
723 si->m_framesSent = (int)(si->m_seekFrame - ((float)si->m_startOffset * (float)si->m_sampleRate) / 1000.0f);
724 si->m_seekFrame = -1;
725 m_playerGUIData.m_time = time; //update for GUI
726 si->m_seekNextAtFrame = 0;
731 si->m_framesSent += si->m_sampleRate * (m_playbackSpeed - 1);
732 si->m_seekNextAtFrame = si->m_framesSent + si->m_sampleRate / 2;
733 time = (int64_t)(((float)si->m_framesSent / (float)si->m_sampleRate * 1000.0f) + (float)si->m_startOffset);
736 /* if we are seeking back before the start of the track start normal playback */
737 if (time < si->m_startOffset || si->m_framesSent < 0)
739 time = si->m_startOffset;
740 si->m_framesSent = 0;
741 si->m_seekNextAtFrame = 0;
745 si->m_decoder.Seek(time);
748 int status = si->m_decoder.GetStatus();
749 if (status == STATUS_ENDED ||
750 status == STATUS_NO_FILE ||
751 si->m_decoder.ReadSamples(PACKET_SIZE) == RET_ERROR ||
752 ((si->m_endOffset) && (si->m_framesSent / si->m_sampleRate >= (si->m_endOffset - si->m_startOffset) / 1000)))
754 if (si == m_currentStream && m_continueStream)
756 // update current stream with info of next track
757 si->m_startOffset = m_FileItem->m_lStartOffset * 1000 / 75;
758 if (m_FileItem->m_lEndOffset)
759 si->m_endOffset = m_FileItem->m_lEndOffset * 1000 / 75;
762 si->m_framesSent = 0;
764 int64_t streamTotalTime = si->m_decoder.TotalTime() - si->m_startOffset;
766 streamTotalTime = si->m_endOffset - si->m_startOffset;
768 // calculate time when to prepare next stream
769 si->m_prepareNextAtFrame = 0;
770 if (streamTotalTime >= TIME_TO_CACHE_NEXT_FILE + m_defaultCrossfadeMS)
771 si->m_prepareNextAtFrame = (int)((streamTotalTime - TIME_TO_CACHE_NEXT_FILE - m_defaultCrossfadeMS) * si->m_sampleRate / 1000.0f);
773 si->m_prepareTriggered = false;
774 si->m_playNextAtFrame = 0;
775 si->m_playNextTriggered = false;
776 si->m_seekNextAtFrame = 0;
778 //update the current stream to start playing the next track at the correct frame.
779 UpdateStreamInfoPlayNextAtFrame(m_currentStream, m_upcomingCrossfadeMS);
782 m_callback.OnPlayBackStarted();
783 m_continueStream = false;
787 CLog::Log(LOGINFO, "PAPlayer::ProcessStream - Stream Finished");
795 /* update free buffer time if we are running */
798 if (si->m_stream->IsBuffering())
799 freeBufferTime = 1.0;
802 double free_space = (double)(si->m_stream->GetSpace() / si->m_bytesPerSample) / si->m_sampleRate;
803 freeBufferTime = std::max(freeBufferTime , free_space);
810 bool PAPlayer::QueueData(StreamInfo *si)
812 unsigned int space = si->m_stream->GetSpace();
813 unsigned int samples = std::min(si->m_decoder.GetDataSize(), space / si->m_bytesPerSample);
817 void* data = si->m_decoder.GetData(samples);
820 CLog::Log(LOGERROR, "PAPlayer::QueueData - Failed to get data from the decoder");
824 unsigned int added = si->m_stream->AddData(data, samples * si->m_bytesPerSample);
825 si->m_framesSent += added / si->m_bytesPerFrame;
827 const ICodec* codec = si->m_decoder.GetCodec();
828 m_playerGUIData.m_cacheLevel = codec ? codec->GetCacheLevel() : 0; //update for GUI
833 void PAPlayer::OnExit()
838 void PAPlayer::RegisterAudioCallback(IAudioCallback* pCallback)
840 CSharedLock lock(m_streamsLock);
841 m_audioCallback = pCallback;
842 if (m_currentStream && m_currentStream->m_stream)
843 m_currentStream->m_stream->RegisterAudioCallback(pCallback);
846 void PAPlayer::UnRegisterAudioCallback()
848 CSharedLock lock(m_streamsLock);
849 /* only one stream should have the callback, but we do it to all just incase */
850 for(StreamList::iterator itt = m_streams.begin(); itt != m_streams.end(); ++itt)
851 if ((*itt)->m_stream)
852 (*itt)->m_stream->UnRegisterAudioCallback();
853 m_audioCallback = NULL;
856 void PAPlayer::OnNothingToQueueNotify()
861 bool PAPlayer::IsPlaying() const
866 bool PAPlayer::IsPaused() const
871 void PAPlayer::Pause()
877 m_callback.OnPlayBackResumed();
882 SoftStop(true, false);
883 m_callback.OnPlayBackPaused();
887 void PAPlayer::SetVolume(float volume)
892 void PAPlayer::SetDynamicRangeCompression(long drc)
897 void PAPlayer::ToFFRW(int iSpeed)
899 m_playbackSpeed = iSpeed;
900 m_signalSpeedChange = true;
903 int64_t PAPlayer::GetTimeInternal()
905 CSharedLock lock(m_streamsLock);
906 if (!m_currentStream)
909 double time = ((double)m_currentStream->m_framesSent / (double)m_currentStream->m_sampleRate);
910 if (m_currentStream->m_stream)
911 time -= m_currentStream->m_stream->GetDelay();
912 time = time * 1000.0;
914 m_playerGUIData.m_time = (int64_t)time; //update for GUI
916 return (int64_t)time;
919 int64_t PAPlayer::GetTime()
921 return m_playerGUIData.m_time;
924 int64_t PAPlayer::GetTotalTime64()
926 CSharedLock lock(m_streamsLock);
927 if (!m_currentStream)
930 int64_t total = m_currentStream->m_decoder.TotalTime();
931 if (m_currentStream->m_endOffset)
932 total = m_currentStream->m_endOffset;
933 total -= m_currentStream->m_startOffset;
937 int64_t PAPlayer::GetTotalTime()
939 return m_playerGUIData.m_totalTime;
942 int PAPlayer::GetCacheLevel() const
944 return m_playerGUIData.m_cacheLevel;
947 void PAPlayer::GetAudioStreamInfo(int index, SPlayerAudioStreamInfo &info)
949 info.bitrate = m_playerGUIData.m_audioBitrate;
950 info.channels = m_playerGUIData.m_channelCount;
951 info.audioCodecName = m_playerGUIData.m_codec;
952 info.samplerate = m_playerGUIData.m_sampleRate;
953 info.bitspersample = m_playerGUIData.m_bitsPerSample;
956 bool PAPlayer::CanSeek()
958 return m_playerGUIData.m_canSeek;
961 void PAPlayer::Seek(bool bPlus, bool bLargeStep, bool bChapterOverride)
963 if (!CanSeek()) return;
966 if (g_advancedSettings.m_musicUseTimeSeeking && GetTotalTime() > 2 * g_advancedSettings.m_musicTimeSeekForwardBig)
969 seek = bPlus ? g_advancedSettings.m_musicTimeSeekForwardBig : g_advancedSettings.m_musicTimeSeekBackwardBig;
971 seek = bPlus ? g_advancedSettings.m_musicTimeSeekForward : g_advancedSettings.m_musicTimeSeekBackward;
979 percent = bPlus ? (float)g_advancedSettings.m_musicPercentSeekForwardBig : (float)g_advancedSettings.m_musicPercentSeekBackwardBig;
981 percent = bPlus ? (float)g_advancedSettings.m_musicPercentSeekForward : (float)g_advancedSettings.m_musicPercentSeekBackward;
982 seek = (__int64)(GetTotalTime64() * (GetPercentage() + percent) / 100);
988 void PAPlayer::SeekTime(int64_t iTime /*=0*/)
990 if (!CanSeek()) return;
992 CSharedLock lock(m_streamsLock);
993 if (!m_currentStream)
996 int seekOffset = (int)(iTime - GetTimeInternal());
998 if (m_playbackSpeed != 1)
1001 m_currentStream->m_seekFrame = (int)((float)m_currentStream->m_sampleRate * ((float)iTime + (float)m_currentStream->m_startOffset) / 1000.0f);
1002 m_callback.OnPlayBackSeek((int)iTime, seekOffset);
1005 void PAPlayer::SeekPercentage(float fPercent /*=0*/)
1007 if (fPercent < 0.0f ) fPercent = 0.0f;
1008 if (fPercent > 100.0f) fPercent = 100.0f;
1009 SeekTime((int64_t)(fPercent * 0.01f * (float)GetTotalTime64()));
1012 float PAPlayer::GetPercentage()
1014 if (m_playerGUIData.m_totalTime > 0)
1015 return m_playerGUIData.m_time * 100.0f / m_playerGUIData.m_totalTime;
1020 bool PAPlayer::SkipNext()
1025 void PAPlayer::UpdateGUIData(StreamInfo *si)
1027 /* Store data need by external threads in member
1028 * structure to prevent locking conflicts when
1029 * data required by GUI and main application
1031 CSharedLock lock(m_streamsLock);
1033 m_playerGUIData.m_sampleRate = si->m_sampleRate;
1034 m_playerGUIData.m_bitsPerSample = si->m_bytesPerSample << 3;
1035 m_playerGUIData.m_channelCount = si->m_channelInfo.Count();
1036 m_playerGUIData.m_canSeek = si->m_decoder.CanSeek();
1038 const ICodec* codec = si->m_decoder.GetCodec();
1040 m_playerGUIData.m_audioBitrate = codec ? codec->m_Bitrate : 0;
1041 strncpy(m_playerGUIData.m_codec,codec ? codec->m_CodecName : "",20);
1042 m_playerGUIData.m_cacheLevel = codec ? codec->GetCacheLevel() : 0;
1044 int64_t total = si->m_decoder.TotalTime();
1045 if (si->m_endOffset)
1046 total = m_currentStream->m_endOffset;
1047 total -= m_currentStream->m_startOffset;
1048 m_playerGUIData.m_totalTime = total;
1051 void PAPlayer::OnJobComplete(unsigned int jobID, bool success, CJob *job)
1053 CExclusiveLock lock(m_streamsLock);