📄 simpleextdevice.cxx
字号:
/* ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */static const char* const SimpleExtDevice_cxx_Version = "$Id: SimpleExtDevice.cxx,v 1.3 2002/03/19 21:40:16 jason Exp $";#include "global.h"#include <cassert>/* stdlib */#include <iostream>#include <cstdio>#include <unistd.h>#include <sys/ioctl.h>#include <csignal>#include <sys/time.h>/* sockets */#include <sys/types.h>#include <sys/socket.h>#include <VTime.hxx>#include <fcntl.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>/* error handling */#include <cerrno>#include "SimpleExtDevice.hxx"#include "UaDevice.hxx"#include "UaConfiguration.hxx"#include "DeviceEvent.hxx"#include "cpLog.h"#include "DTMFInterface.hxx"#include "RtpEvent.hxx"#include "VCondition.h" using namespace Vocal;static const int ULAW_PAYLOAD = 1012;static const int ULAW_SAMPLE_RATE = 240;static const int RESID_RTP_RATE = 240;static const int NETWORK_RTP_RATE = 160;//***************************************************************************// SimpleExtDevice::SimpleExtDevice// description: Constructor. Initialize the hookstate.// Resets the device to get it ready.//***************************************************************************SimpleExtDevice::SimpleExtDevice(char* hostName, int port, Sptr < Fifo < Sptr < SipProxyEvent > > > inputQ, Sptr < Fifo < Sptr < SipProxyEvent > > > outputQ) : ResGwDevice( hostName, inputQ, outputQ ), hasPlayed(false){ // initialize queues sessionQ = outputQ; myQ = inputQ; myHostName = hostName; myPortNumber = port; fwdFlag = false; hookStateOffhook = false; serverAvailable = false; prevRtpPacketTime.tv_usec = 0; audioStack = 0;} // end SimpleExtDevice::SimpleExtDevice()//***************************************************************************// SimpleExtDevice::~SimpleExtDevice// description: Destructor//***************************************************************************SimpleExtDevice::~SimpleExtDevice(void){// vm.sendClose();// close(ss); } // end SimpleExtDevice::~SimpleExtDevice()//***************************************************************************// SimpleExtDevice::hardwareMain// description: main processing loop of the hardware//***************************************************************************void*SimpleExtDevice::hardwareMain (void* parms){ // process forever on behalf of SimpleExtDevice hardware fd_set readfds; struct timeval tv; int retval; int maxFd = 128; // reset file descriptor FD_ZERO(&readfds); addToFdSet(&readfds); // block on select for asyncronous events, but poll to process // audio and signal requests from endpoints in message queue tv.tv_sec = 0; tv.tv_usec = 20000; if ((retval = select(maxFd, &readfds, 0, 0, &tv)) < 0) { cpLog(LOG_ERR, "select() returned with an error" ); } else { if (process(&readfds) < 0) { cpLog(LOG_ERR, "hardware encountered an error"); assert(0); } } // check for signal requests from the session if (myQ->size() > 0) { processSessionMsg(myQ->getNext()); } return 0;} // end SimpleExtDevice::hardwareMain()//***************************************************************************// SimpleExtDevice::process//// description: process any events from vmserver of type VMCP. // Like play the file, start recording, stop recording etc. //***************************************************************************intSimpleExtDevice::process (fd_set* fd){ deviceMutex.lock(); if ( (hookStateOffhook == true) && (ss > 0 )) { if (FD_ISSET(ss, fd)) { int Msg;// Msg=vm.getMsg(); if ( Msg < 1 ) { reportEvent(sessionQ, DeviceEventHookDown); hookStateOffhook = false; deviceMutex.unlock(); return 0; }#if 0 switch (Msg) {// case Vmcp::Close: { cpLog(LOG_DEBUG,"VMCP:Close"); reportEvent(sessionQ, DeviceEventHookDown); hookStateOffhook = false; vm.sendClose(); close(ss); deviceMutex.unlock(); return 0; } case Vmcp::PlayFile: cpLog(LOG_DEBUG,"VMCP:Playing file %s",vm.getPlayFileName().c_str()); player.add(vm.getPlayFileName()); break; case Vmcp::StartPlay: cpLog(LOG_DEBUG,"VMCP:Start player"); if( !player.start() ) { vm.playStopped(); } break; case Vmcp::RecordFile: cpLog(LOG_DEBUG,"VMCP:RecordFile"); recorder.open(vm.getRecordFileName()); break; case Vmcp::StartRecord: cpLog(LOG_DEBUG,"VMCP:StartRecord"); recorder.start(); break; case Vmcp::StopRecord: cpLog(LOG_DEBUG,"VMCP:StopRecord"); recorder.close(); break; case Vmcp::StopPlay: cpLog(LOG_DEBUG,"VMCP:StopPlayer"); player.stop(); break; default: break; }#endif } } deviceMutex.unlock(); return 0;} // end SimpleExtDevice::process()//***************************************************************************// SimpleExtDevice::processRTP// description: main processing loop for RTP//***************************************************************************voidSimpleExtDevice::processRTP (){ if ( !audioActive ) { vusleep(30000); return ; } if ( !hookStateOffhook ) { vusleep(30000); return ; }// int wait = networkPktSize - (vm_gettimeofday() - nextTime);// if ( wait > 0 ) vusleep(wait*1000);// nextTime += networkPktSize; deviceMutex.lock(); if (audioStack) { inRtpPkt = audioStack->receive (); } if (inRtpPkt) { packetCount++; recorder.write(inRtpPkt->getPayloadLoc(), inRtpPkt->getPayloadUsage()); delete inRtpPkt; inRtpPkt = 0; } if (audioStack) { char buffer[10240]; memset(buffer, 0xFE, networkPktSize*8); if ( !player.getData(buffer, networkPktSize*8) ) {// vm.playStopped(); } audioStack->transmitRaw (buffer, networkPktSize*8); } if (audioStack) { audioStack->processRTCP(); } deviceMutex.unlock();}intSimpleExtDevice::addToFdSet (fd_set* fd){ if( hookStateOffhook && (ss > 0 )) { // set the VM controller to active FD_SET(ss,fd); if ( audioActive ) { RtpSessionState sessionState = audioStack->getSessionState(); if ( sessionState == rtp_session_recvonly || sessionState == rtp_session_sendrecv ) { FD_SET((audioStack->getRtpRecv())->getSocketFD(), fd); FD_SET((audioStack->getRtcpRecv())->getSocketFD(), fd); } } } return 0;} // end SimpleExtDevice::addToFdSet()//***************************************************************************// SimpleExtDevice::getRtpPort()//// description: creates a new rtp session and reserves a rtp port//***************************************************************************intSimpleExtDevice::getRtpPort(){ //this is an arbitrarily defined number const int MAX_RTP_PORT = UaConfiguration::instance()->getMaxRtpPort(); const int MIN_RTP_PORT = UaConfiguration::instance()->getMinRtpPort(); int port = 0; int minPort = MIN_RTP_PORT; deviceMutex.lock(); // create a rtp session if there is no existing session alread // this Rtp session will be idle if ( audioStack == 0 ) { audioStack = new RtpSession(0); } while ( 1 ) { // resever a rtp port port = audioStack->reserveRtpPort(minPort, MAX_RTP_PORT); minPort = (port > minPort) ? port : minPort; // attempt to reserve a rtcp port on port number higher than // the rtp port if ( port != 0 && audioStack->reserveRtcpPort(port + 1, 0) != 0 ) { break; } // if not successful allocating rtcp port, increment the minimum rtp // port and try again. If minPort reaches MAX_RTP_PORT, return 0 // to indicate port allocation failed. minPort += 2; if ( minPort > MAX_RTP_PORT ) { port = 0; break; } } deviceMutex.unlock(); return port;}//***************************************************************************// SimpleExtDevice::releaseRtpPort()//// description: destroy the rtp session and release the rtp port//***************************************************************************voidSimpleExtDevice::releaseRtpPort(){ deviceMutex.lock(); // destroy the rtp session if one exists if ( audioStack != 0 ) { audioStack->unsetDTMFInterface( _DTMFInterface ); int port = audioStack->releaseRtpPort();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -