Skip to content

Commit

Permalink
feat: support to use pipewire
Browse files Browse the repository at this point in the history
audio moudule support to use pipewire

Log:
  • Loading branch information
dengbo11 committed Jul 18, 2023
1 parent 5ab3056 commit de0ebff
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
66 changes: 48 additions & 18 deletions audio1/audio.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
package audio

import (
"bytes"
"errors"
"fmt"
"math"
"os/exec"
"sort"
"strings"
"sync"
"time"

dbus "github.com/godbus/dbus/v5"
"github.com/linuxdeepin/dde-daemon/common/dsync"
systemd1 "github.com/linuxdeepin/go-dbus-factory/system/org.freedesktop.systemd1"
gio "github.com/linuxdeepin/go-gir/gio-2.0"
"github.com/linuxdeepin/go-lib/dbusutil"
"github.com/linuxdeepin/go-lib/dbusutil/gsprop"
Expand All @@ -43,8 +42,7 @@ const (
dbusPath = "/org/deepin/dde/Audio1"
dbusInterface = dbusServiceName

cmdSystemctl = "systemctl"
cmdPulseaudio = "pulseaudio"
dbusPulseaudioServer = "org.pulseaudio.Server"

increaseMaxVolume = 1.5
normalMaxVolume = 1.0
Expand Down Expand Up @@ -209,26 +207,58 @@ func newAudio(service *dbusutil.Service) *Audio {
return a
}

func startPulseaudio(count int) error {
var errBuf bytes.Buffer
cmd := exec.Command(cmdSystemctl, "--user", "start", "pulseaudio")
cmd.Stderr = &errBuf
err := cmd.Run()
if err != nil {
logger.Warningf("failed to start pulseaudio via systemd: err: %v, stderr: %s",
err, errBuf.Bytes())
func startAudioServer(count int, service *dbusutil.Service) error {
var serverPath dbus.ObjectPath
audioServers := []string{"pipewire.service", "pluseaudio.service"}

systemd := systemd1.NewManager(service.Conn())

for _, server := range audioServers {
path, err := systemd.GetUnit(0, server)
if err == nil {
serverPath = path
break
}
}
errBuf.Reset()

err = exec.Command(cmdPulseaudio, "--check").Run()
logger.Debug("ready to start audio server", serverPath)

if len(serverPath) != 0 {
serverSystemdUnit, err := systemd1.NewUnit(service.Conn(), serverPath)
if err != nil {
logger.Warning("failed to create audio server systemd unit", err)
return err
}

state, err := serverSystemdUnit.Unit().ActiveState().Get(0)
if err != nil {
logger.Warning("failed to get audio server active state", err)
return err
}

if state != "active" {
go func() {
_, err := serverSystemdUnit.Unit().Start(0, "replace")
if err != nil {
logger.Warning("failed to start audio server unit:", err)
}
}()
}
}

has, err := service.NameHasOwner(dbusPulseaudioServer)
if err != nil {
logger.Warning("pulseaudio check error", err)
logger.Warningf("failed to get dbus pulseaudio server owner, err: %v", err)
}

if !has {
if count > 0 {
logger.Info("retry start pulseaudio after 500ms")
logger.Debug("retry start audio server after 500ms")

time.Sleep(500 * time.Millisecond)
return startPulseaudio(count - 1)
return startAudioServer(count-1, service)
}
return errors.New("failed to start pulseaudio")
return errors.New("failed to start audio server")
}

return nil
Expand Down
5 changes: 3 additions & 2 deletions audio1/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ func (*Module) GetDependencies() []string {
}

func (m *Module) start() error {
err := startPulseaudio(5) // 为了保证蓝牙模块依赖audio模块,并且audio模块启动pulseaudio完成.
service := loader.GetService()

err := startAudioServer(5, service) // 为了保证蓝牙模块依赖audio模块,并且audio模块启动pulseaudio完成.
if err != nil {
err = xerrors.Errorf("failed to start pulseaudio: %w", err)
logger.Warning(err)
return err
}

service := loader.GetService()
m.audio = newAudio(service)
err = m.audio.init()
if err != nil {
Expand Down

0 comments on commit de0ebff

Please sign in to comment.