zmf_ext.h 15.6 KB
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
#ifndef __ZMF_EXT_H__
#define __ZMF_EXT_H__
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif

/**
 * The pixel format supported by ZMF
 */
typedef enum {
    ZmfPixelFormatI420              = 1,
    ZmfPixelFormatIYUV              = 2,
    ZmfPixelFormatRGB24             = 3,
    ZmfPixelFormatABGR              = 4,
    ZmfPixelFormatARGB              = 5,
    ZmfPixelFormatARGB4444          = 6,
    ZmfPixelFormatRGB565            = 7,
    ZmfPixelFormatARGB1555          = 8,
    ZmfPixelFormatYUY2              = 9,
    ZmfPixelFormatYV12              = 10,
    ZmfPixelFormatUYVY              = 11,
    ZmfPixelFormatMJPG              = 12,
    ZmfPixelFormatNV21              = 13,
    ZmfPixelFormatNV12              = 14,
    ZmfPixelFormatBGRA              = 15
} ZmfPixelFormatType;

/**
 * zmf video file format:
 * [HEAD][LAYER]*
 * HEAD -> uint16_t<WIDHT>, uint16_t <HEIGHT>, uint8_t <FPS>
 * LAYER-> ZmfFileVideoLayer, [Fragment]*
 * Fragment -> uint16_t<LENGTH>, [DATA]
 *
 * - WIDHT, HEIGHT, LENGTH is big endian
 * - DATA is byte buffer
 */
typedef struct {
    /** the number of fragment in one layer */
    unsigned char numFragments;
    /** fragment level = fragPriority << 8 |  fragSvcLevel
     * 0x80 is last layer bit in one image frame, i.e. ZmfVideoCaptureEncoder.bLastFrag
     * 0x70 is layer index, i.e. ZmfVideoCaptureEncoder.simulcastIdx
     */
    unsigned char fragPriority;
    /** 0x40 is keyframe bit */
    unsigned char fragSvcLevel;
} ZmfFileVideoLayer;

/**
 * runtime state of external capture encoder
 */
typedef struct {
    /** [in] codec name */
    const char* plName;
    /** [in] encoded data length */
    unsigned bufLength : 16;
    /** [in] fragment level */
    unsigned fragLevel : 16;
    /** [in] whelther the current captured buffer is last fragment ine one layer */
    unsigned bLastFrag : 1;
    /** [in] whelther the current captured buffer is last layer in one frame*/
    unsigned bLastLayer: 1;
    /** [in] layer index */
    unsigned simulcastIdx : 6;
    /** [in] whelther the current layer is keyframe */
    unsigned bKeyFrame : 1;
    /** [out] set next output keyframe */ 
    unsigned nextKeyFrame : 1;
    /** [out] set new framerate [0-64] */
    unsigned newFrameRate : 6; 
    /** [out] notify percent loss [0-100] */
    unsigned packetLoss   : 16;
    /** [out] set new bitrate, kbps */
    unsigned newBitRate   : 16;
    /** [out] notify rtt, ms */
    unsigned rtt          : 16;
} ZmfVideoCaptureEncoder;

/** the callback to receive captured image
 * iImgAngle - iCamOrient equal to device rotate angle.
 * if encoder is NULL, the pixel format of buf must be ZmfPixelFormatI420
 *
 * @param[in] pUser   the user data registered by Zmf_VideoCaptureAddCallback
 * @param[in] captureId the id of captured image
 * @param[in] iFace   the capture Face
 * @param[in] iImgAngle the image rotated angle (CW)
 * @param[in] iCamOrient the camera fixed orient
 * @param[in] iWidth  the image width
 * @param[in] iHeight the image height
 * @param[in] buf     the image data buffer
 * @param[in] len     the image data length
 * @param[in,out] encoder capture encoder
 */
typedef void (*ZmfVideoCaptureCallback)(void* pUser, const char* captureId, int iType, 
                                        int iImgAngle, int iCamOrient, int* iWidth, int* iHeight,
                                        unsigned char *buf, ZmfVideoCaptureEncoder* encoder);

