搞定P2P跨平台视频通信,利用Agora实现

实现了ubuntu和web应用之间的跨平台视频通信

提前准备技能:html,js,c++,linux

准备工作:下载Agora的两个SDK,Web和Linux C++

下载地址:https://docs.agora.io/cn/Agora%20Platform/downloads

下载版本:

Agora_Web_SDK_v2_8_0_FULL

OpenVideoCall-Linux 4(此版本为Agora的测试版)

在ubuntu行示例程序,发送视频流:

首先在 [Agora.io 注册](https://dashboard.agora.io/cn/signup/) 注册账号,并创建自己的测试项目,获取到 AppID。将 AppID 填写进 run.sh

```  --appId "your app ID"  ```

然后在 [Agora.io SDK](https://www.agora.io/cn/blog/download/) 下载 **视频通话 + 直播 SDK**,解压后将其中**libs**文件夹下的

- libagora_rtc_sdk.so

两个文件复制到本项目的 “OpenVideoCall/libs” 文件夹下。

在sample/OpenVideoCall下 输入命令 make, 便可编译生成openVideoCall demo。在run.sh中配置执行demo所需要的参数。

openVideoCall 为命令行程序。使用方法如下:

command:

- open 开启通话。

- close 关闭通话。

- enable_video 打开/关闭 全局视频功能,enable_video 0 为关闭视频功能,enable_video 1为打开视频功能

- enable_audio 打开/关闭 全局音频功能

- enable_local_video 打开/关闭 本地视频功能,enable_local_video 0 为关闭视频功能,enable_local_video 1为打开视频功能

- mute_local_video 打开/关闭 本地视频流发送,mute_local_video 0 为打开本地视频流发送,mute_local_video 1为关闭本地视频流发送

- mute_local_audio 打开/关闭 本地音频流发送,mute_local_video 0 为打开本地音频流发送,mute_local_video 1为关闭本地音频流发送

- print_device_info  打印设备信息

- set_cur_camera  设置当前工作的摄像头,set_cur_camera ‘device id ’ ,device id可以通过print_device_info 得到

- exit 退出程序

记得更改APPID,并且该ID为无app证书验证的版本!!!

Web端接收视频流页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script type""text/javascript" src="./AgoraRTCSDK-2.8.0.js"></script>
    <script>
        var APPID='**************';
        var CHCHANNEL_KEY=null;
        var CHANNEL_NAME='123456';
        var UID='12345';
        
        var client = AgoraRTC.createClient({mode: 'live'});
        
        client.init(APPID, function(){console.log("AgoraRTC client initialized");}, function(err){console.log("AgoraRTC client init failed", err);});
        
        client.join(CHCHANNEL_KEY,CHANNEL_NAME,null, function(UID) {console.log("User " + UID + " join channel successfully");}, function(err) {console.log("Join channel failed", err);});
        
        function recv(){
            console.log("Start mon");
            
            client.on('stream-added', function (evt) {var stream = evt.stream;console.log("New stream added: " + stream.getId());client.subscribe(stream, function (err) {console.log("Subscribe stream failed", err);});
            });
            
            client.on('stream-subscribed',function(evt){
                var remoteStream = evt.stream;
                console.log("Subscribe remote stream successfully: " + remoteStream.getId());
                
                remoteStream.play('agora_remote');
                });
            
            // 这里使用agora_remote + remoteStream.getId()作为dom元素的id。            
        }
    </script>
</head>
<body>
<input type="button" name="confirmAlter" value="recv" οnclick="recv()"/>
<div>收到消息:</div>
<div id="video" style="margin:0 auto;">
    <div id="agora_remote" style="float:right;width:210px;height:147px;display:inline-block;">video</div>
</div>
</body>
</html>

Web端推送视频流:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <script type""text/javascript" src="./AgoraRTCSDK-2.8.0.js"></script>
    <script>
        var APPID='****************';
        var CHCHANNEL_KEY=null;
        var CHANNEL_NAME='123456';
        var UID='1234';
        
        var client = AgoraRTC.createClient({mode: 'live'});
        
        client.init(APPID, function(){console.log("AgoraRTC client initialized");}, function(err){console.log("AgoraRTC client init failed", err);});
        
        client.join(CHCHANNEL_KEY,CHANNEL_NAME,null, function(UID) {console.log("User " + UID + " join channel successfully");}, function(err) {console.log("Join channel failed", err);});
        
        function push(){
            localStream = AgoraRTC.createStream({streamID: UID, audio: true,  video: true, screen: false});
            localStream.setVideoProfile('720p_3');
            localStream.on("accessAllowed", function() {console.log("accessAllowed");});
            localStream.on("accessDenied", function() {console.log("accessDenied");});
            
            localStream.init(function(){
                console.log("getUserMedia successfully");
                localStream.play('agora_local');
                client.publish(localStream, function (err) {console.log("Publish local stream error: " + err);});
                client.on('stream-published', function (evt) {console.log("Publish local stream successfully");});
            });
        }
    </script>
</head>
<body>
<input type="button" name="confirmAlter" value="push" οnclick="push()"/>
<div>收到消息:</div>
<div id="video" style="margin:0 auto;">
    <div id="agora_local" style="float:right;width:210px;height:147px;display:inline-block;">video</div>
</div>
</body>
</html>

该代码博主测试成功过

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章