分析了DEMO中的核心函数的代码,并添加了注释。详情如下:
- void TrunkWork(int trunkID)
- {
- int VoiceID;
- int result;
- char FileName[200];
- result = DJNo7_GetChnState( trunkID/30,trunkID%30); //检查中继通道状态
- if( result == CH_WAIT_RELEASE ) //
- {
- ResetTrunk(trunkID); //复位中继通道
- DJNo7_SetChnState(trunkID/30,trunkID%30, CH_SET_FREE ); //wait app release resource
- return ; //process the next channel
- }
- switch(Trunk[trunkID].State)
- {
- case TRK_FREE:
- if(result==CALLEE_RECV_IAM) //呼叫到达 接收全地址
- {
- DJNo7_GetCalleeNum( trunkID/30,trunkID%30, Trunk[trunkID].CalleeCode); //获取被叫
- DJNo7_GetCallerNum( trunkID/30,trunkID%30, Trunk[trunkID].CallerCode); //获取主叫
- DJNo7_SetChnStateA( trunkID/30,trunkID%30, CALLEE_SET_ACM, ACM_ADDR_CHARGE | ACM_CALLEE_IDLE | ACM_ALL_NO7_PATH );//send ack signal
- DJNo7_SetChnStateA( trunkID/30,trunkID%30, CALLEE_SET_ANC, CSM_CHARGE );//send offhook signal
- DJTrk_InitDtmfBuf(trunkID); // new add by HJN
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.001");
- DJVoc_PlayFile(trunkID,FileName); //开始播放欢迎语音
- Trunk[trunkID].State = TRK_WELCOME;
- }
- break;
- case TRK_WELCOME:
- VoiceID = DJTrk_GetTrunkPlayID(trunkID); //取得给本通道放音的语音通道号
- if(VoiceID<0)
- {
- MessageBox(NULL,"Get Voice ID Error!","Error",MB_OK);
- ResetTrunk(trunkID);
- }
- else
- {
- //DJVoc_CheckVoiceEnd--检查该语音通道的语音操作是否结束,即录放音是否到达指定的大小
- //DJTrk_GetReciveDtmfNum--读取DTMF拨号的位数,用户按一次键该数将递增,如果在系统DTMF缓冲区中没有收到DTMF,本函数返回0
- if(DJVoc_CheckVoiceEnd(VoiceID)||(DJTrk_GetReciveDtmfNum(trunkID)>0))
- {
- DJVoc_StopPlayFile(trunkID); //停止放音
- strcpy(FileName,DJSys_GetInstallDir());
- strcat(FileName,"voc\\bank.002");
- DJVoc_PlayFile(trunkID,FileName);
- Trunk[trunkID].State=TRK_ACCOUNT;
- }
- }
- break;
- case TRK_ACCOUNT:
- VoiceID=DJTrk_GetTrunkPlayID(trunkID);
- if(VoiceID>=0)
- {
- if(DJVoc_CheckVoiceEnd(VoiceID))
- {
- DJVoc_StopPlayFile(trunkID);
- Trunk[trunkID].State = TRK_GETACCOUNT;
- }
- }
- if(DJTrk_GetReciveDtmfNum(trunkID)>=1)
- {
- if(VoiceID>0) DJVoc_StopPlayFile(trunkID);
- Trunk[trunkID].State = TRK_GETACCOUNT;
- }
- break;
- case TRK_GETACCOUNT:
- if(DJTrk_GetReciveDtmfNum(trunkID)>=8)
- {
- DJTrk_InitDtmfBuf(trunkID);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.003");
- DJVoc_PlayFile(trunkID,FileName);
- Trunk[trunkID].State = TRK_PASSWORD;
- }
- break;
- case TRK_PASSWORD:
- VoiceID=DJTrk_GetTrunkPlayID(trunkID);
- if(VoiceID>=0)
- {
- if(DJVoc_CheckVoiceEnd(VoiceID))
- {
- DJVoc_StopPlayFile(trunkID);
- Trunk[trunkID].State=TRK_GETPASSWORD;
- }
- }
- if(DJTrk_GetReciveDtmfNum(trunkID)>=1)
- {
- if(VoiceID>0) DJVoc_StopPlayFile(trunkID);
- Trunk[trunkID].State=TRK_GETPASSWORD;
- }
- break;
- case TRK_GETPASSWORD:
- if(DJTrk_GetReciveDtmfNum(trunkID)>=6)
- {
- DJTrk_InitDtmfBuf(trunkID);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.004");
- DJVoc_PlayFile(trunkID,FileName);
- Trunk[trunkID].State = TRK_SELECT;
- }
- break;
- case TRK_SELECT:
- VoiceID=DJTrk_GetTrunkPlayID(trunkID);
- if(VoiceID>=0)
- {
- if(DJVoc_CheckVoiceEnd(VoiceID))
- {
- DJVoc_StopPlayFile(trunkID);
- }
- VoiceID=-1;
- }
- if(DJTrk_GetReciveDtmfNum(trunkID)>0)
- {
- char Code = DJTrk_GetLastDtmfCode(trunkID);
- DJTrk_InitDtmfBuf(trunkID);
- switch(Code)
- {
- case '1':
- if(VoiceID>0) DJVoc_StopPlayFile(trunkID);
- DJVoc_InitIndexPlayFile(trunkID); //对某一个中继通道初始化文件索引放音
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.005");
- DJVoc_AddIndexPlayFile(trunkID,FileName); //增加索引放音文件
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d5");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d8");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d9");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d6");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d15");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d8");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.008");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- DJVoc_StartIndexPlayFile(trunkID); //开始一次文件索引放音
- Trunk[trunkID].State = TRK_PLAYRESULT;
- break;
- case '2':
- if(VoiceID>0) DJVoc_StopPlayFile(trunkID);
- DJVoc_InitIndexPlayFile(trunkID);
- strcpy(FileName,DJSys_GetInstallDir());
- strcat(FileName,"voc\\bank.006");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d0");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d15");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d4");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d8");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.008");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- DJVoc_StartIndexPlayFile(trunkID);
- Trunk[trunkID].State = TRK_PLAYRESULT;
- break;
- case '3':
- if(VoiceID>0) DJVoc_StopPlayFile(trunkID);
- DJVoc_InitIndexPlayFile(trunkID);
- strcpy(FileName,DJSys_GetInstallDir());
- strcat(FileName,"voc\\bank.007");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d3");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d7");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d0");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d0");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\d0");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- strcpy ( FileName, DJSys_GetInstallDir () );
- strcat ( FileName, "voc\\bank.008");
- DJVoc_AddIndexPlayFile(trunkID,FileName);
- DJVoc_StartIndexPlayFile(trunkID);
- Trunk[trunkID].State = TRK_PLAYRESULT;
- break;
- default:
- break;
- }
- }
- break;
- case TRK_PLAYRESULT:
- if(DJVoc_CheckIndexPlayEnd(trunkID))
- {
- DJVoc_StopIndexPlayFile(trunkID);
- Trunk[trunkID].State = TRK_HANGUP;
- }
- break;
- case TRK_RECORDFILE:
- VoiceID = DJTrk_GetTrunkPlayID(trunkID);
- if(DJVoc_CheckVoiceEnd(VoiceID))
- {
- DJVoc_StopRecordFile(trunkID);
- Trunk[trunkID].State = TRK_HANGUP;
- break;
- }
- if(DJTrk_GetReciveDtmfNum(trunkID) >= 1)
- {
- if(DJTrk_GetLastDtmfCode(trunkID) == '5')
- {
- DJVoc_StopRecordFile(trunkID);
- Trunk[trunkID].State = TRK_HANGUP;
- }
- }
- break;
- case TRK_PLAYRECORD:
- VoiceID=DJTrk_GetTrunkPlayID(trunkID);
- if(VoiceID<0)
- {
- MessageBox(NULL,"Get Voice ID Error!","Error",MB_OK);
- Trunk[trunkID].State = TRK_HANGUP;
- }
- else
- {
- if(DJVoc_CheckVoiceEnd(VoiceID))
- {
- DJVoc_StopPlayFile(trunkID);
- Trunk[trunkID].State = TRK_HANGUP;
- }
- }
- break;
- case TRK_HANGUP:
- DJNo7_SetChnState(trunkID/30,trunkID%30, CALLEE_SET_CBK );
- Trunk[trunkID].State = TRK_WAITFREE;
- break;
- case TRK_WAITFREE:
- break;
- }//end switch
- }