/**
 * The callback to receive video render data 
 *
 * @param[in] pUser         the user data registered by Zmf_AddVideoRenderCallback
 * @param[in] renderId      video render unique name
 * @param[in] sourceType    video render source type @see ZmfVideoSourceType
 * @param[in] iAngle        
 * @param[in] iMirror 
 * @param[in] iWidth
 * @param[in] iHeight
 * @param[in] buf           I420 render data
 *
 * @return                  if process render data should return > 0, other 0
 *
 * @remarks
 *  if buf == 0 or iWidth ==0 or iHeight == 0, means the render will close,
 *  so should call Zmf_OnVideoRenderRequestRemove.
 */
typedef int  (*ZmfVideoRenderCallback)(void* pUser, const char* renderId, int sourceType, int iAngle,
                                       int iMirror, int* iWidth, int* iHeight, unsigned char *buf,
                                       unsigned long timeStamp);

/** the callback to fill audio output buffer
 * @param[in] pUser  the user data registered by Zmf_AddAudioOutputCallback
 */
typedef int  (*ZmfAudioOutputCallback)(void* pUser, const char* outputId, int iSampleRateHz, int iChannels,
                                       unsigned char *buf, int len);

/** the callback to receive audio input data 
 *
 * @param[in] pUser  the user data registered by Zmf_AddAudioInputCallback
 * @param[in] inputId       unique name of the audio input
 * @param[in] sampleRateHz  the sample rating of the pcm data
 * @param[in] iChannels     the channel number of the pcm data
 * @param[in] buf           the pcm data
 * @param[in] len           the pcm data length
 * @param[in,out] micLevel  
 * @param[in] playDelayMS   the play delay ms
 * @param[in] recDelayMS    the record dely ms
 * @param[in] clockDrift    the clock drift ms
 */
typedef void (*ZmfAudioInputCallback)(void* pUser, const char* inputId, int iSampleRateHz, int iChannels,
                                      const unsigned char *buf, int len, int *micLevel,
                                      int playDelayMS, int recDelayMS, int clockDrift);
/**
 * The Event Callback
 *
 */
typedef void (*ZmfEventListenCallback) (int iEventType, const char *json, int len);


/**
 * The sensor data input data entry to ZMF
 *
 */
void Zmf_OnSensorData       (const ZmfSensorData *sensor);

/**
 * The audio input data entry to ZMF
 *
 * @param[in] inputId       unique name of the audio input
 * @param[in] sampleRateHz  the sample rating of the pcm data
 * @param[in] iChannels     the channel number of the pcm data
 * @param[in] buf           the pcm data
 * @param[in] len           the pcm data length
 * @param[in,out] micLevel  
 * @param[in] playDelayMS
 * @param[in] recDelayMS
 * @param[in] clockDrift
 *
 */
void Zmf_OnAudioInput       (const char *inputId, int sampleRateHz, int iChannels,  const unsigned char *buf, int len,
                             int *micLevel, int playDelayMS, int recDelayMS, int clockDrift);

/**
 * The outlet which audio output can get data from.
 *
 * @param[in] outputId      unique name of the audio output
 * @param[in] sampleRateHz  the sample rating of the pcm data
 * @param[in] iChannels     the channel number of the pcm data
 * @param[in] buf           the pcm data to be filled
 * @param[in] len           the pcm data length
 */
void Zmf_OnAudioOutput      (const char *outputId, int sampleRateHz, int iChannels, unsigned char *buf, int len);

/**
 * The video capture data entry to ZMF
 * iImgAngle - iCamOrient equal to device rotate angle.
 * if encoder is NULL, the pixel format of buf must be ZmfPixelFormatI420
 *
 * @param[in] captureId     unique name of the video capture
 * @param[in] iFace         the capture face, @see ZmfVideoFaceType
 * @param[in] iImgAngle     the image rotated angle (CW)
 * @param[in] iCamOrient    the camera fixed orient
 * @param[in,out] iWidth    the image width, expected width
 * @param[in,out] iHeight   the image height, expected height
 * @param[in] buf           the image data
 * @param[in,out] encoder   the capture encoder
 */
