一个基于exosip的软电话例子

本文来自 wffy的VoIP世界

exosip针对UA是对osip进行扩展,oSIP不提供任何快速产生请求消息和响应消息的方 法,所有请求消息和响应消息的形成必须调用一组 sip message api来手动组装完成,所以作者在osip上基础上开发了exosip,用exosip开发软电话非常方便,仅需几个API就可以完成.exosip中附 带一个例子:josua,不过josua相对复杂了点,下面给出一个最简单的例子供大家参考,因为例子实在太简单所以没有给出注释,用到exosip的 API的参数请参看exosip源代码,看懂这个例子再研究josua就很简单了.我使用的是osip 2.0.9+exosip 0.77.

#include "assert.h"
#include <conio.h>
#include <iostream>
#include <osip2/osip_mt.h>
#include <eXosip/eXosip.h>
#include <eXosip/eXosip_cfg.h>

using namespace std;

class jcall;

class jcall {
public:
 int cid;
 int did;
 
 char reason_phrase[50];
 int  status_code;
 
 char textinfo[256];
 char req_uri[256];
 char local_uri[256];
 char remote_uri[256];
 char subject[256];
 
 char remote_sdp_audio_ip[50];
 int  remote_sdp_audio_port;
 int  payload;
 char payload_name[50];
 
 int state;
 
 jcall() {}
 
 int build(eXosip_event_t *je)
 {
  jcall *ca = this;
  
  ca->cid = je->cid;
  ca->did = je->did;
  
  if (ca->did<1 && ca->cid<1)
  {
   assert(0);
   return -1; /* not enough information for this event?? */
  }
  
  osip_strncpy(ca->textinfo,   je->textinfo, 255);
  osip_strncpy(ca->req_uri,    je->req_uri, 255);
  osip_strncpy(ca->local_uri,  je->local_uri, 255);
  osip_strncpy(ca->remote_uri, je->remote_uri, 255);
  osip_strncpy(ca->subject,    je->subject, 255);
  
  if (ca->remote_sdp_audio_ip[0]==''/0'')
  {
   osip_strncpy(ca->remote_sdp_audio_ip, je->remote_sdp_audio_ip, 49);
   ca->remote_sdp_audio_port = je->remote_sdp_audio_port;
   ca->payload = je->payload;
   osip_strncpy(ca->payload_name, je->payload_name, 49);
   
  }
  
  if (je->reason_phrase[0]!=''/0'')
  {
   osip_strncpy(ca->reason_phrase, je->reason_phrase, 49);
   ca->status_code = je->status_code;
  }
  
  ca->state = je->type;
  return 0;
 }
 
};


jcall call;

void __exit( int r )
{
 char line[256];
 gets( line );
 exit( r );
}

void josua_printf(char* buf)
{
 printf( "/n" );
}


int josua_event_get()
{
 int counter =0;
 /* use events to print some info */
 eXosip_event_t *je;
 for (;;)
 {
  char buf[100];
  je = eXosip_event_wait(0,50);
  if (je==NULL)
   break;
  counter++;
  if (je->type==EXOSIP_CALL_NEW)
  {
   printf( "<- (%i %i) INVITE from: %s",
    je->cid, je->did,
    je->remote_uri);
   josua_printf(buf);
   
   call.build(je);
  }
  else if (je->type==EXOSIP_CALL_ANSWERED)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_PROCEEDING)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_RINGING)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_REDIRECTED)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_REQUESTFAILURE)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_SERVERFAILURE)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_GLOBALFAILURE)
  {
   printf( "<- (%i %i) [%i %s] %s",
    je->cid, je->did,
    je->status_code,
    je->reason_phrase,
    je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_CLOSED)
  {
   printf( "<- (%i %i) BYE from: %s",
    je->cid, je->did, je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_HOLD)
  {
   printf( "<- (%i %i) INVITE (On Hold) from: %s",
    je->cid, je->did, je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_CALL_OFFHOLD)
  {
   printf( "<- (%i %i) INVITE (Off Hold) from: %s",
    je->cid, je->did, je->remote_uri);
   josua_printf(buf);
  }
  else if (je->type==EXOSIP_REGISTRATION_SUCCESS)
  {
   printf( "<- (%i) [%i %s] %s for REGISTER %s",
    je->rid,
    je->status_code,
    je->reason_phrase,
    je->remote_uri,
    je->req_uri);
   josua_printf(

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值