YARP
Yet Another Robot Platform
MjpegStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef MJPEGSTREAM_INC
10 #define MJPEGSTREAM_INC
11 
12 #include <yarp/os/InputStream.h>
13 #include <yarp/os/OutputStream.h>
14 #include <yarp/os/TwoWayStream.h>
17 #include <yarp/os/ManagedBytes.h>
18 #include <yarp/sig/Image.h>
20 
22 
23 #include "MjpegDecompression.h"
24 
25 class MjpegStream :
27  public yarp::os::InputStream,
29 {
30 private:
31  yarp::os::TwoWayStream *delegate;
38  MjpegDecompression decompression;
39  int phase;
40  char *cursor;
41  int remaining;
42  bool autocompress;
43  yarp::os::Bytes envelope;
44 public:
45  MjpegStream(TwoWayStream *delegate, bool autocompress) :
46  delegate(delegate),
47  blobHeader(yarp::wire_rep_utils::BlobNetworkHeader{0,0,0}),
48  phase(0),
49  cursor(NULL),
50  remaining(0),
51  autocompress(autocompress)
52  {}
53 
54  virtual ~MjpegStream() {
55  delete delegate;
56  }
57 
58  yarp::os::InputStream& getInputStream() override { return *this; }
59  yarp::os::OutputStream& getOutputStream() override { return *this; }
60 
61 
62  const yarp::os::Contact& getLocalAddress() const override {
63  return delegate->getLocalAddress();
64  }
65 
66  const yarp::os::Contact& getRemoteAddress() const override {
67  return delegate->getRemoteAddress();
68  }
69 
70  bool isOk() const override {
71  return delegate->isOk();
72  }
73 
74  void reset() override {
75  delegate->reset();
76  }
77 
78  void close() override {
79  delegate->close();
80  }
81 
82  void beginPacket() override {
83  delegate->beginPacket();
84  }
85 
86  void endPacket() override {
87  delegate->endPacket();
88  }
89 
91  void write(const yarp::os::Bytes& b) override;
92 
95 
96  void interrupt() override {
97  delegate->getInputStream().interrupt();
98  }
99 
101  if (!autocompress) {
102  return false;
103  }
104  return decompression.setReadEnvelopeCallback(callback, data);
105  }
106 };
107 
108 #endif
yarp::os::TwoWayStream::getLocalAddress
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
yarp::os::TwoWayStream
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
yarp::sig::ImageNetworkHeader
Byte order in image header for network transmission.
Definition: ImageNetworkHeader.h:33
ImageNetworkHeader.h
BlobNetworkHeader.h
yarp::os::OutputStream::write
virtual void write(char ch)
Write a single byte to the stream.
Definition: OutputStream.cpp:17
yarp::os::OutputStream
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
yarp::os::InputStream::interrupt
virtual void interrupt()
Interrupt the stream.
Definition: InputStream.cpp:45
OutputStream.h
yarp::os::TwoWayStream::reset
virtual void reset()=0
Reset the stream.
yarp::os::StringOutputStream
An OutputStream that produces a string.
Definition: StringOutputStream.h:25
MjpegStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: MjpegStream.h:82
StringInputStream.h
MjpegStream::write
void write(const yarp::os::Bytes &b) override
Write a block of bytes to the stream.
Definition: MjpegStream.cpp:118
yarp::os::InputStream::readEnvelopeCallbackType
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:128
TwoWayStream.h
yarp::wire_rep_utils::BlobNetworkHeader
Definition: BlobNetworkHeader.h:22
MjpegStream::getInputStream
yarp::os::InputStream & getInputStream() override
Get an InputStream to read from.
Definition: MjpegStream.h:58
ManagedBytes.h
yarp::os::ManagedBytes
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
yarp::os::TwoWayStream::close
virtual void close()=0
Terminate the stream.
MjpegStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: MjpegStream.h:86
MjpegStream
Definition: MjpegStream.h:29
yarp::sig::FlexImage
Image class with user control of representation details.
Definition: Image.h:403
yarp::os::TwoWayStream::endPacket
virtual void endPacket()=0
Mark the end of a logical packet (see beginPacket).
MjpegStream::getLocalAddress
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: MjpegStream.h:62
MjpegStream::~MjpegStream
virtual ~MjpegStream()
Definition: MjpegStream.h:54
yarp::conf::ssize_t
::ssize_t ssize_t
Definition: numeric.h:60
yarp::os::TwoWayStream::getRemoteAddress
virtual const Contact & getRemoteAddress() const =0
Get the address of the remote side of the stream.
yarp::os::StringInputStream
An InputStream that reads from a string.
Definition: StringInputStream.h:25
MjpegStream::interrupt
void interrupt() override
Interrupt the stream.
Definition: MjpegStream.h:96
MjpegStream::getOutputStream
yarp::os::OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: MjpegStream.h:59
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
Image.h
MjpegDecompression
Definition: MjpegDecompression.h:18
StringOutputStream.h
yarp::os::TwoWayStream::beginPacket
virtual void beginPacket()=0
Mark the beginning of a logical packet.
MjpegDecompression::setReadEnvelopeCallback
bool setReadEnvelopeCallback(yarp::os::InputStream::readEnvelopeCallbackType callback, void *data)
Definition: MjpegDecompression.cpp:223
yarp::os::InputStream::read
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::TwoWayStream::isOk
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
InputStream.h
MjpegStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: MjpegStream.h:70
yarp::os::TwoWayStream::getInputStream
virtual InputStream & getInputStream()=0
Get an InputStream to read from.
MjpegStream::close
void close() override
Terminate the stream.
Definition: MjpegStream.h:78
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
MjpegStream::setReadEnvelopeCallback
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:100
MjpegDecompression.h
MjpegStream::reset
void reset() override
Reset the stream.
Definition: MjpegStream.h:74
MjpegStream::getRemoteAddress
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: MjpegStream.h:66
MjpegStream::MjpegStream
MjpegStream(TwoWayStream *delegate, bool autocompress)
Definition: MjpegStream.h:45