xbmc
PAPlayer.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "AudioDecoder.h"
12 #include "cores/AudioEngine/Interfaces/AE.h"
13 #include "cores/AudioEngine/Interfaces/IAudioCallback.h"
14 #include "cores/IPlayer.h"
15 #include "threads/CriticalSection.h"
16 #include "threads/Thread.h"
17 #include "utils/Job.h"
18 
19 #include <atomic>
20 #include <list>
21 #include <vector>
22 
23 class IAEStream;
24 class CFileItem;
25 class CProcessInfo;
26 
27 class PAPlayer : public IPlayer, public CThread, public IJobCallback
28 {
29 friend class CQueueNextFileJob;
30 public:
31  explicit PAPlayer(IPlayerCallback& callback);
32  ~PAPlayer() override;
33 
34  bool OpenFile(const CFileItem& file, const CPlayerOptions &options) override;
35  bool QueueNextFile(const CFileItem &file) override;
36  void OnNothingToQueueNotify() override;
37  bool CloseFile(bool reopen = false) override;
38  bool IsPlaying() const override;
39  void Pause() override;
40  bool HasVideo() const override { return false; }
41  bool HasAudio() const override { return true; }
42  bool CanSeek() const override;
43  void Seek(bool bPlus = true, bool bLargeStep = false, bool bChapterOverride = false) override;
44  void SeekPercentage(float fPercent = 0.0f) override;
45  void SetVolume(float volume) override;
46  void SetDynamicRangeCompression(long drc) override;
47  void SetSpeed(float speed = 0) override;
48  int GetCacheLevel() const override;
49  void SetTotalTime(int64_t time) override;
50  void GetAudioStreamInfo(int index, AudioStreamInfo& info) const override;
51  void SetTime(int64_t time) override;
52  void SeekTime(int64_t iTime = 0) override;
53  void GetAudioCapabilities(std::vector<int>& audioCaps) const override {}
54 
55  int GetAudioStreamCount() const override { return 1; }
56  int GetAudioStream() override { return 0; }
57 
58  // implementation of IJobCallback
59  void OnJobComplete(unsigned int jobID, bool success, CJob *job) override;
60 
61  struct
62  {
63  char m_codec[21];
64  int64_t m_time;
65  int64_t m_totalTime;
66  int m_channelCount;
67  int m_bitsPerSample;
68  int m_sampleRate;
69  int m_audioBitrate;
70  int m_cacheLevel;
71  bool m_canSeek;
72  } m_playerGUIData;
73 
74 protected:
75  // implementation of CThread
76  void OnStartup() override {}
77  void Process() override;
78  void OnExit() override;
79  float GetPercentage();
80 
81 private:
82  struct StreamInfo
83  {
84  std::unique_ptr<CFileItem> m_fileItem;
85  std::unique_ptr<CFileItem> m_nextFileItem;
86  CAudioDecoder m_decoder; /* the stream decoder */
87  int64_t m_startOffset; /* the stream start offset */
88  int64_t m_endOffset; /* the stream end offset */
89  int64_t m_decoderTotal = 0;
90  AEAudioFormat m_audioFormat;
91  unsigned int m_bytesPerSample; /* number of bytes per audio sample */
92  unsigned int m_bytesPerFrame; /* number of bytes per audio frame */
93 
94  bool m_started; /* if playback of this stream has been started */
95  bool m_finishing; /* if this stream is finishing */
96  int m_framesSent; /* number of frames sent to the stream */
97  int m_prepareNextAtFrame; /* when to prepare the next stream */
98  bool m_prepareTriggered; /* if the next stream has been prepared */
99  int m_playNextAtFrame; /* when to start playing the next stream */
100  bool m_playNextTriggered; /* if this stream has started the next one */
101  bool m_fadeOutTriggered; /* if the stream has been told to fade out */
102  int m_seekNextAtFrame; /* the FF/RR sample to seek at */
103  int m_seekFrame; /* the exact position to seek too, -1 for none */
104 
105  IAE::StreamPtr m_stream; /* the playback stream */
106  float m_volume; /* the initial volume level to set the stream to on creation */
107 
108  bool m_isSlaved; /* true if the stream has been slaved to another */
109  bool m_waitOnDrain; /* wait for stream being drained in AE */
110  };
111 
112  typedef std::list<StreamInfo*> StreamList;
113 
114  bool m_signalSpeedChange = false; /* true if OnPlaybackSpeedChange needs to be called */
115  bool m_signalStarted = true;
116  std::atomic_int m_playbackSpeed; /* the playback speed (1 = normal) */
117  bool m_isPlaying = false;
118  bool m_isPaused = false;
119  bool m_isFinished = false; /* if there are no more songs in the queue */
120  bool m_fullScreen;
121  unsigned int m_defaultCrossfadeMS = 0; /* how long the default crossfade is in ms */
122  unsigned int m_upcomingCrossfadeMS = 0; /* how long the upcoming crossfade is in ms */
123  CEvent m_startEvent; /* event for playback start */
124  StreamInfo* m_currentStream = nullptr;
125  IAudioCallback* m_audioCallback; /* the viz audio callback */
126 
127  CCriticalSection m_streamsLock; /* lock for the stream list */
128  StreamList m_streams; /* playing streams */
129  StreamList m_finishing; /* finishing streams */
130  int m_jobCounter = 0;
131  CEvent m_jobEvent;
132  int64_t m_newForcedPlayerTime = -1;
133  int64_t m_newForcedTotalTime = -1;
134  std::unique_ptr<CProcessInfo> m_processInfo;
135 
136  bool QueueNextFileEx(const CFileItem &file, bool fadeIn);
137  void SoftStart(bool wait = false);
138  void SoftStop(bool wait = false, bool close = true);
139  void CloseAllStreams(bool fade = true);
140  void ProcessStreams(double &freeBufferTime);
141  bool PrepareStream(StreamInfo *si);
142  bool ProcessStream(StreamInfo *si, double &freeBufferTime);
143  bool QueueData(StreamInfo *si);
144  int64_t GetTotalTime64();
145  void UpdateCrossfadeTime(const CFileItem& file);
146  void UpdateStreamInfoPlayNextAtFrame(StreamInfo *si, unsigned int crossFadingTime);
147  void UpdateGUIData(StreamInfo *si);
148  int64_t GetTimeInternal();
149  bool SetTimeInternal(int64_t time);
150  bool SetTotalTimeInternal(int64_t time);
151  void CloseFileCB(StreamInfo &si);
152  void AdvancePlaylistOnError(CFileItem &fileItem);
153 };
154 
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: Thread.h:44
Base class for jobs that are executed asynchronously.
Definition: Job.h:109
Definition: IAudioCallback.h:15
Definition: AudioDecoder.h:40
IAEStream Stream Interface for streaming audio.
Definition: AEStream.h:52
Definition: StreamInfo.h:55
void SetTime(int64_t time) override
Sets the current time. This can be used for injecting the current time. This is not to be confused wi...
Definition: PAPlayer.cpp:1034
Definition: PAPlayer.h:27
Definition: IPlayer.h:87
The audio format structure that fully defines a stream&#39;s audio information.
Definition: AEAudioFormat.h:19
void OnJobComplete(unsigned int jobID, bool success, CJob *job) override
The callback used when a job completes.
Definition: PAPlayer.cpp:1164
Definition: ProcessInfo.h:26
Definition: IPlayerCallback.h:18
Callback interface for asynchronous jobs.
Definition: Job.h:31
void SetTotalTime(int64_t time) override
Set the total time in milliseconds this can be used for injecting the duration in case its not availa...
Definition: PAPlayer.cpp:1052
Definition: IPlayer.h:31
Represents a file on a share.
Definition: FileItem.h:102