티스토리 뷰

WebRTC

[WebRTC] Janus Gateway 설치 및 데모

Dotori.ai 2022. 7. 19. 17:20

Janus Gateway 는 Meetecho에서 설계 및 개발한 WebRTC 오픈 소스 서버이다.

 

기본적인 WebRTC 기능을 포함하고 있으며, Janus 에서 WebRTC 기능을 Use Case 에 따라 편리하게 구현할 수 있도록 플러그인 형태로 제공하고 있다. 플러그인 종류는 아래 페이지를 참고하면 된다.

Janus Plugin 리스트

 

Janus 에 대한 설명은 이렇게 간단히 정리하고, Janus Gateway 설치 방법에 대해 설명한다.

 

  • 오라클 클라우드에 Janus 설치 및 실행
    • Janus 도커 이미지 생성
    • 도커 이미지 빌드 및 실행
  • 안드로이드 단말에서 Janus 서버에 접속하고 Video Room 데모

오라클 클라우드에 Janus 설치 및 실행


 

1. 오라클 클라우드에 무료로 생성할 수 있는 instance 를 생성한다.

2. OS 는 Ubuntu 18.04 버전을 설치했다.

3. Docker 를 설치한다.(https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository)

4. 아래와 같이 Docker file 작성

FROM ubuntu:bionic
RUN echo ' \n\
APT::Periodic::Update-Package-Lists "0";\n\
APT::Periodic::Unattended-Upgrade "1";\n'\
> /etc/apt/apt.conf.d/20auto-upgrades
RUN set -x \
    && apt-get update && apt-get install -y build-essential snapd aptitude git wget golang \
    python3 python3-pip python3-setuptools python3-wheel ninja-build \
    libgstreamer1.0-dev libgirepository1.0-dev libunwind-dev apt-utils libsrtp-dev \
    gdb \
    && aptitude install -y libmicrohttpd-dev libjansson-dev libnice-dev \
    libssl-dev libsofia-sip-ua-dev libglib2.0-dev libopus-dev libogg-dev \
    libconfig-dev libavutil-dev libavcodec-dev libavformat-dev libnanomsg-dev \
    libcurl4-openssl-dev liblua5.3-dev pkg-config gengetopt libtool automake curl jq httpie vim screen doxygen graphviz
RUN set -x \
    && wget https://cmake.org/files/v3.16/cmake-3.16.2.tar.gz \
    && tar -xvzf cmake-3.16.2.tar.gz \
    && cd cmake-3.16.2 \
    && ./bootstrap --prefix=/usr/local \
    && make && make install
RUN set -x \
    && python3 -m pip install meson \
    && python3 -m pip install ninja
RUN  set -x \
    && cd  \
    && wget https://github.com/cisco/libsrtp/archive/v2.2.0.tar.gz \
    && tar xfv v2.2.0.tar.gz  \
    && cd libsrtp-2.2.0/ \
    && ./configure --prefix=/usr --enable-openssl \
    && make shared_library && make install
RUN  set -x \
    && git clone https://boringssl.googlesource.com/boringssl \
    && cd boringssl \
    && sed -i s/" -Werror"//g CMakeLists.txt \
    && mkdir -p build \
    && cd build \
    && cmake -DCMAKE_CXX_FLAGS="-lrt" .. \
    && make \
    && cd .. \
    && mkdir -p /opt/boringssl \
    && cp -R include /opt/boringssl/ \
    && mkdir -p /opt/boringssl/lib \
    && cp build/ssl/libssl.a /opt/boringssl/lib/ \
    && cp build/crypto/libcrypto.a /opt/boringssl/lib/
RUN  set -x \
    && git clone https://github.com/sctplab/usrsctp \
    && cd usrsctp \
    && ./bootstrap \
    && ./configure --prefix=/usr --disable-programs --disable-inet --disable-inet6 \
    && make && make install
RUN  set -x \
    && git clone https://github.com/warmcat/libwebsockets.git \
    && cd libwebsockets \
    && mkdir build \
    && cd build \
    && cmake -DLWS_MAX_SMP=1 -DLWS_WITHOUT_EXTENSIONS=0 -DCMAKE_INSTALL_PREFIX:PATH=/usr -DCMAKE_C_FLAGS="-fpic" .. \
    && make && make install
RUN set -x \
    && git clone https://github.com/eclipse/paho.mqtt.c.git \
    && cd paho.mqtt.c \
    && make && make install
RUN set -x \
    && git clone https://github.com/alanxz/rabbitmq-c \
    && cd rabbitmq-c \
    && git submodule init \
    && git submodule update \
    && mkdir build && cd build \
    && cmake -DCMAKE_INSTALL_PREFIX=/usr .. \
    && make && make install
RUN set -x \
    && git clone https://gitlab.freedesktop.org/libnice/libnice \
    && cd libnice \
    && meson --prefix=/usr build && ninja -C build && ninja -C build install
RUN export JANUS_WITH_POSTPROCESSING
RUN  set -x \
    && git clone https://github.com/meetecho/janus-gateway.git \
    && cd janus-gateway \
    && sh autogen.sh \
    && ./configure --prefix=/opt/janus \
    && make \
    && make install \
    && make configs
RUN adduser --disabled-password --gecos '' janus
USER janus
CMD ["/opt/janus/bin/janus"]

5. Docker 이미지 생성

docker build -t janus-image .

6. conf 파일 수정

docker cp [HOST 파일경로] [container name]:/opt/janus/etc/janus/

7. janus.jcfg 파일 수정

media: {
        // RTP 포트 범위 설정
        rtp_port_range = "20000-40000"
}

nat: {
        // STUN 서버 설정
        stun_server = "stun.l.google.com"
        stun_port = 19302
        
        // 외부 고정 IP 설정
        nat_1_1_mapping = "xxx.xxx.xxx.xxx"
}

8. Janus 에서 사용하는 Port를 위한 방화벽 설정

  • 8088: RESTful API
  • 8188: Websocket API
iptables -I INPUT 5 -i ens3 -p tcp --dport 8088 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -I INPUT 5 -i ens3 -p tcp --dport 8188 -m state --state NEW,ESTABLISHED -j ACCEPT

9. Docker compose 생성 및 실행

version: '3'
services:
  jg:
    image: janus-image
    restart: unless-stopped
    user: root
    ulimits:
      nofile:
        soft: "65536"
        hard: "65536"
    logging:
      driver: "json-file"
      options:
        max-file: "5"
        max-size: "50m"
    volumes:
      - /home/ubuntu/workspace/janus/conf:/opt/janus/etc/janus/
      - /home/ubuntu/workspace/janus/data/log:/var/log
      - /home/ubuntu/workspace/janus/data/records:/tmp/
    network_mode: host

안드로이드 단말에서 Janus 서버에 접속하고 Video Room 데모


아래와 같은 형태로 Restful 을 사용하여 오라클 클라우드에서 설치한 Janus 에 접속할 수 있다.

 

package com.sing.videoroom.api

import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava3.RxJava3CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

interface VideoRoomRestService {
    companion object {
        private const val BASE_URL = "http://xxx.xxx.xxx.xxx:8088"
        fun create(): VideoRoomService {

            val logger = HttpLoggingInterceptor().apply {
                level = HttpLoggingInterceptor.Level.BODY
            }

            val client = OkHttpClient.Builder().addInterceptor(logger).build()

            return Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava3CallAdapterFactory.create())
                .build()
                .create(VideoRoomRestService::class.java)
        }
    }
}

오라클 클라우드 서버에서 Docker log 를 통해 확인해보면, 정상적으로 연결됐을 때 아래와 같은 로그가 출력되는 것을 볼 수 있다.

docker logs -f [CONTAINER NAME]