|
|
/*****************************************************************************
* OpenTel Telephony Server & Framework
* Copyright (c) 1995-97 OpenComm do Brasil Ltda.
* Todos os direitos reservados.
* Este arquivo contem material confidencial de OpenComm do Brasil, e deve
* darse tratamento como tal.
******************************************************************************/
#ident "@(#) $Header: /cvsroot/opentel/opentel/server/drivers/dialogic/otdialar.cpp,v 1.2 2000/03/24 05:16:43 jbernab Exp $"
#include <otconfig.h>
#ifdef _WINDOWS
#include <windows.h>
#include <io.h>
#endif
extern "C" {
#include <srllib.h>
#include <dxxxlib.h>
};
#ifdef __unix
#include <stdio.h>
#include <stdlib.h>
#endif
#include <fcntl.h>
#include <otcallc.includes>
#include <dialogic/otdialogic.h>
#include <dialogic/otdialdxx.h>
#include <otarchive.includes>
#include <otaudiorec.includes>
#include <dialogic/otdialar.h>
#ifdef __unix
extern "C" int open( const char *, int, ... );
#endif
OtDialogicAudioRecorder::OtDialogicAudioRecorder( OtDialogicDxx *dxx ) {
m_dxx = dxx;
if ( m_dxx )
m_dxx->addTarget( this );
setName( OtString("OtDialogicAudioRecorder(") + OtString(ATDV_NAMEP(m_dxx->getFd())) + OtString(")") );
}
OtDialogicAudioRecorder::~OtDialogicAudioRecorder() {
if ( m_dxx )
m_dxx->removeTarget( this );
}
OtTimeSlotListener *OtDialogicAudioRecorder::getListener()
{
return m_dxx->getListener();
}
OtTimeSlotTransmitter *OtDialogicAudioRecorder::getTransmitter()
{
return m_dxx->getTransmitter();
}
/* Message reception */
void OtDialogicAudioRecorder::onMessage( OtMessage *msg ) {
OtEvent *e = ((OtDlgMsg *)msg)->getEv();
if( e->getClass() == OtDialogicEvent::eventClass ) {
if ( e->getType() == TDX_RECORD ) {
/* The recording terminated */
/* Lets see why */
/* if the recording is over, we should close the file now */
close( m_iott.io_fhandle );
m_recording = false;
int evt = ATDX_TERMMSK( sr_getevtdev() );
if ( evt & TM_MAXTIME )
dispatchEvent( OtAudioRecorderEvent::eventMaxTime );
else {
// if ( evt & TM_USRSTOP )
dispatchEvent( OtAudioRecorderEvent::eventUserStop );
}
}
else if ( e->getType() == TDX_ERROR ) {
/// TODO: agregar eventError
dispatchEvent( OtAudioRecorderEvent::eventUserStop );
throw OtDialogicException(m_dxx->getFd(),"Event Error");
}
}
// OtMessageTarget::onMessage( msg );
}
/* Digital sound reproduction */
void OtDialogicAudioRecorder::rec( const OtString &filename, long time, long maxsil) {
if ( isRecording() )
throw OtException( "Error: Recorder already recording!" );
#ifdef __unix
if ( ( m_iott.io_fhandle = open( filename, O_RDWR | O_CREAT |O_TRUNC, 0666) ) < 0 )
#endif
#ifdef _WINDOWS
if ( ( m_iott.io_fhandle = _open( filename, _O_RDWR | _O_CREAT) ) < 0 )
#endif
// if ( ( m_iott.io_fhandle = open( filename, O_RDWR | O_CREAT) ) < 0 )
throw OtException( "Error: Could not open source file!" );
dx_clrtpt( m_tpt, 5 );
/* TODO: agregar razones para detener automaticamente */
int idx=0;
if(time) {
m_tpt[idx].tp_type = IO_CONT;
m_tpt[idx].tp_termno = DX_MAXTIME; /* Maximum time */
m_tpt[idx].tp_length = time * 10; /* terminate after time secs */
m_tpt[idx].tp_flags = TF_MAXTIME; /* default flags */
idx++;
}
m_tpt[idx].tp_type = IO_CONT; /* last entry in the table */
m_tpt[idx].tp_termno = DX_MAXDTMF; /* Maximum digits */
m_tpt[idx].tp_length = 1; /* terminate in the first digit */
m_tpt[idx].tp_flags = TF_MAXDTMF; /* default flags */
idx++;
m_tpt[idx].tp_type = IO_CONT; /* last entry in the table */
m_tpt[idx].tp_termno = DX_MAXSIL; /* Maximum silence */
m_tpt[idx].tp_length = maxsil * 10; /* terminate in maxsil seconds*/
m_tpt[idx].tp_flags = TF_MAXSIL|TF_SETINIT; /* default flags */
m_tpt[idx].tp_data = 20 + maxsil *10; /* 2 Sec + maxsil of initial silence*/
idx++;
if(idx)
m_tpt[idx-1].tp_type = IO_EOT;
m_iott.io_type = IO_EOT | IO_DEV;
m_iott.io_offset = ( unsigned long )0;
m_iott.io_length = -1;
if ( dx_rec( m_dxx->getFd(), &m_iott, m_tpt, EV_ASYNC|MD_ADPCM|RM_ALAW|RM_SR8 ) < 0 )
throw OtDialogicException( m_dxx->getFd(), "Error: dx_rec returned error" );
m_recording = true;
}
/* Stop playback */
void OtDialogicAudioRecorder::stop() {
if ( dx_stopch( m_dxx->getFd(), EV_SYNC ) < 0 )
throw OtDialogicException( m_dxx->getFd(), "Error: dx_stop failed" );
}
void OtDialogicAudioRecorder::dispatchEvent( OtEventType t ) {
OtAudioRecorderEvent ev( *this , t );
postEvent( ev );
}
|