StreamReceiver.h
Go to the documentation of this file.
1 /**
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package TabletTeleoperation::ArmarXObjects::StreamReceiver
19  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
20  * @date 2013
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 
30 
32 #include <VisionX/interface/components/StreamProvider.h>
33 
34 extern "C"
35 {
36  class _GstElement;
37  using GstElement = _GstElement;
38 }
39 
40 extern "C"
41 {
42 #include <libavcodec/avcodec.h>
43 #include <libswscale/swscale.h>
44 }
45 
46 namespace armarx
47 {
48 
49 
50  using ImageMap = std::map<std::string, CByteImage*>;
51 
52  /**
53  * @class StreamReceiver
54  * @brief A brief description
55  *
56  * Detailed Description
57  */
58  class StreamReceiver : virtual public armarx::Component, public Stream::StreamListenerInterface
59  {
60  public:
62  {
63  public:
66  {
67  defineOptionalProperty<std::string>(
68  "UsedStreamProvider",
69  "StreamProvider",
70  "Names of the StreamProvider that is to be used");
71  // defineOptionalProperty<std::string>("UsedSources", "Camera,Test", "Names of sources that are to be used of the StreamProvider. Seperated by comma, space or tab");
72  }
73  };
74 
75  /**
76  * @see armarx::ManagedIceObject::getDefaultName()
77  */
78  virtual std::string
80  {
81  return "StreamReceiver";
82  }
83 
84  /**
85  * @brief getImages retreives the next buffered images for all streams.
86  * @param imagesOut map of already allocated images in correct size
87  */
88  void getImages(std::vector<CByteImage*>& imagesOut);
89 
90  /**
91  * @brief getNumberOfImages can be called after the component was initialized.
92  * @return the number of provided images
93  */
94  int getNumberOfImages();
95 
96 
97  void getImageInformation(int& imageWidth, int& imageHeight, int& imageType);
98 
99 
100  protected:
101  /**
102  * @see armarx::ManagedIceObject::onInitComponent()
103  */
104  virtual void onInitComponent();
105 
106  /**
107  * @see armarx::ManagedIceObject::onConnectComponent()
108  */
109  virtual void onConnectComponent();
110 
111  /**
112  * @see armarx::ManagedIceObject::onDisconnectComponent()
113  */
114  virtual void onDisconnectComponent();
115 
116  /**
117  * @see armarx::ManagedIceObject::onExitComponent()
118  */
119  virtual void onExitComponent();
120 
121  /**
122  * @see PropertyUser::createPropertyDefinitions()
123  */
126  {
129  }
130 
131  private:
132  void receive();
133  void store();
134 
135  class StreamElements;
136  using StreamElementsPtr = std::shared_ptr<StreamElements>;
137 
138  struct StreamElements : Stream::StreamMetaData
139  {
140  public:
141  StreamElements(std::string streamName,
142  int streamID,
143  Stream::StreamProviderPrx streamProvider);
144  GstElement* pipeline;
145  GstElement* appsrc;
146  GstElement* appsink;
147  std::stringstream pipelineString;
148  armarx::Mutex mutex;
149  int fetchedChunks;
150  IceUtil::Time lastFPSCheck;
151  int framesSinceLastFPSCheck;
153  Stream::StreamProviderPrx streamProvider;
154  void receive();
155  int receivedIndex;
156  int realPosition;
157  int streamID;
158 
159  boost::mutex mutexPulling;
160  };
161 
162  using StreamSourceMap = boost::unordered_map<std::string, StreamElementsPtr>;
163  StreamReceiver::StreamElementsPtr getStreamElements(std::string streamName);
164  void getImageFormat(StreamElementsPtr elem, int& height, int& width);
165 
166 
167  Stream::StreamProviderPrx streamProvider;
168  // PeriodicTask<StreamReceiver>::pointer_type taskReceive;
170  StreamSourceMap streamSources;
171 
172 
173  int numberImages;
174 
175  IceUtil::Time start;
176  long transferredBits;
177  // GstElement* appsrc;
178  // GstElement* appsink;
179  // GstElement* pipeline;
180  float bandwidth_kbps;
181  Mutex pipelineMutex;
182  Stream::CodecType codec;
183 
184  IceUtil::Time lastReceiveTimestamp;
185 
186 
187  // StreamListenerInterface interface
188  public:
189  void reportNewStreamData(const Stream::DataChunk& chunk, const Ice::Current&);
190 
191  AVCodec* m_decoder;
192  AVCodecContext* m_decoderContext;
194  AVFrame* m_picture;
195  AVPacket m_packet;
199  };
200 
202 } // namespace armarx
armarx::StreamReceiver::onExitComponent
virtual void onExitComponent()
Definition: StreamReceiver.cpp:326
armarx::StreamReceiver::m_got_picture
int m_got_picture
Definition: StreamReceiver.h:193
armarx::StreamReceiver::decodedImageMutex
Mutex decodedImageMutex
Definition: StreamReceiver.h:196
armarx::StreamReceiver::getImageInformation
void getImageInformation(int &imageWidth, int &imageHeight, int &imageType)
Definition: StreamReceiver.cpp:580
armarx::ImageMap
std::map< std::string, CByteImage * > ImageMap
Definition: StreamReceiver.h:50
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
PeriodicTask.h
armarx::StreamReceiver::pCombinedDecodedImage
CByteImage * pCombinedDecodedImage
Definition: StreamReceiver.h:198
armarx::StreamReceiver::getDefaultName
virtual std::string getDefaultName() const
Definition: StreamReceiver.h:79
armarx::StreamReceiver::getImages
void getImages(std::vector< CByteImage * > &imagesOut)
getImages retreives the next buffered images for all streams.
Definition: StreamReceiver.cpp:471
armarx::PeriodicTask::pointer_type
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
Definition: PeriodicTask.h:68
armarx::StreamReceiver::ppDecodedImages
CByteImage ** ppDecodedImages
Definition: StreamReceiver.h:198
IceInternal::Handle< StreamReceiver >
armarx::StreamReceiver::m_decoderContext
AVCodecContext * m_decoderContext
Definition: StreamReceiver.h:192
armarx::StreamReceiver::getNumberOfImages
int getNumberOfImages()
getNumberOfImages can be called after the component was initialized.
Definition: StreamReceiver.cpp:573
armarx::StreamReceiver::StreamReceiverPropertyDefinitions
Definition: StreamReceiver.h:61
armarx::StreamReceiver::streamDecodeMutex
Mutex streamDecodeMutex
Definition: StreamReceiver.h:197
StreamProviderI.h
armarx::StreamReceiver::reportNewStreamData
void reportNewStreamData(const Stream::DataChunk &chunk, const Ice::Current &)
Definition: StreamReceiver.cpp:371
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::StreamReceiver::onConnectComponent
virtual void onConnectComponent()
Definition: StreamReceiver.cpp:124
armarx::StreamReceiver
A brief description.
Definition: StreamReceiver.h:58
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
GstElement
_GstElement GstElement
Definition: StreamReceiver.h:37
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::StreamReceiver::m_packet
AVPacket m_packet
Definition: StreamReceiver.h:195
armarx::Mutex
boost::mutex Mutex
Definition: Synchronization.h:149
armarx::StreamReceiver::m_picture
AVFrame * m_picture
Definition: StreamReceiver.h:194
armarx::StreamReceiver::m_decoder
AVCodec * m_decoder
Definition: StreamReceiver.h:191
armarx::PeriodicTask
Definition: ArmarXManager.h:70
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::StreamReceiver::onDisconnectComponent
virtual void onDisconnectComponent()
Definition: StreamReceiver.cpp:244
armarx::StreamReceiver::StreamReceiverPropertyDefinitions::StreamReceiverPropertyDefinitions
StreamReceiverPropertyDefinitions(std::string prefix)
Definition: StreamReceiver.h:64
armarx::StreamReceiver::onInitComponent
virtual void onInitComponent()
Definition: StreamReceiver.cpp:61
armarx::StreamReceiver::createPropertyDefinitions
virtual armarx::PropertyDefinitionsPtr createPropertyDefinitions()
Definition: StreamReceiver.h:125
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27