# ── Stage 1: full install — source for cherry-picked plugins ────────────
FROM ros:kilted-ros-core AS gst-builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    gstreamer1.0-plugins-base \
    gstreamer1.0-plugins-good \
    gstreamer1.0-plugins-bad \
    gstreamer1.0-nice \
    && rm -rf /var/lib/apt/lists/*

# Copy only the 8 plugin .so files the pipeline actually uses.
# Each plugin bundle (plugins-base/good/bad/nice) contains dozens more
# that we never touch; leaving them out keeps the runtime image lean.
RUN set -e && ARCH=$(uname -m)-linux-gnu && \
    mkdir /gst-select && \
    for p in \
        libgstapp.so \
        libgstvideoconvertscale.so \
        libgstvpx.so \
        libgstrtp.so \
        libgstrtpmanager.so \
        libgstwebrtc.so \
        libgstdtls.so \
        libgstsrtp.so \
        libgstnice.so \
    ; do cp /usr/lib/${ARCH}/gstreamer-1.0/${p} /gst-select/; done

# ── Stage 2: lean runtime ───────────────────────────────────────────────
FROM ros:kilted-ros-core

RUN apt-get update && apt-get install -y --no-install-recommends \
    # GStreamer core runtime and base utility libs (libgstvideo etc.) — no plugins
    libgstreamer1.0-0 \
    libgstreamer-plugins-base1.0-0 \
    # Python GI bindings + WebRTC/SDP typelibs; pulls libgstreamer-plugins-bad1.0-0
    python3-gi \
    gir1.2-gst-plugins-bad-1.0 \
    # Shared libs the cherry-picked plugins link against
    libvpx9 \
    libnice10 \
    libsrtp2-1 \
    liborc-0.4-0t64 \
    # App deps
    python3-numpy \
    python3-websockets \
    && rm -rf /var/lib/apt/lists/*

# Install cherry-picked plugins and refresh the linker cache
COPY --from=gst-builder /gst-select/ /tmp/gst-select/
RUN ARCH=$(uname -m)-linux-gnu && \
    cp /tmp/gst-select/*.so /usr/lib/${ARCH}/gstreamer-1.0/ && \
    rm -rf /tmp/gst-select && \
    ldconfig

WORKDIR /app
COPY webrtc_streamer.py .
COPY entrypoint.sh .
RUN chmod +x entrypoint.sh

ENV IMAGE_TOPIC=/camera/image_raw
ENV PORT=8443
ENV BITRATE=2000000

EXPOSE 8443
ENTRYPOINT ["./entrypoint.sh"]
