FreeRDP
camera.h
1 
20 #ifndef FREERDP_CLIENT_CAMERA_H
21 #define FREERDP_CLIENT_CAMERA_H
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #if defined(WITH_INPUT_FORMAT_MJPG)
29 #include <libavcodec/avcodec.h>
30 #endif
31 
32 #include <libswscale/swscale.h>
33 #include <libavutil/imgutils.h>
34 
35 #include <winpr/wlog.h>
36 
37 #include <freerdp/api.h>
38 #include <freerdp/types.h>
39 
40 #include <freerdp/client/channels.h>
41 #include <freerdp/channels/log.h>
42 #include <freerdp/channels/rdpecam.h>
43 #include <freerdp/codecs.h>
44 #include <freerdp/primitives.h>
45 
46 #define ECAM_PROTO_VERSION 0x02
47 /* currently supporting 1 stream per device */
48 #define ECAM_DEVICE_MAX_STREAMS 1
49 #define ECAM_MAX_MEDIA_TYPE_DESCRIPTORS 256
50 
51 /* Allow to send up to that many unsolicited samples.
52  * For example, to support 30 fps with 250 ms round trip
53  * ECAM_MAX_SAMPLE_CREDITS has to be at least 8.
54  */
55 #define ECAM_MAX_SAMPLE_CREDITS 8
56 
57 /* Having this hardcoded allows to preallocate and reuse buffer
58  * for sample responses. Excessive size is to make sure any sample
59  * will fit in, even with highest resolution.
60  */
61 #define ECAM_SAMPLE_RESPONSE_BUFFER_SIZE (1024ULL * 4050ULL)
62 
63 typedef struct s_ICamHal ICamHal;
64 
65 typedef struct
66 {
67  IWTSPlugin iface;
68  IWTSListener* listener;
69  GENERIC_LISTENER_CALLBACK* hlistener;
70 
71  /* HAL interface */
72  ICamHal* ihal;
73  char* subsystem;
74 
75  BOOL initialized;
76  BOOL attached;
77 
78  UINT32 version;
79  wHashTable* devices;
80 
81 } CameraPlugin;
82 
83 typedef struct
84 {
85  CAM_MEDIA_FORMAT inputFormat; /* camera side */
86  CAM_MEDIA_FORMAT outputFormat; /* network side */
87 
89 
90 typedef struct
91 {
92  BOOL streaming;
93  CAM_MEDIA_FORMAT_INFO formats;
94  CAM_MEDIA_TYPE_DESCRIPTION currMediaType;
95 
96  GENERIC_CHANNEL_CALLBACK* hSampleReqChannel;
97  INT nSampleCredits;
98  wStream* sampleRespBuffer;
99 
100  H264_CONTEXT* h264;
101 
102 #if defined(WITH_INPUT_FORMAT_MJPG)
103  AVCodecContext* avContext;
104  AVPacket* avInputPkt;
105  AVFrame* avOutFrame;
106 #endif
107 
108  /* sws_scale */
109  struct SwsContext* sws;
110 
112 
113 static INLINE CAM_MEDIA_FORMAT streamInputFormat(CameraDeviceStream* stream)
114 {
115  return stream->formats.inputFormat;
116 }
117 static INLINE CAM_MEDIA_FORMAT streamOutputFormat(CameraDeviceStream* stream)
118 {
119  return stream->formats.outputFormat;
120 }
121 
122 typedef struct
123 {
124  IWTSListener* listener;
125  GENERIC_LISTENER_CALLBACK* hlistener;
126  CameraPlugin* ecam;
127  ICamHal* ihal; /* HAL interface, same as used by CameraPlugin */
128  char deviceId[32];
129  CameraDeviceStream streams[ECAM_DEVICE_MAX_STREAMS];
130 
131 } CameraDevice;
132 
137 typedef UINT (*ICamHalEnumCallback)(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
138  const char* deviceId, const char* deviceName);
139 
140 /* may run in context of different thread */
141 typedef UINT (*ICamHalSampleCapturedCallback)(CameraDevice* dev, int streamIndex,
142  const BYTE* sample, size_t size);
143 
144 struct s_ICamHal
145 {
146  UINT(*Enumerate)
147  (ICamHal* ihal, ICamHalEnumCallback callback, CameraPlugin* ecam,
148  GENERIC_CHANNEL_CALLBACK* hchannel);
149  INT16(*GetMediaTypeDescriptions)
150  (ICamHal* ihal, const char* deviceId, int streamIndex,
151  const CAM_MEDIA_FORMAT_INFO* supportedFormats, size_t nSupportedFormats,
152  CAM_MEDIA_TYPE_DESCRIPTION* mediaTypes, size_t* nMediaTypes);
153  UINT(*StartStream)
154  (ICamHal* ihal, CameraDevice* dev, int streamIndex, const CAM_MEDIA_TYPE_DESCRIPTION* mediaType,
155  ICamHalSampleCapturedCallback callback);
156  UINT (*StopStream)(ICamHal* ihal, const char* deviceId, int streamIndex);
157  UINT (*Free)(ICamHal* hal);
158 };
159 
160 typedef UINT (*PREGISTERCAMERAHAL)(IWTSPlugin* plugin, ICamHal* hal);
161 
162 typedef struct
163 {
164  IWTSPlugin* plugin;
165  PREGISTERCAMERAHAL pRegisterCameraHal;
166  CameraPlugin* ecam;
167  const ADDIN_ARGV* args;
168 
170 
172 
173 /* entry point called by addin manager */
174 typedef UINT(VCAPITYPE* PFREERDP_CAMERA_HAL_ENTRY)(PFREERDP_CAMERA_HAL_ENTRY_POINTS pEntryPoints);
175 
176 /* common functions */
177 UINT ecam_channel_send_generic_msg(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
178  CAM_MSG_ID msg);
179 UINT ecam_channel_send_error_response(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel,
180  CAM_ERROR_CODE code);
181 UINT ecam_channel_write(CameraPlugin* ecam, GENERIC_CHANNEL_CALLBACK* hchannel, CAM_MSG_ID msg,
182  wStream* out, BOOL freeStream);
183 
184 /* ecam device interface */
185 void ecam_dev_destroy(CameraDevice* dev);
186 
187 WINPR_ATTR_MALLOC(ecam_dev_destroy, 1)
188 CameraDevice* ecam_dev_create(CameraPlugin* ecam, const char* deviceId, const char* deviceName);
189 
190 /* video encoding interface */
191 BOOL ecam_encoder_context_init(CameraDeviceStream* stream);
192 BOOL ecam_encoder_context_free(CameraDeviceStream* stream);
193 BOOL ecam_encoder_compress(CameraDeviceStream* stream, const BYTE* srcData, size_t srcSize,
194  BYTE** ppDstData, size_t* pDstSize);
195 
196 #endif /* FREERDP_CLIENT_CAMERA_H */
Definition: camera.h:163