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 template<typename Filter>
152 OMXSelectionStreams RemoveIf(StreamType type, Filter filter)
154 OMXSelectionStreams streams = Get(type);
155 streams.erase(std::remove_if(streams.begin(), streams.end(), filter), streams.end());
159 void Clear (StreamType type, StreamSource source);
160 int Source (StreamSource source, std::string filename);
162 void Update (OMXSelectionStream& s);
163 void Update (CDVDInputStream* input, CDVDDemux* demuxer);
167 #define DVDPLAYER_AUDIO 1
168 #define DVDPLAYER_VIDEO 2
169 #define DVDPLAYER_SUBTITLE 3
170 #define DVDPLAYER_TELETEXT 4
172 class COMXPlayer : public IPlayer, public CThread, public IDVDPlayer
176 COMXPlayer(IPlayerCallback &callback);
177 virtual ~COMXPlayer();
179 virtual bool OpenFile(const CFileItem &file, const CPlayerOptions &options);
180 virtual bool CloseFile();
181 virtual bool IsPlaying() const;
182 virtual void Pause();
183 virtual bool IsPaused() const;
184 virtual bool HasVideo() const;
185 virtual bool HasAudio() const;
186 virtual bool IsPassthrough() const;
187 virtual bool CanSeek();
188 virtual void Seek(bool bPlus, bool bLargeStep, bool bChapterOverride);
189 virtual bool SeekScene(bool bPlus = true);
190 virtual void SeekPercentage(float iPercent);
191 virtual float GetPercentage();
192 virtual float GetCachePercentage();
194 virtual void RegisterAudioCallback(IAudioCallback* pCallback) { m_omxPlayerAudio.RegisterAudioCallback(pCallback); }
195 virtual void UnRegisterAudioCallback() { m_omxPlayerAudio.UnRegisterAudioCallback(); }
196 virtual void SetVolume(float nVolume) { m_omxPlayerAudio.SetVolume(nVolume); }
197 virtual void SetMute(bool bOnOff) { m_omxPlayerAudio.SetMute(bOnOff); }
198 virtual void SetDynamicRangeCompression(long drc) { m_omxPlayerAudio.SetDynamicRangeCompression(drc); }
199 virtual bool ControlsVolume() {return true;}
200 virtual void GetAudioInfo(CStdString &strAudioInfo);
201 virtual void GetVideoInfo(CStdString &strVideoInfo);
202 virtual void GetGeneralInfo(CStdString &strVideoInfo);
203 virtual bool CanRecord();
204 virtual bool IsRecording();
205 virtual bool CanPause();
206 virtual bool Record(bool bOnOff);
207 virtual void SetAVDelay(float fValue = 0.0f);
208 virtual float GetAVDelay();
210 virtual void SetSubTitleDelay(float fValue = 0.0f);
211 virtual float GetSubTitleDelay();
212 virtual int GetSubtitleCount();
213 virtual int GetSubtitle();
214 virtual void GetSubtitleStreamInfo(int index, SPlayerSubtitleStreamInfo &info);
215 virtual void SetSubtitle(int iStream);
216 virtual bool GetSubtitleVisible();
217 virtual void SetSubtitleVisible(bool bVisible);
218 virtual int AddSubtitle(const CStdString& strSubPath);
220 virtual int GetAudioStreamCount();
221 virtual int GetAudioStream();
222 virtual void SetAudioStream(int iStream);
224 virtual TextCacheStruct_t* GetTeletextCache();
225 virtual void LoadPage(int p, int sp, unsigned char* buffer);
227 virtual int GetChapterCount();
228 virtual int GetChapter();
229 virtual void GetChapterName(CStdString& strChapterName);
230 virtual int SeekChapter(int iChapter);
232 virtual void SeekTime(int64_t iTime);
233 virtual int64_t GetTime();
234 virtual int64_t GetTotalTime();
235 virtual void ToFFRW(int iSpeed );
236 virtual bool OnAction(const CAction &action);
237 virtual bool HasMenu();
238 virtual int GetSourceBitrate();
239 virtual void GetVideoStreamInfo(SPlayerVideoStreamInfo &info);
241 virtual bool GetStreamDetails(CStreamDetails &details);
242 virtual void GetAudioStreamInfo(int index, SPlayerAudioStreamInfo &info);
244 virtual CStdString GetPlayerState();
245 virtual bool SetPlayerState(CStdString state);
247 virtual CStdString GetPlayingTitle();
249 virtual bool SwitchChannel(const PVR::CPVRChannel &channel);
250 virtual bool CachePVRStream(void) const;
253 { CACHESTATE_DONE = 0
254 , CACHESTATE_FULL // player is filling up the demux queue
255 , CACHESTATE_PVR // player is waiting for some data in each buffer
256 , CACHESTATE_INIT // player is waiting for first packet of each stream
257 , CACHESTATE_PLAY // player is waiting for players to not be stalled
258 , CACHESTATE_FLUSH // temporary state player will choose startup between init or full
261 virtual bool IsCaching() const { return m_caching == CACHESTATE_FULL || m_caching == CACHESTATE_PVR; }
262 virtual int GetCacheLevel() const;
264 virtual int OnDVDNavResult(void* pData, int iMessage);
266 virtual void GetRenderFeatures(std::vector<int> &renderFeatures);
267 virtual void GetDeinterlaceMethods(std::vector<int> &deinterlaceMethods);
268 virtual void GetDeinterlaceModes(std::vector<int> &deinterlaceModes);
269 virtual void GetScalingMethods(std::vector<int> &scalingMethods);
270 virtual void GetAudioCapabilities(std::vector<int> &audioCaps);
271 virtual void GetSubtitleCapabilities(std::vector<int> &subCaps);
273 friend class COMXSelectionStreams;
275 class OMXStreamLock : public CSingleLock
278 inline OMXStreamLock(COMXPlayer* comxplayer) : CSingleLock(comxplayer->m_critStreamSection) {}
281 virtual void OnStartup();
282 virtual void OnExit();
283 virtual void Process();
285 bool OpenAudioStream(int iStream, int source, bool reset = true);
286 bool OpenVideoStream(int iStream, int source, bool reset = true);
287 bool OpenSubtitleStream(int iStream, int source);
289 /** \brief Switches forced subtitles to forced subtitles matching the language of the current audio track.
290 * If these are not available, subtitles are disabled.
291 * \return true if the subtitles were changed, false otherwise.
293 bool AdaptForcedSubtitles();
294 bool OpenTeletextStream(int iStream, int source);
295 bool CloseAudioStream(bool bWaitForBuffers);
296 bool CloseVideoStream(bool bWaitForBuffers);
297 bool CloseSubtitleStream(bool bKeepOverlays);
298 bool CloseTeletextStream(bool bWaitForBuffers);
300 void ProcessPacket(CDemuxStream* pStream, DemuxPacket* pPacket);
301 void ProcessAudioData(CDemuxStream* pStream, DemuxPacket* pPacket);
302 void ProcessVideoData(CDemuxStream* pStream, DemuxPacket* pPacket);
303 void ProcessSubData(CDemuxStream* pStream, DemuxPacket* pPacket);
304 void ProcessTeletextData(CDemuxStream* pStream, DemuxPacket* pPacket);
306 bool ShowPVRChannelInfo();
308 int AddSubtitleFile(const std::string& filename, const std::string& subfilename = "", CDemuxStream::EFlags flags = CDemuxStream::FLAG_NONE);
311 * one of the DVD_PLAYSPEED defines
313 void SetPlaySpeed(int iSpeed);
314 int GetPlaySpeed() { return m_playSpeed; }
315 void SetCaching(ECacheState state);
317 int64_t GetTotalTimeInMsec();
319 double GetQueueTime();
320 bool GetCachingTimes(double& play_left, double& cache_left, double& file_offset);
323 void FlushBuffers(bool queued, double pts = DVD_NOPTS_VALUE, bool accurate = true);
326 void HandleMessages();
327 void HandlePlaySpeed();
328 bool IsInMenu() const;
330 void SynchronizePlayers(unsigned int sources);
331 void SynchronizeDemuxer(unsigned int timeout);
332 void CheckAutoSceneSkip();
333 void CheckContinuity(COMXCurrentStream& current, DemuxPacket* pPacket);
334 bool CheckSceneSkip(COMXCurrentStream& current);
335 bool CheckPlayerInit(COMXCurrentStream& current, unsigned int source);
336 bool CheckStartCaching(COMXCurrentStream& current);
337 void UpdateCorrection(DemuxPacket* pkt, double correction);
338 void UpdateTimestamps(COMXCurrentStream& current, DemuxPacket* pPacket);
339 void SendPlayerMessage(CDVDMsg* pMsg, unsigned int target);
340 bool ReadPacket(DemuxPacket*& packet, CDemuxStream*& stream);
341 bool IsValidStream(COMXCurrentStream& stream);
342 bool IsBetterStream(COMXCurrentStream& current, CDemuxStream* stream);
343 bool CheckDelayedChannelEntry(void);
344 bool OpenInputStream();
345 bool OpenDemuxStream();
346 void OpenDefaultStreams(bool reset = true);
348 void UpdateApplication(double timeout);
349 void UpdatePlayState(double timeout);
350 double m_UpdateApplication;
352 bool m_bAbortRequest;
354 std::string m_filename; // holds the actual filename
355 std::string m_mimetype; // hold a hint to what content file contains (mime type)
356 ECacheState m_caching;
358 XbmcThreads::EndTime m_ChannelEntryTimeOut;
361 COMXCurrentStream m_CurrentAudio;
362 COMXCurrentStream m_CurrentVideo;
363 COMXCurrentStream m_CurrentSubtitle;
364 COMXCurrentStream m_CurrentTeletext;
366 COMXSelectionStreams m_SelectionStreams;
371 double lastpts; // holds last display pts during ff/rw operations
378 CDVDMessageQueue m_messenger; // thread messenger
380 OMXPlayerVideo m_omxPlayerVideo; // video part
381 OMXPlayerAudio m_omxPlayerAudio; // audio part
382 CDVDPlayerSubtitle m_dvdPlayerSubtitle; // subtitle part
383 CDVDTeletextData m_dvdPlayerTeletext; // teletext part
385 CDVDClock m_clock; // master clock
391 double m_last_check_time; // we periodically check for gpu underrun
392 double m_stamp; // last media stamp
394 CDVDOverlayContainer m_overlayContainer;
396 CDVDInputStream* m_pInputStream; // input stream for current playing file
397 CDVDDemux* m_pDemuxer; // demuxer for current playing file
398 CDVDDemux* m_pSubtitleDemuxer;
400 CStdString m_lastSub;
406 state = DVDSTATE_NORMAL;
407 iSelectedSPUStream = -1;
408 iSelectedAudioStream = -1;
410 iDVDStillStartTime = 0;
413 int state; // current dvdstate
414 unsigned int iDVDStillTime; // total time in ticks we should display the still before continuing
415 unsigned int iDVDStillStartTime; // time in ticks when we started the still
416 int iSelectedSPUStream; // mpeg stream id, or -1 if disabled
417 int iSelectedAudioStream; // mpeg stream id, or -1 if disabled
427 friend class OMXPlayerVideo;
428 friend class OMXPlayerAudio;
432 SPlayerState() { Clear(); }
440 time_src = ETIMESOURCE_CLOCK;
441 dts = DVD_NOPTS_VALUE;
458 int player; // source of this data
460 double timestamp; // last time of update
461 double time_offset; // difference between time and pts
463 double time; // current playback time
464 double time_total; // total playback time
465 ETimeSource time_src; // current time source
466 double dts; // last known dts
468 std::string player_state; // full player state
470 int chapter; // current chapter
471 std::string chapter_name; // name of current chapter
472 int chapter_count;// number of chapter
474 bool canrecord; // can input stream record
475 bool recording; // are we currently recording
477 bool canpause; // pvr: can pause the current playing item
478 bool canseek; // pvr: can seek in the current playing item
480 std::string demux_video;
481 std::string demux_audio;
483 int64_t cache_bytes; // number of bytes current's cached
484 double cache_level; // current estimated required cache level
485 double cache_delay; // time until cache is expected to reach estimated level
486 double cache_offset; // percentage of file ahead of current position
487 } m_State, m_StateInput;
488 CCriticalSection m_StateSection;
491 CCriticalSection m_critStreamSection; // need to have this lock when switching streams (audio / video)
495 struct SEdlAutoSkipMarkers {
500 commbreak_start = -1;
502 seek_to_start = false;
506 int cut; // last automatically skipped EDL cut seek position
507 int commbreak_start; // start time of the last commercial break automatically skipped
508 int commbreak_end; // end time of the last commercial break automatically skipped
509 bool seek_to_start; // whether seeking can go back to the start of a previously skipped break
510 bool mute; // whether EDL mute is on
512 } m_EdlAutoSkipMarkers;
514 CPlayerOptions m_PlayerOptions;
519 bool m_DemuxerPausePending;