首页 > 其他分享 >如何基于 XSwitch 拨通sip号码并播放声音

如何基于 XSwitch 拨通sip号码并播放声音

时间:2024-10-09 18:23:22浏览次数:1  
标签:拨通 sip uuid JSONObject XSwitch System rpc put new

关键点

  1. 部署Xswitch docker模式部署
  2. 开启Xcc 高级-->模块配置--> Xcc --> 启用Xcc模块,开启nats-url  nats://127.0.0.1:4222 启用,其他配置不用修改 使用默认的即可
  3. 部署NATS docker模式部署 (docker模式没有账号密码)
  4. 配置环境变量   有三个  NATS_URL:nats://你部署nats服务的IP:4222      XCTRL_SUBJECT:cn.xswitch.ctrl     XSWITCH_SUBJECT:cn.xswitch.node.test
  5. 了解RPC通信我这里用的java ,使用 Broadcast的 API,构造json数据
  6. 发送数据到NATS

贴一下 基本代码,后面再修改

package ivr;

import java.nio.charset.StandardCharsets;
import java.time.Duration;

import io.nats.client.Connection;
import io.nats.client.Options;
import io.nats.client.Message;
import io.nats.client.NUID;
import io.nats.client.Nats;
import io.nats.client.Subscription;

// import io.nats.client.*;

import java.util.Map;
import java.io.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Future;
import java.util.UUID;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

class CGlobals {
    public static UUID ctrl_uuid = UUID.randomUUID();
}

public class Call {
    static String service = System.getenv("XSWITCH_SUBJECT");
    static String nats_url = System.getenv("NATS_URL");
     static String dialString = "user/1006";
//    static String dialString = "sofia/public/[email protected]:20003;transport=tcp";


//    static String dialString = "sip:[email protected]:7060;transport=tcp";

    private static JSONObject make_rpc(String method) {
        JSONObject rpc = new JSONObject();
        rpc.put("jsonrpc", "2.0");
        rpc.put("id", UUID.randomUUID().toString());
        rpc.put("method", method);
        return rpc;
    }

