分享

基于Webrtc服务器搭建音视频通话的详细步骤(附源码下载)

 邸彦强 2022-08-11 发布于河北

基于Webrtc服务器搭建音视频通话。PineAppRtc为android webrtc客户端 ServerProject为服务端,包含房间服务器,信令服务器,穿透服务器。

文章图片1
文章图片2
文章图片3
文章图片4
文章图片5
文章图片6
文章图片7
文章图片8
文章图片9
文章图片10
文章图片11
文章图片12
文章图片13
upstream roomserver { server 192.168.6.54:8080; } server { #listen 80 default_server; #listen [::]:80 default_server; listen 80; # SSL configuration # # listen 443 ssl default_server; # listen [::]:443 ssl default_server; # # Note: You should disable gzip for SSL traffic. # See: https://bugs./773332 # # Read up on ssl_ciphers to ensure a secure configuration. # See: https://bugs./765782 # # Self signed certs generated by the ssl-cert package # Don't use them in a production server! # # include snippets/snakeoil.conf; root /var/www/html; # Add index.php to the list if you are using PHP index index.html index.htm index.nginx-debian.html index.php; #此处添加index.php server_name _; # location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. # try_files $uri $uri/ =404; # } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { include snippets/fastcgi-php.conf; # With php7.0-cgi alone: # fastcgi_pass 127.0.0.1:9000; # With php7.0-fpm: fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location / { proxy_pass http://roomserver$request_uri; proxy_set_header Host $host; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
文章图片14
 <?php              $request_username = $_GET['username'];              if(empty($request_username)) {                  echo 'username == null';                  exit;              }              $request_key = $_GET['key'];              $time_to_live = 600;              $timestamp = time() + $time_to_live;//失效时间              $response_username = $timestamp.':'.$_GET['username'];              $response_key = $request_key;              if(empty($response_key))              $response_key = 'code_key'; //constants.py中CEOD_KEY              $response_password = getSignature($response_username, $response_key);              $jsonObj = new Response();              $jsonObj->username = $response_username;              $jsonObj->password = $response_password;              $jsonObj->ttl = 86400;            //此处需配置自己的服务器            $jsonObj->uris= array('stun:192.168.6.54:3478','turn:192.168.6.54:3478?transport=udp','turn:192.168.6.54:3478?transport=tcp');            echo json_encode($jsonObj);          /**            * 使用HMAC-SHA1算法生成签名值            *            * @param $str 源串            * @param $key 密钥            *            * @return 签名值            */        function getSignature($str, $key) {        $signature = '';        if (function_exists('hash_hmac')) {        $signature = base64_encode(hash_hmac('sha1', $str, $key, true));        } else {        $blocksize = 64;        $hashfunc = 'sha1';        if (strlen($key) > $blocksize) {        $key = pack('H*', $hashfunc($key));        }        $key = str_pad($key, $blocksize, chr(0x00));        $ipad = str_repeat(chr(0x36), $blocksize);        $opad = str_repeat(chr(0x5c), $blocksize);        $hmac = pack(        'H*', $hashfunc(        ($key ^ $opad) . pack(        'H*', $hashfunc(        ($key ^ $ipad) . $str        )        )        )        );        $signature = base64_encode($hmac);        }            return $signature;            }            class Response {                  public $username = '';                  public $password = '';                  public $ttl = '';                  public $uris = array('');              }          ?> 
文章图片15
<?php $request_username = 'inesadt'; //配置成自己的turn服务器用户名 if(empty($request_username)) { echo 'username == null'; exit; } $request_key = 'inesadt'; //配置成自己的turn服务器密码 $time_to_live = 600; $timestamp = time() + $time_to_live;//失效时间 $response_username = $timestamp.':'.$_GET['username']; $response_key = $request_key; if(empty($response_key)) $response_key = 'CEOD_KEY';//constants.py中CEOD_KEY $response_password = getSignature($response_username, $response_key); $arrayObj = array(); $arrayObj[0]['username'] = $response_username; $arrayObj[0]['credential'] = $response_password; //配置成自己的stun/turn服务器 $arrayObj[0]['urls'][0] = 'stun:192.168.6.54:3478'; $arrayObj[0]['urls'][1] = 'turn:192.168.6.54:3478?transport=tcp'; $arrayObj[0]['uris'][0] = 'stun:192.168.6.54:3478'; $arrayObj[0]['uris'][1] = 'turn:192.168.6.54:3478?transport=tcp'; $jsonObj = new Response(); $jsonObj->lifetimeDuration = '300.000s'; $jsonObj->iceServers = $arrayObj; echo json_encode($jsonObj); /** * 使用HMAC-SHA1算法生成签名值 * * @param $str 源串 * @param $key 密钥 * * @return 签名值 */ function getSignature($str, $key) { $signature = ''; if (function_exists('hash_hmac')) { $signature = base64_encode(hash_hmac('sha1', $str, $key, true)); } else { $blocksize = 64; hashfunc = 'sha1'; if (strlen($key) > $blocksize) { $key = pack('H*', $hashfunc($key)); } $key = str_pad($key, $blocksize, chr(0x00)); $ipad = str_repeat(chr(0x36), $blocksize); $opad = str_repeat(chr(0x5c), $blocksize); $hmac = pack( 'H*', $hashfunc( ($key ^ $opad) . pack( 'H*', $hashfunc( ($key ^ $ipad) . $str ) ) ) ); $signature = base64_encode($hmac); } return $signature; } class Response { public $lifetimeDuration = ''; public $iceServers = array(''); } ?>
文章图片16
文章图片17
文章图片18

源码下载地址:
https://github.com/frozenfires/WebRTC/archive/master.zip

技术介绍地址:
https://me.csdn.net/u014374009

源码主页地址:
https://github.com/YouAreOnlyOne

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多