0


QT嵌入支持WEBRTC的网页

本文主要讲在QT开发中,WEBRTC网页版通信的第一步,浏览器与媒体设备(即麦克风和摄像头)的交互的实现。

先编译好支持音视频的QtWebEngine或QCefView的其中一个,编译过程可参考

编译支持音视频的QtWebEngine + Win10_x64 + QT5.15.0 + VS2019_qt webengine win10-CSDN博客

编译QCefView + Win10_x64 + QT5.15.0 + VS2019-CSDN博客

就可以开始进行QT上的带视频的网页相关开发了,下面的例子在QT界面插入网页,网页里调用了getUserMedia获取本地视频。

1.网页代码index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>打开摄像头</title>
  6. <style>
  7. video {
  8. width: 50%;
  9. height: 50%;
  10. margin: 50px auto; /*则表示上下边界为50,左右则根据宽度自适应相同值(即水平居中)*/
  11. background-color: #484848 ;
  12. display: block; /*将元素显示为块级元素。*/
  13. }
  14. .button {
  15. background-color: #4CAF50; /* Green */
  16. border: none;
  17. color: white;
  18. padding: 15px 32px;
  19. text-align: center;
  20. text-decoration: none;
  21. display: inline-block;
  22. font-size: 16px;
  23. margin: 4px 2px;
  24. cursor: pointer;
  25. -webkit-transition-duration: 0.4s; /* Safari */
  26. transition-duration: 0.4s;
  27. box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
  28. }
  29. .button:hover {
  30. box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19);
  31. }
  32. </style>
  33. <script>
  34. // 打开摄像头,最新的标准API
  35. function openVideo() {
  36. var video = document.getElementById("video");
  37. navigator.mediaDevices.getUserMedia({video : {width: 800, height: 600}}).then(success).catch(error);
  38. }
  39. // 视频播放回调函数
  40. function success(stream) {
  41. video.srcObject = stream; //将视频流设置为video元素的源
  42. video.play(); //播放视频
  43. }
  44. // 异常的回调函数
  45. function error(error) {
  46. console.log("访问用户媒体设备失败:",error.name,error.message);
  47. }
  48. </script>
  49. </head>
  50. <body>
  51. <video id="video"></video>
  52. <div align="center">
  53. <button id="open" class="button" onclick="openVideo()">打开摄像头</button>
  54. </div>
  55. </body>
  56. </html>

2.QtWebEngine的使用

MainWindow.hpp

  1. #include <QtWebEngineWidgets/QWebEngineView>
  2. ...
  3. protected:
  4. void slotFeaturePermissionRequested(const QUrl& securityOrigin, QWebEnginePage::Feature feature);
  5. ...
  6. private:
  7. QWebEngineView* m_engineView = nullptr;
  8. ...

MainWindow.cpp 下面代码直接给了权限,也可以根据实际情况给权限

  1. ...
  2. QHBoxLayout* hLayTest = new QHBoxLayout();
  3. //获取程序的当前路径
  4. QDir dir = QCoreApplication::applicationDirPath();
  5. //程序路径+html相对路径
  6. QString path = QDir::toNativeSeparators(dir.filePath("web/index.html"));
  7. m_engineView = new QWebEngineView(this);
  8. m_engineView->load(QUrl::fromLocalFile(path));
  9. connect(m_engineView->page(), &QWebEnginePage::featurePermissionRequested, this, &CMainWindow::slotFeaturePermissionRequested);
  10. hLayTest->addWidget(m_engineView);
  11. ...
  12. //给权限
  13. void CMainWindow::slotFeaturePermissionRequested(const QUrl& securityOrigin, QWebEnginePage::Feature feature)
  14. {
  15. m_engineView->page()->setFeaturePermission(securityOrigin, feature, QWebEnginePage::PermissionGrantedByUser);
  16. }
  17. ...

即可在QT的界面上显示出本机视频设备的视频。

3.QCefView的使用

先拷贝QCefView例子下的这两个文件到自己项目中

main.cpp

  1. #include <QCefContext.h>
  2. ...
  3. QApplication a(argc, argv);
  4. // build QCefConfig
  5. QCefConfig config;
  6. config.setUserAgent("QCefViewTest");
  7. config.setLogLevel(QCefConfig::LOGSEVERITY_DEFAULT);
  8. config.setBridgeObjectName("CallBridge");
  9. config.setRemoteDebuggingPort(9000);
  10. config.setBackgroundColor(Qt::lightGray);
  11. // add command line args
  12. // config.addCommandLineSwitch("allow-universal-access-from-files");
  13. config.addCommandLineSwitch("enable-media-stream");
  14. config.addCommandLineSwitch("use-mock-keychain");
  15. config.addCommandLineSwitch("allow-file-access-from-files");
  16. config.addCommandLineSwitch("disable-spell-checking");
  17. config.addCommandLineSwitch("disable-site-isolation-trials");
  18. config.addCommandLineSwitch("enable-aggressive-domstorage-flushing");
  19. config.addCommandLineSwitchWithValue("renderer-process-limit", "1");
  20. config.addCommandLineSwitchWithValue("disable-features", "BlinkGenPropertyTrees,TranslateUI,site-per-process");
  21. // initialize QCefContext instance with config
  22. QCefContext cefContext(&a, argc, argv, &config);
  23. CMainWindow* mainWindow = new CMainWindow();
  24. mainWindow->show();
  25. QApplication::exec();
  26. ...

MainWindow.hpp

  1. #include "CefViewWidget.h"
  2. ...
  3. private:
  4. CefViewWidget* m_cefViewWidget = nullptr;
  5. ...

MainWindow.cpp

  1. ...
  2. QHBoxLayout* hLayTest = new QHBoxLayout();
  3. //音视频
  4. QCefSetting setting;
  5. setting.setWindowlessFrameRate(60);
  6. setting.setBackgroundColor(QColor::fromRgba(qRgba(255, 255, 220, 255)));
  7. //获取程序的当前路径
  8. QDir dir = QCoreApplication::applicationDirPath();
  9. //程序路径+html相对路径
  10. QString path = QDir::toNativeSeparators(dir.filePath("web/index.html"));
  11. m_cefViewWidget = new CefViewWidget(path, &setting);
  12. hLayTest->addWidget(m_cefViewWidget);
  13. ...

即可在QT的界面上显示出本机视频设备的视频。

标签: qt webrtc c++

本文转载自: https://blog.csdn.net/qq_40321622/article/details/140492630
版权归原作者 chocolate7777777 所有, 如有侵权,请联系我们删除。

“QT嵌入支持WEBRTC的网页”的评论:

还没有评论