## 一、前言说明
之前已经在推流基础上,实现了音视频通话,原理就是采集到音视频的
数据推流到流媒体服务程序,然后从流媒体服务程序拉流播放对方的音视频流即可,目前演示效果是做的一对一通话,稍微拓展一丢丢就可以支持多对多通话。之前演示的都是在局域网,很多人问是否可以外网通话,那肯定可以的啊,只需要搞个云服务器将流媒体程序放上面即可,需要他进行中转,那有些菜鸡又要问,能不能不要中转直接就能外网通信呢,答案是否定的,用屁股想想也知道,局域网的设备,
没有固定的公网ip,对方要和你通信,连IP地址都不知道,怎么通信,无论如何都需要外网进行中转的,哪怕是握手打洞的中转。别说什么花生壳绑定公网地址啥的,非常鸡肋几乎不好用,还需要路由器设备支持。
实现外网音视频通话,只需要在系统设置那边,将推流和拉流地址,对应的IP都换成云服务器的地址即可,也就是通话方A的音视频采集的流,会推送到服务器,通话B的采集的流也是推送到服务器,然后双方都从云服务器拉取各自需要的流即可。当然也可以加上权限验证的一系列自己的逻辑处理,避免串流。总体用下来效果还是非常不错的,唯一要注意的是网络带宽,如果双方都是1080P的摄像头,码流一般在2-4MB,意外着你的云服务器的网络带宽至少4MB,留一点冗余的话至少要5MB,主要是视频占用带宽,音频数据几乎可以忽略。如果带宽太小无法改变,那只能降低摄像头的分辨率,一般采集的时候可以设置按照多大的分辨率来采集。
## 二、效果图
window.open('http://www.qtcn.org/bbs/attachment/Mon_2507/44_110085_b5ac5698127da93.jpg?311');" style="max-width:700px;max-height:700px;" onload="if(is_ie6&&this.offsetWidth>700)this.width=700;" >## 三、相关代码
```cpp
#include "frmconfig.h"
#include "frmmain.h"
#include "ui_frmmain.h"
#include "qthelper.h"
#include "apphelper.h"
#include "osdgraph.h"
#include "ffmpegthread.h"
#include "ffmpegthreadcheck.h"
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
{
ui->setupUi(this);
this->initForm();
this->installEventFilter(this);
QMetaObject::invokeMethod(this, "formChanged", Qt::QueuedConnection);
}
frmMain::~frmMain()
{
delete ui;
}
void frmMain::savePos()
{
AppConfig::FormMax = this->isMaximized();
if (!AppConfig::FormMax) {
AppConfig::FormGeometry = this->geometry();
}
AppConfig::writeConfig();
}
bool frmMain::eventFilter(QObject *watched, QEvent *event)
{
//尺寸发生变化或者窗体移动位置记住窗体位置
int type = event->type();
if (type == QEvent::Resize || type == QEvent::Move) {
QMetaObject::invokeMethod(this, "savePos", Qt::QueuedConnection);
} else if (type == QEvent::Close) {
audioInput->stop(false);
audioOutput->stop(false);
exit(0);
}
//尺寸发生变化重新调整小预览窗体的位置
if (this->isVisible() && type == QEvent::Resize) {
this->formChanged();
}
return QWidget::eventFilter(watched, event);
}
void frmMain::initForm()
{
//初始化输入输出视频控件
int decodeType = AppConfig::DecodeType;
AppHelper::initVideoWidget(ui->videoInput, decodeType);
AppHelper::initVideoWidget(ui->videoOutput, decodeType);
//初始化输入输出音频
线程 audioInput = new FFmpegThread(this);
audioOutput = new FFmpegThread(this);
checkInput = new FFmpegThreadCheck(audioInput, this);
AppHelper::initAudioThread(audioInput, ui->levelInput, decodeType);
AppHelper::initAudioThread(audioOutput, ui->levelOutput, decodeType);
//输入打开成功后立即推流
connect(audioInput, SIGNAL(receivePlayStart(int)), this, SLOT(receivePlayStart(int)));
connect(ui->videoInput, SIGNAL(sig_receivePlayStart(int)), this, SLOT(receivePlayStart(int)));
ui->ckInput->setChecked(AppConfig::MuteInput ? Qt::Checked : Qt::Unchecked);
ui->ckOutput->setChecked(AppConfig::MuteOutput ? Qt::Checked : Qt::Unchecked);
if (AppConfig::StartServer) {
on_btnStart_clicked();
}
//打印完整地址
FFmpegThread::debugInfo = 1;
}
void frmMain::clearLevel()
{
ui->levelInput->setLevel(0);
ui->levelOutput->setLevel(0);
}
void frmMain::formChanged()
{
AppHelper::changeWidget(ui->videoInput, ui->videoOutput, ui->gridLayout, NULL);
}
void frmMain::receivePlayStart(int time)
{
QObject *obj = sender();
if (obj == ui->videoInput) {
#ifdef betaversion
OsdGraph::testOsd(ui->videoInput);
#endif
ui->videoInput->recordStart(AppConfig::VideoPush);
} else if (obj == audioInput) {
audioInput->recordStart(AppConfig::AudioPush);
}
}
void frmMain::on_btnStart_clicked()
{
if (ui->btnStart->text() == "启动服务") {
if (AppConfig::VideoUrl == "video=" || AppConfig::AudioUrl == "audio=") {
QtHelper::showMessageBoxError("请先打开系统设置, 选择对应的视音频设备");
//return;
}
if (!AppConfig::VideoUrl.isEmpty()) {
ui->videoInput->open(AppConfig::VideoUrl);
}
if (!AppConfig::VideoPull.isEmpty()) {
ui->videoOutput->open(AppConfig::VideoPull);
}
if (!AppConfig::AudioUrl.isEmpty()) {
audioInput->setMediaUrl(AppConfig::AudioUrl);
audioInput->play();
}
if (!AppConfig::AudioPull.isEmpty()) {
audioOutput->setMediaUrl(AppConfig::AudioPull);
audioOutput->play();
}
checkInput->start();
ui->btnStart->setText("停止服务");
//设置旋转角度
if (AppConfig::RotateInput > 0) {
ui->videoInput->getVideoThread()->setRotate(AppConfig::RotateInput);
}
if (AppConfig::RotateOutput > 0) {
ui->videoOutput->getVideoThread()->setRotate(AppConfig::RotateOutput);
}
} else {
ui->videoInput->stop();
ui->videoOutput->stop();
audioInput->stop();
audioOutput->stop();
checkInput->stop();
ui->btnStart->setText("启动服务");
QMetaObject::invokeMethod(this, "clearLevel", Qt::QueuedConnection);
}
AppConfig::StartServer = (ui->btnStart->text() == "停止服务");
AppConfig::writeConfig();
}
void frmMain::on_btnConfig_clicked()
{
static frmConfig *config = NULL;
if (!config) {
config = new frmConfig;
connect(config, SIGNAL(formChanged()), this, SLOT(formChanged()));
}
config->show();
config->activateWindow();
}
void frmMain::on_ckInput_stateChanged(int arg1)
{
bool muted = (arg1 != 0);
audioInput->setMuted(muted);
AppConfig::MuteInput = muted;
AppConfig::writeConfig();
}
void frmMain::on_ckOutput_stateChanged(int arg1)
{
bool muted = (arg1 != 0);
audioOutput->setMuted(muted);
AppConfig::MuteOutput = muted;
AppConfig::writeConfig();
}
```
## 四、相关地址
1. 国内站点:[
https://gitee.com/feiyangqingyun](https://gitee.com/feiyangqingyun)
2. 国际站点:[
https://github.com/feiyangqingyun](https://github.com/feiyangqingyun)
3. 个人作品:[
https://blog.csdn.net/feiyangqingyun/article/details/97565652](https://blog.csdn.net/feiyangqingyun/article/details/97565652)
4.
文件地址:[
https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g](https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g) 提取码:01jf 文件名:bin_video_call。
## 五、功能特点
1. 支持局域网和外网音视频实时通话,延迟极低,资源占用极低。
2. 自动获取本地所有视音频输入设备,本地摄像头设备自动罗列所有支持的分辨率、帧率、采集
格式等信息。
3. 可以指定采集的视频设备和音频输入设备,自由组合,视频设备可以设置不同的分辨率、帧率、采集格式。
4. 支持本地桌面屏幕作为视频设备采集,支持多个屏幕,自动识别屏幕分辨率。
5. 可以选择不同的声卡设备播放声音。
6. 内置自动重连机制,视音频设备支持热插拔。
7. 支持固定画中画功能,可交换主画面和浮窗画面,可设置画面左右排列等布局方式。
8. 可自定义悬浮画面位置,指定左上角、右上角、左下角、右下角、自定义位置和大小。
9. 内置流媒体服务程序,程序启动后自动启动流媒体服务,自动推拉流。
10. 视音频流数据支持rtsp/rtmp/http/webrtc等方式拉流,可以直接网页上打开视频画面。
11. 实时
显示本地音频振幅和远程音量振幅,可以分别对输入输出音量设置静音,方便测试。
12. 支持自定义水印,包括文字和图片水印,支持多个水印,指定任意位置。
13. 支持不同的视音频设备组合,比如本地摄像头加电脑麦克风而不是摄像头的麦克风,比如本地电脑桌面屏幕加摄像头的麦克风等。
14. 纯Qt+ffmpeg编写,支持windows和linux以及macos等系统,支持所有Qt版本、所有系统、所有
编译器。
15. 支持嵌入式linux板子和树莓派香橙派等,以及国产linux系统。