diff --git a/build/egress/Dockerfile b/build/egress/Dockerfile index fbfa47b0..2f3c210e 100644 --- a/build/egress/Dockerfile +++ b/build/egress/Dockerfile @@ -15,17 +15,18 @@ FROM livekit/gstreamer:1.22.8-dev ARG TARGETPLATFORM +ARG TARGETARCH +ENV TARGETARCH=${TARGETARCH} +ENV TARGETPLATFORM=${TARGETPLATFORM} WORKDIR /workspace # install go -RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then GOARCH=arm64; else GOARCH=amd64; fi && \ - wget https://go.dev/dl/go1.22.1.linux-${GOARCH}.tar.gz && \ +RUN wget https://go.dev/dl/go1.22.1.linux-${TARGETARCH}.tar.gz && \ rm -rf /usr/local/go && \ - tar -C /usr/local -xzf go1.22.1.linux-${GOARCH}.tar.gz + tar -C /usr/local -xzf go1.22.1.linux-${TARGETARCH}.tar.gz ENV PATH="/usr/local/go/bin:${PATH}" - # download go modules COPY go.mod . COPY go.sum . @@ -42,8 +43,13 @@ COPY --from=livekit/egress-templates workspace/build/ cmd/server/templates/ RUN find cmd/server/templates/ -name *.map | xargs rm # build -RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then GOARCH=arm64; else GOARCH=amd64; fi && \ - CGO_ENABLED=1 GOOS=linux GOARCH=${GOARCH} GO111MODULE=on GODEBUG=disablethp=1 go build -a -o egress ./cmd/server +RUN CGO_ENABLED=1 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on GODEBUG=disablethp=1 go build -a -o egress ./cmd/server + +# install tini +ENV TINI_VERSION v0.19.0 + +ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-${TARGETARCH} /tini +RUN chmod +x /tini FROM livekit/gstreamer:1.22.8-prod @@ -73,8 +79,10 @@ RUN useradd -ms /bin/bash -g root -G sudo,pulse,pulse-access egress RUN mkdir -p home/egress/tmp home/egress/.cache/xdgr && \ chown -R egress /home/egress + # copy files COPY --from=0 /workspace/egress /bin/ +COPY --from=0 /tini /tini COPY build/egress/entrypoint.sh / # run diff --git a/build/egress/entrypoint.sh b/build/egress/entrypoint.sh index b406a3fc..9e40cd50 100755 --- a/build/egress/entrypoint.sh +++ b/build/egress/entrypoint.sh @@ -23,4 +23,4 @@ rm -rf /var/run/pulse /var/lib/pulse /home/egress/.config/pulse /home/egress/.ca pulseaudio -D --verbose --exit-idle-time=-1 --disallow-exit # Run egress service -exec egress +exec /tini -- egress diff --git a/pkg/pipeline/source/web.go b/pkg/pipeline/source/web.go index bbba4905..c7106871 100644 --- a/pkg/pipeline/source/web.go +++ b/pkg/pipeline/source/web.go @@ -43,9 +43,9 @@ const ( ) type WebSource struct { - pulseSink string - xvfb *exec.Cmd - chromeCancel context.CancelFunc + pulseSink string + xvfb *exec.Cmd + closeChrome context.CancelFunc startRecording chan struct{} endRecording chan struct{} @@ -109,10 +109,10 @@ func (s *WebSource) GetEndedAt() int64 { } func (s *WebSource) Close() { - if s.chromeCancel != nil { + if s.closeChrome != nil { logger.Debugw("closing chrome") - s.chromeCancel() - s.chromeCancel = nil + s.closeChrome() + s.closeChrome = nil } if s.xvfb != nil { @@ -254,9 +254,12 @@ func (s *WebSource) launchChrome(ctx context.Context, p *config.PipelineConfig, ) } - allocCtx, _ := chromedp.NewExecAllocator(context.Background(), opts...) - chromeCtx, cancel := chromedp.NewContext(allocCtx) - s.chromeCancel = cancel + allocCtx, allocCancel := chromedp.NewExecAllocator(context.Background(), opts...) + chromeCtx, chromeCancel := chromedp.NewContext(allocCtx) + s.closeChrome = func() { + chromeCancel() + allocCancel() + } chromedp.ListenTarget(chromeCtx, func(ev interface{}) { switch ev := ev.(type) { diff --git a/pkg/server/server_rpc.go b/pkg/server/server_rpc.go index 27fa39a2..56b09c8f 100644 --- a/pkg/server/server_rpc.go +++ b/pkg/server/server_rpc.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" "path" + "syscall" "time" "google.golang.org/protobuf/encoding/protojson" @@ -119,6 +120,7 @@ func (s *Server) launchProcess(req *rpc.StartEgressRequest, info *livekit.Egress cmd.Dir = "/" cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr + cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true} s.monitor.EgressStarted(req)