summaryrefslogtreecommitdiff
path: root/player-server/internal/service/playback_hints_test.go
blob: 9d15402c5b21a1ba6e414cb21b320386244fe849 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package service

import (
	"testing"

	"codeberg.org/snonux/player/internal/model"
)

func TestContainerFromPath(t *testing.T) {
	tests := []struct {
		filename string
		want     string
	}{
		{"movie.mp4", "mp4"},
		{"audio.FLAC", "flac"},
		{"video.MKV", "mkv"},
		{"no-ext", ""},
		{"archive.tar.gz", "gz"},
		{"doc.PDF", "pdf"},
	}
	for _, tt := range tests {
		t.Run(tt.filename, func(t *testing.T) {
			got := containerFromPath(tt.filename)
			if got != tt.want {
				t.Errorf("containerFromPath(%q) = %q, want %q", tt.filename, got, tt.want)
			}
		})
	}
}

func TestSplitCodecs(t *testing.T) {
	tests := []struct {
		codec     string
		wantVideo string
		wantAudio string
	}{
		{"h264/aac", "h264", "aac"},
		{"h264 / aac", "h264", "aac"},
		{"h264", "h264", ""},
		{"", "", ""},
		{"vp9/opus", "vp9", "opus"},
	}
	for _, tt := range tests {
		t.Run(tt.codec, func(t *testing.T) {
			v, a := splitCodecs(tt.codec)
			if v != tt.wantVideo || a != tt.wantAudio {
				t.Errorf("splitCodecs(%q) = (%q, %q), want (%q, %q)", tt.codec, v, a, tt.wantVideo, tt.wantAudio)
			}
		})
	}
}

func TestNeedsTranscode(t *testing.T) {
	tests := []struct {
		name       string
		container  string
		videoCodec string
		audioCodec string
		want       bool
	}{
		// mp4 + h264 + aac — fully native
		{"mp4 h264 aac", "mp4", "h264", "aac", false},
		// mkv container — always transcode
		{"mkv", "mkv", "h264", "aac", true},
		// webm + vp9 + opus — native
		{"webm vp9 opus", "webm", "vp9", "opus", false},
		// mp4 + exotic video codec
		{"mp4 wmv", "mp4", "wmv", "aac", true},
		// mp4 + flac audio
		{"mp4 flac", "mp4", "h264", "flac", true},
		// audio-only mp3
		{"mp3 no video", "mp3", "", "mp3", false},
		// unknown container
		{"avi", "avi", "xvid", "mp3", true},
		// m4a audio
		{"m4a", "m4a", "", "aac", false},
		// hevc is listed as native (Apple)
		{"mp4 hevc", "mp4", "hevc", "aac", false},
		// av1 is native
		{"webm av1", "webm", "av1", "opus", false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := needsTranscode(tt.container, tt.videoCodec, tt.audioCodec)
			if got != tt.want {
				t.Errorf("needsTranscode(%q, %q, %q) = %v, want %v",
					tt.container, tt.videoCodec, tt.audioCodec, got, tt.want)
			}
		})
	}
}

func TestBuildPlaybackHint(t *testing.T) {
	media := &model.Media{
		ID:            42,
		FileName:      "sample.mp4",
		Codec:         "h264/aac",
		Duration:      123.5,
		FileSizeBytes: 9876543,
		Width:         1920,
		Height:        1080,
		Bitrate:       4000000,
	}

	hint := buildPlaybackHint(media)

	if hint.StreamURL != "/api/v1/media/42/stream" {
		t.Errorf("unexpected StreamURL: %s", hint.StreamURL)
	}
	if hint.Container != "mp4" {
		t.Errorf("unexpected Container: %s", hint.Container)
	}
	if hint.VideoCodec != "h264" {
		t.Errorf("unexpected VideoCodec: %s", hint.VideoCodec)
	}
	if hint.AudioCodec != "aac" {
		t.Errorf("unexpected AudioCodec: %s", hint.AudioCodec)
	}
	if hint.DurationSeconds != 123.5 {
		t.Errorf("unexpected DurationSeconds: %f", hint.DurationSeconds)
	}
	if hint.FileSizeBytes != 9876543 {
		t.Errorf("unexpected FileSizeBytes: %d", hint.FileSizeBytes)
	}
	if hint.Width != 1920 {
		t.Errorf("unexpected Width: %d", hint.Width)
	}
	if hint.Height != 1080 {
		t.Errorf("unexpected Height: %d", hint.Height)
	}
	if hint.Bitrate != 4000000 {
		t.Errorf("unexpected Bitrate: %d", hint.Bitrate)
	}
	if hint.NeedsTranscode {
		t.Errorf("expected NeedsTranscode=false for mp4/h264/aac")
	}
}

func TestBuildPlaybackHint_MKV(t *testing.T) {
	media := &model.Media{
		ID:       7,
		FileName: "film.mkv",
		Codec:    "h264/ac3",
	}
	hint := buildPlaybackHint(media)
	if !hint.NeedsTranscode {
		t.Errorf("expected NeedsTranscode=true for mkv/ac3")
	}
	if hint.Container != "mkv" {
		t.Errorf("expected container mkv, got %s", hint.Container)
	}
}