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 25f140e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
26 changes: 26 additions & 0 deletions pkg/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os"
"strconv"
"strings"
"syscall"
"time"

"github.com/docker/docker/api/types"
Expand Down Expand Up @@ -112,6 +113,31 @@ func (r *DockerRunner) Start(c *Container) (*ContainerStatus, error) {
Target: m.ContainerPath,
}
}
if c.Sound {
level.Debug(r.Logger).Log("msg", "enabling sound")
hostConfig.Devices = append(hostConfig.Devices, container.DeviceMapping{
PathOnHost: "/dev/snd",
PathInContainer: "/dev/snd",
CgroupPermissions: "rwm",
})
// // /dev/snd gid
fi, err := os.Stat("/dev/snd/seq")
if err != nil {
return nil, fmt.Errorf("couldn't stat /dev/snd: %w", err)
}
hostConfig.GroupAdd = []string{fmt.Sprintf("%d", fi.Sys().(*syscall.Stat_t).Gid)}

Check failure on line 128 in pkg/container/docker.go

View workflow job for this annotation

GitHub Actions / build (windows, amd64)

undefined: syscall.Stat_t

Check failure on line 128 in pkg/container/docker.go

View workflow job for this annotation

GitHub Actions / build (windows, aarch64)

undefined: syscall.Stat_t
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
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 25f140e

Please sign in to comment.