MythTV  master
omxcontext.h
Go to the documentation of this file.
1 #ifndef OMXCONTEXT_H
2 #define OMXCONTEXT_H
3 
4 #include <cstring> // memset
5 
6 #include <OMX_Component.h>
7 #include <OMX_Image.h>
8 #include <OMX_Video.h>
9 #ifdef USING_BROADCOM
10 #include <OMX_Broadcom.h>
11 #endif
12 #ifdef USING_BELLAGIO
13 #include <bellagio/omxcore.h>
14 #endif
15 
16 #include <QString>
17 #include <QVector>
18 #include <QMutex>
19 #include <QSemaphore>
20 
21 #include "mythexp.h"
22 #include "mythlogging.h"
23 
24 class OMXComponentCtx;
26 
28 {
29  friend class OMXComponentCtx;
30 
31  // No copying
33  OMXComponent& operator =(OMXComponent &rhs);
34 
35  public:
36  // NB Shutdown() must be called before OMXComponentCtx is invalidated
37  OMXComponent(const QString &name, OMXComponentCtx &);
38  virtual ~OMXComponent();
39 
40  OMX_ERRORTYPE Init(OMX_INDEXTYPE); // OMX_IndexParamVideoInit etc
41  void Shutdown();
42 
43  bool IsValid() const { return m_state >= OMX_StateLoaded && Ports() > 0; }
44  OMX_HANDLETYPE Handle() const { return m_handle; }
45  operator OMX_HANDLETYPE () const { return m_handle; }
46  int Id() const { return m_ref; }
47 
48  OMX_U32 Base() const { return m_base; }
49  unsigned Ports() const { return unsigned(m_portdefs.size()); }
50  OMX_ERRORTYPE GetPortDef(unsigned index = 0);
51  const OMX_PARAM_PORTDEFINITIONTYPE &PortDef(unsigned index = 0) const;
52  OMX_PARAM_PORTDEFINITIONTYPE &PortDef(unsigned index = 0);
53  void ShowPortDef(unsigned index = 0, LogLevel_t = LOG_INFO, uint64_t = VB_PLAYBACK) const;
54  void ShowFormats(unsigned index = 0, LogLevel_t = LOG_INFO, uint64_t = VB_PLAYBACK) const;
55 
56  OMX_ERRORTYPE SendCommand(OMX_COMMANDTYPE cmd, OMX_U32 nParam = 0,
57  void *pCmdData = nullptr, int ms = -1, OMXComponentAbstractCB *cb = nullptr);
58  bool IsCommandComplete() const { return m_state_sema.available() > 0; }
59  OMX_ERRORTYPE WaitComplete(int ms = -1);
60  OMX_ERRORTYPE LastError();
61 
62  OMX_ERRORTYPE SetState(OMX_STATETYPE state, int ms = -1,
63  OMXComponentAbstractCB *cb = nullptr);
64  OMX_STATETYPE GetState();
65 
66  OMX_ERRORTYPE SetParameter(OMX_INDEXTYPE type, OMX_PTR p) {
67  return OMX_SetParameter(m_handle, type, p); }
68  OMX_ERRORTYPE GetParameter(OMX_INDEXTYPE type, OMX_PTR p) const {
69  return OMX_GetParameter(m_handle, type, p); }
70 
71  OMX_ERRORTYPE SetConfig(OMX_INDEXTYPE type, OMX_PTR p) {
72  return OMX_SetConfig(m_handle, type, p); }
73  OMX_ERRORTYPE GetConfig(OMX_INDEXTYPE type, OMX_PTR p) const {
74  return OMX_GetConfig(m_handle, type, p); }
75 
76  OMX_ERRORTYPE PortEnable(unsigned index = 0, int ms = -1,
77  OMXComponentAbstractCB *cb = nullptr) {
78  return SendCommand(OMX_CommandPortEnable, Base() + index, nullptr, ms, cb); }
79  OMX_ERRORTYPE PortDisable(unsigned index = 0, int ms = -1,
80  OMXComponentAbstractCB *cb = nullptr) {
81  return SendCommand(OMX_CommandPortDisable, Base() + index, nullptr, ms, cb); }
82 
83  static void EnumComponents(const QRegExp &re, LogLevel_t level = LOG_INFO,
84  uint64_t mask = VB_PLAYBACK)
85  { GetComponent(nullptr, re, level, mask); }
86 
87  private:
88  static OMX_ERRORTYPE GetComponent(OMXComponent*, const QRegExp&,
89  LogLevel_t level = LOG_INFO, uint64_t mask = VB_PLAYBACK);
90  static bool IncrRef();
91  static void DecrRef();
92 
93  static OMX_ERRORTYPE EventCB(OMX_IN OMX_HANDLETYPE, OMX_IN OMX_PTR,
94  OMX_IN OMX_EVENTTYPE, OMX_IN OMX_U32, OMX_IN OMX_U32, OMX_IN OMX_PTR);
95  static OMX_ERRORTYPE EmptyBufferCB(OMX_IN OMX_HANDLETYPE, OMX_IN OMX_PTR,
96  OMX_IN OMX_BUFFERHEADERTYPE *);
97  static OMX_ERRORTYPE FillBufferCB(OMX_IN OMX_HANDLETYPE, OMX_IN OMX_PTR,
98  OMX_IN OMX_BUFFERHEADERTYPE *);
99 
100  OMX_ERRORTYPE ReleaseBuffers();
101 
102  private:
103  int const m_ref;
105  OMX_HANDLETYPE m_handle {nullptr}; // OMX component handle
106  OMX_U32 m_base {0}; // OMX port base index
107  QVector<OMX_PARAM_PORTDEFINITIONTYPE> m_portdefs;
108  QMutex mutable m_state_lock {QMutex::Recursive};
109  OMX_STATETYPE m_state {OMX_StateInvalid};
110  OMX_ERRORTYPE m_error {OMX_ErrorNone};
111  QSemaphore m_state_sema; // EventCB signal
112 };
113 
114 // Mix-in class for creators of OMXComponent
116 {
117  public:
118  // OpenMAX callback handlers, override as required
119  virtual OMX_ERRORTYPE Event(OMXComponent&, OMX_EVENTTYPE, OMX_U32, OMX_U32, OMX_PTR);
120  virtual OMX_ERRORTYPE EmptyBufferDone(OMXComponent&, OMX_BUFFERHEADERTYPE*);
121  virtual OMX_ERRORTYPE FillBufferDone(OMXComponent&, OMX_BUFFERHEADERTYPE*);
122  // Called from OMXComponent::Shutdown to release OpenMAX buffers
123  virtual void ReleaseBuffers(OMXComponent&) {}
124 };
125 
126 // Interface class to handle callbacks from OMXComponent::SendCommand
128 {
129  public:
130  virtual ~OMXComponentAbstractCB() = default;
131  virtual OMX_ERRORTYPE Action(OMXComponent*) = 0;
132 };
133 
134 // Template class for OMXComponent::SendCommand callbacks
135 // Usage:
136 // OMX_ERRORTYPE mytype::callback() { ... }
137 // OMXComponentCB<mytype> cb(this, &mytype::callback);
138 // e = cmpnt.SetState(OMX_StateIdle, 0, &cb);
139 template<typename T>
141 {
142  public:
143  OMXComponentCB( T *inst, OMX_ERRORTYPE (T::*cb)() ) :
144  m_inst(inst), m_cb(cb) {}
145 
146  OMX_ERRORTYPE Action(OMXComponent *) override // OMXComponentAbstractCB
147  { return (m_inst->*m_cb)(); }
148 
149  protected:
150  T * const m_inst {nullptr};
151  OMX_ERRORTYPE (T::* const m_cb)() {nullptr};
152 };
153 
154 namespace omxcontext {
155 
156 #ifndef OMX_VERSION
157 // For Bellagio. Assume little endian architecture
158 #define OMX_VERSION (((SPECSTEP)<<24) | ((SPECREVISION)<<16) | ((SPECVERSIONMINOR)<<8) | (SPECVERSIONMAJOR))
159 #endif
160 
161 // Initialize an OpenMAX struct
162 template<typename T> inline void OMX_DATA_INIT(T &s)
163 {
164  std::memset(&s, 0, sizeof s);
165  s.nSize = sizeof s;
166 #ifdef OMX_VERSION
167  s.nVersion.nVersion = OMX_VERSION;
168 #else
169  s.nVersion.s.nVersionMajor = SPECVERSIONMAJOR;
170  s.nVersion.s.nVersionMinor = SPECVERSIONMINOR;
171  s.nVersion.s.nRevision = SPECREVISION;
172  s.nVersion.s.nStep = SPECSTEP;
173 #endif
174 }
175 
176 /*
177  * Convert OMX_TICKS <> int64_t
178  */
179 #ifndef OMX_SKIP64BIT
180 # define TICKS_TO_S64(n) (n)
181 # define S64_TO_TICKS(n) (n)
182 #else
183 static inline int64_t TICKS_TO_S64(OMX_TICKS ticks)
184 {
185  return ticks.nLowPart + (int64_t(ticks.nHighPart) << 32);
186 }
187 static inline OMX_TICKS S64_TO_TICKS(int64_t n)
188 {
189  OMX_TICKS ticks;
190  ticks.nLowPart = OMX_U32(n);
191  ticks.nHighPart = OMX_U32(n >> 32);
192  return ticks;
193 }
194 #endif
195 
196 MPUBLIC const char *Coding2String(OMX_VIDEO_CODINGTYPE);
197 MPUBLIC const char *Coding2String(OMX_IMAGE_CODINGTYPE);
198 MPUBLIC const char *Coding2String(OMX_AUDIO_CODINGTYPE);
199 MPUBLIC const char *Other2String(OMX_OTHER_FORMATTYPE);
200 MPUBLIC const char *Format2String(OMX_COLOR_FORMATTYPE);
201 MPUBLIC const char *Event2String(OMX_EVENTTYPE);
202 MPUBLIC const char *Error2String(OMX_ERRORTYPE);
203 MPUBLIC const char *State2String(OMX_STATETYPE);
204 MPUBLIC const char *Command2String(OMX_COMMANDTYPE);
205 MPUBLIC QString HeaderFlags(OMX_U32 nFlags);
206 #ifdef USING_BROADCOM
207 MPUBLIC const char *Interlace2String(OMX_INTERLACETYPE);
208 #endif
209 MPUBLIC const char *Filter2String(OMX_IMAGEFILTERTYPE);
210 } // namespace omxcontext
211 
212 #endif // ndef OMXCONTEXT_H
OMX_ERRORTYPE GetConfig(OMX_INDEXTYPE type, OMX_PTR p) const
Definition: omxcontext.h:73
int Id() const
Definition: omxcontext.h:46
OMXComponentCtx & m_cb
Definition: omxcontext.h:104
#define MPUBLIC
Definition: mythexp.h:10
QVector< OMX_PARAM_PORTDEFINITIONTYPE > m_portdefs
Definition: omxcontext.h:107
OMX_U32 Base() const
Definition: omxcontext.h:48
bool IsValid() const
Definition: omxcontext.h:43
QSemaphore m_state_sema
Definition: omxcontext.h:111
OMX_ERRORTYPE SetConfig(OMX_INDEXTYPE type, OMX_PTR p)
Definition: omxcontext.h:71
#define S64_TO_TICKS(n)
Definition: omxcontext.h:181
OMXComponentCB(T *inst, OMX_ERRORTYPE(T::*cb)())
Definition: omxcontext.h:143
virtual void ReleaseBuffers(OMXComponent &)
Definition: omxcontext.h:123
An action (for this plugin) consists of a description, and a set of key sequences.
Definition: action.h:38
OMX_ERRORTYPE PortEnable(unsigned index=0, int ms=-1, OMXComponentAbstractCB *cb=nullptr)
Definition: omxcontext.h:76
const char * Coding2String(OMX_VIDEO_CODINGTYPE eCompressionFormat)
Definition: omxcontext.cpp:792
#define GetState(a, b)
OMX_ERRORTYPE GetParameter(OMX_INDEXTYPE type, OMX_PTR p) const
Definition: omxcontext.h:68
static void EnumComponents(const QRegExp &re, LogLevel_t level=LOG_INFO, uint64_t mask=VB_PLAYBACK)
Definition: omxcontext.h:83
const char * State2String(OMX_STATETYPE eState)
void ShowFormats(const OMXComponent &cmpnt, unsigned n, LogLevel_t level, uint64_t mask)
Definition: omxcontext.cpp:408
int const m_ref
Definition: omxcontext.h:103
void OMX_DATA_INIT(T &s)
Definition: omxcontext.h:162
const char * name
Definition: ParseText.cpp:328
#define TICKS_TO_S64(n)
Definition: omxcontext.h:180
const char * Event2String(OMX_EVENTTYPE eEvent)
Definition: omxcontext.cpp:991
OMX_ERRORTYPE PortDisable(unsigned index=0, int ms=-1, OMXComponentAbstractCB *cb=nullptr)
Definition: omxcontext.h:79
const char * Filter2String(OMX_IMAGEFILTERTYPE eType)
OMX_ERRORTYPE Action(OMXComponent *) override
Definition: omxcontext.h:146
const char * Other2String(OMX_OTHER_FORMATTYPE eFormat)
Definition: omxcontext.cpp:900
OMX_ERRORTYPE SetParameter(OMX_INDEXTYPE type, OMX_PTR p)
Definition: omxcontext.h:66
QString HeaderFlags(OMX_U32 nFlags)
Event details.
Definition: zmdefines.h:25
const char * Error2String(OMX_ERRORTYPE eError)
bool IsCommandComplete() const
Definition: omxcontext.h:58
const char * Command2String(OMX_COMMANDTYPE cmd)
unsigned Ports() const
Definition: omxcontext.h:49
const char * Format2String(OMX_COLOR_FORMATTYPE eColorFormat)
Definition: omxcontext.cpp:922
#define OMX_VERSION
Definition: omxcontext.h:158
OMX_HANDLETYPE Handle() const
Definition: omxcontext.h:44