0030612: Visualization - provide texture map with video as image source
[occt.git] / src / Media / Media_PlayerContext.hxx
1 // Created by: Kirill GAVRILOV
2 // Copyright (c) 2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #ifndef _Media_PlayerContext_HeaderFile
16 #define _Media_PlayerContext_HeaderFile
17
18 #include <Media_IFrameQueue.hxx>
19 #include <Media_Timer.hxx>
20 #include <OSD_Thread.hxx>
21 #include <Standard_Condition.hxx>
22 #include <Standard_Mutex.hxx>
23 #include <Standard_Transient.hxx>
24 #include <Standard_Type.hxx>
25 #include <TCollection_AsciiString.hxx>
26
27 class Media_BufferPool;
28 class Media_CodecContext;
29 class Media_FormatContext;
30 class Media_Scaler;
31
32 //! Player context.
33 class Media_PlayerContext : public Standard_Transient
34 {
35   DEFINE_STANDARD_RTTIEXT(Media_PlayerContext, Standard_Transient)
36 public:
37
38   //! Dump first video frame.
39   //! @param theSrcVideo [in] path to the video
40   //! @param theMediaInfo [out] video description
41   Standard_EXPORT static Handle(Media_Frame) DumpFirstFrame (const TCollection_AsciiString& theSrcVideo,
42                                                              TCollection_AsciiString& theMediaInfo);
43
44   //! Dump first video frame.
45   //! @param theSrcVideo [in] path to the video
46   //! @param theOutImage [in] path to make a screenshot
47   //! @param theMediaInfo [out] video description
48   //! @param theMaxSize [in] when positive - downscales image to specified size
49   Standard_EXPORT static bool DumpFirstFrame (const TCollection_AsciiString& theSrcVideo,
50                                               const TCollection_AsciiString& theOutImage,
51                                               TCollection_AsciiString& theMediaInfo,
52                                               int theMaxSize = 0);
53
54 public:
55
56   //! Main constructor.
57   //! Note that Frame Queue is stored as pointer,
58   //! and it is expected that this context is stored as a class field of Frame Queue.
59   Standard_EXPORT Media_PlayerContext (Media_IFrameQueue* theFrameQueue);
60
61   //! Destructor.
62   Standard_EXPORT virtual ~Media_PlayerContext();
63
64 public:
65
66   //! Set new input for playback.
67   Standard_EXPORT void SetInput (const TCollection_AsciiString& theInputPath,
68                                  Standard_Boolean theToWait);
69
70   //! Return playback state.
71   Standard_EXPORT void PlaybackState (Standard_Boolean& theIsPaused,
72                                       Standard_Real& theProgress,
73                                       Standard_Real& theDuration);
74
75   //! Pause/Pause playback depending on the current state.
76   Standard_EXPORT void PlayPause (Standard_Boolean& theIsPaused,
77                                   Standard_Real& theProgress,
78                                   Standard_Real& theDuration);
79
80   //! Seek to specified position.
81   Standard_EXPORT void Seek (Standard_Real thePosSec);
82
83   //! Pause playback.
84   void Pause() { pushPlayEvent (Media_PlayerEvent_PAUSE); }
85
86   //! Resume playback.
87   void Resume() { pushPlayEvent (Media_PlayerEvent_RESUME); }
88
89   //! Return TRUE if queue requires RGB pixel format or can handle also YUV pixel format; TRUE by default.
90   bool ToForceRgb() const { return myToForceRgb; }
91
92   //! Set if queue requires RGB pixel format or can handle also YUV pixel format.
93   void SetForceRgb (bool theToForce) { myToForceRgb = theToForce; }
94
95 private:
96
97   //! Internal enumeration for events.
98   enum Media_PlayerEvent
99   {
100     Media_PlayerEvent_NONE = 0,
101     Media_PlayerEvent_PAUSE,
102     Media_PlayerEvent_RESUME,
103     Media_PlayerEvent_SEEK,
104     Media_PlayerEvent_NEXT,
105   };
106
107 private:
108
109   //! Thread loop.
110   Standard_EXPORT void doThreadLoop();
111
112   //! Push new playback event.
113   Standard_EXPORT void pushPlayEvent (Media_PlayerEvent thePlayEvent);
114
115   //! Fetch new playback event.
116   Standard_EXPORT bool popPlayEvent (Media_PlayerEvent& thePlayEvent,
117                                      const Handle(Media_FormatContext)& theFormatCtx,
118                                      const Handle(Media_CodecContext)& theVideoCtx,
119                                      const Handle(Media_Frame)& theFrame);
120
121   //! Decode new frame.
122   bool receiveFrame (const Handle(Media_Frame)& theFrame,
123                      const Handle(Media_CodecContext)& theVideoCtx);
124
125   //! Thread creation callback.
126   static Standard_Address doThreadWrapper (Standard_Address theData)
127   {
128     Media_PlayerContext* aThis = (Media_PlayerContext* )theData;
129     aThis->doThreadLoop();
130     return 0;
131   }
132
133 private:
134
135   Media_IFrameQueue*          myFrameQueue;     //!< frame queue
136   OSD_Thread                  myThread;         //!< working thread
137   Standard_Mutex              myMutex;          //!< mutex for events
138   Standard_Condition          myWakeEvent;      //!< event to wake up working thread and proceed new playback event
139   Standard_Condition          myNextEvent;      //!< event to check if working thread processed next file event (e.g. released file handles of previous input)
140   Media_Timer                 myTimer;          //!< playback timer       
141   Standard_Real               myDuration;       //!< playback duration
142
143   Handle(Media_BufferPool)    myBufferPools[4]; //!< per-plane pools
144   Handle(Media_Frame)         myFrameTmp;       //!< temporary object holding decoded frame
145   Handle(Media_Scaler)        myScaler;         //!< pixel format conversion tool
146   bool                        myToForceRgb;     //!< flag indicating if queue requires RGB pixel format or can handle also YUV pixel format
147
148   volatile bool               myToShutDown;     //!< flag to terminate working thread
149   TCollection_AsciiString     myInputPath;      //!< new input to open
150   volatile Standard_Real      mySeekTo;         //!< new seeking position
151   volatile Media_PlayerEvent  myPlayEvent;      //!< playback event
152
153 };
154
155 #endif // _Media_PlayerContext_HeaderFile