class AudioSwitch::Model

Constants

MODULE_NULL_SINK
MODULE_RTP_SEND
RTP

Public Class Methods

new(pactl) click to toggle source
# File lib/audio_switch/model.rb, line 7
def initialize(pactl)
  @pactl = pactl
end

Public Instance Methods

mute_sources() click to toggle source
# 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_off() click to toggle source
# 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_on() click to toggle source
# 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?() click to toggle source
# File lib/audio_switch/model.rb, line 28
def rtp_on?
  @pactl.sinks.any? { |sink| sink[:name] == RTP }
end
select_sink(sink_id) click to toggle source
# 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
sinks() click to toggle source
# File lib/audio_switch/model.rb, line 24
def sinks
  @pactl.sinks
end
sources_mute?() click to toggle source
# File lib/audio_switch/model.rb, line 73
def sources_mute?
  sources.all? { |source| source[:mute] }
end
unmute_sources() click to toggle source
# 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() { || ... } click to toggle source
# File lib/audio_switch/model.rb, line 11
def watch(&block)
  @pactl.subscribe { |event| handle(event, block) }
  yield
end

Private Instance Methods

default_sink() click to toggle source
# File lib/audio_switch/model.rb, line 88
def default_sink
  (@pactl.sinks.find { |sink| sink[:default] } || {})[:id]
end
handle(event, block) click to toggle source
# File lib/audio_switch/model.rb, line 83
def handle(event, block)
  block.call if event[:object] == :sink ||
                event[:object] == :source
end
sources() click to toggle source
# File lib/audio_switch/model.rb, line 79
def sources
  @pactl.sources.reject { |source| source[:name] == 'rtp.monitor' }
end