Created
June 15, 2010 20:38
-
-
Save cloudvoxcode/439697 to your computer and use it in GitHub Desktop.
Asterisk music on hold in any conference room
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# play music on hold (MP3 or other) as a second conference "participant", | |
# then disconnect (stop the music) when a real second participant joins. | |
# | |
# NOTE: this is a reference example. It hasn't been tested and | |
# may not run exactly as-is. | |
class ConferenceDirectory | |
def initialize | |
@rooms = {} | |
end | |
def join_room(room, call) | |
@rooms[room] ||= ConferenceRoom.new(room) | |
@rooms[room].join(call) | |
end | |
end | |
class ConferenceRoom | |
def initialize(name) | |
@name = name | |
@participants = [] | |
end | |
def join(call) | |
# this method will be called twice: once with music_on_room defined, | |
# where we'll be playing music, and once without it, where | |
# we'll be joining the conference. | |
if call.call.variables[:music_for_room] | |
if call.call.channel == @music_generation_channel | |
loop do | |
call.playback 'pretty-music' | |
end | |
else | |
@music_playback_channel = call.call.channel | |
call.execute 'Conference', @name | |
call.hangup | |
end | |
else | |
@participants << call | |
call.execute 'Conference', @name | |
manage_music | |
end | |
end | |
def part(call) | |
@participants.delete(call) | |
manage_music | |
end | |
def manage_music | |
# only play as long as we're keeping someone company | |
if @participants.length == 1 | |
start_music | |
else | |
stop_music | |
end | |
end | |
def start_music(call) | |
# place a call to this AGI (channel parameter), and when it answers, make the call get | |
# handled by this AGI (extension, context, priority, and variable). | |
# this AGI will get hit twice -- once with a "music_for_room" variable, and once | |
# without. could use 2 AGIs if you wanted: one that joins the room, and one that | |
# plays a music loop. | |
response = manager.originate :channel => "Local/#{call.extension}@#{call.context}", | |
:extension => call.extension, :context => call.context, :priority => 1, | |
'Variable' => "music_for_room=#{@name}" | |
@music_generation_channel = response['Channel'] | |
end | |
def stop_music | |
if @music_playback_channel | |
manager.hangup @music_playback_channel | |
end | |
if @music_generation_channel | |
manager.hangup @music_generation_channel | |
end | |
end | |
private | |
def manager | |
Adhearsion::VoIP::Asterisk.manager_interface | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment