WebRTC技术实现的音视频通话服务器搭建教程

发表时间: 2020-03-02 18:32

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














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.debian.org/773332            #            # Read up on ssl_ciphers to ensure a secure configuration.            # See: https://bugs.debian.org/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;            #}        }


 <?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("");              }          ?> 


<?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("");            }         ?>





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

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

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