void Zmf_OnVideoCapture     (const char *captureId, int iFace, int iImgAngle, int iCamAngle,
                             int *iWidth, int *iHeight, unsigned char *bufI420,
                             ZmfVideoCaptureEncoder* encoder);


/**
 * The video render data entry to ZMF
 *
 * @param[in] renderId      unique name of the video render source
 * @param[in] sourceType    the render source type, @see ZmfVideoSourceType
 * @param[in] iAngle        the image rotated angle (CW)ZmfVideoCaptureCallback
 * @param[in] iWidth        the image width
 * @param[in] iHeight       the image height
 * @param[in] buf           the image data I420 buffer
 */
void Zmf_OnVideoRender      (const char *renderId, int sourceType, int iAngle, int iMirror,
                             int *iWidth, int *iHeight, unsigned char *bufI420, unsigned long timeStamp);

/**
 * add video capture data callback
 *
 * @param[in] pUser         the callback user data
 * @param[in] pfnCb         the callback
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_VideoCaptureAddCallback    (void *pUser, ZmfVideoCaptureCallback pfnCb);

/**
 * remove video capture data callback
 *
 * @param[in]  pUser        the callback user data
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_VideoCaptureRemoveCallback (void *pUser);

/**
 * add render data callback
 *
 * @param[in] pUser         the callback user data
 * @param[in] pfnCb         the callback
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_VideoRenderAddCallback     (void *pUser, ZmfVideoRenderCallback pfnCb);

/**
 * remove render data callback
 *
 * @param[in]  pUser        the callback user data
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_VideoRenderRemoveCallback  (void *pUser);

/**
 * remove speak data callback
 *
 * @param[in]  pUser        the callback user data
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_AudioOutputAddCallback     (void *pUser, ZmfAudioOutputCallback pfnCb);

/**
 * remove speak data callback
 *
 * @param[in]  pUser        the callback user data
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_AudioOutputRemoveCallback  (void *pUser);

/**
 * add mic data callback
 *
 * @param[in] pUser         the callback user data
 * @param[in] pfnCb         the callback
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_AudioInputAddCallback      (void *pUser, ZmfAudioInputCallback pfnCb);

/**
 * remove mic data callback
 *
 * @param[in]  pUser        the callback user data
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_AudioInputRemoveCallback   (void *pUser);

/**
 * utility for convert pixel from I420
 *
 * @param[in] dstFrame      the buffer to save the convert result
 * @param[in] dstFormat     the convert result pixel format
 * @param[in] srcFrame      the source I420 buffer
 * @param[in] srcWidth      the width of image
 * @param[in] srcHeight     the height of image
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_ConvertFromI420 (void* dstFrame, int dstFormat,
                          const void* srcFrame, int srcWidth, int srcHeight);

/**
 * utility for convert pixel to I420
 *
 * @param[in] dstFrame      the I420 buffer to save the convert result
 * @param[in] dstFormat     the convert result pixel format
 * @param[in] srcFrame      the source buffer
 * @param[in] srcWidth      the width of image
 * @param[in] srcHeight     the height of image
 * @param[in] rotateAngle   the clockwise rotation angle
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_ConvertToI420(void* dstframe,
                       int srcFormat, const void* srcFrame, unsigned srcBufLen,
                       int srcWidth, int srcHeight,int crop_x, int crop_y,
                       int *dstWidth, int *dstHeight, int rotateAngle);

/**
 * utility for scale i420
 *
 * @param[in] srcFrame      the source buffer
 * @param[in] srcWidth      the source width of image
 * @param[in] srcHeight     the source height of image
 * @param[in] dstFrame      the I420 buffer to save the convert result
 * @param[in] dstWidth      the target width of image
 * @param[in] dstHeight     the target height of image
 *
 * @return                  0 on succeed, otherwise failed.
 */
int  Zmf_ScaleI420(void* srcframe, int srcWidth, int srcHeight,
                   void* dstframe, int dstWidth, int dstHeight);

/**
 * trigger ZmfVideoRenderDidReceive event
 *
 * @param[in] renderId      unique name of the render
 * @param[in] hWnd          the window which the render has been added
 * @param[in] iWidth        the width of image
 * @param[in] iHeight       the height of image
 */
