Skip to content

Commit

Permalink
Add sound device when engine.sound is set
Browse files Browse the repository at this point in the history
  • Loading branch information
discordianfish committed Jun 6, 2024
1 parent 9973714 commit 46fc96e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
29 changes: 29 additions & 0 deletions pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -112,6 +113,34 @@ func (r *DockerRunner) Start(c *Container) (*ContainerStatus, error) {
Target: m.ContainerPath,
}
}
if c.Sound {
if runtime.GOOS == "windows" {
return nil, fmt.Errorf("sound is not supported on windows")
}
level.Debug(r.Logger).Log("msg", "enabling sound")
hostConfig.Devices = append(hostConfig.Devices, container.DeviceMapping{
PathOnHost: "/dev/snd",
PathInContainer: "/dev/snd",
CgroupPermissions: "rwm",
})

agid, err := getGID("/dev/snd/seq")
if err != nil {
return nil, err
}
hostConfig.GroupAdd = []string{fmt.Sprintf("%d", agid)}
hostConfig.Mounts = append(hostConfig.Mounts, mount.Mount{
Type: mount.TypeBind,
Source: "/etc/group",
Target: "/etc/group",
ReadOnly: true,
}, mount.Mount{
Type: mount.TypeBind,
Source: "/usr/share/alsa",
Target: "/usr/share/alsa",
ReadOnly: true,
})
}
level.Debug(r.Logger).Log("msg", "creating container", "config", fmt.Sprintf("%#v", config), "hostConfig", fmt.Sprintf("%#v", hostConfig))
dc, err := r.Client.ContainerCreate(ctx, config, hostConfig, nil, nil, "")
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions pkg/container/getgid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build !windows

package container

import (
"fmt"
"os"
"syscall"
)

func getGID(path string) (uint32, error) {
fi, err := os.Stat(path)
if err != nil {
return 0, fmt.Errorf("couldn't stat /dev/snd: %w", err)
}
return fi.Sys().(*syscall.Stat_t).Gid, nil
}
5 changes: 5 additions & 0 deletions pkg/container/getgid_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package container

func getGID(path string) (uint32, error) {
panic("GetGid is not implemented on windows")
}
1 change: 1 addition & 0 deletions pkg/container/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ type Container struct {
Hostname string
WorkingDir string
IPCMode string
Sound bool

// If true, the entrypoint of the image will be overridden. Only used for
// `diambra agent test`.
Expand Down
3 changes: 0 additions & 3 deletions pkg/diambra/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ func EnsureCredentials(logger log.Logger, credPath string) error {

if exists {
var err error
if err != nil {
return fmt.Errorf("couldn't create client: %w", err)
}
user, err := dc.User()
if err == nil {
level.Info(logger).Log("msg", "logged in", "user", user.Username)
Expand Down
2 changes: 2 additions & 0 deletions pkg/diambra/diambra.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func newEnvContainer(config *EnvConfig, envID, randomSeed int) (*container.Conta
container.NewBindMount(config.CredPath, "/tmp/.diambra/credentials"),
container.NewBindMount(config.RomsPath, "/opt/diambraArena/roms"),
},
Sound: args.Sound,
}
c.BindMounts = append(c.BindMounts, config.Mounts...)

Expand All @@ -276,6 +277,7 @@ func newEnvContainer(config *EnvConfig, envID, randomSeed int) (*container.Conta
if config.SeccompProfile != "" {
c.SecurityOpt = []string{"seccomp=" + config.SeccompProfile}
}

return c, nil
}

Expand Down

0 comments on commit 46fc96e

Please sign in to comment.