Torc  0.1
torcomxtunnel.cpp
Go to the documentation of this file.
1 /* Class TorcOMXTunnel
2 *
3 * This file is part of the Torc project.
4 *
5 * Copyright (C) Mark Kendall 2013-18
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 * USA.
21 */
22 
23 // Torc
24 #include "torclogging.h"
25 #include "torcomxtunnel.h"
26 
27 TorcOMXTunnel::TorcOMXTunnel(TorcOMXComponent *Source, OMX_U32 SourceIndex, OMX_INDEXTYPE SourceDomain,
28  TorcOMXComponent *Destination, OMX_U32 DestinationIndex, OMX_INDEXTYPE DestinationDomain)
29  : m_connected(false),
30  m_lock(QMutex::NonRecursive),
31  m_source(Source),
32  m_sourceIndex(SourceIndex),
33  m_sourceDomain(SourceDomain),
34  m_sourcePort(0),
35  m_destination(Destination),
36  m_destinationIndex(DestinationIndex),
37  m_destinationDomain(DestinationDomain),
38  m_destinationPort(0)
39 {
40  m_sourcePort = m_source->GetPort(OMX_DirOutput, m_sourceIndex, m_sourceDomain);
41  m_destinationPort = m_destination->GetPort(OMX_DirInput, m_destinationIndex, m_destinationDomain);
42 }
43 
45 {
46  Destroy();
47 }
48 
50 {
51  return m_connected;
52 }
53 
54 OMX_ERRORTYPE TorcOMXTunnel::Flush(void)
55 {
56  if (!m_connected)
57  return OMX_ErrorUndefined;
58 
59  QMutexLocker locker(&m_lock);
60 
61  OMX_CHECK(m_source->FlushBuffer(OMX_DirOutput, m_sourceIndex, m_sourceDomain), m_source->GetName(), QStringLiteral("Tunnel failed to flush source"));
62  OMX_CHECK(m_destination->FlushBuffer(OMX_DirInput, m_destinationIndex, m_destinationDomain), m_destination->GetName(), QStringLiteral("Tunnel failed to flush destination"));
63  OMX_CHECK(m_source->WaitForResponse(OMX_CommandFlush, m_sourcePort, 200), m_source->GetName(), QStringLiteral("Tunnel failed to flush source"));
64  OMX_CHECK(m_destination->WaitForResponse(OMX_CommandFlush, m_destinationPort, 200), m_destination->GetName(), QStringLiteral("Tunnel failed to flush destination"));
65 
66  return OMX_ErrorNone;
67 }
68 
69 OMX_ERRORTYPE TorcOMXTunnel::Create(void)
70 {
71  // TODO add error checking to EnablePort calls
72 
73  if (!m_source || !m_destination)
74  return OMX_ErrorUndefined;
75 
76  if (!m_source->GetHandle() || !m_destination->GetHandle())
77  return OMX_ErrorUndefined;
78 
79  QMutexLocker locker(&m_lock);
80  m_connected = false;
81  QString description = QStringLiteral("%1:%2->%3:%4").arg(m_source->GetName()).arg(m_sourcePort).arg(m_destination->GetName()).arg(m_destinationPort);
82 
83  OMX_CHECK(OMX_SetupTunnel(m_source->GetHandle(), m_sourcePort, m_destination->GetHandle(), m_destinationPort), QStringLiteral(""), QStringLiteral("Failed to create tunnel: %1").arg(description));
84 
85  LOG(VB_GENERAL, LOG_INFO, QStringLiteral("Created tunnel: %1").arg(description));
86 
87  m_connected = true;
88  return OMX_ErrorNone;
89 }
90 
91 OMX_ERRORTYPE TorcOMXTunnel::Destroy(void)
92 {
93  if (!m_source || !m_destination)
94  return OMX_ErrorUndefined;
95 
96  if (!m_source->GetHandle() || !m_destination->GetHandle() )
97  {
98  return OMX_ErrorUndefined;
99  }
100 
101  QMutexLocker locker(&m_lock);
102 
103  m_source->EnablePort(OMX_DirOutput, m_sourceIndex, false, m_sourceDomain);
104  m_destination->EnablePort(OMX_DirInput, m_destinationIndex, false, m_destinationDomain);
105 
106  OMX_ERRORTYPE error = OMX_SetupTunnel(m_source->GetHandle(), m_sourcePort, nullptr, 0);
107  if (OMX_ErrorNone != error)
108  OMX_ERROR(error, m_source->GetName(), QStringLiteral("Failed to destroy tunnel input"));
109 
110  error = OMX_SetupTunnel(m_destination->GetHandle(), m_destinationPort, nullptr, 0);
111  if (OMX_ErrorNone != error)
112  OMX_ERROR(error, m_destination->GetName(), QStringLiteral("Failed to destroy tunnel output"));
113 
114  m_connected = false;
115  return OMX_ErrorNone;
116 }
OMX_U32 GetPort(OMX_DIRTYPE Direction, OMX_U32 Index, OMX_INDEXTYPE Domain)
OMX_ERRORTYPE Create(void)
OMX_ERRORTYPE Flush(void)
QString GetName(void)
OMX_ERRORTYPE EnablePort(OMX_DIRTYPE Direction, OMX_U32 Index, bool Enable, OMX_INDEXTYPE Domain, bool Wait=true)
OMX_HANDLETYPE GetHandle(void)
#define OMX_ERROR(Error, Component, Message)
Definition: torcomxcore.h:42
#define OMX_CHECK(Error, Component, Message)
Definition: torcomxcore.h:44
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: torclogging.h:20
TorcOMXTunnel(TorcOMXComponent *Source, OMX_U32 SourceIndex, OMX_INDEXTYPE SourceDomain, TorcOMXComponent *Destination, OMX_U32 DestinationIndex, OMX_INDEXTYPE DestinationDomain)
OMX_ERRORTYPE FlushBuffer(OMX_DIRTYPE Direction, OMX_U32 Index, OMX_INDEXTYPE Domain)
OMX_ERRORTYPE Destroy(void)
OMX_ERRORTYPE WaitForResponse(OMX_U32 Command, OMX_U32 Data2, OMX_S32 Timeout)
bool IsConnected(void)