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

Arcturis / 2024-10-09 / 原文

关键点

  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/10000200@xyt.xswitch.cn:20003;transport=tcp";


//    static String dialString = "sip:1003@172.31.146.57: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();
        }
    }
}