实时视频通话系列第四篇。上一篇我们介绍了在Windows环境下如何集成即构音视频SDK,回顾《Windows实时视频通话SDK集成指引》,这一篇将介绍视频直播功能实现流程。
还是以 2 人间的实时视频为例,主要流程如下:
请注意:
上面流程中以 2 名房间成员间的实时视频为例,实际上 Zego SDK 支持多人实时视频。建议开发者按需设计。
为了便于开发者更快理解 VideoTalk 中的逻辑,下述每节会将功能核心源码片段挑出来并加以讲解。开发者亦可直接阅读 VideoTalk 源码,两者是一致的。
1、登录房间
用户间进行实时视频对话前,需要先登录到同一个房间。
VideoTalk 中登录相关源码片段演示如下,仅供参考:
ZegoVideoTalkDialog.cpp// 执行登录房间操作,成功后将在回调中发布直播
if (!LIVEROOM::LoginRoom(m_pChatRoom->getRoomId().toStdString().c_str(), role))
{QMessageBox::information(NULL, tr("提示"), tr("进入房间失败"));
}//LoginRoom 的回调函数 OnLoginRoom
void ZegoVideoTalkDialog::OnLoginRoom(int errorCode, const QString& strRoomID, QVector<StreamPtr> vStreamList)
{if (errorCode != 0){addLogString(tr(登录房间失败. error: %1").arg(errorCode));QMessageBox::information(NULL, tr("提示"), tr("登录房间失败. error: %1").arg(errorCode));this->close();return;}addLogString(tr("登录房间成功. roomID: %1").arg(strRoomID));//加入房间列表roomMemberAdd(m_strCurUserName);//登录房间成功即推流以及拉流StartPublishStream();for (auto& stream : vStreamList){StartPlayStream(stream);}}
2、开始视频通话
登录房间成功后,即可开始走推流、拉流流程,以推送己方画面和观看对方画面。
VideoTalk 中开始视频通话(即推流、拉流)相关源码片段演示如下,仅供参考:
ZegoVideoTalkDialog.cpp// 同意视频通话,房间成员开始推流
void ZegoVideoTalkDialog::StartPublishStream()
{QTime currentTime = QTime::currentTime();//获取当前时间的毫秒int ms = currentTime.msec();QString strStreamId;
#ifdef Q_OS_WINstrStreamId = QString("s-windows-%1-%2").arg(m_strCurUserID).arg(ms);
#elsestrStreamId = QString("s-mac-%1-%2").arg(m_strCurUserID).arg(ms);
#endifm_strPublishStreamID = strStreamId;StreamPtr pPublishStream(new QZegoStreamModel(m_strPublishStreamID, m_strCurUserID, m_strCurUserName, "", true));m_pChatRoom->addStream(pPublishStream);//推流前调用双声道LIVEROOM::SetAudioChannelCount(2);if (m_avaliableView.size() > 0){int nIndex = takeLeastAvaliableViewIndex();pPublishStream->setPlayView(nIndex);addAVView(nIndex);if (theApp.GetBase().getUseSurfaceMerge()){
#if (defined Q_OS_WIN) && (defined USE_SURFACE_MERGE) StartSurfaceMerge();
#endif}else{LIVEROOM::SetVideoFPS(m_pAVSettings->GetFps());LIVEROOM::SetVideoBitrate(m_pAVSettings->GetBitrate());LIVEROOM::SetVideoCaptureResolution(m_pAVSettings->GetResolution().cx, m_pAVSettings->GetResolution().cy);LIVEROOM::SetVideoEncodeResolution(m_pAVSettings->GetResolution().cx, m_pAVSettings->GetResolution().cy);//配置ViewLIVEROOM::SetPreviewView((void *)AVViews.last()->winId());LIVEROOM::SetPreviewViewMode(LIVEROOM::ZegoVideoViewModeScaleAspectFill);LIVEROOM::StartPreview();}QString streamID = m_strPublishStreamID;m_anchorStreamInfo = pPublishStream;addLogString(tr("创建流成功, streamID: %1").arg(streamID));if (LIVEROOM::StartPublishing(m_pChatRoom->getRoomName().toStdString().c_str(), streamID.toStdString().c_str(), LIVEROOM::ZEGO_JOIN_PUBLISH, "")){m_bIsPublishing = true;addLogString(tr("开始直播,流ID: %1").arg(streamID));}}
}// 推流成功,开始拉流,观看房间其他成员画面
void ZegoVideoTalkDialog::StartPlayStream(StreamPtr stream)
{if (stream == nullptr) { return; }m_pChatRoom->addStream(stream);if (m_avaliableView.size() > 0){int nIndex = takeLeastAvaliableViewIndex();qDebug() << "playStream nIndex = " << nIndex << " play stream id is " << stream->getStreamId();stream->setPlayView(nIndex);addAVView(nIndex);//配置ViewLIVEROOM::SetViewMode(LIVEROOM::ZegoVideoViewModeScaleAspectFill, stream->getStreamId().toStdString().c_str());LIVEROOM::StartPlayingStream(stream->getStreamId().toStdString().c_str(), (void *)AVViews.last()->winId());}
}
3、结束视频通话
视频通话结束后的操作主要是停止推流、停止拉流、清理视图、登出房间等,开发者可按需调用。
VideoTalk 中结束视频通话相关源码片段演示如下,仅供参考:
ZegoVideoTalkDialog.cpp// 停止预览,停止推拉流,清理 view
void ZegoVideoTalkDialog::cleanBeforeGetOut()
{//离开房间时先把混音功能和声卡采集关闭
#ifdef Q_OS_WINif (m_isUseDefaultAux)EndAux();else{AUDIOHOOK::StopAudioRecord();LIVEROOM::EnableAux(false);AUDIOHOOK::UnInitAudioHook();}
#elseEndAux();
#endifif (ui.m_bCapture->text() == tr("停止采集"))
#ifdef Q_OS_WINLIVEROOM::EnableMixSystemPlayout(false);
#endiffor (auto& stream : m_pChatRoom->getStreamList()){if (stream != nullptr){if (stream->isCurUserCreated()){StopPublishStream(stream->getStreamId());}else{StopPlayStream(stream->getStreamId());}}}roomMemberDelete(m_strCurUserName);LIVEROOM::LogoutRoom();if (timer != nullptr)timer->stop();//释放堆内存delete m_cbMircoPhoneListView;delete m_cbCameraListView;delete m_memberModel;delete m_cbMircoPhoneModel;delete m_cbCameraModel;delete timer;delete gridLayout;//指针置为空m_cbMircoPhoneListView = nullptr;m_cbCameraListView = nullptr;m_memberModel = nullptr;m_cbMircoPhoneModel = nullptr;m_cbCameraModel = nullptr;timer = nullptr;gridLayout = nullptr;
}
4、 API 调用时序图
实时视频 API 调用时序图如下: