-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
280 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"github.com/grafov/m3u8" | ||
"github.com/julienschmidt/httprouter" | ||
caterrs "github.com/livepeer/catalyst-api/errors" | ||
"github.com/livepeer/catalyst-api/log" | ||
"github.com/livepeer/catalyst-api/playback" | ||
ffmpeg "github.com/u2takey/ffmpeg-go" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strconv" | ||
) | ||
|
||
type ImageHandler struct { | ||
PublicBucketURLs []*url.URL | ||
} | ||
|
||
func NewImageHandler(urls []*url.URL) *ImageHandler { | ||
return &ImageHandler{ | ||
PublicBucketURLs: urls, | ||
} | ||
} | ||
|
||
func (p *ImageHandler) Handle(w http.ResponseWriter, req *http.Request, params httprouter.Params) { | ||
timeString := req.URL.Query().Get("time") | ||
time, err := strconv.ParseFloat(timeString, 64) | ||
if err != nil { | ||
caterrs.WriteHTTPBadRequest(w, "failed to parse time", nil) | ||
return | ||
} | ||
|
||
playbackID := params.ByName("playbackID") | ||
if playbackID == "" { | ||
caterrs.WriteHTTPBadRequest(w, "playbackID was empty", nil) | ||
return | ||
} | ||
|
||
err = p.handle(w, playbackID, time) | ||
if err != nil { | ||
log.LogNoRequestID("image API error", "err", err) | ||
switch { | ||
case errors.Is(err, caterrs.ObjectNotFoundError): | ||
caterrs.WriteHTTPNotFound(w, "not found", nil) | ||
default: | ||
caterrs.WriteHTTPInternalServerError(w, "internal server error", nil) | ||
} | ||
} | ||
} | ||
|
||
func (p *ImageHandler) handle(w http.ResponseWriter, playbackID string, time float64) error { | ||
var ( | ||
err error | ||
segmentFile string | ||
dir = playbackID | ||
) | ||
|
||
fileInfoReader, err := playback.OsFetch(p.PublicBucketURLs, dir, "index.m3u8", "") | ||
if err != nil { | ||
return fmt.Errorf("failed to read manifest: %w", err) | ||
} | ||
|
||
// download master playlist | ||
manifest, _, err := m3u8.DecodeFrom(fileInfoReader.Body, true) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode manifest: %w", err) | ||
} | ||
masterPlaylist, ok := manifest.(*m3u8.MasterPlaylist) | ||
if !ok || masterPlaylist == nil { | ||
return fmt.Errorf("failed to parse playlist as MasterPlaylist") | ||
} | ||
if len(masterPlaylist.Variants) < 1 { | ||
return fmt.Errorf("no renditions found") | ||
} | ||
|
||
renditionUri := masterPlaylist.Variants[0].URI | ||
fileInfoReader, err = playback.OsFetch(p.PublicBucketURLs, dir, renditionUri, "") | ||
if err != nil { | ||
return fmt.Errorf("failed to read manifest: %w", err) | ||
} | ||
dir = filepath.Join(dir, filepath.Dir(renditionUri)) | ||
manifest, _, err = m3u8.DecodeFrom(fileInfoReader.Body, true) | ||
if err != nil { | ||
return fmt.Errorf("failed to decode manifest: %w", err) | ||
} | ||
mediaPlaylist, ok := manifest.(*m3u8.MediaPlaylist) | ||
if !ok || mediaPlaylist == nil { | ||
return fmt.Errorf("failed to parse playlist as MediaPlaylist") | ||
} | ||
|
||
// find the segment required | ||
currentTime := 0.0 | ||
extractTime := 0.0 | ||
for _, segment := range mediaPlaylist.GetAllSegments() { | ||
currentTime += segment.Duration | ||
if currentTime > time { | ||
segmentFile = segment.URI | ||
extractTime = time - currentTime + segment.Duration | ||
break | ||
} | ||
} | ||
if segmentFile == "" { | ||
return fmt.Errorf("playbackID media not found: %w", caterrs.ObjectNotFoundError) | ||
} | ||
|
||
tmpDir, err := os.MkdirTemp(os.TempDir(), "image-api-*") | ||
if err != nil { | ||
return fmt.Errorf("temp file creation failed: %w", err) | ||
} | ||
defer os.RemoveAll(tmpDir) | ||
|
||
// download the segment | ||
fileInfoReader, err = playback.OsFetch(p.PublicBucketURLs, dir, segmentFile, "") | ||
if err != nil { | ||
return fmt.Errorf("failed to get media: %w", err) | ||
} | ||
segBytes, err := io.ReadAll(fileInfoReader.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to get bytes: %w", err) | ||
} | ||
|
||
inputFile := path.Join(tmpDir, "in.ts") | ||
if err = os.WriteFile(inputFile, segBytes, 0644); err != nil { | ||
return fmt.Errorf("failed to write input file: %w", err) | ||
} | ||
outputFile := path.Join(tmpDir, "out.jpg") | ||
|
||
// TODO disable ffmpeg verbose output | ||
err = ffmpeg. | ||
Input(inputFile). | ||
Output( | ||
outputFile, | ||
ffmpeg.KwArgs{ | ||
"ss": fmt.Sprintf("00:00:%d", int64(extractTime)), | ||
"vframes": "1", | ||
// TODO change resolution to a queryparam | ||
"vf": "scale=320:240:force_original_aspect_ratio=decrease", | ||
}, | ||
).OverWriteOutput().ErrorToStdOut().Run() | ||
if err != nil { | ||
return fmt.Errorf("ffmpeg failed: %w", err) | ||
} | ||
|
||
bs, err := os.ReadFile(outputFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
w.WriteHeader(http.StatusOK) | ||
count, err := w.Write(bs) | ||
if err != nil { | ||
log.LogNoRequestID("image handler failed to write response", "err", err) | ||
} else { | ||
log.LogNoRequestID("image handler wrote response", "count", count) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"log" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"os" | ||
"testing" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
"github.com/stretchr/testify/require" | ||
"gopkg.in/vansante/go-ffprobe.v2" | ||
) | ||
|
||
func TestImageHandler_Handle(t *testing.T) { | ||
wd, err := os.Getwd() | ||
require.NoError(t, err) | ||
handler := &ImageHandler{ | ||
PublicBucketURLs: []*url.URL{{Scheme: "file", Path: wd + "/../test"}}, | ||
} | ||
tests := []struct { | ||
name string | ||
time string | ||
playbackID string | ||
expectedStatus int | ||
}{ | ||
{ | ||
name: "first segment", | ||
time: "5", | ||
}, | ||
{ | ||
name: "second segment", | ||
time: "21", | ||
}, | ||
{ | ||
name: "final segment", | ||
time: "29", | ||
}, | ||
{ | ||
name: "out of bounds", | ||
time: "30", | ||
expectedStatus: http.StatusNotFound, | ||
}, | ||
{ | ||
name: "invalid time", | ||
time: "", | ||
expectedStatus: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "playbackID not found", | ||
time: "29", | ||
playbackID: "foo", | ||
expectedStatus: http.StatusNotFound, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
w := httptest.NewRecorder() | ||
req, err := http.NewRequest(http.MethodGet, "?time="+tt.time, nil) | ||
require.NoError(t, err) | ||
|
||
if tt.playbackID == "" { | ||
tt.playbackID = "fixtures" // just use the fixtures directory for testing | ||
} | ||
handler.Handle(w, req, []httprouter.Param{{ | ||
Key: "playbackID", | ||
Value: tt.playbackID, | ||
}}) | ||
resp := w.Result() | ||
if tt.expectedStatus == 0 { | ||
tt.expectedStatus = 200 | ||
} | ||
require.Equal(t, tt.expectedStatus, resp.StatusCode) | ||
|
||
if tt.expectedStatus != 200 { | ||
return | ||
} | ||
respBytes, err := io.ReadAll(resp.Body) | ||
require.NoError(t, err) | ||
|
||
outfile, err := os.CreateTemp(os.TempDir(), "out*.jpg") | ||
require.NoError(t, err) | ||
defer os.Remove(outfile.Name()) | ||
_, err = outfile.Write(respBytes) | ||
require.NoError(t, err) | ||
log.Println(outfile.Name()) | ||
probeData, err := ffprobe.ProbeURL(context.Background(), outfile.Name()) | ||
require.NoError(t, err) | ||
require.Equal(t, "image2", probeData.Format.FormatName) | ||
require.Len(t, probeData.Streams, 1) | ||
require.Greater(t, probeData.Streams[0].Width, 0) | ||
require.Greater(t, probeData.Streams[0].Height, 0) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#EXTM3U | ||
#EXT-X-VERSION:3 | ||
#EXT-X-STREAM-INF:PROGRAM-ID=0,BANDWIDTH=1036798,RESOLUTION=1280x720,NAME="0-720p0" | ||
tiny.m3u8 |