Instrument Neutral Distributed Interface INDI 2.2.5
streammanager_p.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 by Pawel Soja <[email protected]>
3 Copyright (C) 2015 by Jasem Mutlaq <[email protected]>
4 Copyright (C) 2014 by geehalel <[email protected]>
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"
29#include "fpsmeter.h"
30#include "uniquequeue.h"
31#include "gammalut16.h"
32#include "frametracker.h"
33
34#include <atomic>
35#include <string>
36#include <map>
37#include <thread>
38
39#include "indiccdchip.h"
40#include "indisensorinterface.h"
41
42/* Smart Widget-Property */
43#include "indipropertytext.h"
44#include "indipropertynumber.h"
45#include "indipropertyswitch.h"
46#include "indipropertyblob.h"
47namespace INDI
48{
49
51{
52 public:
53 struct FrameInfo
54 {
55 size_t x, y, w, h;
57
59 : x(0)
60 , y(0)
61 , w(0)
62 , h(0)
63 , bytesPerColor(0)
64 { }
65
66 explicit FrameInfo(const CCDChip &ccdChip, size_t bytesPerColor = 1)
67 : x(ccdChip.getSubX() / ccdChip.getBinX())
68 , y(ccdChip.getSubY() / ccdChip.getBinY())
69 , w(ccdChip.getSubW() / ccdChip.getBinX())
70 , h(ccdChip.getSubH() / ccdChip.getBinY())
72 { }
73
74 explicit FrameInfo(const SensorInterface &sensorInterface, size_t bytesPerColor = 1)
75 : x(0)
76 , y(0)
77 , w(sensorInterface.getBufferSize() * 8 / sensorInterface.getBPS())
78 , h(1)
80 { }
81
82 size_t pixels() const
83 {
84 return w * h;
85 }
86
87 size_t totalSize() const
88 {
89 return w * h * bytesPerColor;
90 }
91
92 size_t lineSize() const
93 {
94 return w * bytesPerColor;
95 }
96
97 bool operator!=(const FrameInfo &other) const
98 {
99 return other.x != x || other.y != y || other.w != w || other.h != h;
100 }
101 };
102 public:
103 StreamManagerPrivate(DefaultDevice *defaultDevice);
104 virtual ~StreamManagerPrivate();
105
106 public:
107 bool initProperties();
108 void ISGetProperties(const char *dev);
109
110 bool ISNewText(const char * dev, const char * name, char * texts[], char * names[], int n);
111 bool ISNewSwitch(const char * dev, const char * name, ISState * states, char * names[], int n);
112 bool ISNewNumber(const char * dev, const char * name, double values[], char * names[], int n);
113
114 void newFrame(const uint8_t * buffer, uint32_t nbytes, uint64_t timestamp);
115
116 bool updateProperties();
117 bool setStream(bool enable);
118
119 const char *getDeviceName() const;
120
121 void setSize(uint16_t width, uint16_t height);
122 bool setPixelFormat(INDI_PIXEL_FORMAT pixelFormat, uint8_t pixelDepth);
123
127 void asyncStreamThread();
128
129 // helpers
130 static std::string expand(const std::string &fname, const std::map<std::string, std::string> &patterns);
131
132 // Utility for record file
133 bool startRecording();
134
135 // Stop recording. Force stop even in abnormal state if needed.
136 bool stopRecording(bool force = false);
137
144 bool uploadStream(const uint8_t *buffer, uint32_t nbytes);
145
150 bool recordStream(const uint8_t *buffer, uint32_t nbytes, double deltams, uint64_t timestamp);
151
152 void getStreamFrame(uint16_t * x, uint16_t * y, uint16_t * w, uint16_t * h) const;
153 void setStreamFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h);
154 void setStreamFrame(const FrameInfo &frameInfo);
155
156 FrameInfo updateSourceFrameInfo();
157
158 static void subframe(
159 const uint8_t *srcBuffer,
160 const FrameInfo &srcFrameInfo,
161 uint8_t *dstBuffer,
162 const FrameInfo &dstFrameInfo
163 );
164
173 void processTracking(const uint8_t *buffer, size_t width, size_t height, size_t originX, size_t originY);
174
175 void setTracking(bool enable);
176
177 public:
179
181
182 /* Stream switch */
184
186
187 /* Record switch */
189 enum
190 {
195 };
196
197 /* Record File Info */
199
201 enum
202 {
205 };
206
207 /* Measured FPS */
210
211 /* Record Options */
213
214 // Stream Frame
216
217 /* BLOBs */
219
220 // Encoder Selector. It's static now but should this implemented as plugin interface?
223
224 // Recorder Selector. Static but should be implemented as a dynamic plugin interface
227
228 // Limits. Maximum queue size for incoming frames. FPS Limit for preview
231
232 // Full bit depth streaming (skip 8-bit downscale for 16-bit cameras)
235
236 // Centroid tracking of extended targets (planets, Moon, Sun) in the stream
239
240 // Averaged position of the target and its offset from the lock point, in sensor (binned) pixels.
241 // State: Busy while acquiring a lock, Ok while locked, Alert when the target is lost.
243 enum
244 {
252 };
253
254 // Lock point, set from the first valid measurement or by the client
257
260
261 std::atomic<bool> isTracking { false };
262 // Guards frameTracker and the tracking properties, written from both the frame and main threads
263 std::mutex trackingMutex;
266 uint32_t trackingSequence = 0;
268
269 std::atomic<bool> isStreaming { false };
270 std::atomic<bool> isRecording { false };
271 std::atomic<bool> isRecordingAboutToClose { false };
272 bool hasStreamingExposure { true };
273
274 // Recorder
277 bool direct_record = false;
278 std::string recordfiledir, recordfilename; /* in case we should move it */
279
280 // Encoders
283
284 // Measure FPS
289
290 uint32_t frameCountDivider = 0;
291
293 uint8_t PixelDepth = 8;
294 uint16_t rawWidth = 0, rawHeight = 0;
295 std::string Format;
296
297 // Processing for streaming
298 typedef struct
299 {
300 double time;
301 uint64_t timestamp;
302 std::vector<uint8_t> frame;
303 } TimeFrame;
304
305 std::thread framesThread; // async incoming frames processing
306 std::atomic<bool> framesThreadTerminate {false};
308
309 std::mutex fastFPSUpdate;
310 std::mutex recordMutex;
311
313};
314
315}
The CCDChip class provides functionality of a CCD Chip within a CCD.
Definition indiccdchip.h:39
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
The EncoderInterface class is the base class for video streaming encoders.
The EncoderManager class contains a list of active supported encoders.
The FrameTracker class measures where a bright extended target (planet, Moon or Sun disk) sits in a v...
Provides generic container for INDI properties.
The RecorderInterface class is the base class for recorders.
The RecorderManager class contains a list of active supported recorders.
The SensorDevice class provides functionality of a Sensor Device within a Sensor.
bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n)
std::atomic< bool > isRecording
bool setPixelFormat(INDI_PIXEL_FORMAT pixelFormat, uint8_t pixelDepth)
INDI::PropertyNumber TrackingSettingsNP
INDI::PropertySwitch RecordStreamSP
static std::string expand(const std::string &fname, const std::map< std::string, std::string > &patterns)
void setStreamFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
INDI::PropertySwitch EncoderSP
std::atomic< bool > isStreaming
std::atomic< bool > isTracking
INDI::PropertySwitch StreamSP
static void subframe(const uint8_t *srcBuffer, const FrameInfo &srcFrameInfo, uint8_t *dstBuffer, const FrameInfo &dstFrameInfo)
void newFrame(const uint8_t *buffer, uint32_t nbytes, uint64_t timestamp)
std::atomic< bool > isRecordingAboutToClose
bool stopRecording(bool force=false)
UniqueQueue< TimeFrame > framesIncoming
INDI::PropertySwitch FullDepthSP
void processTracking(const uint8_t *buffer, size_t width, size_t height, size_t originX, size_t originY)
processTracking Measure the target in a frame and publish the averaged offset.
std::atomic< bool > framesThreadTerminate
INDI::PropertyNumber RecordOptionsNP
const char * getDeviceName() const
INDI::PropertyNumber TrackingOffsetNP
INDI::PropertyNumber StreamTimeNP
INDI::PropertyNumber FpsNP
void setSize(uint16_t width, uint16_t height)
bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n)
INDI::PropertyNumber TrackingLockNP
bool uploadStream(const uint8_t *buffer, uint32_t nbytes)
uploadStream Upload frame to client using the selected encoder
INDI_PIXEL_FORMAT PixelFormat
INDI::PropertySwitch TrackingSP
bool recordStream(const uint8_t *buffer, uint32_t nbytes, double deltams, uint64_t timestamp)
recordStream Calls the backend recorder to record a single frame.
RecorderInterface * recorder
INDI::PropertyText RecordFileTP
INDI::PropertyNumber StreamFrameNP
INDI::PropertyNumber LimitsNP
StreamManagerPrivate(DefaultDevice *defaultDevice)
void getStreamFrame(uint16_t *x, uint16_t *y, uint16_t *w, uint16_t *h) const
void asyncStreamThread()
Thread processing frames and forwarding to recording and preview.
void ISGetProperties(const char *dev)
bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
INDI::PropertyNumber StreamExposureNP
INDI::PropertySwitch RecorderSP
The UniqueQueue class is a thread-safe FIFO container adapter.
Definition uniquequeue.h:36
Constants and Data structure definitions for the interface to the reference INDI C API implementation...
ISState
Switch state.
Definition indiapi.h:148
INDI_PIXEL_FORMAT
@ INDI_MONO
*brief user defined string literal for JSON values *sa std::size_t n
Namespace to encapsulate INDI client, drivers, and mediator classes.
bool operator!=(const FrameInfo &other) const
FrameInfo(const SensorInterface &sensorInterface, size_t bytesPerColor=1)
FrameInfo(const CCDChip &ccdChip, size_t bytesPerColor=1)