File: indi_webcam.h

package info (click to toggle)
indi-webcam 1.0%2B20221222161740-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 440 kB
  • sloc: cpp: 1,654; makefile: 2
file content (229 lines) | stat: -rw-r--r-- 7,735 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
INDI Webcam CCD Driver

Copyright (C) 2018 Robert Lancaster (rlancaste AT gmail DOT com)

This driver was inspired by the INDI FFMpeg Driver written by
Geehalel (geehalel AT gmail DOT com), see: https://github.com/geehalel/indi-ffmpeg

This driver is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#ifndef indi_webcam_H
#define indi_webcam_H

#include <indiccd.h>
#include <stream/streammanager.h>

#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <libavutil/version.h>

#ifdef __cplusplus
}
#endif
//#include <ctime>
#include <thread>

//These are required to check for AVFoundation Devices
//The reason is that we have to print and parse the output
//These can't be in indi_webcam class declaration because the callback method has to be passed to FFMpeg
//We need access to these variables in the callback method
std::vector<std::string> listOfSources;
bool connectedOnce = false;
bool allDevicesFound = false;
bool checkingDevices = false;

class indi_webcam : public INDI::CCD
{
public:
    indi_webcam();
    ~indi_webcam();
    void ISGetProperties(const char *dev) override;
    virtual bool ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n) override;
    virtual bool ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n) override;
    virtual bool ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n) override;
protected:
    // General device functions
    bool Connect() override;
    bool Disconnect() override;
    const char *getDefaultName() override;
    bool initProperties() override;
    bool updateProperties() override;

    void debugTriggered(bool enabled) override;



    //Related to exposures
    bool StartExposure(float duration) override;
    bool AbortExposure() override;
    void finishExposure();
    void TimerHit() override;
    float CalcTimeLeft();
    int timerID = 0;
    bool grabImage();

    bool UpdateCCDFrame(int x, int y, int w, int h) override;

    //Related to streaming
    virtual bool StartStreaming() override;
    virtual bool StopStreaming() override;

    bool saveConfigItems(FILE *fp) override;

private:

    //Related to exposures
    struct timeval ExpStart { 0, 0 };
    float ExposureRequest { 0 };
    bool convertINDI_RGBtoFITS_RGB(uint8_t *originalImage, uint8_t *convertedImage);

    //These are related to how we change sources
    bool ConnectToSource(std::string device, std::string source, int framerate, std::string videosize, std::string inputpixelformat,  std::string urlSource);
    bool ChangeSource(std::string newDevice, std::string newSource, int newFramerate, std::string newInputPixelFormat, std::string newVideosize);
    bool ChangeOnlineSource(std::string newProtocol, std::string newIPAddress, std::string newPort, std::string newUserName, std::string newPassword);
    bool ChangeOnlineSource(std::string newURL);
    bool reconnectSource();

    //These are related to updating the device list
    void findAVFoundationVideoSources();
    int getNumOfInputDevices();
    bool refreshInputDevices();
    bool refreshInputSources();
    ISwitch RefreshS[1];
    ISwitchVectorProperty RefreshSP;

    //webcam stacking.
    bool webcamStacking = false;
    bool gotAnImageAlready = false;
    bool loadingSettings = false;
    bool averaging = false;
    float *stackBuffer = nullptr;
    int numberOfFramesInStack = 0;
    bool addToStack();
    void copyFinalStackToPrimaryFrameBuffer();
    void setImageDataValueFromFloat(int x, int y, float value, bool roundAnswer);
    void setRGBImageDataValueFromFloat(int x, int y, int channel, float value, bool roundAnswer);
    float getImageDataFloatValue(int x, int y);
    float getRGBImageDataFloatValue(int x, int y, int channel);

    //These are our device capture settings
    bool use16Bit = true;
    std::string videoDevice = "";
    std::string videoSource = "";
    int frameRate = 0;
    std::string videoSize = "";
    std::string inputPixelFormat = "";
    std::string outputFormat = "";
    //These are our online device capture settings
    std::string protocol = "";
    std::string IPAddress = "";
    std::string port = "";
    std::string username = "";
    std::string password = "";
    std::string customURL = "";
    std::string url = "";

    //The timeout for avformat commands like av_open_input and av_read_frame
    double ffmpegTimeout = 0;
    //The timeout for how long of a wait time constitutes a buffered frame vs a new frame
    double bufferTimeout = 0;

    //The pixel size for the camera
    double pixelSize = 0;

    //Related to Options in the Control Panel
    IText InputOptionsT[5] {};
    ITextVectorProperty InputOptionsTP;
    IText OnlineInputOptions[4] {};
    ITextVectorProperty OnlineInputOptionsP;
    IText URLPathT[1] {};
    ITextVectorProperty URLPathTP;

    ISwitch *OnlineProtocols = nullptr;
    ISwitchVectorProperty OnlineProtocolSelection;
    ISwitch *CaptureDevices = nullptr;
    ISwitchVectorProperty CaptureDeviceSelection;
    ISwitch *CaptureSources = nullptr;
    ISwitchVectorProperty CaptureSourceSelection;
    ISwitch *FrameRates = nullptr;
    ISwitchVectorProperty FrameRateSelection;
    ISwitch *PixelFormats = nullptr;
    ISwitchVectorProperty PixelFormatSelection;
    ISwitch *VideoSizes = nullptr;
    ISwitchVectorProperty VideoSizeSelection;
    ISwitch *RapidStacking = nullptr;
    ISwitchVectorProperty RapidStackingSelection;
    ISwitch *OutputFormats = nullptr;
    ISwitchVectorProperty OutputFormatSelection;
    ISwitch *PixelSizes = nullptr;
    ISwitchVectorProperty PixelSizeSelection;

    INumber TimeoutOptionsT[2] {};
    INumberVectorProperty TimeoutOptionsTP;
    INumber PixelSizeT[1] {};
    INumberVectorProperty PixelSizeTP;
    INumber VideoAdjustmentsT[3] {};
    INumberVectorProperty VideoAdjustmentsTP;


    //Webcam setup, release, and frame capture
    bool flush_frame_buffer();
    bool setupStreaming();
    void freeMemory();
    bool getStreamFrame();

    //Related to streaming
    std::thread capture_thread;
    static void RunCaptureThread(indi_webcam *webcam);
    void run_capture();
    bool is_capturing = false;
    bool is_streaming = false;
    void start_capturing();
    void stop_capturing();

    //FFMpeg Variables to make captures work.
    struct SwsContext *sws_ctx;
    uint8_t *buffer;
    int numBytes = 0;
    AVPixelFormat out_pix_fmt;
    AVFormatContext *pFormatCtx;
    int              videoStream;
    AVCodecContext  *pCodecCtx;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 0, 100)
    AVCodec         *pCodec;
#else
    const AVCodec         *pCodec;
#endif
    AVFrame         *pFrame;
    AVFrame         *pFrameOUT;
    AVDictionary *optionsDict;

    //FFMpeg Video Adjustments
    double brightness = 0.0;
    double contrast = 1.0;
    double saturation = 1.0;
    void updateVideoAdjustments();

};
#endif // indi_webcam_H