    public static void main(String[] args) {
        System.out.println("Welcome to XCC!");
        JSONParser parser = new JSONParser();

        if (nats_url == null)
            nats_url = "nats://localhost:4222";
        if (service == null)
            service = "cn.xswitch.node";

        try {
            System.out.println("Connecting to: " + nats_url);
            Connection nc = Nats.connect(nats_url);
            System.out.println("Connected: " + nc.getConnectedUrl());

            // create dial request
            JSONObject rpc = make_rpc("XNode.Dial");
            JSONObject destination = new JSONObject();
            JSONObject global_params = new JSONObject();
            global_params.put("ignore_early_media", "true");
            destination.put("global_params", global_params);
            JSONObject call_param = new JSONObject();
            call_param.put("uuid", UUID.randomUUID().toString());
            call_param.put("dial_string", dialString);
            JSONArray call_params = new JSONArray();
            call_params.add(call_param);
            destination.put("call_params", call_params);
            JSONObject params = new JSONObject();
            params.put("ctrl_uuid", CGlobals.ctrl_uuid.toString());
            params.put("destination", destination);
            rpc.put("params", params);
            StringWriter request = new StringWriter();
            rpc.writeJSONString(request);
            System.out.println(request);

            Message msg = nc.request(service, request.toString().getBytes(StandardCharsets.UTF_8),
                    Duration.ofSeconds(20));
            System.out.println(new String(msg.getData(), StandardCharsets.UTF_8));

            JSONObject response = (JSONObject) parser.parse(new String(msg.getData(), StandardCharsets.UTF_8));
            System.out.println(response);

            JSONObject result = (JSONObject) response.get("result");
            System.out.println(result);

            String cause = (String) result.get("cause");
            System.out.println(cause);

            if (cause.equals("SUCCESS")) {
                System.out.println("Good ...");
                String uuid = (String) result.get("uuid");
                System.out.println("UUID : "+uuid);
                String node_uuid = (String) result.get("node_uuid");
                System.out.println(node_uuid);

                Thread.sleep(500); // wait for media setup

                service = "cn.xswitch.node." + node_uuid;
                // play tts
//                rpc = make_rpc("XNode.Play");
//                JSONObject p = new JSONObject();
//                p.put("uuid", uuid);
//                p.put("ctrl_uuid", CGlobals.ctrl_uuid.toString());

//                JSONObject media = new JSONObject();
//                media.put("data", "你好");
//                media.put("type", "TEXT");
//                media.put("engine", "ali");
//                media.put("voice", "default");
//                p.put("media", media);

//                JSONObject media = new JSONObject();
//                media.put("data", "C:\\Users\\Administrator\\Documents\\2024\\voice\\1.wav");
//                media.put("type", "FILE");
//                p.put("media", media);

                //play BroadCast
                rpc = make_rpc("XNode.Broadcast");
                JSONObject p = new JSONObject();
                p.put("uuid", uuid);
//                p.put("ctrl_uuid", CGlobals.ctrl_uuid.toString());
                JSONObject media = new JSONObject();

                media.put("data", "http://172.31.146.57/voice/1.wav");
//                media.put("data", "https://xswitch.cn/download/wav/xiaoyingtao.wav");
                media.put("type", "FILE");
                p.put("media", media);
                p.put("option", "ALEG");

                JSONObject apps = new JSONObject();

                apps.put("playback", "");
                apps.put("hangup", "NORMAL_CLEARING");

                rpc.put("params", p);
                request = new StringWriter();
                rpc.writeJSONString(request);
                System.out.println(request);
                Future<Message> incoming = nc.request(service,
                        request.toString().getBytes(StandardCharsets.UTF_8));
                msg = incoming.get(5000, TimeUnit.MILLISECONDS);
                String call_response = new String(msg.getData(), StandardCharsets.UTF_8);
                System.out.println("switch response 返回");
                System.out.println(call_response);

                Thread.sleep(6000);
                System.out.println("准备挂断");
                
                // hangup
//                rpc = make_rpc("XNode.Hangup");
//                p = new JSONObject();
//                p.put("uuid", uuid);
//                p.put("ctrl_uuid", CGlobals.ctrl_uuid.toString());
//                p.put("cause", "NORMAL_CLEARING");
//                rpc.put("params", p);
//                request = new StringWriter();
//                rpc.writeJSONString(request);
//                System.out.println(request);
//                incoming = nc.request(service,
//                        request.toString().getBytes(StandardCharsets.UTF_8));
//                msg = incoming.get(5000, TimeUnit.MILLISECONDS);
//                String hangup_response = new String(msg.getData(), StandardCharsets.UTF_8);
//                System.out.println(hangup_response);

                System.out.println("已挂断");
            }

            // Close the connection
            nc.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

标签:拨通,sip,uuid,JSONObject,XSwitch,System,rpc,put,new
From: https://www.cnblogs.com/fengfenghuifei/p/18454846

相关文章

  • 从零搭建Xswitch进行测试
    1xswitch官网拉取社区版xwitchdocker镜像,编译之,修改.env文件,把docker跑起来,这个是核心服务跑起来如下,端口映射不需要管,他内部做好的,默认sip使用7060前端ws连接端口 8081wss连接端口8082 2自己照着官网ES6demo例子写Vetro例子,我是用的vue搞的前端页面3编译vue......
  • XD6500S LoRa SIP模块芯片 集成了射频前端和LoRa射频收发器SX1262 动能世纪
    ​XD6500S是一系列LoRaSIP模块,集成了射频前端和LoRa射频收发器SX1262系列,支持LoRa和FSK调制。  收发器SX1262系列,支持LoRa和FSK调制。LoRa技术是一种扩频协议,针对LPWAN应用的低数据速率、超远距离和超低功耗通信进行了优化。通信进行了优化。  XD6500S的主动接收电流......
  • sipp模拟uas发送reinvite
    概述freeswitch是一款简单好用的VOIP开源软交换平台。在更新了sipp模拟update的配置方案之后,我希望对比一下fs对update和reinvite的处理流程。本文档记录sipp的配置方案,该方案中包含了update和reinvite的信令。环境CentOS7.9freeswitch1.10.7sipp.3.6.2方案描述测试......
  • GB28181和SIP网关优缺点分析
    GB28181和SIP网关之间的关系主要体现在协议转换和互通上。GB28181作为视频监控领域的国家标准协议,在信令层面采用了SIP协议作为其控制协议。然而,由于两者并不完全兼容,因此在需要将GB28181视频监控系统与其他基于SIP的通信系统进行融合时,就需要通过SIP网关来实现协议之间的转换和互......
  • sipp模拟uas发送update
     概述freeswitch是一款简单好用的VOIP开源软交换平台。但是fs在处理update消息时候有BUG,为了复现问题,使用sipp模拟uas,发送update并发送DTMF码。本文档记录sipp的配置方案。环境CentOS7.9freeswitch1.10.7sipp.3.6.2问题描述在与运营商对接的过程中,运营商内部会先返......
  • Gossip协议理解
    概述Gossip协议,又称epidemic协议,基于流行病传播方式的节点或进程之间信息交换的协议,在分布式系统中被广泛使用。在1987年8月由施乐-帕洛阿尔托研究中心发表ACM上的论文《EpidemicAlgorithmsforReplicatedDatabaseMaintenance》中被提出。原本用于分布式数据库中节点同步数......
  • XD6500S— LoRa SIP模块芯片 集成了射频前端和LoRa射频收发器SX1262 应用温湿度传感器
    XD6500S是一系列LoRaSIP模块,集成了射频前端和LoRa射频收发器SX1262系列,支持LoRa和FSK调制。收发器SX1262系列,支持LoRa和FSK调制。LoRa技术是一种扩频协议,针对LPWAN应用的低数据速率、超远距离和超低功耗通信进行了优化。通信进行了优化。XD6500S的主动接收电流消耗为4.2mA,电池寿......
  • 记最近一次紧张的sip客户端问题排查
    我司开发了一个sip软电话客户端(sip协议常用于网络音视频通讯),基于开源的linphonesdk,使用C#CPF框架开发,.netcore运行时。有windows版本和uos版本。windows版本之前说是要支持视频,CPF里面支持视频只找到一个办法,就是NativeElement控件使用winform的pictureBox来显示,只能用4.x版本的......
  • 使用 caesar sipher 加密和解密
    我需要符合所有要求的代码,以便我可以完成从某家公司获得推荐信的任务。我需要帮助。实际上我尝试过使用人工智能,但该公司正在使用人工智能探测器,如果它发现我在人工智能的帮助下完成了任务,那么我将不会获得任何证书,并且我将被该公司禁止10年。我需要人工编写的代码。......
  • Hisiphp2.0.11的文件上传
    侵权声明本文章中的所有内容(包括但不限于文字、图像和其他媒体)仅供教育和参考目的。如果在本文章中使用了任何受版权保护的材料,我们满怀敬意地承认该内容的版权归原作者所有。如果您是版权持有人,并且认为您的作品被侵犯,请通过以下方式与我们联系:[[email protected]]。我们将在确......