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

编程入门 行业动态 更新时间:2024-10-10 07:25:39

搞定P2P跨平台视频<a href=https://www.elefans.com/category/jswz/34/1769529.html style=通信,利用Agora实现"/>

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

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

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

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

下载地址:/downloads

下载版本:

Agora_Web_SDK_v2_8_0_FULL

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

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

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

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

然后在 [Agora.io SDK](/) 下载 **视频通话 + 直播 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" onclick="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" onclick="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>

该代码博主测试成功过

更多推荐

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

本文发布于:2024-03-07 08:26:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1717329.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通信   平台   视频   P2P   Agora

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!