Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for running go-transcode on Windows #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/go-transcode
/bin
/.env
.vscode/
21 changes: 1 addition & 20 deletions hls/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (m *ManagerCtx) Start() error {
m.cmd.Stdout = write

// create a new process group
m.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
m.cmd.SysProcAttr = ConfigureAsProcessGroup()

m.active = false
m.lastRequest = time.Now()
Expand Down Expand Up @@ -195,25 +195,6 @@ func (m *ManagerCtx) Start() error {
return err
}

func (m *ManagerCtx) Stop() {
m.mu.Lock()
defer m.mu.Unlock()

if m.cmd != nil && m.cmd.Process != nil {
m.logger.Debug().Msg("performing stop")

pgid, err := syscall.Getpgid(m.cmd.Process.Pid)
if err == nil {
err := syscall.Kill(-pgid, syscall.SIGKILL)
m.logger.Err(err).Msg("killing process group")
} else {
m.logger.Err(err).Msg("could not get process group id")
err := m.cmd.Process.Kill()
m.logger.Err(err).Msg("killing process")
}
}
}

func (m *ManagerCtx) Cleanup() {
m.mu.Lock()
diff := time.Since(m.lastRequest)
Expand Down
29 changes: 29 additions & 0 deletions hls/processgroup_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !windows
// +build !windows

package hls

import "syscall"

func ConfigureAsProcessGroup() *syscall.SysProcAttr {
return &syscall.SysProcAttr{Setpgid: true}
}

func (m *ManagerCtx) Stop() {
m.mu.Lock()
defer m.mu.Unlock()

if m.cmd != nil && m.cmd.Process != nil {
m.logger.Debug().Msg("performing stop")

pgid, err := syscall.Getpgid(m.cmd.Process.Pid)
if err == nil {
err := syscall.Kill(-pgid, syscall.SIGKILL)
m.logger.Err(err).Msg("killing process group")
} else {
m.logger.Err(err).Msg("could not get process group id")
err := m.cmd.Process.Kill()
m.logger.Err(err).Msg("killing process")
}
}
}
41 changes: 41 additions & 0 deletions hls/processgroup_win.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//go:build windows
// +build windows

package hls

import (
"os"
"os/exec"
"strconv"
"syscall"
)

func ConfigureAsProcessGroup() *syscall.SysProcAttr {
return &syscall.SysProcAttr{CreationFlags: syscall.CREATE_NEW_PROCESS_GROUP}
}

func TaskkillWithChildrenWindows(cmd *exec.Cmd) error {
// Function adopted from: https://stackoverflow.com/a/44551450/6278
// Taskkill command documentation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/taskkill

kill := exec.Command("TASKKILL", "/T", "/PID", strconv.Itoa(cmd.Process.Pid))
kill.Stderr = os.Stderr
kill.Stdout = os.Stdout
return kill.Run()
}

func (m *ManagerCtx) Stop() {
m.mu.Lock()
defer m.mu.Unlock()

if m.cmd != nil && m.cmd.Process != nil {
m.logger.Debug().Msg("performing stop")

err := TaskkillWithChildrenWindows(m.cmd)
if err == nil {
m.logger.Debug().Msg("killing process group")
} else {
m.logger.Err(err).Msg("failed to kill process group")
}
}
}
2 changes: 1 addition & 1 deletion internal/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (a *ApiManagerCtx) Http(r chi.Router) {
// dummy input for testing purposes
file := a.config.AbsPath("profiles", "http-test.sh")
cmd := exec.Command(file)
logger.Info().Msg("command startred")
logger.Info().Msg("command started")

read, write := io.Pipe()
cmd.Stdout = write
Expand Down
4 changes: 2 additions & 2 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (a *ApiManagerCtx) ProfilePath(folder string, profile string) (string, erro
return "", fmt.Errorf("invalid profile path")
}

profilePath := path.Join(a.config.Profiles, folder, fmt.Sprintf("%s.sh", profile))
profilePath := path.Join(a.config.Profiles, folder, fmt.Sprintf("%s.cmd", profile))
if _, err := os.Stat(profilePath); os.IsNotExist(err) {
return "", err
}
Expand All @@ -90,6 +90,6 @@ func (a *ApiManagerCtx) transcodeStart(profilePath string, input string) (*exec.
return nil, fmt.Errorf("stream not found")
}

log.Info().Str("profilePath", profilePath).Str("url", url).Msg("command startred")
log.Info().Str("profilePath", profilePath).Str("url", url).Msg("command started")
return exec.Command(profilePath, url), nil
}