Skip to content
This repository was archived by the owner on May 19, 2023. It is now read-only.
This repository was archived by the owner on May 19, 2023. It is now read-only.

ResponseWriter.Write() not writing to client #116

@seriousm4x

Description

@seriousm4x

So I tried to convert a video with ffmpeg and stream the output to the client while encoding. Since fiber doesn't support streaming I tried it with net/http and it works without problems.

Then i found this adaptor and tried to wrap my function inside it but the ffmpeg output never reaches the browser.

I guess that it's got something to do with ResponseWriter.Write() and the adaptor but I don't know what.

Here is the example (make sure to have ffmpeg):

package main

import (
	"fmt"
	"io"
	"net/http"
	"os/exec"

	"github.com/gofiber/adaptor/v2"
	"github.com/gofiber/fiber/v2"
)

func main() {
	// New fiber app
	app := fiber.New()

	// http.HandlerFunc -> fiber.Handler
	app.Get("/", adaptor.HTTPHandlerFunc(greet))

	// Listen on port 3000
	app.Listen(":3000")
}

func greet(w http.ResponseWriter, r *http.Request) {
	cmd := exec.Command("ffmpeg",
		"-i", "https://test-streams.mux.dev/x36xhzz/url_6/193039199_mp4_h264_aac_hq_7.m3u8",
		"-c", "copy",
		"-bsf:a", "aac_adtstoasc",
		"-movflags", "frag_keyframe+empty_moov",
		"-f", "mp4",
		"-")

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}

	if err = cmd.Start(); err != nil {
		fmt.Println(err)
		panic(err)
	}
	fmt.Println("ffmpeg running")

	data := make([]byte, 1024)
	for {
		w.Header().Set("Content-Type", "video/mp4")
		w.Header().Add("Content-Disposition", "attachment; filename=test.mp4")
		n, err := stdout.Read(data)
		if err == io.EOF {
			fmt.Println("eof")
			break
		}
		if n > 0 {
			valid_data := data[:n]
			if _, err = w.Write(valid_data); err != nil {
				fmt.Println("client disconnected")
				cmd.Process.Kill()
				break
			}
		}
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions