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
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:59
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: MjpegStream.h:70
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: MjpegStream.h:86
virtual ~MjpegStream()
Definition: MjpegStream.h:54
yarp::os::InputStream & getInputStream() override
Get an InputStream to read from.
Definition: MjpegStream.h:58
MjpegStream(TwoWayStream *delegate, bool autocompress)
Definition: MjpegStream.h:45
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
void close() override
Terminate the stream.
Definition: MjpegStream.h:78
const yarp::os::Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: MjpegStream.h:66
void beginPacket() override
Mark the beginning of a logical packet.
Definition: MjpegStream.h:82
const yarp::os::Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: MjpegStream.h:62
void reset() override
Reset the stream.
Definition: MjpegStream.h:74
void interrupt() override
Interrupt the stream.
Definition: MjpegStream.h:96
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
virtual int read()
Read and return a single byte.
Definition: InputStream.cpp:23
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
virtual void interrupt()
Interrupt the stream.
Definition: InputStream.cpp:45
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Definition: ManagedBytes.h:25
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
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:29
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:403
Byte order in image header for network transmission.
::ssize_t ssize_t
Definition: numeric.h:60
The main, catch-all namespace for YARP.
Definition: environment.h:18