WebRTC VideoEngine综合应用示例(一)——视频通话的基本流程(转)
本系列目前共三篇文章,后续还会更新
WebRTC VideoEngine综合应用示例(一)——视频通话的基本流程
WebRTC VideoEngine综合应用示例(二)——集成OPENH264编解码器
WebRTC VideoEngine综合应用示例(三)——集成X264编码和ffmpeg解码
WebRTC技术的出现改变了传统即时通信的现状,它是一套开源的旨在建立浏览器端对端的通信标准的技术,支持浏览器平台,使用P2P架构。WebRTC所采用的技术都是当前VoIP先进的技术,如内部所采用的音频引擎是Google收购知名GIPS公司获得的核心技术:视频编解码则采用了VP8。
大家都说WebRTC好,是未来的趋势,但是不得不说这个开源项目对新手学习实在是太不友好,光是windows平台下的编译就能耗费整整一天的精力,还未必能成功,关于这个问题在我之前的文章中有所描述。编译成功之后打开一看,整个solution里面有215个项目,绝对让人当时就懵了,而且最重要的是,google方面似乎没给出什么有用的文档供人参考,网络上有关的资料也多是有关于web端开发的,和Native API开发有关的内容少之又少,于是我决定把自己这两天学习VideoEngine的成果分享出来,供大家参考,有什么问题也欢迎大家指出,一起学习一起进步。
首先需要说明的是,webrtc项目的all.sln下有一个vie_auto_test项目,里面包含了一些针对VideoEngine的测试程序,我这里的demo就是基于此修改得到的。
先来看一下VideoEngine的核心API,基本上就在以下几个头文件中了。
具体来说
ViEBase用于
- 创建和销毁 VideoEngine 实例
- 创建和销毁 channels - 将 video channel 和相应的 voice channel 连接到一起并同步 - 发送和接收的开始与停止
ViECapture用于
- 分配capture devices. - 将 capture device 与一个或多个 channels连接起来. - 启动或停止 capture devices. - 获得capture device 的可用性.
ViECodec用于
- 设置发送和接收的编解码器.
- 设置编解码器特性.
- Key frame signaling.
- Stream management settings.
ViEError即一些预定义的错误消息
ViEExternalCodec用于注册除VP8之外的其他编解码器
ViEImageProcess提供以下功能
- Effect filters - 抗闪烁 - 色彩增强
ViENetwork用于
- 配置发送和接收地址. - External transport support. - 端口和地址过滤. - Windows GQoS functions and ToS functions. - Packet timeout notification. - Dead‐or‐Alive connection observations.
ViERender用于
- 为输入视频流、capture device和文件指定渲染目标. - 配置render streams.
ViERTP_RTCP用于
- Callbacks for RTP and RTCP events such as modified SSRC or CSRC.
- SSRC handling. - Transmission of RTCP reports. - Obtaining RTCP data from incoming RTCP sender reports. - RTP and RTCP statistics (jitter, packet loss, RTT etc.). - Forward Error Correction (FEC). - Writing RTP and RTCP packets to binary files for off‐line analysis of the call quality. - Inserting extra RTP packets into active audio stream.
下面将以实现一个视频通话功能为实例详细介绍VideoEngine的使用,在文末将附上相应源码的下载地址
第一步是创建一个VideoEngine实例,如下
webrtc::VideoEngine* ptrViE = NULL;
ptrViE = webrtc::VideoEngine::Create();
if (ptrViE == NULL)
{
printf("ERROR in VideoEngine::Create\n");
return -;
}
然后初始化VideoEngine并创建一个Channel
webrtc::ViEBase* ptrViEBase = webrtc::ViEBase::GetInterface(ptrViE);
if (ptrViEBase == NULL)
{
printf("ERROR in ViEBase::GetInterface\n");
return -;
} error = ptrViEBase->Init();//这里的Init其实是针对VideoEngine的初始化
if (error == -)
{
printf("ERROR in ViEBase::Init\n");
return -;
} webrtc::ViERTP_RTCP* ptrViERtpRtcp = webrtc::ViERTP_RTCP::GetInterface(ptrViE);
if (ptrViERtpRtcp == NULL)
{
printf("ERROR in ViERTP_RTCP::GetInterface\n");
return -;
} int videoChannel = -;
error = ptrViEBase->CreateChannel(videoChannel);
if (error == -)
{
printf("ERROR in ViEBase::CreateChannel\n");
return -;
}
列出可用的capture devices等待用户进行选择, 然后进行allocate和connect,最后start选中的capture device
webrtc::ViECapture* ptrViECapture = webrtc::ViECapture::GetInterface(ptrViE);
if (ptrViEBase == NULL)
{
printf("ERROR in ViECapture::GetInterface\n");
return -;
} const unsigned int KMaxDeviceNameLength = ;
const unsigned int KMaxUniqueIdLength = ;
char deviceName[KMaxDeviceNameLength];
memset(deviceName, , KMaxDeviceNameLength);
char uniqueId[KMaxUniqueIdLength];
memset(uniqueId, , KMaxUniqueIdLength); printf("Available capture devices:\n");
int captureIdx = ;
for (captureIdx = ;
captureIdx < ptrViECapture->NumberOfCaptureDevices();
captureIdx++)
{
memset(deviceName, , KMaxDeviceNameLength);
memset(uniqueId, , KMaxUniqueIdLength); error = ptrViECapture->GetCaptureDevice(captureIdx, deviceName, KMaxDeviceNameLength, uniqueId, KMaxUniqueIdLength);
if (error == -)
{
printf("ERROR in ViECapture::GetCaptureDevice\n");
return -;
}
printf("\t %d. %s\n", captureIdx + , deviceName);
}
printf("\nChoose capture device: "); if (scanf("%d", &captureIdx) != )
{
printf("Error in scanf()\n");
return -;
}
getchar();
captureIdx = captureIdx - ; // Compensate for idx start at 1. error = ptrViECapture->GetCaptureDevice(captureIdx, deviceName, KMaxDeviceNameLength, uniqueId, KMaxUniqueIdLength);
if (error == -)
{
printf("ERROR in ViECapture::GetCaptureDevice\n");
return -;
} int captureId = ;
error = ptrViECapture->AllocateCaptureDevice(uniqueId, KMaxUniqueIdLength, captureId);
if (error == -)
{
printf("ERROR in ViECapture::AllocateCaptureDevice\n");
return -;
} error = ptrViECapture->ConnectCaptureDevice(captureId, videoChannel);
if (error == -)
{
printf("ERROR in ViECapture::ConnectCaptureDevice\n");
return -;
} error = ptrViECapture->StartCapture(captureId);
if (error == -)
{
printf("ERROR in ViECapture::StartCapture\n");
return -;
}
设置RTP/RTCP所采用的模式
error = ptrViERtpRtcp->SetRTCPStatus(videoChannel, webrtc::kRtcpCompound_RFC4585);
if (error == -)
{
printf("ERROR in ViERTP_RTCP::SetRTCPStatus\n");
return -;
}
设置接收端解码器出问题的时候,比如关键帧丢失或损坏,如何重新请求关键帧的方式
error = ptrViERtpRtcp->SetKeyFrameRequestMethod(videoChannel, webrtc::kViEKeyFrameRequestPliRtcp);
if (error == -)
{
printf("ERROR in ViERTP_RTCP::SetKeyFrameRequestMethod\n");
return -;
}
设置是否为当前channel使用REMB(Receiver Estimated Max Bitrate)包,发送端可以用它表明正在编码当前channel
接收端用它来记录当前channel的估计码率
error = ptrViERtpRtcp->SetRembStatus(videoChannel, true, true);
if (error == -)
{
printf("ERROR in ViERTP_RTCP::SetTMMBRStatus\n");
return -;
}
设置rendering用于显示
webrtc::ViERender* ptrViERender = webrtc::ViERender::GetInterface(ptrViE);
if (ptrViERender == NULL)
{
printf("ERROR in ViERender::GetInterface\n");
return -;
}
显示本地摄像头数据,这里的window1和下面的window2都是显示窗口,更详细的内容后面再说
error = ptrViERender->AddRenderer(captureId, window1, , 0.0, 0.0, 1.0, 1.0);
if (error == -)
{
printf("ERROR in ViERender::AddRenderer\n");
return -;
} error = ptrViERender->StartRender(captureId);
if (error == -)
{
printf("ERROR in ViERender::StartRender\n");
return -;
}
显示接收端收到的解码数据
error = ptrViERender->AddRenderer(videoChannel, window2, , 0.0, 0.0, 1.0, 1.0);
if (error == -)
{
printf("ERROR in ViERender::AddRenderer\n");
return -;
} error = ptrViERender->StartRender(videoChannel);
if (error == -)
{
printf("ERROR in ViERender::StartRender\n");
return -;
}
设置编解码器
webrtc::ViECodec* ptrViECodec = webrtc::ViECodec::GetInterface(ptrViE);
if (ptrViECodec == NULL)
{
printf("ERROR in ViECodec::GetInterface\n");
return -;
} VideoCodec videoCodec;
int numOfVeCodecs = ptrViECodec->NumberOfCodecs();
for (int i = ; i<numOfVeCodecs; ++i)
{
if (ptrViECodec->GetCodec(i, videoCodec) != -)
{
if (videoCodec.codecType == kVideoCodecVP8)
{
break;
}
}
} videoCodec.targetBitrate = ;
videoCodec.minBitrate = ;
videoCodec.maxBitrate = ;
videoCodec.maxFramerate = ; error = ptrViECodec->SetSendCodec(videoChannel, videoCodec);
assert(error != -); error = ptrViECodec->SetReceiveCodec(videoChannel, videoCodec);
assert(error != -);
设置接收和发送地址,然后开始发送和接收
webrtc::ViENetwork* ptrViENetwork = webrtc::ViENetwork::GetInterface(ptrViE);
if (ptrViENetwork == NULL)
{
printf("ERROR in ViENetwork::GetInterface\n");
return -;
}
//VideoChannelTransport是由我们自己定义的类,后面将会详细介绍
VideoChannelTransport* video_channel_transport = NULL; video_channel_transport = new VideoChannelTransport(ptrViENetwork, videoChannel); const char* ipAddress = "127.0.0.1";
const unsigned short rtpPort = ;
std::cout << std::endl;
std::cout << "Using rtp port: " << rtpPort << std::endl;
std::cout << std::endl; error = video_channel_transport->SetLocalReceiver(rtpPort);
if (error == -)
{
printf("ERROR in SetLocalReceiver\n");
return -;
}
error = video_channel_transport->SetSendDestination(ipAddress, rtpPort);
if (error == -)
{
printf("ERROR in SetSendDestination\n");
return -;
} error = ptrViEBase->StartReceive(videoChannel);
if (error == -)
{
printf("ERROR in ViENetwork::StartReceive\n");
return -;
} error = ptrViEBase->StartSend(videoChannel);
if (error == -)
{
printf("ERROR in ViENetwork::StartSend\n");
return -;
}
设置按下回车键即停止通话
printf("\n call started\n\n");
printf("Press enter to stop...");
while ((getchar()) != '\n')
{
}
停止通话后的各种stop
error = ptrViEBase->StopReceive(videoChannel);
if (error == -)
{
printf("ERROR in ViEBase::StopReceive\n");
return -;
} error = ptrViEBase->StopSend(videoChannel);
if (error == -)
{
printf("ERROR in ViEBase::StopSend\n");
return -;
} error = ptrViERender->StopRender(captureId);
if (error == -)
{
printf("ERROR in ViERender::StopRender\n");
return -;
} error = ptrViERender->RemoveRenderer(captureId);
if (error == -)
{
printf("ERROR in ViERender::RemoveRenderer\n");
return -;
} error = ptrViERender->StopRender(videoChannel);
if (error == -)
{
printf("ERROR in ViERender::StopRender\n");
return -;
} error = ptrViERender->RemoveRenderer(videoChannel);
if (error == -)
{
printf("ERROR in ViERender::RemoveRenderer\n");
return -;
}
error = ptrViECapture->StopCapture(captureId);
if (error == -)
{
printf("ERROR in ViECapture::StopCapture\n");
return -;
} error = ptrViECapture->DisconnectCaptureDevice(videoChannel);
if (error == -)
{
printf("ERROR in ViECapture::DisconnectCaptureDevice\n");
return -;
} error = ptrViECapture->ReleaseCaptureDevice(captureId);
if (error == -)
{
printf("ERROR in ViECapture::ReleaseCaptureDevice\n");
return -;
} error = ptrViEBase->DeleteChannel(videoChannel);
if (error == -)
{
printf("ERROR in ViEBase::DeleteChannel\n");
return -;
} delete video_channel_transport; int remainingInterfaces = ;
remainingInterfaces = ptrViECodec->Release();
remainingInterfaces += ptrViECapture->Release();
remainingInterfaces += ptrViERtpRtcp->Release();
remainingInterfaces += ptrViERender->Release();
remainingInterfaces += ptrViENetwork->Release();
remainingInterfaces += ptrViEBase->Release();
if (remainingInterfaces > )
{
printf("ERROR: Could not release all interfaces\n");
return -;
} bool deleted = webrtc::VideoEngine::Delete(ptrViE);
if (deleted == false)
{
printf("ERROR in VideoEngine::Delete\n");
return -;
} return ;
以上就是VideoEngine的基本使用流程,下面说一下显示窗口如何创建
这里使用了webrtc已经为我们定义好的类ViEWindowCreator,它有一个成员函数CreateTwoWindows可以直接创建两个窗口,只需实现定义好窗口名称、窗口大小以及坐标即可,如下
ViEWindowCreator windowCreator;
ViEAutoTestWindowManagerInterface* windowManager = windowCreator.CreateTwoWindows();
VideoEngineSample(windowManager->GetWindow1(), windowManager->GetWindow2());
这里的VideoEngineSample就是我们在前面所写的包含全部流程的示例程序,它以两个窗口的指针作为参数。
至于前面提到的VideoChannelTransport定义如下
class VideoChannelTransport : public webrtc::test::UdpTransportData
{
public:
VideoChannelTransport(ViENetwork* vie_network, int channel); virtual ~VideoChannelTransport(); // Start implementation of UdpTransportData.
virtual void IncomingRTPPacket(const int8_t* incoming_rtp_packet,
const int32_t packet_length,
const char* /*from_ip*/,
const uint16_t /*from_port*/) OVERRIDE; virtual void IncomingRTCPPacket(const int8_t* incoming_rtcp_packet,
const int32_t packet_length,
const char* /*from_ip*/,
const uint16_t /*from_port*/) OVERRIDE;
// End implementation of UdpTransportData. // Specifies the ports to receive RTP packets on.
int SetLocalReceiver(uint16_t rtp_port); // Specifies the destination port and IP address for a specified channel.
int SetSendDestination(const char* ip_address, uint16_t rtp_port); private:
int channel_;
ViENetwork* vie_network_;
webrtc::test::UdpTransport* socket_transport_;
}; VideoChannelTransport::VideoChannelTransport(ViENetwork* vie_network,
int channel)
: channel_(channel),
vie_network_(vie_network)
{
uint8_t socket_threads = ;
socket_transport_ = webrtc::test::UdpTransport::Create(channel, socket_threads);
int registered = vie_network_->RegisterSendTransport(channel,
*socket_transport_);
} VideoChannelTransport::~VideoChannelTransport()
{
vie_network_->DeregisterSendTransport(channel_);
webrtc::test::UdpTransport::Destroy(socket_transport_);
} void VideoChannelTransport::IncomingRTPPacket(
const int8_t* incoming_rtp_packet,
const int32_t packet_length,
const char* /*from_ip*/,
const uint16_t /*from_port*/)
{
vie_network_->ReceivedRTPPacket(
channel_, incoming_rtp_packet, packet_length, PacketTime());
} void VideoChannelTransport::IncomingRTCPPacket(
const int8_t* incoming_rtcp_packet,
const int32_t packet_length,
const char* /*from_ip*/,
const uint16_t /*from_port*/)
{
vie_network_->ReceivedRTCPPacket(channel_, incoming_rtcp_packet, packet_length);
} int VideoChannelTransport::SetLocalReceiver(uint16_t rtp_port)
{
int return_value = socket_transport_->InitializeReceiveSockets(this, rtp_port);
if (return_value == )
{
return socket_transport_->StartReceiving();
}
return return_value;
} int VideoChannelTransport::SetSendDestination(const char* ip_address, uint16_t rtp_port)
{
return socket_transport_->InitializeSendSockets(ip_address, rtp_port);
}
继承自UdpTransportData类,主要重写了IncomingRTPPacket和IncomingRTCPPacket两个成员函数,分别调用了vie_network的ReceivedRTPPacket和ReceivedRTCPPacket方法,当需要将接收到的RTP和RTCP包传给VideoEngine时就应该使用这两个函数。
该示例程序最后效果如下,我这里是几个虚拟摄像头,然后会有两个窗口,一个是摄像头画面,一个是解码的画面。
源码地址在这里,这是一个可以脱离webrtc那个大项目而独立运行的工程。
原文转自 http://blog.csdn.net/nonmarking/article/details/47375849#
WebRTC VideoEngine综合应用示例(一)——视频通话的基本流程(转)的更多相关文章
- WebRTC VoiceEngine综合应用示例(二)——音频通话的基本流程(转)
下面将以实现一个音频通话功能为示例详细介绍VoiceEngine的使用,在文末将附上相应源码的下载地址.这里参考的是voiceengine\voe_cmd_test. 第一步是创建VoiceEngin ...
- WebRTC VoiceEngine综合应用示例(一)——基本结构分析(转)
把自己这两天学习VoiceEngine的成果分享出来,供大家参考,有什么问题也欢迎大家指出,一起学习一起进步. 本文将对VoiceEngine的基本结构做一个分析,分析的方法是自底向上的:看一个音频编 ...
- WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码
转自:http://blog.csdn.net/nonmarking/article/details/47958395 本系列目前共三篇文章,后续还会更新 WebRTC VideoEngine超详细教 ...
- 全互联结构DVPN综合配置示例
以下内容摘自正在全面热销的最新网络设备图书“豪华四件套”之一<H3C路由器配置与管理完全手册>(第二版)(其余三本分别是:<Cisco交换机配置与管理完全手册>(第二版).&l ...
- PIE SDK组件式开发综合运用示例
1. 功能概述 关于PIE SDK的功能开发,在我们的博客上已经分门别类的进行了展示,点击PIESat博客就可以访问,为了初学者入门,本章节将对从PIE SDK组件式二次开发如何搭建界面.如何综合开发 ...
- Django笔记&教程 5-3 综合使用示例
Django 自学笔记兼学习教程第5章第3节--综合使用示例 点击查看教程总目录 1 - 生成学号场景 场景描述: 教务管理系统中,学生注册账号,学生选择年级后,生成唯一学号. 细节分析: 学生学号由 ...
- zigbee学习:示例程序SampleApp中通讯流程
zigbee学习:示例程序SampleApp中通讯流程 本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明. 参考链接: http://wjf88223.bl ...
- 结合WebSocket编写WebGL综合场景示例
在WebGL场景中导入多个Babylon骨骼模型,在局域网用WebSocket实现多用户交互控制. 首先是场景截图: 上图在场景中导入一个Babylon骨骼模型,使用asdw.空格.鼠标控制加速度移动 ...
- 一文带你了解webrtc基本原理(动手实现1v1视频通话)
webrtc (Web Real-Time Communications) 是一个实时通讯技术,也是实时音视频技术的标准和框架. 大白话讲,webrtc是一个集大成的实时音视频技术集,包含了各种客户端 ...
随机推荐
- retain, copy, assign以及autorelease
一,retain, copy, assign区别 1. 假设你用malloc分配了一块内存,并且把它的地址赋值给了指针a,后来你希望指针b也共享这块内存,于是你又把a赋值给(assign)了b.此时a ...
- jenkins+maven+svn 自动化部署
背景: 公司的web平台使用JAVA写的,但是不是用Tomcat部署的,代码内部自带了Web服务器,所以只需要有JAVA环境,将代码打包上传,启动脚本就可以. 项目是根据pom.xml打包成的是.zi ...
- Goroutine 中执行匿名函数 坑
//相对应for 循环 goroutine跑到慢 所以这里很大概率只会打印最后一条数据 func goRun() { values := []int{1, 2, 3} for _, v := rang ...
- python将excel数据写入数据库,或从库中读取出来
首先介绍一下SQL数据库的一些基本操作: 1创建 2删除 3写入 4更新(修改) 5条件选择 有了以上基本操作,就可以建立并存储一个简单的数据库了. 放出python调用的代码: 此处是调用dos 操 ...
- viewController备注
1.按结构可以对iOS的所有ViewController分成两类: 1).主要用于展示内容的ViewController,这种ViewController主要用于为用户展示内容,并与用户交互,如UIT ...
- Linux学习-Linux 的开机流程分析
开机流程一览 系统开机的经过可以汇整成底下的流程的: 加载 BIOS 的硬件信息与进行自我测试,并依据设定取得第一个可开机的装置; 读取并执行第一个开机装置内 MBR 的 boot Loader (亦 ...
- selenium2中TestNG相关解释
testNg官网:http://testng.org/doc/documentation-main.html 新建testNG class的时候,同时也新建了一个TestNG.xml的文件. 此xml ...
- 《鸟哥的Linux私房菜》学习笔记(1)——文件与目录
在Linux中,任何设备都是文件,不仅如此,连数据通信的接口也有专门的文件负责.可以说,一切皆文件,目录也是一种文件,是路径映射.因此,文件系统是Linux的基础. 一.文件与目录管理命令 1.ls( ...
- 将系统从.Net Core2.0升级到.Net Core2.1
最近将手头的一个.Net Core2.0开发的小系统升级到最新的Core2.1.升级期间遇到了一些问题,现将问题以及解决方法整理汇总一下. 一是作为笔记,二也为跟各位分享一下.如过能帮到看到这帖子的人 ...
- Python 综合应用小项目一
数据库报错重连机制 利用异常捕获来获取mysql断开的报错,然后再重连 import MySQLdb as mysql class DB: def __init__(self,host,user,pa ...