Class: AudioSwitch::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/audio_switch/model.rb

Constant Summary collapse

MODULE_RTP_SEND =
'module-rtp-send'.freeze
MODULE_NULL_SINK =
'module-null-sink'.freeze
RTP =
'rtp'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(pactl) ⇒ Model



7
8
9
# File 'lib/audio_switch/model.rb', line 7

def initialize(pactl)
  @pactl = pactl
end

Instance Method Details

#mute_sourcesObject



62
63
64
65
66
67
# File 'lib/audio_switch/model.rb', line 62

def mute_sources
  AudioSwitch::LOG.info 'muting all sources'
  sources.each do |source|
    @pactl.mute_source(source[:id])
  end
end

#rtp_offObject



56
57
58
59
60
# File 'lib/audio_switch/model.rb', line 56

def rtp_off
  AudioSwitch::LOG.info 'turning RTP off'
  @pactl.unload_module(MODULE_RTP_SEND)
  @pactl.unload_module(MODULE_NULL_SINK)
end

#rtp_onObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/audio_switch/model.rb', line 35

def rtp_on
  AudioSwitch::LOG.info 'turning RTP on'
  # prevent positive feedback loop
  mute_sources
  # see https://cgit.freedesktop.org/pulseaudio/paprefs/tree/src/paprefs.cc
  @pactl.load_module(
    MODULE_NULL_SINK,
    'sink_name' => 'rtp',
    'format' => 's16be',
    'channels' => '2',
    'rate' => '44100',
    'sink_properties' => {
      'device.description' => 'RTP Multicast',
      'device.bus' => 'network',
      'device.icon_name' => 'network-server'
    }
  )
  @pactl.load_module(MODULE_RTP_SEND, 'source' => 'rtp.monitor')
  select_sink(default_sink)
end

#rtp_on?Boolean



28
29
30
31
32
33
# File 'lib/audio_switch/model.rb', line 28

def rtp_on?
  return false unless @pactl.sinks.any? { |sink| sink[:name] == RTP }
  return false unless @pactl.modules.any? { |mod| mod[:name] == MODULE_RTP_SEND }

  true
end

#select_sink(sink_id) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/audio_switch/model.rb', line 16

def select_sink(sink_id)
  AudioSwitch::LOG.info "selecting sink '#{sink_id}'"
  @pactl.default_sink = sink_id
  @pactl.inputs.each do |input|
    @pactl.move_input(input[:id], sink_id)
  end
end

#sinksObject



24
25
26
# File 'lib/audio_switch/model.rb', line 24

def sinks
  @pactl.sinks
end

#sources_mute?Boolean



76
77
78
# File 'lib/audio_switch/model.rb', line 76

def sources_mute?
  sources.all? { |source| source[:mute] }
end

#unmute_sourcesObject



69
70
71
72
73
74
# File 'lib/audio_switch/model.rb', line 69

def unmute_sources
  AudioSwitch::LOG.info 'unmuting all sources'
  sources.each do |source|
    @pactl.unmute_source(source[:id])
  end
end

#watch(&block) ⇒ Object



11
12
13
14
# File 'lib/audio_switch/model.rb', line 11

def watch(&block)
  @pactl.subscribe { |event| handle(event, block) }
  yield
end