Skip to content

環境構築ガイド

shogo4405 edited this page Feb 11, 2023 · 1 revision

nginx-rtmp-moduleを利用したRTMPサーバー環境の構築方法について紹介します。

前提条件

次の環境で構築しました。

  • macOS Venture 13.2のApple Silicon
  • Homebrew

インストール

$ brew tap marcqualie/nginx
$ brew install nginx-full --with-rtmp-module

時間を置くと無事にインストールできます。この後の設定でconfを編集します。nginx -Vコマンドで--conf-path=の場所をメモしましょう。

$ nginx -V
nginx version: nginx/1.23.3
built by clang 14.0.0 (clang-1400.0.29.202)
built with OpenSSL 1.1.1s  1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/opt/homebrew/Cellar/nginx-full/1.23.3 --with-http_ssl_module --with-pcre --with-ipv6 --sbin-path=/opt/homebrew/Cellar/nginx-full/1.23.3/bin/nginx --with-cc-opt='-I/opt/homebrew/include -I/opt/homebrew/opt/pcre/include -I/opt/homebrew/opt/openssl@1.1/include' --with-ld-opt='-L/opt/homebrew/lib -L/opt/homebrew/opt/pcre/lib -L/opt/homebrew/opt/openssl@1.1/lib' --conf-path=/opt/homebrew/etc/nginx/nginx.conf --pid-path=/opt/homebrew/var/run/nginx.pid --lock-path=/opt/homebrew/var/run/nginx.lock --http-client-body-temp-path=/opt/homebrew/var/run/nginx/client_body_temp --http-proxy-temp-path=/opt/homebrew/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/opt/homebrew/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/opt/homebrew/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/opt/homebrew/var/run/nginx/scgi_temp --http-log-path=/opt/homebrew/var/log/nginx/access.log --error-log-path=/opt/homebrew/var/log/nginx/error.log --add-module=/opt/homebrew/share/rtmp-nginx-module

nginxを起動します。

$ sudo nginx

confの設定

前項で確認したconfを編集していきます。私の環境では、/opt/homebrew/etc/nginx/nginx.confにありました。検証目的であれば次の設定で十分です。行末あたりに追加してください。

rtmp {
  server {
    listen [::]:1935; # Standard port
    chunk_size 8192;
    # Application configuration
    application live {
      live on;
      record off;
    }
  }
}

次にconfを反映させましょう。

$ sudo nginx -t // conf のシンタックスエラーなどないか確認
$ sudo nginx -s reload

設定が完了しました。

配信テスト

検証用のプロジェクトで配信テストを行っていきます。 手元のmacOSのipアドレスを調べておきます。ifconfigで調べてメモします。

$ ifconfig

検証用プロジェクトにあるPreference.swiftを次のように編集していきます。

struct Preference {
  static var defaultInstance = Preference()

  var uri: String? = "rtmp://#{macOSのipアドレス}/live"
  var streamName: String? = "live"
}

iOSの場合シュミレーターだとカメラが動作しないため実機のビルドをおすすめします。また、iOS実機の場合はプロビジョニングの設定が必要です。すぐに用意できない場合は、macOS向けのターゲットを起動しても検証可能です。起動したら、配信開始ボタンをクリックして配信していることを確認しましょう。

配信できているかの確認

ffplayコマンドを使うと便利です。

ffplay rtmp://localhost/live/live