| @@ -9,7 +9,9 @@ import ( |
| 9 | "os/exec" |
9 | "os/exec" |
| 10 | "path/filepath" |
10 | "path/filepath" |
| 11 | "runtime" |
11 | "runtime" |
| |
12 | "strings" |
| 12 | ) |
13 | ) |
| |
14 | |
| 13 | var logFile = "debug.log" |
15 | var logFile = "debug.log" |
| 14 | |
16 | |
| 15 | func getMPVPath() (string, error) { |
17 | func getMPVPath() (string, error) { |
| @@ -22,12 +24,36 @@ func getMPVPath() (string, error) { |
| 22 | return mpvPath, nil |
24 | return mpvPath, nil |
| 23 | } |
25 | } |
| 24 | |
26 | |
| 25 | func StartVideo(link string, args []string, title string) (string, error) { |
27 | func StartVideo(link string, args []string, title string, anime *Anime) (string, error) { |
| 26 | var command *exec.Cmd |
28 | var command *exec.Cmd |
| |
29 | var mpvSocketPath string |
| |
30 | var err error |
| |
31 | |
| |
32 | // Check if we have an existing socket and if MPV is still running |
| |
33 | if anime.Ep.Player.SocketPath != "" && IsMPVRunning(anime.Ep.Player.SocketPath) { |
| |
34 | // Reuse existing socket |
| |
35 | mpvSocketPath = anime.Ep.Player.SocketPath |
| |
36 | |
| |
37 | // Load the new file in the existing MPV instance |
| |
38 | command := []interface{}{"loadfile", link} |
| |
39 | _, err = MPVSendCommand(mpvSocketPath, command) |
| |
40 | if err != nil { |
| |
41 | return "", fmt.Errorf("failed to load file in existing MPV instance: %w", err) |
| |
42 | } |
| |
43 | |
| |
44 | // Set the new title |
| |
45 | _, err = MPVSendCommand(mpvSocketPath, []interface{}{"set_property", "force-media-title", title}) |
| |
46 | if err != nil { |
| |
47 | Log(fmt.Sprintf("Failed to set title: %v", err), logFile) |
| |
48 | } |
| 27 | |
49 | |
| |
50 | return mpvSocketPath, nil |
| |
51 | } |
| |
52 | |
| |
53 | if anime.Ep.Player.SocketPath == "" { |
| 28 | // Generate a random number for the socket path |
54 | // Generate a random number for the socket path |
| 29 | randomBytes := make([]byte, 4) |
55 | randomBytes := make([]byte, 4) |
| 30 | _, err := rand.Read(randomBytes) |
56 | _, err = rand.Read(randomBytes) |
| 31 | if err != nil { |
57 | if err != nil { |
| 32 | Log("Failed to generate random number", logFile) |
58 | Log("Failed to generate random number", logFile) |
| 33 | return "", fmt.Errorf("failed to generate random number: %w", err) |
59 | return "", fmt.Errorf("failed to generate random number: %w", err) |
| @@ -36,15 +62,20 @@ func StartVideo(link string, args []string, title string) (string, error) { |
| 36 | randomNumber := fmt.Sprintf("%x", randomBytes) |
62 | randomNumber := fmt.Sprintf("%x", randomBytes) |
| 37 | |
63 | |
| 38 | // Create the mpv socket path with the random number |
64 | // Create the mpv socket path with the random number |
| 39 | var mpvSocketPath string |
| |
| 40 | if runtime.GOOS == "windows" { |
65 | if runtime.GOOS == "windows" { |
| 41 | mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber) |
66 | mpvSocketPath = fmt.Sprintf(`\\.\pipe\curd_mpvsocket_%s`, randomNumber) |
| 42 | } else { |
67 | } else { |
| 43 | mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber) |
68 | mpvSocketPath = fmt.Sprintf("/tmp/curd_mpvsocket_%s", randomNumber) |
| |
69 | } |
| |
70 | } else { |
| |
71 | mpvSocketPath = anime.Ep.Player.SocketPath |
| 44 | } |
72 | } |
| 45 | |
73 | |
| 46 | // Add the title to MPV arguments |
74 | // Add the title to MPV arguments |
| 47 | titleArgs := []string{fmt.Sprintf("--title=%s", title),fmt.Sprintf("--force-media-title=%s", title)} |
75 | titleArgs := []string{fmt.Sprintf("--title=%s", title), fmt.Sprintf("--force-media-title=%s", title)} |
| |
76 | |
| |
77 | // Keep the window open after episode completes, new episode starts in the same mpv window |
| |
78 | args = append(args, "--force-window=yes", "--idle=yes") |
| 48 | args = append(args, titleArgs...) |
79 | args = append(args, titleArgs...) |
| 49 | |
80 | |
| 50 | // Prepare arguments for mpv |
81 | // Prepare arguments for mpv |
| @@ -52,7 +83,8 @@ func StartVideo(link string, args []string, title string) (string, error) { |
| 52 | mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link) |
83 | mpvArgs = append(mpvArgs, "--no-terminal", "--really-quiet", fmt.Sprintf("--input-ipc-server=%s", mpvSocketPath), link) |
| 53 | // Add any additional arguments passed |
84 | // Add any additional arguments passed |
| 54 | if len(args) > 0 { |
85 | if len(args) > 0 { |
| 55 | mpvArgs = append(mpvArgs, args...) } |
86 | mpvArgs = append(mpvArgs, args...) |
| |
87 | } |
| 56 | |
88 | |
| 57 | if runtime.GOOS == "windows" { |
89 | if runtime.GOOS == "windows" { |
| 58 | // Get the path to mpv.exe for Windows |
90 | // Get the path to mpv.exe for Windows |
| @@ -192,3 +224,41 @@ func PercentageWatched(playbackTime int, duration int) float64 { |
| 192 | } |
224 | } |
| 193 | return float64(0) |
225 | return float64(0) |
| 194 | } |
226 | } |
| |
227 | |
| |
228 | func HasActivePlayback(ipcSocketPath string) (bool, error) { |
| |
229 | // Get the time-pos property from MPV |
| |
230 | timePos, err := MPVSendCommand(ipcSocketPath, []interface{}{"get_property", "time-pos"}) |
| |
231 | if err != nil { |
| |
232 | // Check specifically for "property unavailable" error |
| |
233 | if strings.Contains(err.Error(), "property unavailable") { |
| |
234 | // This indicates nothing is playing |
| |
235 | return false, nil |
| |
236 | } |
| |
237 | return false, fmt.Errorf("error getting time-pos: %w", err) |
| |
238 | } |
| |
239 | |
| |
240 | // If we got a valid response, something is playing |
| |
241 | if timePos != nil { |
| |
242 | return true, nil |
| |
243 | } |
| |
244 | |
| |
245 | // Default to false if we can't determine the status |
| |
246 | return false, nil |
| |
247 | } |
| |
248 | |
| |
249 | func IsMPVRunning(socketPath string) bool { |
| |
250 | if socketPath == "" { |
| |
251 | return false |
| |
252 | } |
| |
253 | |
| |
254 | // Try to connect to the socket |
| |
255 | conn, err := connectToPipe(socketPath) |
| |
256 | if err != nil { |
| |
257 | return false |
| |
258 | } |
| |
259 | defer conn.Close() |
| |
260 | |
| |
261 | // Send a simple command to check if MPV responds |
| |
262 | _, err = MPVSendCommand(socketPath, []interface{}{"get_property", "pid"}) |
| |
263 | return err == nil |
| |
264 | } |