EchoLib  0.14.0
EchoLinkQso.h
Go to the documentation of this file.
1 
40 #ifndef ECHOLINK_QSO_INCLUDED
41 #define ECHOLINK_QSO_INCLUDED
42 
43 
44 /****************************************************************************
45  *
46  * System Includes
47  *
48  ****************************************************************************/
49 
50 #include <sys/time.h>
51 #include <sigc++/sigc++.h>
52 #include <stdint.h>
53 #include <string>
54 #ifdef SPEEX_MAJOR
55 #include <speex/speex.h>
56 #endif
57 
58 
59 /****************************************************************************
60  *
61  * Project Includes
62  *
63  ****************************************************************************/
64 
65 extern "C" {
66 #include <gsm.h>
67 }
68 #include <AsyncTimer.h>
69 #include <AsyncIpAddress.h>
70 #include <AsyncAudioSink.h>
71 #include <AsyncAudioSource.h>
72 
73 
74 /****************************************************************************
75  *
76  * Local Includes
77  *
78  ****************************************************************************/
79 
80 
81 
82 /****************************************************************************
83  *
84  * Forward declarations
85  *
86  ****************************************************************************/
87 
88 
89 
90 /****************************************************************************
91  *
92  * Namespace
93  *
94  ****************************************************************************/
95 
96 namespace EchoLink
97 {
98 
99 /****************************************************************************
100  *
101  * Forward declarations inside the declared namespace
102  *
103  ****************************************************************************/
104 
105 
106 
107 /****************************************************************************
108  *
109  * Defines & typedefs
110  *
111  ****************************************************************************/
112 
113 
114 
115 /****************************************************************************
116  *
117  * Exported Global Variables
118  *
119  ****************************************************************************/
120 
121 
122 
123 /****************************************************************************
124  *
125  * Class definitions
126  *
127  ****************************************************************************/
128 
148 class Qso
149  : public SigC::Object, public Async::AudioSink, public Async::AudioSource
150 {
151  public:
152  struct VoicePacket
153  {
154  struct {
155  uint8_t version;
156  uint8_t pt;
157  uint16_t seqNum;
158  uint32_t time;
159  uint32_t ssrc;
160  } header;
161  uint8_t data[1024];
162  } __attribute__ ((packed));
163 
164  struct RawPacket
165  {
167  int length;
168  short *samples;
169  };
170 
174  typedef enum
175  {
180  } State;
181 
189  Qso(const Async::IpAddress& ip, const std::string& callsign="",
190  const std::string& name="", const std::string& info="");
191 
195  ~Qso(void);
196 
205  bool initOk(void) { return init_ok; }
206 
212  bool setLocalCallsign(const std::string& callsign);
213 
218  const std::string& localCallsign(void) const { return callsign; }
219 
225  bool setLocalName(const std::string& name);
226 
231  const std::string& localName(void) const { return name; }
232 
238  void setLocalInfo(const std::string& info);
239 
244  const std::string& localInfo(void) const { return local_stn_info; }
245 
258  bool connect(void);
259 
277  bool accept(void);
278 
284  bool disconnect(void);
285 
291  bool sendInfoData(const std::string& info="");
292 
298  bool sendChatData(const std::string& msg);
299 
304  const Async::IpAddress& remoteIp(void) const
305  {
306  return remote_ip;
307  }
308 
319  bool sendAudioRaw(RawPacket *raw_packet);
320 
325  void setRemoteParams(const std::string& priv);
326 
331  void setRemoteName(const std::string& name) { remote_name = name; }
332 
338  const std::string& remoteName(void) const { return remote_name; }
339 
344  void setRemoteCallsign(const std::string& call) { remote_call = call; }
345 
351  const std::string& remoteCallsign(void) const { return remote_call; }
352 
360  bool isRemoteInitiated(void) const { return is_remote_initiated; }
361 
367  bool receivingAudio(void) const { return receiving_audio; }
368 
373  State currentState(void) const { return state; }
374 
379  SigC::Signal1<void, const std::string&> infoMsgReceived;
380 
385  SigC::Signal1<void, const std::string&> chatMsgReceived;
386 
391  SigC::Signal1<void, State> stateChange;
392 
399  SigC::Signal1<void, bool> isReceiving;
400 
410  SigC::Signal1<void, RawPacket*> audioReceivedRaw;
411 
412 
430  virtual int writeSamples(const float *samples, int count);
431 
440  virtual void flushSamples(void);
441 
449  virtual void resumeOutput(void);
450 
451 
452  protected:
461  virtual void allSamplesFlushed(void);
462 
463 
464  private:
465  static const int KEEP_ALIVE_TIME = 10000;
466  static const int MAX_CONNECT_RETRY_CNT = 5;
467  static const int CON_TIMEOUT_TIME = 50000;
468  static const int RX_INDICATOR_HANG_TIME = 200;
469  static const int FRAME_COUNT = 4;
470  static const int BUFFER_SIZE = FRAME_COUNT*160; // 20ms/frame
471 
472  typedef enum
473  {
474  CODEC_NONE,
475  CODEC_GSM,
476  CODEC_SPEEX
477  } Codec;
478 
479  bool init_ok;
480  unsigned char sdes_packet[1500];
481  int sdes_length;
482  State state;
483  gsm gsmh;
484 #ifdef SPEEX_MAJOR
485  SpeexBits enc_bits;
486  SpeexBits dec_bits;
487  void * enc_state;
488  void * dec_state;
489 #endif
490  uint16_t next_audio_seq;
491  Async::Timer * keep_alive_timer;
492  int connect_retry_cnt;
493  Async::Timer * con_timeout_timer;
494  std::string callsign;
495  std::string name;
496  std::string local_stn_info;
497  short receive_buffer[BUFFER_SIZE];
498  short send_buffer[BUFFER_SIZE];
499  int send_buffer_cnt;
500  Async::IpAddress remote_ip;
501  Async::Timer * rx_indicator_timer;
502  struct timeval last_audio_packet_received;
503  std::string remote_name;
504  std::string remote_call;
505  Codec remote_codec;
506  bool is_remote_initiated;
507  bool receiving_audio;
508 
509  Qso(const Qso&);
510  Qso& operator=(const Qso&);
511  void printData(const unsigned char *buf, int len);
512  void handleCtrlInput(unsigned char *buf, int len);
513  inline void handleByePacket(unsigned char *buf, int len);
514  inline void handleSdesPacket(unsigned char *buf, int len);
515  void handleAudioInput(unsigned char *buf, int len);
516  inline void handleNonAudioPacket(unsigned char *buf, int len);
517  inline void handleAudioPacket(unsigned char *buf, int len);
518  void micAudioRead(void *buf, size_t len);
519  bool sendSdesPacket(void);
520  void sendKeepAlive(Async::Timer *timer);
521  void setState(State state);
522  void connectionTimeout(Async::Timer *timer);
523  bool setupConnection(void);
524  void cleanupConnection(void);
525  bool sendVoicePacket(void);
526  void checkRxActivity(Async::Timer *timer);
527  bool sendByePacket(void);
528 
529 
530 }; /* class Qso */
531 
532 
533 } /* namespace */
534 
535 #endif /* ECHOLINK_QSO_INCLUDED */
536 
537 
538 
539 /*
540  * This file has not been truncated
541  */
542