Fawkes API  Fawkes Development Version
jpeg_stream_producer.h
1 
2 /***************************************************************************
3  * jpeg_stream_producer.h - Camera image stream producer
4  *
5  * Created: Thu Feb 06 12:48:34 2014
6  * Copyright 2006-2014 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef _PLUGINS_WEBVIEW_JPEG_STREAM_PRODUCER_H_
23 #define _PLUGINS_WEBVIEW_JPEG_STREAM_PRODUCER_H_
24 
25 #include <aspect/clock.h>
26 #include <core/threading/thread.h>
27 #include <core/utils/lock_list.h>
28 
29 #include <memory>
30 #include <string>
31 
32 namespace firevision {
33 class SharedMemoryCamera;
34 class JpegImageCompressor;
35 } // namespace firevision
36 
37 namespace fawkes {
38 
39 class TimeWait;
40 class Mutex;
41 class WaitCondition;
42 
44 {
45 public:
46  class Buffer
47  {
48  public:
49  Buffer(unsigned char *data, size_t size);
50  ~Buffer();
51 
52  /** Get data buffer.
53  * @return data buffer */
54  const unsigned char *
55  data() const
56  {
57  return data_;
58  }
59 
60  /** Get buffer size.
61  * @return b uffer size. */
62  size_t
63  size() const
64  {
65  return size_;
66  }
67 
68  private:
69  unsigned char *data_;
70  size_t size_;
71  };
72 
73  class Subscriber
74  {
75  public:
76  virtual ~Subscriber();
77  virtual void handle_buffer(std::shared_ptr<Buffer> buffer) = 0;
78  };
79 
80 public:
81  WebviewJpegStreamProducer(const std::string &image_id,
82  unsigned int quality,
83  float fps,
84  bool vflip);
86 
87  void add_subscriber(Subscriber *subscriber);
88  void remove_subscriber(Subscriber *subscriber);
89  std::shared_ptr<Buffer> wait_for_next_frame();
90 
91  virtual void init();
92  virtual void loop();
93  virtual void finalize();
94 
95 private:
96  std::string image_id_;
97  unsigned int quality_;
98  float fps_;
99  bool vflip_;
100  unsigned char *in_buffer_;
101 
102  TimeWait *timewait_;
103 
107 
108  std::shared_ptr<Buffer> last_buf_;
109  fawkes::Mutex * last_buf_mutex_;
110  fawkes::WaitCondition * last_buf_waitcond_;
111 };
112 
113 } // end namespace fawkes
114 
115 #endif
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:34
List with a lock.
Definition: lock_list.h:45
Mutex mutual exclusion lock.
Definition: mutex.h:33
Thread class encapsulation of pthreads.
Definition: thread.h:46
Time wait utility.
Definition: wait.h:33
Wait until a given condition holds.
Image buffer passed to stream subscribers.
Buffer(unsigned char *data, size_t size)
Constructor.
const unsigned char * data() const
Get data buffer.
virtual void handle_buffer(std::shared_ptr< Buffer > buffer)=0
Notification if a new buffer is available.
void add_subscriber(Subscriber *subscriber)
Add a subscriber.
virtual void loop()
Code to execute in the thread.
std::shared_ptr< Buffer > wait_for_next_frame()
Blocks caller until new thread is available.
virtual void finalize()
Finalize the thread.
virtual void init()
Initialize the thread.
void remove_subscriber(Subscriber *subscriber)
Remove a subscriber.
WebviewJpegStreamProducer(const std::string &image_id, unsigned int quality, float fps, bool vflip)
Constructor.
Jpeg image compressor.
Shared memory camera.
Definition: shmem.h:36
Fawkes library namespace.