YARP
Yet Another Robot Platform
MjpegStream.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef MJPEGSTREAM_INC
7 #define MJPEGSTREAM_INC
8 
9 #include <yarp/os/InputStream.h>
10 #include <yarp/os/OutputStream.h>
11 #include <yarp/os/TwoWayStream.h>
14 #include <yarp/os/ManagedBytes.h>
15 #include <yarp/sig/Image.h>
17 
19 
20 #include "MjpegDecompression.h"
21 
22 class MjpegStream :
24  public yarp::os::InputStream,
26 {
27 private:
28  yarp::os::TwoWayStream *delegate;
35  MjpegDecompression decompression;
36  int phase;
37  char *cursor;
38  int remaining;
39  bool autocompress;
40  yarp::os::Bytes envelope;
41 public:
42  MjpegStream(TwoWayStream *delegate, bool autocompress) :
43  delegate(delegate),
44  blobHeader(yarp::wire_rep_utils::BlobNetworkHeader{0,0,0}),
45  phase(0),
46  cursor(NULL),
47  remaining(0),
48  autocompress(autocompress)
49  {}
50 
51  virtual ~MjpegStream() {
52  delete delegate;
53  }
54 
55  yarp::os::InputStream& getInputStream() override { return *this; }
56  yarp::os::OutputStream& getOutputStream() override { return *this; }
57 
58 
59  const yarp::os::Contact& getLocalAddress() const override {
60  return delegate->getLocalAddress();
61  }
62 
63  const yarp::os::Contact& getRemoteAddress() const override {
64  return delegate->getRemoteAddress();
65  }
66 
67  bool isOk() const override {
68  return delegate->isOk();
69  }
70 
71  void reset() override {
72  delegate->reset();
73  }
74 
75  void close() override {
76  delegate->close();
77  }
78 
79  void beginPacket() override {
80  delegate->beginPacket();
81  }
82 
83  void endPacket() override {
84  delegate->endPacket();
85  }
86 
88  void write(const yarp::os::Bytes& b) override;
89 
92 
93  void interrupt() override {
94  delegate->getInputStream().interrupt();
95  }
96 
98  if (!autocompress) {
99  return false;
100  }
101  return decompression.setReadEnvelopeCallback(callback, data);
102  }
103 };
104 
105 #endif
bool setReadEnvelopeCallback(yarp::os::InputStream::readEnvelopeCallbackType callback, void *data)
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
yarp::os::OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: MjpegStream.h:56
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: MjpegStream.h:67
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: MjpegStream.h:83
virtual ~MjpegStream()
Definition: MjpegStream.h:51
yarp::os::InputStream & getInputStream() override
Get an InputStream to read from.
Definition: MjpegStream.h:55
MjpegStream(TwoWayStream *delegate, bool autocompress)
Definition: MjpegStream.h:42
bool setReadEnvelopeCallback(yarp::os::InputStream::readEnvelopeCallbackType callback, void *data) override
Install a callback that the InputStream will have to call when the envelope is read from a message in...
Definition: MjpegStream.h:97
void close() override
Terminate the stream.
Definition: MjpegStream.h:75
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: MjpegStream.h:63
void beginPacket() override
Mark the beginning of a logical packet.
Definition: MjpegStream.h:79
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: MjpegStream.h:59
void reset() override
Reset the stream.
Definition: MjpegStream.h:71
void interrupt() override
Interrupt the stream.
Definition: MjpegStream.h:93
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:20
void(* readEnvelopeCallbackType)(void *, const yarp::os::Bytes &envelope)
Callback type for setting the envelope from a message in carriers that cannot be escaped.
Definition: InputStream.h:125
virtual void interrupt()
Interrupt the stream.
Definition: InputStream.cpp:42
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:22
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
virtual void write(char ch)
Write a single byte to the stream.
An InputStream that reads from a string.
An OutputStream that produces a string.
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26
virtual void endPacket()=0
Mark the end of a logical packet (see beginPacket).
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
virtual void close()=0
Terminate the stream.
virtual void reset()=0
Reset the stream.
virtual InputStream & getInputStream()=0
Get an InputStream to read from.
virtual void beginPacket()=0
Mark the beginning of a logical packet.
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
Image class with user control of representation details.
Definition: Image.h:414
Byte order in image header for network transmission.
::ssize_t ssize_t
Definition: numeric.h:86
The main, catch-all namespace for YARP.
Definition: dirs.h:16