EchoLib  0.14.0
EchoLinkQso.h
Go to the documentation of this file.
00001 
00040 #ifndef ECHOLINK_QSO_INCLUDED
00041 #define ECHOLINK_QSO_INCLUDED
00042 
00043 
00044 /****************************************************************************
00045  *
00046  * System Includes
00047  *
00048  ****************************************************************************/
00049 
00050 #include <sys/time.h>
00051 #include <sigc++/sigc++.h>
00052 #include <stdint.h>
00053 #include <string>
00054 #ifdef SPEEX_MAJOR
00055 #include <speex/speex.h>
00056 #endif
00057 
00058 
00059 /****************************************************************************
00060  *
00061  * Project Includes
00062  *
00063  ****************************************************************************/
00064 
00065 extern "C" {
00066 #include <gsm.h>
00067 }
00068 #include <AsyncTimer.h>
00069 #include <AsyncIpAddress.h>
00070 #include <AsyncAudioSink.h>
00071 #include <AsyncAudioSource.h>
00072 
00073 
00074 /****************************************************************************
00075  *
00076  * Local Includes
00077  *
00078  ****************************************************************************/
00079 
00080 
00081 
00082 /****************************************************************************
00083  *
00084  * Forward declarations
00085  *
00086  ****************************************************************************/
00087 
00088 
00089 
00090 /****************************************************************************
00091  *
00092  * Namespace
00093  *
00094  ****************************************************************************/
00095 
00096 namespace EchoLink
00097 {
00098 
00099 /****************************************************************************
00100  *
00101  * Forward declarations inside the declared namespace
00102  *
00103  ****************************************************************************/
00104 
00105 
00106 
00107 /****************************************************************************
00108  *
00109  * Defines & typedefs
00110  *
00111  ****************************************************************************/
00112 
00113 
00114 
00115 /****************************************************************************
00116  *
00117  * Exported Global Variables
00118  *
00119  ****************************************************************************/
00120 
00121 
00122 
00123 /****************************************************************************
00124  *
00125  * Class definitions
00126  *
00127  ****************************************************************************/
00128 
00148 class Qso
00149   : public SigC::Object, public Async::AudioSink, public Async::AudioSource
00150 {
00151   public:
00152     struct VoicePacket
00153     {
00154       struct {
00155         uint8_t version;
00156         uint8_t pt;
00157         uint16_t seqNum;
00158         uint32_t time;
00159         uint32_t ssrc;
00160       } header;
00161       uint8_t data[1024];
00162     } __attribute__ ((packed));
00163     
00164     struct RawPacket
00165     {
00166       VoicePacket *voice_packet;
00167       int length;
00168       short *samples;
00169     };
00170 
00174     typedef enum
00175     {
00176       STATE_DISCONNECTED, 
00177       STATE_CONNECTING,   
00178       STATE_BYE_RECEIVED, 
00179       STATE_CONNECTED     
00180     } State;
00181 
00189     Qso(const Async::IpAddress& ip, const std::string& callsign="",
00190         const std::string& name="", const std::string& info="");
00191     
00195     ~Qso(void);
00196   
00205     bool initOk(void) { return init_ok; }
00206     
00212     bool setLocalCallsign(const std::string& callsign);
00213     
00218     const std::string& localCallsign(void) const { return callsign; }
00219     
00225     bool setLocalName(const std::string& name);
00226 
00231     const std::string& localName(void) const { return name; }
00232     
00238     void setLocalInfo(const std::string& info);
00239         
00244     const std::string& localInfo(void) const { return local_stn_info; }
00245     
00258     bool connect(void);
00259     
00277     bool accept(void);
00278     
00284     bool disconnect(void);
00285     
00291     bool sendInfoData(const std::string& info="");
00292     
00298     bool sendChatData(const std::string& msg);
00299     
00304     const Async::IpAddress& remoteIp(void) const
00305     {
00306       return remote_ip;
00307     }
00308     
00319     bool sendAudioRaw(RawPacket *raw_packet);
00320 
00325     void setRemoteParams(const std::string& priv);
00326     
00331     void setRemoteName(const std::string& name) { remote_name = name; }
00332     
00338     const std::string& remoteName(void) const { return remote_name; }
00339     
00344     void setRemoteCallsign(const std::string& call) { remote_call = call; }
00345     
00351     const std::string& remoteCallsign(void) const { return remote_call; }
00352     
00360     bool isRemoteInitiated(void) const { return is_remote_initiated; }
00361     
00367     bool receivingAudio(void) const { return receiving_audio; }
00368 
00373     State currentState(void) const { return state; }
00374     
00379     SigC::Signal1<void, const std::string&> infoMsgReceived;
00380     
00385     SigC::Signal1<void, const std::string&> chatMsgReceived;
00386     
00391     SigC::Signal1<void, State> stateChange;
00392     
00399     SigC::Signal1<void, bool> isReceiving;
00400     
00410     SigC::Signal1<void, RawPacket*>  audioReceivedRaw;
00411     
00412 
00430     virtual int writeSamples(const float *samples, int count);
00431     
00440     virtual void flushSamples(void);
00441 
00449     virtual void resumeOutput(void);
00450     
00451 
00452   protected:
00461     virtual void allSamplesFlushed(void);
00462 
00463     
00464   private:
00465     static const int    KEEP_ALIVE_TIME         = 10000;
00466     static const int    MAX_CONNECT_RETRY_CNT   = 5;
00467     static const int    CON_TIMEOUT_TIME        = 50000;
00468     static const int    RX_INDICATOR_HANG_TIME  = 200;
00469     static const int    FRAME_COUNT             = 4;
00470     static const int    BUFFER_SIZE             = FRAME_COUNT*160; // 20ms/frame
00471 
00472     typedef enum
00473     {
00474       CODEC_NONE,
00475       CODEC_GSM,
00476       CODEC_SPEEX
00477     } Codec;
00478 
00479     bool                init_ok;
00480     unsigned char       sdes_packet[1500];
00481     int                 sdes_length;
00482     State               state;
00483     gsm                 gsmh;
00484 #ifdef SPEEX_MAJOR
00485     SpeexBits           enc_bits;
00486     SpeexBits           dec_bits;
00487     void *              enc_state;
00488     void *              dec_state;
00489 #endif
00490     uint16_t            next_audio_seq;
00491     Async::Timer *      keep_alive_timer;
00492     int                 connect_retry_cnt;
00493     Async::Timer *      con_timeout_timer;
00494     std::string         callsign;
00495     std::string         name;
00496     std::string         local_stn_info;
00497     short               receive_buffer[BUFFER_SIZE];
00498     short               send_buffer[BUFFER_SIZE];
00499     int                 send_buffer_cnt;
00500     Async::IpAddress    remote_ip;
00501     Async::Timer *      rx_indicator_timer;
00502     struct timeval      last_audio_packet_received;
00503     std::string         remote_name;
00504     std::string         remote_call;
00505     Codec               remote_codec;
00506     bool                is_remote_initiated;
00507     bool                receiving_audio;
00508 
00509     Qso(const Qso&);
00510     Qso& operator=(const Qso&);
00511     void printData(const unsigned char *buf, int len);
00512     void handleCtrlInput(unsigned char *buf, int len);
00513     inline void handleByePacket(unsigned char *buf, int len);
00514     inline void handleSdesPacket(unsigned char *buf, int len);
00515     void handleAudioInput(unsigned char *buf, int len);
00516     inline void handleNonAudioPacket(unsigned char *buf, int len);
00517     inline void handleAudioPacket(unsigned char *buf, int len);
00518     void micAudioRead(void *buf, size_t len);
00519     bool sendSdesPacket(void);
00520     void sendKeepAlive(Async::Timer *timer);
00521     void setState(State state);
00522     void connectionTimeout(Async::Timer *timer);
00523     bool setupConnection(void);
00524     void cleanupConnection(void);
00525     bool sendVoicePacket(void);
00526     void checkRxActivity(Async::Timer *timer);
00527     bool sendByePacket(void);
00528 
00529     
00530 };  /* class Qso */
00531 
00532 
00533 } /* namespace */
00534 
00535 #endif /* ECHOLINK_QSO_INCLUDED */
00536 
00537 
00538 
00539 /*
00540  * This file has not been truncated
00541  */
00542