Instrument Neutral Distributed Interface INDI  1.9.5
streammanager_p.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2020 by Pawel Soja <kernel32.pl@gmail.com>
3  Copyright (C) 2015 by Jasem Mutlaq <mutlaqja@ikarustech.com>
4  Copyright (C) 2014 by geehalel <geehalel@gmail.com>
5 
6  Stream Recorder
7 
8  This library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Lesser General Public
10  License as published by the Free Software Foundation; either
11  version 2.1 of the License, or (at your option) any later version.
12 
13  This library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public
19  License along with this library; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 */
23 
24 #pragma once
25 
26 #include "indiapi.h"
28 #include "encoder/encodermanager.h"
29 #include "fpsmeter.h"
30 #include "uniquequeue.h"
31 #include "gammalut16.h"
32 
33 #include <atomic>
34 #include <string>
35 #include <map>
36 #include <thread>
37 
38 #include "indiccdchip.h"
39 #include "indisensorinterface.h"
40 
41 /* Smart Widget-Property */
42 #include "indipropertytext.h"
43 #include "indipropertynumber.h"
44 #include "indipropertyswitch.h"
45 namespace INDI
46 {
47 
49 {
50 public:
51  struct FrameInfo
52  {
53  size_t x, y, w, h;
54  size_t bytesPerColor;
55 
57  : x(0)
58  , y(0)
59  , w(0)
60  , h(0)
61  , bytesPerColor(0)
62  { }
63 
64  explicit FrameInfo(const CCDChip &ccdChip, size_t bytesPerColor = 1)
65  : x(ccdChip.getSubX() / ccdChip.getBinX())
66  , y(ccdChip.getSubY() / ccdChip.getBinY())
67  , w(ccdChip.getSubW() / ccdChip.getBinX())
68  , h(ccdChip.getSubH() / ccdChip.getBinY())
70  { }
71 
72  explicit FrameInfo(const SensorInterface &sensorInterface, size_t bytesPerColor = 1)
73  : x(0)
74  , y(0)
75  , w(sensorInterface.getBufferSize() * 8 / sensorInterface.getBPS())
76  , h(1)
78  { }
79 
80  size_t pixels() const
81  { return w * h; }
82 
83  size_t totalSize() const
84  { return w * h * bytesPerColor; }
85 
86  size_t lineSize() const
87  { return w * bytesPerColor; }
88 
89  bool operator!=(const FrameInfo &other) const
90  { return other.x != x || other.y != y || other.w != w || other.h != h; }
91  };
92 public:
93  StreamManagerPrivate(DefaultDevice *defaultDevice);
94  virtual ~StreamManagerPrivate();
95 
96 public:
97  bool initProperties();
98  void ISGetProperties(const char *dev);
99 
100  bool ISNewText(const char * dev, const char * name, char * texts[], char * names[], int n);
101  bool ISNewSwitch(const char * dev, const char * name, ISState * states, char * names[], int n);
102  bool ISNewNumber(const char * dev, const char * name, double values[], char * names[], int n);
103 
104  void newFrame(const uint8_t * buffer, uint32_t nbytes);
105 
106  bool updateProperties();
107  bool setStream(bool enable);
108 
109  const char *getDeviceName() const;
110 
111  void setSize(uint16_t width, uint16_t height);
112  bool setPixelFormat(INDI_PIXEL_FORMAT pixelFormat, uint8_t pixelDepth);
113 
117  void asyncStreamThread();
118 
119  // helpers
120  static std::string expand(const std::string &fname, const std::map<std::string, std::string> &patterns);
121 
122  // Utility for record file
123  bool startRecording();
124 
125  // Stop recording. Force stop even in abnormal state if needed.
126  bool stopRecording(bool force = false);
127 
134  bool uploadStream(const uint8_t *buffer, uint32_t nbytes);
135 
140  bool recordStream(const uint8_t *buffer, uint32_t nbytes, double deltams);
141 
142  void getStreamFrame(uint16_t * x, uint16_t * y, uint16_t * w, uint16_t * h) const;
143  void setStreamFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
144  void setStreamFrame(const FrameInfo &frameInfo);
145 
146  FrameInfo updateSourceFrameInfo();
147 
148  static void subframe(
149  const uint8_t *srcBuffer,
150  const FrameInfo &srcFrameInfo,
151  uint8_t *dstBuffer,
152  const FrameInfo &dstFrameInfo
153  );
154 
155 public:
157 
159 
160  /* Stream switch */
162 
164 
165  /* Record switch */
167  enum
168  {
173  };
174 
175  /* Record File Info */
177 
179  enum
180  {
183  };
184 
185  /* Measured FPS */
188 
189  /* Record Options */
191 
192  // Stream Frame
194 
195  /* BLOBs */
197 
198  // Encoder Selector. It's static now but should this implemented as plugin interface?
201 
202  // Recorder Selector. Static but should be implmeneted as a dynamic plugin interface
205 
206  // Limits. Maximum queue size for incoming frames. FPS Limit for preview
209 
210  std::atomic<bool> isStreaming { false };
211  std::atomic<bool> isRecording { false };
212  std::atomic<bool> isRecordingAboutToClose { false };
213  bool hasStreamingExposure { true };
214 
215  // Recorder
218  bool direct_record = false;
219  std::string recordfiledir, recordfilename; /* in case we should move it */
220 
221  // Encoders
224 
225  // Measure FPS
230 
231  uint32_t frameCountDivider = 0;
232 
234  uint8_t PixelDepth = 8;
235  uint16_t rawWidth = 0, rawHeight = 0;
236  std::string Format;
237 
238  // Processing for streaming
239  typedef struct {
240  double time;
241  std::vector<uint8_t> frame;
242  } TimeFrame;
243 
244  std::thread framesThread; // async incoming frames processing
245  std::atomic<bool> framesThreadTerminate {false};
247 
248  std::mutex fastFPSUpdate;
249  std::mutex recordMutex;
250 
252 };
253 
254 }
INDI::StreamManagerPrivate::ISNewSwitch
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
Definition: streammanager.cpp:757
INDI::StreamManagerPrivate::newFrame
void newFrame(const uint8_t *buffer, uint32_t nbytes)
Definition: streammanager.cpp:264
indipropertytext.h
indipropertynumber.h
INDI::StreamManagerPrivate::rawWidth
uint16_t rawWidth
Definition: streammanager_p.h:235
indiccdchip.h
INDI::StreamManagerPrivate::setSize
void setSize(uint16_t width, uint16_t height)
Definition: streammanager.cpp:494
INDI::PropertyText
Definition: indipropertytext.h:27
INDI::StreamManagerPrivate::gammaLut16
GammaLut16 gammaLut16
Definition: streammanager_p.h:251
INDI::StreamManagerPrivate::frameCountDivider
uint32_t frameCountDivider
Definition: streammanager_p.h:231
INDI::StreamManagerPrivate::RECORDER_RAW
@ RECORDER_RAW
Definition: streammanager_p.h:204
INDI::StreamManagerPrivate::recordfiledir
std::string recordfiledir
Definition: streammanager_p.h:219
INDI::StreamManagerPrivate::FrameInfo::pixels
size_t pixels() const
Definition: streammanager_p.h:80
INDI_MONO
@ INDI_MONO
Definition: indibasetypes.h:65
indipropertyswitch.h
encodermanager.h
INDI::StreamManagerPrivate::FrameInfo::w
size_t w
Definition: streammanager_p.h:53
indiapi.h
Constants and Data structure definitions for the interface to the reference INDI C API implementation...
INDI::StreamManagerPrivate::RECORD_FRAME
@ RECORD_FRAME
Definition: streammanager_p.h:171
INDI::StreamManagerPrivate::recordMutex
std::mutex recordMutex
Definition: streammanager_p.h:249
INDI::StreamManagerPrivate::encoderManager
EncoderManager encoderManager
Definition: streammanager_p.h:222
INDI::StreamManagerPrivate::RecordOptionsNP
INDI::PropertyNumber RecordOptionsNP
Definition: streammanager_p.h:190
INDI::PropertySwitch
Definition: indipropertyswitch.h:27
INDI::StreamManagerPrivate::FrameInfo::y
size_t y
Definition: streammanager_p.h:53
INDI::StreamManagerPrivate::isRecording
std::atomic< bool > isRecording
Definition: streammanager_p.h:211
INDI::StreamManagerPrivate::RecordFileTP
INDI::PropertyText RecordFileTP
Definition: streammanager_p.h:176
INDI::StreamManagerPrivate::FPSPreview
FPSMeter FPSPreview
Definition: streammanager_p.h:228
INDI::StreamManagerPrivate::recordfilename
std::string recordfilename
Definition: streammanager_p.h:219
INDI::SensorInterface
The SensorDevice class provides functionality of a Sensor Device within a Sensor.
Definition: indisensorinterface.h:70
INDI::StreamManagerPrivate::isStreaming
std::atomic< bool > isStreaming
Definition: streammanager_p.h:210
INDI::StreamManagerPrivate::FrameInfo::totalSize
size_t totalSize() const
Definition: streammanager_p.h:83
INDI::StreamManagerPrivate::RECORD_TIME
@ RECORD_TIME
Definition: streammanager_p.h:170
INDI::StreamManagerPrivate::setStream
bool setStream(bool enable)
Definition: streammanager.cpp:1015
INDI::StreamManagerPrivate::FrameInfo::x
size_t x
Definition: streammanager_p.h:53
INDI::StreamManagerPrivate::LIMITS_BUFFER_MAX
@ LIMITS_BUFFER_MAX
Definition: streammanager_p.h:208
INDI::StreamManagerPrivate::TimeFrame::time
double time
Definition: streammanager_p.h:240
INDI::StreamManagerPrivate::getStreamFrame
void getStreamFrame(uint16_t *x, uint16_t *y, uint16_t *w, uint16_t *h) const
Definition: streammanager.cpp:1136
INDI::StreamManagerPrivate::ISGetProperties
void ISGetProperties(const char *dev)
Definition: streammanager.cpp:181
INDI::StreamManagerPrivate::FrameInfo::FrameInfo
FrameInfo(const SensorInterface &sensorInterface, size_t bytesPerColor=1)
Definition: streammanager_p.h:72
INDI::StreamManagerPrivate::expand
static std::string expand(const std::string &fname, const std::map< std::string, std::string > &patterns)
Definition: streammanager.cpp:590
INDI::StreamManagerPrivate::framesIncoming
UniqueQueue< TimeFrame > framesIncoming
Definition: streammanager_p.h:246
INDI::CCDChip
The CCDChip class provides functionality of a CCD Chip within a CCD.
Definition: indiccdchip.h:48
INDI::StreamManagerPrivate::RECORDER_OGV
@ RECORDER_OGV
Definition: streammanager_p.h:204
INDI::StreamManagerPrivate::FPS_AVERAGE
@ FPS_AVERAGE
Definition: streammanager_p.h:187
gammalut16.h
INDI::StreamManagerPrivate::recorderManager
RecorderManager recorderManager
Definition: streammanager_p.h:216
INDI::StreamManagerPrivate::initProperties
bool initProperties()
Definition: streammanager.cpp:91
INDI::StreamManagerPrivate::FrameInfo::operator!=
bool operator!=(const FrameInfo &other) const
Definition: streammanager_p.h:89
INDI::StreamManagerPrivate::RecordStreamSP
INDI::PropertySwitch RecordStreamSP
Definition: streammanager_p.h:166
INDI::StreamManagerPrivate::dstFrameInfo
FrameInfo dstFrameInfo
Definition: streammanager_p.h:158
INDI::StreamManagerPrivate::RecorderSP
INDI::PropertySwitch RecorderSP
Definition: streammanager_p.h:203
INDI::StreamManagerPrivate::PixelFormat
INDI_PIXEL_FORMAT PixelFormat
Definition: streammanager_p.h:233
INDI::StreamManagerPrivate::encoder
EncoderInterface * encoder
Definition: streammanager_p.h:223
INDI::StreamManagerPrivate::currentDevice
DefaultDevice * currentDevice
Definition: streammanager_p.h:156
INDI::StreamManagerPrivate::STREAM_DIVISOR
@ STREAM_DIVISOR
Definition: streammanager_p.h:182
UniqueQueue
The UniqueQueue class is a thread-safe FIFO container adapter.
Definition: uniquequeue.h:35
INDI::StreamManagerPrivate::FrameInfo::FrameInfo
FrameInfo(const CCDChip &ccdChip, size_t bytesPerColor=1)
Definition: streammanager_p.h:64
INDI::PropertyNumber
Definition: indipropertynumber.h:27
INDI::StreamManagerPrivate::framesThread
std::thread framesThread
Definition: streammanager_p.h:244
INDI::StreamManagerPrivate::~StreamManagerPrivate
virtual ~StreamManagerPrivate()
Definition: streammanager.cpp:63
INDI::StreamManagerPrivate::asyncStreamThread
void asyncStreamThread()
Thread processing frames and forwarding to recording and preview.
Definition: streammanager.cpp:409
INDI::StreamManagerPrivate::FPSRecorder
FPSMeter FPSRecorder
Definition: streammanager_p.h:229
INDI::StreamManagerPrivate::rawHeight
uint16_t rawHeight
Definition: streammanager_p.h:235
INDI::StreamManagerPrivate::STREAM_EXPOSURE
@ STREAM_EXPOSURE
Definition: streammanager_p.h:181
INDI::StreamManagerPrivate::StreamFrameNP
INDI::PropertyNumber StreamFrameNP
Definition: streammanager_p.h:193
INDI::StreamManagerPrivate::StreamSP
INDI::PropertySwitch StreamSP
Definition: streammanager_p.h:161
INDI::StreamManagerPrivate::StreamExposureNP
INDI::PropertyNumber StreamExposureNP
Definition: streammanager_p.h:178
uniquequeue.h
INDI::StreamManagerPrivate::ENCODER_RAW
@ ENCODER_RAW
Definition: streammanager_p.h:200
INDI::StreamManagerPrivate::LIMITS_PREVIEW_FPS
@ LIMITS_PREVIEW_FPS
Definition: streammanager_p.h:208
INDI::StreamManagerPrivate::fastFPSUpdate
std::mutex fastFPSUpdate
Definition: streammanager_p.h:248
INDI::StreamManagerPrivate::FrameInfo::FrameInfo
FrameInfo()
Definition: streammanager_p.h:56
INDI::StreamManagerPrivate::FpsNP
INDI::PropertyNumber FpsNP
Definition: streammanager_p.h:186
INDI::StreamManagerPrivate::setStreamFrame
void setStreamFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
Definition: streammanager.cpp:1144
INDI::PropertyView< IBLOB >
INDI::StreamManagerPrivate::TimeFrame::frame
std::vector< uint8_t > frame
Definition: streammanager_p.h:241
INDI::StreamManagerPrivate::FPSFast
FPSMeter FPSFast
Definition: streammanager_p.h:227
INDI::StreamManagerPrivate::direct_record
bool direct_record
Definition: streammanager_p.h:218
INDI::StreamManagerPrivate::ISNewNumber
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
Definition: streammanager.cpp:928
INDI::EncoderInterface
The EncoderInterface class is the base class for video streaming encoders.
Definition: encoderinterface.h:43
INDI::StreamManagerPrivate::recorder
RecorderInterface * recorder
Definition: streammanager_p.h:217
name
const char * name
Definition: indiserver.c:116
INDI::StreamManagerPrivate::subframe
static void subframe(const uint8_t *srcBuffer, const FrameInfo &srcFrameInfo, uint8_t *dstBuffer, const FrameInfo &dstFrameInfo)
Definition: streammanager.cpp:387
INDI::EncoderManager
The EncoderManager class contains a list of active supported encoders.
Definition: encodermanager.h:33
indisensorinterface.h
INDI::StreamManagerPrivate::ENCODER_MJPEG
@ ENCODER_MJPEG
Definition: streammanager_p.h:200
INDI::StreamManagerPrivate::imageBP
INDI::PropertyView< IBLOB > * imageBP
Definition: streammanager_p.h:196
INDI::StreamManagerPrivate::updateProperties
bool updateProperties()
Definition: streammanager.cpp:208
INDI::StreamManagerPrivate::FrameInfo
Definition: streammanager_p.h:51
INDI::StreamManagerPrivate::FPSAverage
FPSMeter FPSAverage
Definition: streammanager_p.h:226
INDI
Namespace to encapsulate INDI client, drivers, and mediator classes.
Definition: AlignmentSubsystemForClients.cpp:11
INDI::StreamManagerPrivate::RECORD_ON
@ RECORD_ON
Definition: streammanager_p.h:169
INDI::StreamManagerPrivate::ISNewText
bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
Definition: streammanager.cpp:898
INDI::StreamManagerPrivate::hasStreamingExposure
bool hasStreamingExposure
Definition: streammanager_p.h:213
INDI::StreamManagerPrivate::LimitsNP
INDI::PropertyNumber LimitsNP
Definition: streammanager_p.h:207
INDI::RecorderInterface
The RecorderInterface class is the base class for recorders.
Definition: recorderinterface.h:56
INDI::FPSMeter
Definition: fpsmeter.h:27
fpsmeter.h
ISState
ISState
Switch state.
Definition: indiapi.h:148
recordermanager.h
INDI::StreamManagerPrivate
Definition: streammanager_p.h:48
INDI::StreamManagerPrivate::getDeviceName
const char * getDeviceName() const
Definition: streammanager.cpp:80
INDI::StreamManagerPrivate::StreamManagerPrivate
StreamManagerPrivate(DefaultDevice *defaultDevice)
Definition: streammanager.cpp:44
INDI::StreamManagerPrivate::uploadStream
bool uploadStream(const uint8_t *buffer, uint32_t nbytes)
uploadStream Upload frame to client using the selected encoder
Definition: streammanager.cpp:1163
INDI::StreamManagerPrivate::FrameInfo::bytesPerColor
size_t bytesPerColor
Definition: streammanager_p.h:54
INDI::StreamManagerPrivate::stopRecording
bool stopRecording(bool force=false)
Definition: streammanager.cpp:720
INDI::StreamManagerPrivate::startRecording
bool startRecording()
Definition: streammanager.cpp:613
INDI::DefaultDevice
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Definition: defaultdevice.h:118
INDI::StreamManagerPrivate::FrameInfo::lineSize
size_t lineSize() const
Definition: streammanager_p.h:86
INDI::StreamManagerPrivate::EncoderSP
INDI::PropertySwitch EncoderSP
Definition: streammanager_p.h:199
INDI::RecorderManager
The RecorderManager class contains a list of active supported recorders.
Definition: recordermanager.h:34
INDI::StreamManagerPrivate::FPS_INSTANT
@ FPS_INSTANT
Definition: streammanager_p.h:187
INDI::StreamManagerPrivate::RECORD_OFF
@ RECORD_OFF
Definition: streammanager_p.h:172
INDI::StreamManagerPrivate::TimeFrame
Definition: streammanager_p.h:239
INDI::StreamManagerPrivate::StreamTimeNP
INDI::PropertyNumber StreamTimeNP
Definition: streammanager_p.h:163
INDI::StreamManagerPrivate::PixelDepth
uint8_t PixelDepth
Definition: streammanager_p.h:234
INDI::StreamManagerPrivate::updateSourceFrameInfo
FrameInfo updateSourceFrameInfo()
Definition: streammanager.cpp:349
INDI::StreamManagerPrivate::recordStream
bool recordStream(const uint8_t *buffer, uint32_t nbytes, double deltams)
recordStream Calls the backend recorder to record a single frame.
Definition: streammanager.cpp:581
INDI::StreamManagerPrivate::framesThreadTerminate
std::atomic< bool > framesThreadTerminate
Definition: streammanager_p.h:245
Aux::buffer
std::vector< uint8_t > buffer
Definition: celestronauxpacket.h:38
GammaLut16
Definition: gammalut16.h:29
INDI_PIXEL_FORMAT
INDI_PIXEL_FORMAT
Definition: indibasetypes.h:63
INDI::StreamManagerPrivate::isRecordingAboutToClose
std::atomic< bool > isRecordingAboutToClose
Definition: streammanager_p.h:212
INDI::StreamManagerPrivate::setPixelFormat
bool setPixelFormat(INDI_PIXEL_FORMAT pixelFormat, uint8_t pixelDepth)
Definition: streammanager.cpp:539
INDI::StreamManagerPrivate::Format
std::string Format
Definition: streammanager_p.h:236
INDI::StreamManagerPrivate::FrameInfo::h
size_t h
Definition: streammanager_p.h:53