声网平行操控 SDK 支持主流的 Linux、Android 设备端平台,Windows、Web、Linux、Android、iOS、macOS 操控端平台。
本文介绍如何通过声网平行操控 SDK 在 Linux 设备端实现主要功能。
基于如下技术框架图,你可以结合你的实际业务进行开发。
参考如下要求分别准备硬件环境和软件环境。
请确保你的 Linux 设备上有可用的摄像头。可通过 ls /dev/video*
命令查看全部可用摄像头。
如需支持其他架构,请联系销售。
如果你的操作系统为 Ubuntu,安装以下依赖:
# 安装 aptitude
sudo apt install aptitude
# 安装 build-essential libx11-dev libxcomposite-dev libxext-dev libxfixes-dev libxdamage-dev cmake
sudo aptitude install libx11-dev libxcomposite-dev libxext-dev libxfixes-dev libxdamage-dev cmake
如果你的操作系统为 CentOS,安装以下依赖:
sudo yum groupinstall "Development Tools"
sudo yum install wget
sudo yum groupinstall X11
你需要联系销售获取平行操控 Linux SDK。
初始化 SDK 并与声网 RTC 频道建立连接。详见准备工作。
YuvCapturer
对象。static resolution s;
static YuvCapturer capturer(s);
static Buffer b;
capturer.set_resolution(options.video.width, options.video.height); // 设置分辨率,默认值为 640 × 480
capturer.set_fps(options.video.frameRate); // 设置帧率,默认值为 30
capturer.set_device(options.video.device_name); // 指定采集视频流的摄像头,默认值为 /dev/video0
capturer.init(); // 初始化摄像头采集
capturer.camera_capture_start(); // 开始摄像头采集
capturer.get_one_frame();
// 发送 YUV 格式的原始视频数据
videoFrameSender->sendVideoFrame(videoFrame);
// 发送编码后的 H.264 格式的视频数据
videoH264FrameSender->sendEncodedVideoImage(
reinterpret_cast<uint8_t*>(h264Frame.get()->buffer.get()), h264Frame.get()->bufferLen,
videoEncodedFrameInfo);
clear_one_frame
从内存中清除获取的视频帧。成功释放设备内存后,才能继续调用 get_one_frame
获取下一个视频帧。示例代码如下:capturer.clear_one_frame();
RtmClient
对象,并设置事件监听。auto service = createAndInitAgoraService(false, true, true, true, false, options.appId.c_str());
auto con = service->createRtcConnection(agora::rtc::RtcConnectionConfiguration());
// 使用新的流订阅模式
auto p = con->getAgoraParameter(); p->setBool("rtc.vos_aut_use_old_sync",false);
// 创建 RtmClient
auto rtm_client = agora::rtm::createAgoraRtmClient();
rtmHandler handle;
// 配置 RtmClient
agora::rtm::RtmConfig config;
// 设置事件监听程序
config.eventHandler = &handle;
config.appId = options.appId.c_str();
// userId 中仅支持传入内容为数值的字符串,例如 "1234567"
config.userId = options.userId.c_str();
// 初始化 RtmClient
rtm_client->initialize(config);
StreamChannel
类中的任何 API 之前,你需要先调用 createStreamChannel
创建 Stream Channel。// 创建 Stream Channel
auto stream_channel = rtm_client->createStreamChannel(options.channelId.c_str());
agora::rtm::JoinChannelOptions opt;
opt.token = options.appId.c_str();
// 加入频道
stream_channel->join(opt);
agora::rtm::JoinTopicOptions topic_join_opt;
// 加入 Topic
stream_channel->joinTopic(options.topic.c_str(), topic_join_opt);
// 在 Topic 中发布消息
stream_channel->publishTopicMessage(options.topic.c_str(), "hellortm", 9);
agora::rtm::TopicOptions topic_opt;
const char *users[] = { options.remoteUserId.c_str() };
topic_opt.users = users;
topic_opt.userCount = 1;
stream_channel->subscribeTopic(options.topic.c_str(), topic_opt);
onMessageEvent
事件通知接收远端用户的消息。// 继承 IRtmEventHandler 类
class rtmHandler : public agora::rtm::IRtmEventHandler {
public:
// 监听消息事件通知
void onMessageEvent(MessageEvent &event) override
{
if(_stream_channel){
int r = _stream_channel->publishTopicMessage(_topic.c_str(), event.message, 12);
printf("send %d rrr \n",r);
}
// 获取系统时间
get_stime();
// 打印消息发送和接收的时间戳
printf("send ts: %s, receive ts: %s \n", event.message, timestr);
}
agora::rtm::IStreamChannel* _stream_channel = nullptr;
std::string _topic;
};
stream_channel->leaveTopic(options.topic.c_str()); // 退出 Topic
stream_channel->leave(); // 退出频道
stream_channel->release(); // 销毁 Stream Channel
rtm_client->release(); // 销毁 RtmClient
声网在 SDK 包中提供了以下示例项目供你参考:
├── agora_sdk # 声网的 SDK 库文件和头文件
└── example # 示例代码
├── capture_yuv
│ └── sample_send_capture_yuv.cpp # 采集并发送音视频流
└── rtm
├── receive_rtm_message.cpp # 接收控制信令
└── send_rtm_message.cpp # 发送控制信令
你可以参考以下 API 文档: