FROM debian:stretch-backports
ARG GST_VERSION=master
ARG PREFIX=/usr

RUN apt-get update && apt-get install -y --no-install-recommends \
    autoconf `# libnice` \
    automake `# libnice` \
    autotools-dev \
    autopoint \
    bison \
    build-essential \
    ca-certificates \
    flex \
    gettext \
    git \
    gnutls-dev `# libnice` \
    gtk-doc-tools `# libnice` \
    libffi-dev \
    libglib2.0 \
    libnice-dev \
    libopus-dev \
    libpcre3-dev \
    libsrtp-dev \
    libssl-dev `# needed for DTLS requirement`\
    libtool `# libnice` \
    libvpx-dev \
    libx264-dev \
    mount \
    meson \
    perl \
    python3 \
    gobject-introspection `# needed for gst-python`\
    libgirepository1.0-dev `# needed for gst-python`\
    python-gi-dev \
    python3-dev \
    wget \
    zlib1g 


WORKDIR /tmp
RUN git clone https://github.com/sctplab/usrsctp \
    && cd usrsctp \
    && ./bootstrap \
    && ./configure --prefix ${PREFIX} --libdir /usr/lib/$(dpkg-architecture --query DEB_BUILD_MULTIARCH) \
    && make -j$(nproc) \
    && make install \
    && ldconfig 

WORKDIR /tmp
RUN set -ex && for module in gstreamer gst-python gst-libav gst-rtsp-server gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly; do \
  		git clone https://gitlab.freedesktop.org/gstreamer/$module.git --branch ${GST_VERSION} ; done 

WORKDIR /tmp/gstreamer
RUN ./autogen.sh --disable-gtk-doc --prefix=${PREFIX} --enable-introspection  \
	&& make -j$(nproc) \
	&& make install \
     && ldconfig

RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y locales

RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && \
    dpkg-reconfigure --frontend=noninteractive locales && \
    update-locale LANG=en_US.UTF-8

ENV LANG en_US.UTF-8 

RUN apt-get install -yq python3-pip libwebrtc-audio-processing-dev && pip3 install --upgrade meson
RUN apt-get remove libnice-dev libnice10 -yq
WORKDIR /tmp
RUN git clone https://gitlab.freedesktop.org/libnice/libnice.git 


RUN   cd libnice && mkdir build \
      && meson build/ --prefix=${PREFIX} -Dintrospection=enabled \ 
      && ninja -C build install 

ENV PYTHON=/usr/bin/python3
WORKDIR /tmp/gst-plugins-base
RUN set -ex && \
	./autogen.sh --disable-gtk-doc --prefix=${PREFIX} --enable-introspection  \
	&& make -j$(nproc) \
	&& make install \
    && ldconfig

WORKDIR /tmp/gst-plugins-good
RUN set -ex && \
	./autogen.sh --disable-gtk-doc --prefix=${PREFIX}  \
	&& make -j$(nproc) \
	&& make install \
    && ldconfig 

WORKDIR /tmp/gst-plugins-ugly
RUN set -ex && \
	./autogen.sh --disable-gtk-doc --prefix=${PREFIX} \
	&& make -j$(nproc) \
	&& make install \
     && ldconfig 

RUN ldconfig
WORKDIR /tmp/gst-plugins-bad
RUN   mkdir build \
      && meson build/ --prefix=${PREFIX} -Denable-sctp=enabled -Dintrospection=enabled -Dwebrtc=enabled \
      && ninja -C build install 


WORKDIR /tmp/gst-python
RUN   mkdir build \
      && meson build/ --prefix=${PREFIX} -Dpygi-overrides-dir=/usr/lib/python3/dist-packages/gi/overrides -Dpython=python3 \
      && ninja -C build install 


ENV GST_PLUGIN_PATH=/usr/lib/x86_64-linux-gnu/gstreamer-1.0

RUN set -ex && gst-inspect-1.0 python \
    && gst-inspect-1.0 webrtc \
    && gst-inspect-1.0 nice \
    && gst-inspect-1.0 sctp \
    && python3 -c 'import gi; gi.require_version("Gst", "1.0")' \
    && python3 -c 'import gi; gi.require_version("GstWebRTC", "1.0")' 

RUN apt-get install -y libjson-glib-dev

WORKDIR /opt
RUN git clone https://github.com/centricular/gstwebrtc-demos.git \
    && cd gstwebrtc-demos/sendrecv/gst \
    && make