Class: Net::RAOP::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/raop/client.rb

Constant Summary collapse

VERSION =

The version of Net::RAOP::Client you’re using

'0.1.1'
@@cache =
"\0" * 16387

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host) ⇒ Client

Create a new Net::RAOP::Client to connect to host Airport Express



11
12
13
14
15
16
17
# File 'lib/raop/client.rb', line 11

def initialize(host)
  @host         = host
  @aes_crypt    = aes_cipher
  @rtsp_client  = nil
  @session_id   = nil
  @data_socket  = nil
end

Class Method Details

.encode_alac(bits) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/raop/client.rb', line 145

def encode_alac(bits)
  new_bits =
    bits.length == 16384 ?
    @@cache.dup : "\0" * (bits.length + 3)

  new_bits[0] = 32
  new_bits[2] = 2

  i = 0
  len = bits.length
  while i < len
    data = bits[i + 1]
    data1 = bits[i]

    new_bits[i + 2] |= data >> 7
    new_bits[i + 3] |= ((data & 0x7F) << 1) | (data1 >> 7)
    new_bits[i + 4] |= (data1 & 0x7F) << 1

    i += 2
  end
  new_bits
end

Instance Method Details

#connectObject

Connect to the Airport Express



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/raop/client.rb', line 21

def connect
  random_data = Array.new(28) { |x| rand(0xFF) }.pack('C*')

  sid = sprintf('%0#10d', random_data.slice!(0, 4).unpack('L').first)
  sci = sprintf('%0#18X', random_data.slice!(0, 8).unpack('Q').first)\
    .slice(2..-1)
  sac = [random_data].pack('m')

  key = [rsa_encrypt(@aes_key)].pack('m')
  iv  = [@aes_iv].pack('m')

  @rtsp_client = Net::RTSP.new(@host, sid, sci)

  announce = Net::RTSP::Announce.new(sac, key, iv)
  response = @rtsp_client.request(announce)

  # FIXME Check for audio cable hookup

  response = @rtsp_client.request(Net::RTSP::Setup.new)
  transport_info = {}
  response['transport'].split(';').each do |token|
    k, v = token.split('=', 2)
    transport_info[k] = v
  end
  @data_socket = TCPSocket.open(@host, transport_info['server_port'])
  @session_id = response['session']

  response = @rtsp_client.request(Net::RTSP::Record.new(@session_id))
  params = Net::RTSP::SetParameter.new(@session_id,
                                       { :volume => -30 }
                                      )
  response = @rtsp_client.request(params)
end

#disconnectObject

Disconnect from the Airport Express



76
77
78
# File 'lib/raop/client.rb', line 76

def disconnect
  @rtsp_client.request(Net::RTSP::Teardown.new)
end

#play(file) ⇒ Object

Stream file to the Airport Express



68
69
70
71
72
# File 'lib/raop/client.rb', line 68

def play(file)
  while data = file.read(4096 * 2 * 2)
    send_sample(self.class.encode_alac(data))
  end
end

#volume=(volume) ⇒ Object

Set the volume on the Airport Express. -144 is quiet, 0 is loud.

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
# File 'lib/raop/client.rb', line 57

def volume=(volume)
  volume = volume.abs
  raise ArgumentError if volume > 144
  params = Net::RTSP::SetParameter.new(@session_id,
                                       { :volume => "-#{volume}".to_i }
                                      )
  response = @rtsp_client.request(params)
end