Janus 的videoroom插件客户端API梳理

456 阅读2分钟

VideoRoom 插件支持很多API。这些API中,一些是同步请求,一些则是异步请求。但无论是同步还是异步请求,当遇到无效的JSON格式或无效的请求时,都使用同步进行错误响应。

接下来,我们首先看看都有那些同步请求API。createdestroyeditexistslistallowedkicklistparticipants是同步请求API。create允许您动态创建一个新的音视频房间;edit允许您动态编辑房间的属性(例如 修改PIN码);destroy首先释放视频资源,然后踢除房间里的所有用户,最后销毁音视频房间;exists检查指定的音视频房间是否存在;list列出所有有效的音视频房间; listparticipants列出指定房间中所有激活的参与者及其详细信息。

异步请求API有:joinjoinandconfigureconfigurepublishunpublishstartpauseswitchleavejoin允许你加入指定的音视频房间;configure可用于修改某些属性(例如,比特率范围);joinandconfigure的含义是将前两个请求合并为一个请求(该请求仅适用于发布者);publish发布媒体流给所有订阅者; unpublish正好与publish相反;start允许你开始接收订阅的媒体流;pause暂停发送媒体流;switch更改指定PeerConnection的媒体源(例如,你正在看A,现在改为看B),但无需为此创建新的Handle;leave离开视频房间。

创建新的音视频房间,其格式如下:

{
        "request""create""room":<可选,房间ID。如果不填,则由插件随机生成>,
        "permanent":<true | false,是否创建永久房间,默认= false>,
        "description""<可选,房间的名称>""secret""<可选,编辑/销毁房间时用的密码>""pin""<可选,加入房间的密码>""is_private":<true | false,是否是私有房间?如果是私有房间则不会出现在房间列表中>,
        "allowed":[可选,用户加入房间的token数组],
        ...
}

如果create成功,则会返回created响应,格式如下:

{
        "videoroom":“created",
        "room":<房间ID>,
        "permanent":<是否是创建的永久房间?是则为true,否则为false>
}

如果create请求失败,则返回错误信息,格式如下:

{
        "videoroom""event""error_code":<错误码,每个错误码的含义需要看插件实现代码中的宏定义>,
        "error""<错误描述字符串>"
}

例如:

janus.send({
      message: { request: 'create', room: 1234 },
      success(msg) {
        console.log(msg, 'msg');
      },
    });

关闭摄像头

// Remove local video
echotest.createOffer(
    {
        media: { removeVideo: true },
        success: function(jsep) {
            Janus.debug(jsep);
            echotest.send({message: {audio: true, video: true}, "jsep": jsep});
        },
        error: function(error) {
            bootbox.alert("WebRTC error... " + JSON.stringify(error));
        }
    });

更换摄像头

echotest.createOffer(
    {
        media: {
            video: {
                deviceId: "44f4740bee234ce6ddcfea8e59e8ed7505054f75edf27e3a12294686b37ff6a7"
            },
            replaceVideo: true
        },
        success: function(jsep) {
            Janus.debug(jsep);
            echotest.send({message: {audio: true, video: true}, "jsep": jsep});
        },
        error: function(error) {
            bootbox.alert("WebRTC error... " + JSON.stringify(error));
        }
    });

重启连接

echotest.createOffer({
    iceRestart: true,
    media: { data: true },
    success: function(jsep) {
        echotest.send({message: {audio: true, video: true}, jsep: jsep});
        }
});