Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / cores / omxplayer / OMXPlayer.h
1 #pragma once
2 /*
3  *      Copyright (C) 2011-2013 Team XBMC
4  *      http://xbmc.org
5  *
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)
9  *  any later version.
10  *
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.
15  *
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/>.
19  *
20  */
21
22 #include "cores/IPlayer.h"
23 #include "threads/Thread.h"
24
25 #include "cores/dvdplayer/IDVDPlayer.h"
26
27 #include "DVDMessageQueue.h"
28 #include "OMXCore.h"
29 #include "OMXClock.h"
30 #include "OMXPlayerAudio.h"
31 #include "OMXPlayerVideo.h"
32 #include "DVDPlayerSubtitle.h"
33 #include "DVDPlayerTeletext.h"
34
35 //#include "DVDChapterReader.h"
36 #include "DVDSubtitles/DVDFactorySubtitle.h"
37 #include "utils/BitstreamStats.h"
38
39 #include "linux/DllBCM.h"
40 #include "Edl.h"
41 #include "FileItem.h"
42 #include "threads/SingleLock.h"
43
44 class COMXPlayer;
45 class OMXPlayerVideo;
46 class OMXPlayerAudio;
47
48 namespace PVR
49 {
50   class CPVRChannel;
51 }
52
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
57
58 class COMXCurrentStream
59 {
60 public:
61   int              id;     // demuxerid of current playing stream
62   int              source;
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
69   bool             inited;
70   bool             started; // has the player started
71   const StreamType type;
72   const int        player;
73   // stuff to handle starting after seek
74   double   startpts;
75
76   COMXCurrentStream(StreamType t, int i)
77     : type(t)
78     , player(i)
79   {
80     Clear();
81   }
82
83   void Clear()
84   {
85     id     = -1;
86     source = STREAM_SOURCE_NONE;
87     dts    = DVD_NOPTS_VALUE;
88     dts_state = DVD_NOPTS_VALUE;
89     dur    = DVD_NOPTS_VALUE;
90     hint.Clear();
91     stream = NULL;
92     changes = 0;
93     inited = false;
94     started = false;
95     startpts  = DVD_NOPTS_VALUE;
96   }
97   double dts_end()
98   {
99     if(dts == DVD_NOPTS_VALUE)
100       return DVD_NOPTS_VALUE;
101     if(dur == DVD_NOPTS_VALUE)
102       return dts;
103     return dts + dur;
104   }
105 };
106
107 typedef struct
108 {
109   StreamType   type;
110   int          type_index;
111   std::string  filename;
112   std::string  filename2;  // for vobsub subtitles, 2 files are necessary (idx/sub) 
113   std::string  language;
114   std::string  name;
115   CDemuxStream::EFlags flags;
116   int          source;
117   int          id;
118   std::string  codec;
119   int          channels;
120 } OMXSelectionStream;
121
122 typedef std::vector<OMXSelectionStream> OMXSelectionStreams;
123
124 class COMXSelectionStreams
125 {
126   CCriticalSection m_section;
127   OMXSelectionStream  m_invalid;
128 public:
129   COMXSelectionStreams()
130   {
131     m_invalid.id = -1;
132     m_invalid.source = STREAM_SOURCE_NONE;
133     m_invalid.type = STREAM_NONE;
134   }
135   std::vector<OMXSelectionStream> m_Streams;
136
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);
142
143   OMXSelectionStreams Get(StreamType type);
144   template<typename Compare> OMXSelectionStreams Get(StreamType type, Compare compare)
145   {
146     OMXSelectionStreams streams = Get(type);
147     std::stable_sort(streams.begin(), streams.end(), compare);
148     return streams;
149   }
150
151   void             Clear   (StreamType type, StreamSource source);
152   int              Source  (StreamSource source, std::string filename);
153
154   void             Update  (OMXSelectionStream& s);
155   void             Update  (CDVDInputStream* input, CDVDDemux* demuxer);
156 };
157
158
159 #define DVDPLAYER_AUDIO    1
160 #define DVDPLAYER_VIDEO    2
161 #define DVDPLAYER_SUBTITLE 3
162 #define DVDPLAYER_TELETEXT 4
163
164 class COMXPlayer : public IPlayer, public CThread, public IDVDPlayer
165 {
166 public:
167
168   COMXPlayer(IPlayerCallback &callback);
169   virtual ~COMXPlayer();
170   
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();
185
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();
201
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);
211
212   virtual int   GetAudioStreamCount();
213   virtual int   GetAudioStream();
214   virtual void  SetAudioStream(int iStream);
215
216   virtual TextCacheStruct_t* GetTeletextCache();
217   virtual void  LoadPage(int p, int sp, unsigned char* buffer);
218
219   virtual int   GetChapterCount();
220   virtual int   GetChapter();
221   virtual void  GetChapterName(CStdString& strChapterName);
222   virtual int   SeekChapter(int iChapter);
223
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);
232
233   virtual bool GetStreamDetails(CStreamDetails &details);
234   virtual void GetAudioStreamInfo(int index, SPlayerAudioStreamInfo &info);
235
236   virtual CStdString GetPlayerState();
237   virtual bool SetPlayerState(CStdString state);
238   
239   virtual CStdString GetPlayingTitle();
240
241   virtual bool SwitchChannel(const PVR::CPVRChannel &channel);
242   virtual bool CachePVRStream(void) const;
243   
244   enum ECacheState
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
251   };
252
253   virtual bool  IsCaching() const                                 { return m_caching == CACHESTATE_FULL || m_caching == CACHESTATE_PVR; }
254   virtual int   GetCacheLevel() const;
255
256   virtual int  OnDVDNavResult(void* pData, int iMessage);
257
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);
264 protected:
265   friend class COMXSelectionStreams;
266
267   class OMXStreamLock : public CSingleLock
268   {
269   public:
270     inline OMXStreamLock(COMXPlayer* comxplayer) : CSingleLock(comxplayer->m_critStreamSection) {}
271   };
272
273   virtual void  OnStartup();
274   virtual void  OnExit();
275   virtual void  Process();
276
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);
280
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.
284   */
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);
291
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);
297
298   bool ShowPVRChannelInfo();
299
300   int  AddSubtitleFile(const std::string& filename, const std::string& subfilename = "", CDemuxStream::EFlags flags = CDemuxStream::FLAG_NONE);
301   void SetSubtitleVisibleInternal(bool bVisible);
302
303   /**
304    * one of the DVD_PLAYSPEED defines
305    */
306   void SetPlaySpeed(int iSpeed);
307   int GetPlaySpeed()                                                { return m_playSpeed; }
308   void    SetCaching(ECacheState state);
309
310   int64_t GetTotalTimeInMsec();
311
312   double  GetQueueTime();
313   bool    GetCachingTimes(double& play_left, double& cache_left, double& file_offset);
314
315
316   void FlushBuffers(bool queued, double pts = DVD_NOPTS_VALUE, bool accurate = true);
317
318
319   void  HandleMessages();
320   void    HandlePlaySpeed();
321   bool  IsInMenu() const;
322
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);
340
341   void UpdateApplication(double timeout);
342   void UpdatePlayState(double timeout);
343   double m_UpdateApplication;
344
345   bool m_bAbortRequest;
346
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;
350   CFileItem    m_item;
351   XbmcThreads::EndTime m_ChannelEntryTimeOut;
352
353
354   COMXCurrentStream m_CurrentAudio;
355   COMXCurrentStream m_CurrentVideo;
356   COMXCurrentStream m_CurrentSubtitle;
357   COMXCurrentStream m_CurrentTeletext;
358
359   COMXSelectionStreams m_SelectionStreams;
360
361   int m_playSpeed;
362   struct SSpeedState
363   {
364     double lastpts;  // holds last display pts during ff/rw operations
365     double lasttime;
366   } m_SpeedState;
367
368   int m_errorCount;
369   double m_offset_pts;
370
371   CDVDMessageQueue m_messenger;     // thread messenger
372
373   OMXPlayerVideo m_omxPlayerVideo; // video part
374   OMXPlayerAudio m_omxPlayerAudio; // audio part
375   CDVDPlayerSubtitle m_dvdPlayerSubtitle; // subtitle part
376   CDVDTeletextData m_dvdPlayerTeletext; // teletext part
377
378   CDVDClock m_clock;                // master clock
379   OMXClock m_av_clock;
380
381   bool m_stepped;
382   int m_video_fifo;
383   int m_audio_fifo;
384   double m_last_check_time;         // we periodically check for gpu underrun
385   double m_stamp;                   // last media stamp
386
387   CDVDOverlayContainer m_overlayContainer;
388
389   CDVDInputStream* m_pInputStream;  // input stream for current playing file
390   CDVDDemux* m_pDemuxer;            // demuxer for current playing file
391   CDVDDemux*            m_pSubtitleDemuxer;
392
393   CStdString m_lastSub;
394
395   struct SDVDInfo
396   {
397     void Clear()
398     {
399       state                =  DVDSTATE_NORMAL;
400       iSelectedSPUStream   = -1;
401       iSelectedAudioStream = -1;
402       iDVDStillTime        =  0;
403       iDVDStillStartTime   =  0;
404     }
405
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
411   } m_dvd;
412
413   enum ETimeSource
414   {
415     ETIMESOURCE_CLOCK,
416     ETIMESOURCE_INPUT,
417     ETIMESOURCE_MENU,
418   };
419
420   friend class OMXPlayerVideo;
421   friend class OMXPlayerAudio;
422
423   struct SPlayerState
424   {
425     SPlayerState() { Clear(); }
426     void Clear()
427     {
428       player        = 0;
429       timestamp     = 0;
430       time          = 0;
431       time_total    = 0;
432       time_offset   = 0;
433       time_src      = ETIMESOURCE_CLOCK;
434       dts           = DVD_NOPTS_VALUE;
435       player_state  = "";
436       chapter       = 0;
437       chapter_name  = "";
438       chapter_count = 0;
439       canrecord     = false;
440       recording     = false;
441       canpause      = false;
442       canseek       = false;
443       demux_video   = "";
444       demux_audio   = "";
445       cache_bytes   = 0;
446       cache_level   = 0.0;
447       cache_delay   = 0.0;
448       cache_offset  = 0.0;
449     }
450
451     int    player;            // source of this data
452
453     double timestamp;         // last time of update
454     double time_offset;       // difference between time and pts
455
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
460
461     std::string player_state;  // full player state
462
463     int         chapter;      // current chapter
464     std::string chapter_name; // name of current chapter
465     int         chapter_count;// number of chapter
466
467     bool canrecord;           // can input stream record
468     bool recording;           // are we currently recording
469
470     bool canpause;            // pvr: can pause the current playing item
471     bool canseek;             // pvr: can seek in the current playing item
472
473     std::string demux_video;
474     std::string demux_audio;
475
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;
482
483   CEvent m_ready;
484   CCriticalSection m_critStreamSection; // need to have this lock when switching streams (audio / video)
485
486   CEdl m_Edl;
487
488   struct SEdlAutoSkipMarkers {
489
490     void Clear()
491     {
492       cut = -1;
493       commbreak_start = -1;
494       commbreak_end = -1;
495       seek_to_start = false;
496       mute = false;
497     }
498
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
504
505   } m_EdlAutoSkipMarkers;
506
507   CPlayerOptions          m_PlayerOptions;
508
509   bool m_HasVideo;
510   bool m_HasAudio;
511
512   bool m_DemuxerPausePending;
513 };