/* =========================================================================

DESCRIPTION
   Netease && Xunfei speech processing main program

Copyright (c) 2017 by Netease, Co,LTD.  All Rights Reserved.
============================================================================ */

/* =========================================================================

                             REVISION

when                       who why
--------                    ---------
------------------------------------------- 2017/06/28          wangzijiao
Created.
============================================================================ */

#ifndef __MSC_H__
#define __MSC_H__

#ifdef __cplusplus
extern "C" {
#endif /* C++ */
#define TEST_SERVER_CFG_FILE ("/mnt/UDISK/usedTest.ini")
//#define DEBUG_CODE_LINE()               (printf("++++++++++++++++++%s(%d)\n",
//__FUNCTION__, __LINE__))

#define USED_NETEASE_AI (1)   // netease MSC library
#define USED_NETEASE_DC (0)   // netease data collection library
#define USED_NETEASE_FMAE (0) // netease CAE library
#define USED_NETEASE_DUILITE (1)
#define USED_NETEASE_DUILITE_VAD (1)
#define USED_XUNFEI_CAE (0)
//#define ENABLE_YUNXIN

#if USED_NETEASE_FMAE
#include <fmae/fmae.h>
#else
#include "cae.h"
#include "cae_lib.h"
#include "cae_thread.h"
#include <cae/cae_errors.h>
#include <cae/cae_intf.h>
#endif

struct IAT_HD;

typedef enum {
    IAT_STATUS_INIT,
    IAT_STATUS_AUDIOIN,
    IAT_STATUS_GETRESULT,
    IAT_STATUS_FINISH
} IAT_STATUS;

typedef int (*IATSessionCreate)(struct IAT_HD *hd, char *recType,
                                char *confirmParam, char *playerStatus);
typedef int (*IATSessionDestory)(struct IAT_HD *hd);

typedef int (*AudioWrite)(const char *sessionID, const void *waveData,
                          unsigned int waveLen, int audioStatus, int *epStatus,
                          int *recogStatus);

typedef const char *(*GetResult)(const char *sessionID, int *rsltStatus,
                                 int waitTime, int *errorCode);

struct IAT_HD {
    IAT_STATUS status;
    pthread_t tid_result;
    NATIVE_EVENT_HANDLE event;
    unsigned char running;

    char *sessionId;
    unsigned long long time_usec;
    struct Buffer *buffer;
    pthread_mutex_t mutex_lock;
    int time_create_session;
    int time_audioin_end;
    int time_recog_end;
    IATSessionCreate sessionCreate;
    IATSessionDestory sessionDestory;
    int angle;
    AudioWrite audioWrite;
    GetResult getResult;
};

/* ------------------------------------------------------------------------
** Types
** ------------------------------------------------------------------------ */
void Netease_IAT_DeInit();
int Netease_IAT_Init();
void Netease_IAT_write(const void *audiobuf, int audiolen);
int Netease_MSP_Init(CAEDATA **cdata);
int Netease_MSP_Deinit();
void Netease_IAT_checkflag(int angle);

// 1: fail, 0: success
int Netease_IAT_begin_session(char *recType, char *confirmParam,
                              char *playerStatus);
void Netease_IAT_end_session();

int netease_session_create(struct IAT_HD *hd, char *recType, char *confirmParam,
                           char *playerStatus);
int netease_session_destroy(struct IAT_HD *hd);
int netease_audio_write(const char *sessionID, const void *waveData,
                        unsigned int waveLen, int audioStatus, int *epStatus,
                        int *recogStatus);
const char *netease_get_result(const char *sessionID, int *rsltStatus,
                               int waitTime, int *errorCode);

#if USED_NETEASE_DC
#define WAKE_CACHE_DATA_SIZE (1920000U) //((16 * 1000 * 12 * 4) * 2.5)
unsigned int netease_dc_status(void);
void netease_dc_clean_status(void);
unsigned char* getDCCachebuf(void);
void BufRdLock(void);
void BufRdunLock(void);
#endif 

#ifdef __cplusplus
} /* extern "C" */
#endif /* C++ */

#endif /* __RAECORD_H__ */
