Commit d06e424d90
Unsigned
Layout: unified · split
internal/player.go +57 −5
| @@ -5,6 +5,8 @@ import ( | |||
| 5 | "fmt" | 5 | "fmt" |
| 6 | "os/exec" | 6 | "os/exec" |
| 7 | "encoding/json" | 7 | "encoding/json" |
| 8 | "net" | ||
| 9 | "time" | ||
| 8 | "os" | 10 | "os" |
| 9 | ) | 11 | ) |
| 10 | var logFile = "debug.log" | 12 | var logFile = "debug.log" |
| @@ -15,17 +17,67 @@ func StartMpv(videoURL, socketPath string) error { | |||
| 15 | return cmd.Start() | 17 | return cmd.Start() |
| 16 | } | 18 | } |
| 17 | 19 | ||
| 20 | // sendIpcMessage sends a JSON message to the mpv IPC socket and reads the response. | ||
| 21 | func sendIpcMessage(socketPath string, msg map[string]interface{}) (map[string]interface{}, error) { | ||
| 22 | // Create a Unix socket connection | ||
| 23 | conn, err := net.Dial("unix", socketPath) | ||
| 24 | if err != nil { | ||
| 25 | return nil, fmt.Errorf("failed to connect to IPC socket: %w", err) | ||
| 26 | } | ||
| 27 | defer conn.Close() | ||
| 28 | |||
| 29 | // Marshal the message into JSON | ||
| 30 | data, err := json.Marshal(msg) | ||
| 31 | if err != nil { | ||
| 32 | return nil, fmt.Errorf("failed to marshal JSON message: %w", err) | ||
| 33 | } | ||
| 34 | |||
| 35 | // Write the message to the socket | ||
| 36 | _, err = conn.Write(append(data, '\n')) // Ensure newline at the end | ||
| 37 | if err != nil { | ||
| 38 | return nil, fmt.Errorf("failed to send message to IPC socket: %w", err) | ||
| 39 | } | ||
| 40 | |||
| 41 | // Set a read deadline to prevent blocking forever | ||
| 42 | conn.SetReadDeadline(time.Now().Add(2 * time.Second)) | ||
| 43 | |||
| 44 | // Read the response from the socket | ||
| 45 | var response map[string]interface{} | ||
| 46 | decoder := json.NewDecoder(conn) | ||
| 47 | if err := decoder.Decode(&response); err != nil { | ||
| 48 | return nil, fmt.Errorf("failed to decode response: %w", err) | ||
| 49 | } | ||
| 50 | |||
| 51 | return response, nil | ||
| 52 | } | ||
| 53 | |||
| 18 | // GetMpvProperty retrieves a property from mpv via IPC. | 54 | // GetMpvProperty retrieves a property from mpv via IPC. |
| 19 | func GetMpvProperty(socketPath, property string) (string, error) { | 55 | func GetMpvProperty(socketPath, property string) (string, error) { |
| 20 | // Prepare the command to read from the socket | 56 | // Prepare the JSON message to request the property |
| 21 | cmd := exec.Command("mpv", "--input-ipc-server="+socketPath, "get_property", property) | 57 | msg := map[string]interface{}{ |
| 22 | output, err := cmd.Output() | 58 | "command": []interface{}{"get_property", property}, |
| 59 | } | ||
| 60 | |||
| 61 | // Send the message via IPC and get the response | ||
| 62 | response, err := sendIpcMessage(socketPath, msg) | ||
| 23 | if err != nil { | 63 | if err != nil { |
| 24 | return "", fmt.Errorf("failed to get property '%s': %w", property, err) | 64 | return "", fmt.Errorf("failed to send IPC message: %w", err) |
| 25 | } | 65 | } |
| 26 | return string(output), nil | 66 | |
| 67 | // Check the response for errors and extract the value | ||
| 68 | if errVal, ok := response["error"]; ok && errVal != "success" { | ||
| 69 | return "", fmt.Errorf("mpv returned an error: %v", errVal) | ||
| 70 | } | ||
| 71 | |||
| 72 | // Retrieve the value from the response | ||
| 73 | if data, ok := response["data"]; ok { | ||
| 74 | return fmt.Sprintf("%v", data), nil | ||
| 75 | } | ||
| 76 | |||
| 77 | return "", fmt.Errorf("no data in response") | ||
| 27 | } | 78 | } |
| 28 | 79 | ||
| 80 | |||
| 29 | // SendMpvCommand sends a command to mpv via IPC. | 81 | // SendMpvCommand sends a command to mpv via IPC. |
| 30 | func SendMpvCommand(socketPath, command string, args ...string) error { | 82 | func SendMpvCommand(socketPath, command string, args ...string) error { |
| 31 | msg := map[string]interface{}{ | 83 | msg := map[string]interface{}{ |