void Zmf_OnVideoRenderDidReceived  (const char *renderId, void* hWnd, int iWidth, int iHeight);

/**
 * trigger ZmfVideoRenderDidResize event
 *
 * @param[in] renderId      unique name of the render
 * @param[in] hWnd          the window which the render has been added
 * @param[in] iWidth        the new width of image
 * @param[in] iHeight       the new height of image
 */
void Zmf_OnVideoRenderDidResized   (const char *renderId, void* hWnd, int iWidth, int iHeight);

/**
 * trigger ZmfVideoRenderDidStart event
 *
 * @param[in] renderId      unique name of the render
 * @param[in] hWnd          the window which the render has been added
 */
void Zmf_OnVideoRenderDidStarted   (const char *renderId, void* hWnd, int iWidth, int iHeight);

/**
 * trigger ZmfVideoRenderRequestRemove event
 *
 * @param[in] renderId      unique name of the render
 * @param[in] hWnd          the window which the render has been added
 */
void Zmf_OnVideoRenderRequestRemove(const char *renderId, void* hWnd);

/**
 * trigger ZmfVideoRenderDidMatch event
 *
 * @param[in] renderId      unique name of the render
 * @param[in] hWnd          the window which the render has been added
 * @param[in] matching      the percent of mathcing
 */
void Zmf_OnVideoRenderDidMatch (const char *provideId, void* hWnd, int matching);

/**
 * tell ZMF the video capture has stopped
 * 
 * @param[in] captureId     unique name of the device
 */
void Zmf_OnVideoCaptureDidStop     (const char *captureId);

/**
 * tell ZMF the video capture exposure state changed
 *
 * @param[in] captureId     unique name of the video capture
 * @param[in] bStarted      bStarted 0 means the exposure is off , others on.
 */
void Zmf_OnVideoCaptureExposure   (const char *captureId, int bStarted);

/**
 * tell ZMF the audio output has stopped
 *
 * @param[in] outputId      unique name of the device
 */
void Zmf_OnAudioOutputDidStop      (const char *outputId);

/**
 * tell ZMF the audio input has stopped
 *
 * @param[in] inputId       unique name of the device
 */
void Zmf_OnAudioInputDidStop       (const char *inputId);

/**
 * Set Audio Event Callback
 */
int Zmf_AudioSetListener   (ZmfEventListenCallback pfnAudioListen);

/**
 * Set Video Event Callback
 */
int Zmf_VideoSetListener   (ZmfEventListenCallback pfnVideoListen);

typedef enum {
    ZmfLogDebug     = 0,
    ZmfLogInfo      = 1,
    ZmfLogWarn      = 2,
    ZmfLogError     = 3,
} ZmfLogLevel;

/**
 * Set  Log level
 */
void Zmf_LogSetLevel (int iLogLevel);
void Zmf_LogV(int iLogLevel, const char *format, va_list argv);

/** Font layout info */
typedef struct {
    unsigned        shadowRGBA;
    float           shadowBlurRadius;
    float           shadowOffsetX, shadowOffsetY;
    unsigned        outlineRGBA;
    float           outlineWidth;
    unsigned        fontRGBA;
    float           fontSize;
    unsigned        boundingWidth;
    float           scale;
    char            fontName[1024];
} ZmfFontLayout;

/** I420 Buffer */
typedef struct _ZmfI420Stencil {
    unsigned char   *bufI420;
    unsigned        width, height;
} ZmfI420Stencil;

/**
 * alloc I420 buffer
 *
 * @param[in] str   string
 * @param[in] info  layout info
 * @return I420 buffer
 */
ZmfI420Stencil* Zmf_I420StencilFromString(const char*str, ZmfFontLayout* info);

/**
 * delete I420 buffer
 */
void Zmf_I420StencilDelete(ZmfI420Stencil *stencil);

/**
 * blend I420
 *
 * @param[in] aligns Alignment @ref ZmfAlignmentType
 */
int Zmf_I420StencilBlend(const ZmfI420Stencil*stencil, unsigned dstX, unsigned dstY, unsigned char *dstI420, unsigned dstW, unsigned dstH);

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif