3 Commits
Author SHA1 Message Date
Claude Code cf19a057c5 feat(media): forward the audio copy format to CarrierNode
The stream proxy passes format=mp3 through, so an audio file browsers cannot play (WMA) can be served
as an MP3 copy that CarrierNode makes once and keeps.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-21 15:14:35 +01:00
Claude Code 5745995a34 feat(web-ui): pass the media error to MediaPlayer onError
onError now receives the element's MediaError, so an app can tell an unsupported source (code 4,
MEDIA_ERR_SRC_NOT_SUPPORTED, as with AVI, WMV or ASF) from other failures. Handlers that take no
argument are unaffected.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
2026-11-21 15:14:35 +01:00
Gabriel20xx 170c7570e4 test(media): verify question marks in stored paths are escaped 2026-11-21 12:10:44 +01:00
5 changed files with 62 additions and 5 deletions

No files matched your search

+3 -2
View File
@@ -140,10 +140,11 @@ func (c *Service) streamMediaIdentifier(w http.ResponseWriter, r *http.Request,
// streamUpstreamPath is the CarrierNode request path for a stream. CarrierNode answers with a live // streamUpstreamPath is the CarrierNode request path for a stream. CarrierNode answers with a live
// H.264 copy when the query carries quality (a height) and start (seconds), which is how browsers get // H.264 copy when the query carries quality (a height) and start (seconds), which is how browsers get
// containers they cannot decode. Those two parameters are the only ones forwarded. // containers they cannot decode, and with an MP3 copy of an audio file when it carries format=mp3.
// Those parameters are the only ones forwarded.
func streamUpstreamPath(upstream string, query url.Values) string { func streamUpstreamPath(upstream string, query url.Values) string {
forward := url.Values{} forward := url.Values{}
for _, key := range []string{"quality", "start"} { for _, key := range []string{"quality", "start", "format"} {
if value := strings.TrimSpace(query.Get(key)); value != "" { if value := strings.TrimSpace(query.Get(key)); value != "" {
forward.Set(key, value) forward.Set(key, value)
} }
+1
View File
@@ -136,6 +136,7 @@ func TestStreamUpstreamPathForwardsOnlyLiveTranscodeOptions(t *testing.T) {
{"blank options are dropped", url.Values{"quality": {" "}, "start": {""}}, "/media/Videos/clip.mp4"}, {"blank options are dropped", url.Values{"quality": {" "}, "start": {""}}, "/media/Videos/clip.mp4"},
{"quality and start are forwarded", url.Values{"mediaKey": {"abc"}, "quality": {"1080"}, "start": {" 12.5 "}}, "/media/Videos/clip.mp4?quality=1080&start=12.5"}, {"quality and start are forwarded", url.Values{"mediaKey": {"abc"}, "quality": {"1080"}, "start": {" 12.5 "}}, "/media/Videos/clip.mp4?quality=1080&start=12.5"},
{"quality alone", url.Values{"quality": {"720"}}, "/media/Videos/clip.mp4?quality=720"}, {"quality alone", url.Values{"quality": {"720"}}, "/media/Videos/clip.mp4?quality=720"},
{"format is forwarded for audio copies", url.Values{"mediaKey": {"abc"}, "format": {"mp3"}}, "/media/Videos/clip.mp4?format=mp3"},
} { } {
if got := streamUpstreamPath("Videos/clip.mp4", tc.query); got != tc.want { if got := streamUpstreamPath("Videos/clip.mp4", tc.query); got != tc.want {
t.Errorf("%s: streamUpstreamPath() = %q, want %q", tc.name, got, tc.want) t.Errorf("%s: streamUpstreamPath() = %q, want %q", tc.name, got, tc.want)
+13 -1
View File
@@ -217,7 +217,7 @@ func TestMediaStreamAndThumbnailAgainstPostgres(t *testing.T) {
t.Run("unsafe stored paths and unknown keys are not found, and never reach CarrierNode", func(t *testing.T) { t.Run("unsafe stored paths and unknown keys are not found, and never reach CarrierNode", func(t *testing.T) {
before := len(h.carrier.seen()) before := len(h.carrier.seen())
for _, key := range []string{"rt-escape", "rt-query", "rt-missing", ""} { for _, key := range []string{"rt-escape", "rt-missing", ""} {
if code := h.do(http.MethodGet, "/media/stream?mediaKey="+key, "alice-token", "").Code; code != http.StatusNotFound { if code := h.do(http.MethodGet, "/media/stream?mediaKey="+key, "alice-token", "").Code; code != http.StatusNotFound {
t.Errorf("stream of %q status = %d, want 404", key, code) t.Errorf("stream of %q status = %d, want 404", key, code)
} }
@@ -227,6 +227,18 @@ func TestMediaStreamAndThumbnailAgainstPostgres(t *testing.T) {
} }
}) })
t.Run("question marks in stored paths are escaped as filename characters", func(t *testing.T) {
before := len(h.carrier.seen())
response := h.do(http.MethodGet, "/media/stream?mediaKey=rt-query", "alice-token", "")
if response.Code != http.StatusOK || response.Body.String() != "BYTES" {
t.Fatalf("stream of rt-query = %d %q, want the proxied bytes", response.Code, response.Body.String())
}
got := h.carrier.seen()
if len(got) != before+1 || got[len(got)-1] != "GET /media/a%3Fb.mp4" {
t.Fatalf("CarrierNode requests after streaming rt-query = %v, want one GET of /media/a%%3Fb.mp4", got[before:])
}
})
t.Run("hidden media is visible to moderators only", func(t *testing.T) { t.Run("hidden media is visible to moderators only", func(t *testing.T) {
for _, target := range []string{"/media/stream?mediaKey=rt-hidden", "/media/thumb?mediaKey=rt-hidden"} { for _, target := range []string{"/media/stream?mediaKey=rt-hidden", "/media/thumb?mediaKey=rt-hidden"} {
if code := h.do(http.MethodGet, target, "alice-token", "").Code; code != http.StatusNotFound { if code := h.do(http.MethodGet, target, "alice-token", "").Code; code != http.StatusNotFound {
@@ -97,7 +97,12 @@
playbackRate?: number | null playbackRate?: number | null
controlsDisabled?: boolean controlsDisabled?: boolean
speedDisabled?: boolean speedDisabled?: boolean
onError?: () => void /**
* Called when the element fails. `error.code` 4 (MEDIA_ERR_SRC_NOT_SUPPORTED) means this browser
* cannot demux or decode the source, as with AVI, WMV or ASF: the app then needs a browser-playable
* copy of the file (a server transcode) rather than the original.
*/
onError?: (error: MediaError | null) => void
onPlay?: (event: Event) => void onPlay?: (event: Event) => void
onPause?: (event: Event) => void onPause?: (event: Event) => void
onTimeUpdate?: (event: Event) => void onTimeUpdate?: (event: Event) => void
@@ -403,7 +408,7 @@
} }
function handleError() { function handleError() {
onError?.() onError?.(mediaElement?.error ?? null)
} }
function seek(value: number) { function seek(value: number) {
+38
View File
@@ -111,6 +111,44 @@ describe('MediaPlayer speed', () => {
}); });
}); });
describe('MediaPlayer errors', () => {
// MEDIA_ERR_SRC_NOT_SUPPORTED: the browser cannot demux or decode the source.
const MEDIA_ERR_SRC_NOT_SUPPORTED = 4;
it('passes the element error to onError so an unsupported source can be told apart', async () => {
const onError = vi.fn();
render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip', onError } });
const element = videoElement();
Object.defineProperty(element, 'error', { configurable: true, value: { code: MEDIA_ERR_SRC_NOT_SUPPORTED } });
await fireEvent.error(element);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toMatchObject({ code: MEDIA_ERR_SRC_NOT_SUPPORTED });
});
it('passes null when the element reports no error object', async () => {
const onError = vi.fn();
render(MediaPlayer, { props: { kind: 'video', src: SOURCE, title: 'Clip', onError } });
await fireEvent.error(videoElement());
expect(onError).toHaveBeenCalledWith(null);
});
it('reports audio sources the same way as video', async () => {
const onError = vi.fn();
render(MediaPlayer, { props: { kind: 'audio', src: 'https://media.example/track.wma', title: 'Track', onError } });
const element = document.querySelector<HTMLAudioElement>('audio');
if (!element) throw new Error('no audio element');
Object.defineProperty(element, 'error', { configurable: true, value: { code: MEDIA_ERR_SRC_NOT_SUPPORTED } });
await fireEvent.error(element);
expect(onError.mock.calls[0][0]).toMatchObject({ code: MEDIA_ERR_SRC_NOT_SUPPORTED });
});
});
describe('MediaPlayer without controls', () => { describe('MediaPlayer without controls', () => {
it('renders the element with no player controls and leaves its src to the caller', () => { it('renders the element with no player controls and leaves its src to the caller', () => {
render(MediaPlayer, { props: { controls: false, kind: 'video' } }); render(MediaPlayer, { props: { controls: false, kind: 'video' } });