UgCS video streamer
video_streamer_c_api.h
Go to the documentation of this file.
1 #ifndef INCLUDE_VIDEO_STREAMER_C_API_H_
2 #define INCLUDE_VIDEO_STREAMER_C_API_H_
3 
4 /** @file video_streamer_c_api.h
5  * API in this file is intended for external clients which will use this library in a plug-in
6  * manner like dlopen(). This is not intended to be compiled by C compiler and thus references C++
7  * code (like enums).
8  * All strings in the API are UTF-8 encoded where not otherwise specified.
9  *
10  * @section workflow Overall workflow
11  *
12  * The library responsibility is to produce
13  * <a href="https://gwg.nga.mil/misb/misp_pubs.html">MISP-compliant</a> video stream with telemetry
14  * metadata integrated. It is up to the library user to obtain the video stream suitable for the
15  * library input (raw H.264 video stream) and telemetry values. Typically the input video stream can
16  * be obtained using ffmpeg or gstreamer library from some local video input devices or network
17  * video stream (see @ref ffmpeg_v4l_example and @ref stdin_example). DJI drones provide such stream
18  * in their DJI SDK. In some cases the source may not provide H.264-encoded stream and uses either
19  * another encoder or provides raw unencoded video. It is up to the library user to perform
20  * transcoding or encoding into H.264 in such case.
21  *
22  * First, the library must be initialized by calling @ref VsInitialize(). The passed callback allows
23  * integration with a logging framework used in your application. No other calls are permitted
24  * before the library is initialized. Then the streamer instance can be created by calling @ref
25  * VsCreate(). To use, for example, with UgCS video server, the URL like
26  * `urtp+connect://<ip_address>://3341` should be passed in @ref VsParams, where `<ip_address>` is
27  * UgCS video server address. The status callback argument allows to specify function which will
28  * receive all updates about the streamer instance status. The returned instance handle should be
29  * used for all the rest instance-bound calls. Then the instance can be started by calling @ref
30  * VsStart() method. After started the video data can be fed by @ref VsFeedData() function. It
31  * expects raw H.264 video stream. At the same time telemetry data can be provided by @ref
32  * VsFeedTelemetry() function. Call @ref VsStop() when done with the streamer instance and @ref
33  * VsTerminate() when done with the entire library.
34  */
35 
36 #include <streamer.h>
37 
38 /** Represents handle for video streamer instance. One instance is bound to one streaming target. */
39 typedef void *VideoStreamerHandle;
40 
41 /** Called on each log message.
42  * @param opaque Caller-defined parameter specified in @ref VsParams.
43  */
44 typedef void (*VsLogCallback)(Log::Level level, const char *msg);
45 
46 /** Called on streamer status change.
47  * @param opaque Caller-defined parameter specified in @ref VsParams.
48  * @see @ref Streamer::Status
49  */
50 typedef void (*VsStatusCallback)(Streamer::Status status, void *opaque);
51 
52 /** Called when queue is overflowed or when it is consumed again.
53  * @param status True when overflowed, false when back in normal state.
54  */
55 typedef void (*VsQueueOverflowCallback)(bool status, void *opaque);
56 
57 /** Error code. Will be extended with more specific error codes when necessarily. */
58 enum class VsError {
59  /** No error. */
60  NONE,
61  /** Parameter is invalid. */
63  /** Requested operation is not permitted in current state. */
64  INVALID_OP,
65  /** Internal error which indicates a software bug. Should not be caught in any way. */
67 
68  /** Other error. */
69  OTHER
70 };
71 
72 /** Streamer parameters. */
73 struct VsParams {
74  /** Caller-defined parameter to pass in callbacks. */
75  void *opaque = nullptr;
76  /** Vehicle ID (typically serial number). */
77  const char *vehicleId;
78  /** User-defined tail number, may be null. */
79  const char *tailNumber = nullptr;
80  /** Target URI for streaming. Examples:
81  * * `udp://1.2.3.4:3338` - Plain UDP streaming, suitable for external MISP-compliant players
82  * such as GIS software or just a regular media player (metadata will not be available in
83  * such case).
84  * * `urtp+connect://1.2.3.4://3341 - URTP streaming with outgoing connection. URTP is UgCS
85  * proprietary protocol supported by its Command Center components. It ensures better
86  * quality on unreliable links.
87  */
88  const char *targetUri;
89  /** Zero value disables transcoding, -1 for auto (based on input bitrate and scale),
90  * otherwise value in bits/s.
91  */
92  int64_t transcodeBitrate = 0;
93  /** Frame size scale factor for transcoding output, 1.0 to preserve input size. */
94  float transcodeScale = 1.0;
95  /** Frame rate limit for transcoding output, fps. Zero to preserve all input frames. */
97 };
98 
99 /** Updated telemetry data block. Any fields can be empty, only non-empty values are sent in next
100  * metadata packet. String fields are empty when null, double values are empty when isPresent flag
101  * is false. All fields correspond to ST 0601.13 defined fields, see there for additional details.
102  */
103 struct VsTelemetry {
104  /** Optional floating point value. */
105  struct DoubleValue {
106  double value = 0.0;
107  /** True if value specified, false if empty. */
108  bool isPresent = false;
109  };
110  const char *missionId = nullptr,
111  *platformDesignation = nullptr,
112  *cameraManufacturer = nullptr,
113  *cameraModel = nullptr;
114  /** Platform heading angle, relative between longitudinal axis and True North measured in
115  * the horizontal plane, radians. Range: [0; 2*PI].
116  */
118  /** Platform pitch angle, angle between longitudinal axis and horizontal plane, radians.
119  * Only angle in range (-20; 20) degrees is properly transferred, other values are
120  * transferred as "out of range" indicator.
121  */
123  /** Platform roll angle, angle between transverse axis and transverse-longitudinal plane,
124  * radians. Positive angles for lowered right wing.
125  * Only angle in range (-50; 50) degrees is properly transferred, other values are
126  * transferred as "out of range" indicator.
127  */
129  /** Sensor coordinates, rad. */
130  latitude, longitude,
131  /** Sensor true altitude, meters. */
133  /** Sensor horizontal field of view angle, radians. Range: [0; PI]. */
135  /** Sensor vertical field of view angle, radians.Range: [0; PI]. */
137  /** Sensor relative azimuth angle, radians. Rotation angle between platform longitudinal axis
138  * and camera pointing direction as seen from above the platform. Range: [0; 2*PI].
139  */
141  /** Sensor relative elevation angle, radians. Relative elevation angle of sensor to platform
142  * longitudinal-transverse plane, negative angles down.
143  * Range: [-PI; PI].
144  */
146  /** Relative roll angle of sensor to aircraft platform, radians. Top of image is zero degrees.
147  * Positive angles are clockwise when looking from behind camera.
148  * Range: [0; 2*PI].
149  */
151  /** Slant range, meters. */
153  /** In millimeters. */
155 };
156 
157 extern "C" {
158 
159 /** Get error from last API call in current thread. Each API call overwrites previously stored
160  * error value. Calling VcGetLastError() does not change last error value.
161  *
162  * @param msgBuf Buffer for accepting error message (mostly for troubleshooting, not for forwarding
163  * to user). UTF-8 encoded null-terminated message is stored here. The string is truncated if buffer
164  * is too small, null terminator is always written.
165  * @param bufSize Should contain buffer size in bytes on input. Contains necessary size to accept
166  * whole the string on return (including null terminating character).
167  * @return Last error code.
168  */
169 VsError
170 VsGetLastError(char *msgBuf, uint32_t *bufSize);
171 
172 /** Initialize the library. Should be called before any other API call.
173  * @param logCbk Callback for connecting logging to the framework used by an application.
174  * @return Zero on success, non-zero on failure.
175  */
176 int
178 
179 #ifdef HAS_FILESYSTEM
180 
181 /** Crash reporting feature is armed after this call. May be not implemented on all platforms.
182  * @param reportDir Directory to store report files. File name is generated automatically.
183  * @return Zero on success, non-zero on failure.
184  */
185 int
186 VsSetupCrashReport(const char *reportDir);
187 
188 #endif /* HAS_FILESYSTEM */
189 
190 /** Shutdown the library. Should be called when done working with the library. */
191 int
192 VsTerminate();
193 
194 /** Create streamer instance. All events callbacks are called in a dedicated thread.
195  *
196  * @param Streamer parameters. Owned by the caller.
197  * @param statusCbk Callback for status change events. See @ref VsStatusCallback.
198  * @param queueOverflowCbk Callback for queue overflow events. See @ref VsQueueOverflowCallback.
199  * @return Returned instance should be later released by VsDestroy(). Null can be returned in case
200  * of failure.
201  */
203 VsCreate(const VsParams *params, VsStatusCallback statusCbk,
204  VsQueueOverflowCallback queueOverflowCbk);
205 
206 /** Destroy the streamer instance and release all resources. */
207 void
209 
210 /** Start the streamer instance. Should be called before trying to pass any video or telemetry to
211  * the streamer instance. VsStop() should be called after successful streamer start.
212  * @return Zero on success, non-zero on failure.
213  */
214 int
216 
217 /** Stop the streamer instance, releasing all the processing resources. Should not be called in
218  * events thread.
219  * @return Zero on success, non-zero on failure.
220  */
221 int
223 
224 /** Set new tail number. This is user-readable part of composite tail number which can be changed at
225  * any time (in contrast with vehicle ID part). Some non-permitted characters may be replaced.
226  * @return Zero on success, non-zero on failure.
227  */
228 int
229 VsSetTailNumber(VideoStreamerHandle vs, const char *tailNumber);
230 
231 /** Feed next chunk of raw H.264 video stream. There are no requirements for chunks alignment
232  * and sizes respectively to H.264 protocol data units.
233  * @param dataBuf Buffer with the data chunk.
234  * @param bufSize Size of the provided buffer in bytes.
235  * @return Zero on success, non-zero on failure.
236  */
237 int
238 VsFeedData(VideoStreamerHandle vs, const uint8_t *dataBuf, uint32_t bufSize);
239 
240 /** Set most recent telemetry values to send in next metadata packet. Fields may be updated
241  * selectively, only non-empty values are accounted in each invocation. Values are validated and
242  * INVALID_PARAM error is set if any value is out of range. Whole the specified telemetry block is
243  * discarded in such case.
244  * @return Zero on success, non-zero on failure.
245  */
246 int
247 VsFeedTelemetry(VideoStreamerHandle vs, const VsTelemetry *telemetry);
248 
249 /** Get statistics report for the streamer instance.
250  *
251  * @param dataBuf Buffer which receives string with JSON statistics data. The data do not have any
252  * fixed structure (actual content is a subject for change at any time) and are intended for
253  * display/logging only. No any logic should be bound to. The returned string is always null
254  * terminated.
255  * @param bufSize Should contain buffer size in bytes on input. Contains necessary size to accept
256  * whole the string on return (including null terminating character). Keep in mind that next call
257  * will most probably return another string so some spare space is recommended.
258  * @return Zero on success, non-zero on failure.
259  */
260 int
261 VsGetStatistics(VideoStreamerHandle vs, char *dataBuf, uint32_t *bufSize);
262 
263 } /* extern "C" */
264 
265 #endif /* INCLUDE_VIDEO_STREAMER_C_API_H_ */
VsTerminate
int VsTerminate()
VsParams::transcodeScale
float transcodeScale
Definition: video_streamer_c_api.h:94
VsError::NONE
@ NONE
VsTelemetry::sensorVerticalFov
DoubleValue sensorVerticalFov
Definition: video_streamer_c_api.h:136
VsParams::transcodeBitrate
int64_t transcodeBitrate
Definition: video_streamer_c_api.h:92
VsTelemetry::altitude
DoubleValue altitude
Definition: video_streamer_c_api.h:132
VsTelemetry::sensorRelativeElevation
DoubleValue sensorRelativeElevation
Definition: video_streamer_c_api.h:145
VsTelemetry::sensorHorizontalFov
DoubleValue sensorHorizontalFov
Definition: video_streamer_c_api.h:134
VsFeedTelemetry
int VsFeedTelemetry(VideoStreamerHandle vs, const VsTelemetry *telemetry)
VsFeedData
int VsFeedData(VideoStreamerHandle vs, const uint8_t *dataBuf, uint32_t bufSize)
VsGetStatistics
int VsGetStatistics(VideoStreamerHandle vs, char *dataBuf, uint32_t *bufSize)
Streamer
Definition: streamer.h:16
VsInitialize
int VsInitialize(VsLogCallback logCbk)
VsStatusCallback
void(* VsStatusCallback)(Streamer::Status status, void *opaque)
Definition: video_streamer_c_api.h:50
VsStart
int VsStart(VideoStreamerHandle vs)
VsDestroy
void VsDestroy(VideoStreamerHandle vs)
VsTelemetry::roll
DoubleValue roll
Definition: video_streamer_c_api.h:128
VsError::INVALID_PARAM
@ INVALID_PARAM
VsTelemetry::pitch
DoubleValue pitch
Definition: video_streamer_c_api.h:122
VsSetTailNumber
int VsSetTailNumber(VideoStreamerHandle vs, const char *tailNumber)
VsTelemetry
Definition: video_streamer_c_api.h:103
VsError::OTHER
@ OTHER
VsTelemetry::sensorRelativeAzimuth
DoubleValue sensorRelativeAzimuth
Definition: video_streamer_c_api.h:140
VsError::INVALID_OP
@ INVALID_OP
VsTelemetry::cameraFocalLength
DoubleValue cameraFocalLength
Definition: video_streamer_c_api.h:154
VsTelemetry::sensorRelativeRoll
DoubleValue sensorRelativeRoll
Definition: video_streamer_c_api.h:150
VsParams::transcodeFramerateLimit
float transcodeFramerateLimit
Definition: video_streamer_c_api.h:96
VsParams::tailNumber
const char * tailNumber
Definition: video_streamer_c_api.h:79
VsTelemetry::DoubleValue::isPresent
bool isPresent
Definition: video_streamer_c_api.h:108
VsStop
int VsStop(VideoStreamerHandle vs)
Streamer::Status
Status
Definition: streamer.h:44
VsParams::vehicleId
const char * vehicleId
Definition: video_streamer_c_api.h:77
VsParams::opaque
void * opaque
Definition: video_streamer_c_api.h:75
VsLogCallback
void(* VsLogCallback)(Log::Level level, const char *msg)
Definition: video_streamer_c_api.h:44
VsTelemetry::heading
DoubleValue heading
Definition: video_streamer_c_api.h:117
VsParams::targetUri
const char * targetUri
Definition: video_streamer_c_api.h:88
VsTelemetry::slantRange
DoubleValue slantRange
Definition: video_streamer_c_api.h:152
VsQueueOverflowCallback
void(* VsQueueOverflowCallback)(bool status, void *opaque)
Definition: video_streamer_c_api.h:55
VsCreate
VideoStreamerHandle VsCreate(const VsParams *params, VsStatusCallback statusCbk, VsQueueOverflowCallback queueOverflowCbk)
VsParams
Definition: video_streamer_c_api.h:73
VsGetLastError
VsError VsGetLastError(char *msgBuf, uint32_t *bufSize)
VsError
VsError
Definition: video_streamer_c_api.h:58
VsTelemetry::DoubleValue
Definition: video_streamer_c_api.h:105
VsError::INTERNAL_ERROR
@ INTERNAL_ERROR
VsTelemetry::latitude
DoubleValue latitude
Definition: video_streamer_c_api.h:130
VideoStreamerHandle
void * VideoStreamerHandle
Definition: video_streamer_c_api.h:39