3 * Copyright (C) 2011-2013 Team XBMC
6 * This Program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This Program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with XBMC; see the file COPYING. If not, see
18 * <http://www.gnu.org/licenses/>.
22 #include "cores/IPlayer.h"
23 #include "threads/Thread.h"
25 #include "cores/dvdplayer/IDVDPlayer.h"
27 #include "DVDMessageQueue.h"
30 #include "OMXPlayerAudio.h"
31 #include "OMXPlayerVideo.h"
32 #include "DVDPlayerSubtitle.h"
33 #include "DVDPlayerTeletext.h"
35 //#include "DVDChapterReader.h"
36 #include "DVDSubtitles/DVDFactorySubtitle.h"
37 #include "utils/BitstreamStats.h"
39 #include "linux/DllBCM.h"
42 #include "threads/SingleLock.h"
53 #define DVDSTATE_NORMAL 0x00000001 // normal dvd state
54 #define DVDSTATE_STILL 0x00000002 // currently displaying a still frame
55 #define DVDSTATE_WAIT 0x00000003 // waiting for demuxer read error
56 #define DVDSTATE_SEEK 0x00000004 // we are finishing a seek request
58 class COMXCurrentStream
61 int id; // demuxerid of current playing stream
63 double dts; // last dts from demuxer, used to find disncontinuities
64 double dur; // last frame expected duration
65 double dts_state; // when did we last send a playback state update
66 CDVDStreamInfo hint; // stream hints, used to notice stream changes
67 void* stream; // pointer or integer, identifying stream playing. if it changes stream changed
68 int changes; // remembered counter from stream to track codec changes
70 bool started; // has the player started
71 const StreamType type;
73 // stuff to handle starting after seek
76 COMXCurrentStream(StreamType t, int i)
86 source = STREAM_SOURCE_NONE;
87 dts = DVD_NOPTS_VALUE;
88 dts_state = DVD_NOPTS_VALUE;
89 dur = DVD_NOPTS_VALUE;
95 startpts = DVD_NOPTS_VALUE;
99 if(dts == DVD_NOPTS_VALUE)
100 return DVD_NOPTS_VALUE;
101 if(dur == DVD_NOPTS_VALUE)
111 std::string filename;
112 std::string filename2; // for vobsub subtitles, 2 files are necessary (idx/sub)
113 std::string language;
115 CDemuxStream::EFlags flags;
120 } OMXSelectionStream;
122 typedef std::vector<OMXSelectionStream> OMXSelectionStreams;
124 class COMXSelectionStreams
126 CCriticalSection m_section;
127 OMXSelectionStream m_invalid;
129 COMXSelectionStreams()
132 m_invalid.source = STREAM_SOURCE_NONE;
133 m_invalid.type = STREAM_NONE;
135 std::vector<OMXSelectionStream> m_Streams;
137 int IndexOf (StreamType type, int source, int id) const;
138 int IndexOf (StreamType type, COMXPlayer& p) const;
139 int Count (StreamType type) const { return IndexOf(type, STREAM_SOURCE_NONE, -1) + 1; }
140 OMXSelectionStream& Get (StreamType type, int index);
141 bool Get (StreamType type, CDemuxStream::EFlags flag, OMXSelectionStream& out);
143 OMXSelectionStreams Get(StreamType type);
144 template<typename Compare> OMXSelectionStreams Get(StreamType type, Compare compare)
146 OMXSelectionStreams streams = Get(type);
147 std::stable_sort(streams.begin(), streams.end(), compare);
151 void Clear (StreamType type, StreamSource source);
152 int Source (StreamSource source, std::string filename);
154 void Update (OMXSelectionStream& s);
155 void Update (CDVDInputStream* input, CDVDDemux* demuxer);
159 #define DVDPLAYER_AUDIO 1
160 #define DVDPLAYER_VIDEO 2
161 #define DVDPLAYER_SUBTITLE 3
162 #define DVDPLAYER_TELETEXT 4
164 class COMXPlayer : public IPlayer, public CThread, public IDVDPlayer
168 COMXPlayer(IPlayerCallback &callback);
169 virtual ~COMXPlayer();
171 virtual bool OpenFile(const CFileItem &file, const CPlayerOptions &options);
172 virtual bool CloseFile(bool reopen = false);
173 virtual bool IsPlaying() const;
174 virtual void Pause();
175 virtual bool IsPaused() const;
176 virtual bool HasVideo() const;
177 virtual bool HasAudio() const;
178 virtual bool IsPassthrough() const;
179 virtual bool CanSeek();
180 virtual void Seek(bool bPlus, bool bLargeStep, bool bChapterOverride);
181 virtual bool SeekScene(bool bPlus = true);
182 virtual void SeekPercentage(float iPercent);
183 virtual float GetPercentage();
184 virtual float GetCachePercentage();
186 virtual void RegisterAudioCallback(IAudioCallback* pCallback) { m_omxPlayerAudio.RegisterAudioCallback(pCallback); }
187 virtual void UnRegisterAudioCallback() { m_omxPlayerAudio.UnRegisterAudioCallback(); }
188 virtual void SetVolume(float nVolume) { m_omxPlayerAudio.SetVolume(nVolume); }
189 virtual void SetMute(bool bOnOff) { m_omxPlayerAudio.SetMute(bOnOff); }
190 virtual void SetDynamicRangeCompression(long drc) { m_omxPlayerAudio.SetDynamicRangeCompression(drc); }
191 virtual bool ControlsVolume() {return true;}
192 virtual void GetAudioInfo(CStdString &strAudioInfo);
193 virtual void GetVideoInfo(CStdString &strVideoInfo);
194 virtual void GetGeneralInfo(CStdString &strVideoInfo);
195 virtual bool CanRecord();
196 virtual bool IsRecording();
197 virtual bool CanPause();
198 virtual bool Record(bool bOnOff);
199 virtual void SetAVDelay(float fValue = 0.0f);
200 virtual float GetAVDelay();
202 virtual void SetSubTitleDelay(float fValue = 0.0f);
203 virtual float GetSubTitleDelay();
204 virtual int GetSubtitleCount();
205 virtual int GetSubtitle();
206 virtual void GetSubtitleStreamInfo(int index, SPlayerSubtitleStreamInfo &info);
207 virtual void SetSubtitle(int iStream);
208 virtual bool GetSubtitleVisible();
209 virtual void SetSubtitleVisible(bool bVisible);
210 virtual int AddSubtitle(const CStdString& strSubPath);
212 virtual int GetAudioStreamCount();
213 virtual int GetAudioStream();
214 virtual void SetAudioStream(int iStream);
216 virtual TextCacheStruct_t* GetTeletextCache();
217 virtual void LoadPage(int p, int sp, unsigned char* buffer);
219 virtual int GetChapterCount();
220 virtual int GetChapter();
221 virtual void GetChapterName(CStdString& strChapterName);
222 virtual int SeekChapter(int iChapter);
224 virtual void SeekTime(int64_t iTime);
225 virtual int64_t GetTime();
226 virtual int64_t GetTotalTime();
227 virtual void ToFFRW(int iSpeed );
228 virtual bool OnAction(const CAction &action);
229 virtual bool HasMenu();
230 virtual int GetSourceBitrate();
231 virtual void GetVideoStreamInfo(SPlayerVideoStreamInfo &info);
233 virtual bool GetStreamDetails(CStreamDetails &details);
234 virtual void GetAudioStreamInfo(int index, SPlayerAudioStreamInfo &info);
236 virtual CStdString GetPlayerState();
237 virtual bool SetPlayerState(CStdString state);
239 virtual CStdString GetPlayingTitle();
241 virtual bool SwitchChannel(const PVR::CPVRChannel &channel);
242 virtual bool CachePVRStream(void) const;
245 { CACHESTATE_DONE = 0
246 , CACHESTATE_FULL // player is filling up the demux queue
247 , CACHESTATE_PVR // player is waiting for some data in each buffer
248 , CACHESTATE_INIT // player is waiting for first packet of each stream
249 , CACHESTATE_PLAY // player is waiting for players to not be stalled
250 , CACHESTATE_FLUSH // temporary state player will choose startup between init or full
253 virtual bool IsCaching() const { return m_caching == CACHESTATE_FULL || m_caching == CACHESTATE_PVR; }
254 virtual int GetCacheLevel() const;
256 virtual int OnDVDNavResult(void* pData, int iMessage);
258 virtual void GetRenderFeatures(std::vector<int> &renderFeatures);
259 virtual void GetDeinterlaceMethods(std::vector<int> &deinterlaceMethods);
260 virtual void GetDeinterlaceModes(std::vector<int> &deinterlaceModes);
261 virtual void GetScalingMethods(std::vector<int> &scalingMethods);
262 virtual void GetAudioCapabilities(std::vector<int> &audioCaps);
263 virtual void GetSubtitleCapabilities(std::vector<int> &subCaps);
265 friend class COMXSelectionStreams;
267 class OMXStreamLock : public CSingleLock
270 inline OMXStreamLock(COMXPlayer* comxplayer) : CSingleLock(comxplayer->m_critStreamSection) {}
273 virtual void OnStartup();
274 virtual void OnExit();
275 virtual void Process();
277 bool OpenAudioStream(int iStream, int source, bool reset = true);
278 bool OpenVideoStream(int iStream, int source, bool reset = true);
279 bool OpenSubtitleStream(int iStream, int source);
281 /** \brief Switches forced subtitles to forced subtitles matching the language of the current audio track.
282 * If these are not available, subtitles are disabled.
283 * \return true if the subtitles were changed, false otherwise.
285 bool AdaptForcedSubtitles();
286 bool OpenTeletextStream(int iStream, int source);
287 bool CloseAudioStream(bool bWaitForBuffers);
288 bool CloseVideoStream(bool bWaitForBuffers);
289 bool CloseSubtitleStream(bool bKeepOverlays);
290 bool CloseTeletextStream(bool bWaitForBuffers);
292 void ProcessPacket(CDemuxStream* pStream, DemuxPacket* pPacket);
293 void ProcessAudioData(CDemuxStream* pStream, DemuxPacket* pPacket);
294 void ProcessVideoData(CDemuxStream* pStream, DemuxPacket* pPacket);
295 void ProcessSubData(CDemuxStream* pStream, DemuxPacket* pPacket);
296 void ProcessTeletextData(CDemuxStream* pStream, DemuxPacket* pPacket);
298 bool ShowPVRChannelInfo();
300 int AddSubtitleFile(const std::string& filename, const std::string& subfilename = "", CDemuxStream::EFlags flags = CDemuxStream::FLAG_NONE);
301 void SetSubtitleVisibleInternal(bool bVisible);
304 * one of the DVD_PLAYSPEED defines
306 void SetPlaySpeed(int iSpeed);
307 int GetPlaySpeed() { return m_playSpeed; }
308 void SetCaching(ECacheState state);
310 int64_t GetTotalTimeInMsec();
312 double GetQueueTime();
313 bool GetCachingTimes(double& play_left, double& cache_left, double& file_offset);
316 void FlushBuffers(bool queued, double pts = DVD_NOPTS_VALUE, bool accurate = true);
319 void HandleMessages();
320 void HandlePlaySpeed();
321 bool IsInMenu() const;
323 void SynchronizePlayers(unsigned int sources);
324 void SynchronizeDemuxer(unsigned int timeout);
325 void CheckAutoSceneSkip();
326 void CheckContinuity(COMXCurrentStream& current, DemuxPacket* pPacket);
327 bool CheckSceneSkip(COMXCurrentStream& current);
328 bool CheckPlayerInit(COMXCurrentStream& current, unsigned int source);
329 bool CheckStartCaching(COMXCurrentStream& current);
330 void UpdateCorrection(DemuxPacket* pkt, double correction);
331 void UpdateTimestamps(COMXCurrentStream& current, DemuxPacket* pPacket);
332 void SendPlayerMessage(CDVDMsg* pMsg, unsigned int target);
333 bool ReadPacket(DemuxPacket*& packet, CDemuxStream*& stream);
334 bool IsValidStream(COMXCurrentStream& stream);
335 bool IsBetterStream(COMXCurrentStream& current, CDemuxStream* stream);
336 bool CheckDelayedChannelEntry(void);
337 bool OpenInputStream();
338 bool OpenDemuxStream();
339 void OpenDefaultStreams(bool reset = true);
341 void UpdateApplication(double timeout);
342 void UpdatePlayState(double timeout);
343 double m_UpdateApplication;
345 bool m_bAbortRequest;
347 std::string m_filename; // holds the actual filename
348 std::string m_mimetype; // hold a hint to what content file contains (mime type)
349 ECacheState m_caching;
351 XbmcThreads::EndTime m_ChannelEntryTimeOut;
354 COMXCurrentStream m_CurrentAudio;
355 COMXCurrentStream m_CurrentVideo;
356 COMXCurrentStream m_CurrentSubtitle;
357 COMXCurrentStream m_CurrentTeletext;
359 COMXSelectionStreams m_SelectionStreams;
364 double lastpts; // holds last display pts during ff/rw operations
371 CDVDMessageQueue m_messenger; // thread messenger
373 OMXPlayerVideo m_omxPlayerVideo; // video part
374 OMXPlayerAudio m_omxPlayerAudio; // audio part
375 CDVDPlayerSubtitle m_dvdPlayerSubtitle; // subtitle part
376 CDVDTeletextData m_dvdPlayerTeletext; // teletext part
378 CDVDClock m_clock; // master clock
384 double m_last_check_time; // we periodically check for gpu underrun
385 double m_stamp; // last media stamp
387 CDVDOverlayContainer m_overlayContainer;
389 CDVDInputStream* m_pInputStream; // input stream for current playing file
390 CDVDDemux* m_pDemuxer; // demuxer for current playing file
391 CDVDDemux* m_pSubtitleDemuxer;
393 CStdString m_lastSub;
399 state = DVDSTATE_NORMAL;
400 iSelectedSPUStream = -1;
401 iSelectedAudioStream = -1;
403 iDVDStillStartTime = 0;
406 int state; // current dvdstate
407 unsigned int iDVDStillTime; // total time in ticks we should display the still before continuing
408 unsigned int iDVDStillStartTime; // time in ticks when we started the still
409 int iSelectedSPUStream; // mpeg stream id, or -1 if disabled
410 int iSelectedAudioStream; // mpeg stream id, or -1 if disabled
420 friend class OMXPlayerVideo;
421 friend class OMXPlayerAudio;
425 SPlayerState() { Clear(); }
433 time_src = ETIMESOURCE_CLOCK;
434 dts = DVD_NOPTS_VALUE;
451 int player; // source of this data
453 double timestamp; // last time of update
454 double time_offset; // difference between time and pts
456 double time; // current playback time
457 double time_total; // total playback time
458 ETimeSource time_src; // current time source
459 double dts; // last known dts
461 std::string player_state; // full player state
463 int chapter; // current chapter
464 std::string chapter_name; // name of current chapter
465 int chapter_count;// number of chapter
467 bool canrecord; // can input stream record
468 bool recording; // are we currently recording
470 bool canpause; // pvr: can pause the current playing item
471 bool canseek; // pvr: can seek in the current playing item
473 std::string demux_video;
474 std::string demux_audio;
476 int64_t cache_bytes; // number of bytes current's cached
477 double cache_level; // current estimated required cache level
478 double cache_delay; // time until cache is expected to reach estimated level
479 double cache_offset; // percentage of file ahead of current position
480 } m_State, m_StateInput;
481 CCriticalSection m_StateSection;
484 CCriticalSection m_critStreamSection; // need to have this lock when switching streams (audio / video)
488 struct SEdlAutoSkipMarkers {
493 commbreak_start = -1;
495 seek_to_start = false;
499 int cut; // last automatically skipped EDL cut seek position
500 int commbreak_start; // start time of the last commercial break automatically skipped
501 int commbreak_end; // end time of the last commercial break automatically skipped
502 bool seek_to_start; // whether seeking can go back to the start of a previously skipped break
503 bool mute; // whether EDL mute is on
505 } m_EdlAutoSkipMarkers;
507 CPlayerOptions m_PlayerOptions;
512 bool m_DemuxerPausePending;