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

Returns a new instance of Model.



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

def initialize(pactl)
  @pactl = pactl
end

Instance Method Details

#mute_sourcesObject



59
60
61
62
63
64
# File 'lib/audio_switch/model.rb', line 59

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

#rtp_offObject



53
54
55
56
57
# File 'lib/audio_switch/model.rb', line 53

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

#rtp_onObject



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

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

Returns:

  • (Boolean)


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

def rtp_on?
  @pactl.sinks.any? { |sink| sink[:name] == RTP }
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

Returns:

  • (Boolean)


73
74
75
# File 'lib/audio_switch/model.rb', line 73

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

#unmute_sourcesObject



66
67
68
69
70
71
# File 'lib/audio_switch/model.rb', line 66

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