Advertisement
SycoLTH

bridge-dial.js

Oct 23rd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*jshint node:true*/
  2. 'use strict';
  3.  
  4. var ari = require('ari-client');
  5. var util = require('util');
  6.  
  7. // ensure endpoint was passed in to script
  8. if (!process.argv[2]) {
  9.   console.error('usage: node bridge-dial.js endpoint');
  10.   process.exit(1);
  11. }
  12.  
  13. ari.connect('http://172.18.0.3:8088', 'asterisk', 'asterisk', clientLoaded);
  14.  
  15. // handler for client being loaded
  16. function clientLoaded (err, client) {
  17.   if (err) {
  18.     throw err;
  19.   }
  20.  
  21.   // handler for StasisStart event
  22.   function stasisStart(event, channel) {
  23.     // ensure the channel is not a dialed channel
  24.     var dialed = event.args[0] === 'dialed';
  25.  
  26.     if (!dialed) {
  27.       channel.answer(function(err) {
  28.         if (err) {
  29.           throw err;
  30.         }
  31.  
  32.         console.log('Channel %s has entered our application', channel.name);
  33.  
  34.         var playback = client.Playback();
  35.         channel.play({media: 'sound:pls-wait-connect-call'},
  36.           playback, function(err, playback) {
  37.             if (err) {
  38.               throw err;
  39.             }
  40.         });
  41.  
  42.         originate(channel);
  43.       });
  44.     } else {
  45.       console.log('Channel %s has entered our application', channel.name);
  46.       console.log('Channel %s has been originated by originator ???', channel.name);
  47.     }
  48.   }
  49.  
  50.   function originate(channel) {
  51.     var dialed = client.Channel();
  52.  
  53.     channel.on('StasisEnd', function(event, channel) {
  54.       hangupDialed(channel, dialed);
  55.     });
  56.  
  57.     dialed.on('ChannelDestroyed', function(event, dialed) {
  58.       hangupOriginal(channel, dialed);
  59.     });
  60.  
  61.     dialed.on('StasisStart', function(event, dialed) {
  62.       joinMixingBridge(channel, dialed);
  63.     });
  64.  
  65.     console.log(channel.id + ' is about to originate a new channel');
  66.     dialed.originate(
  67.       {endpoint: process.argv[2], app: 'bridge-dial', appArgs: 'dialed', originator: channel.id},
  68.       function(err, dialed) {
  69.         if (err) {
  70.           throw err;
  71.         }
  72.     });
  73.   }
  74.  
  75.   // handler for original channel hanging up so we can gracefully hangup the
  76.   // other end
  77.   function hangupDialed(channel, dialed) {
  78.     console.log(
  79.       'Channel %s left our application, hanging up dialed channel %s',
  80.       channel.name, dialed.name);
  81.  
  82.     // hangup the other end
  83.     dialed.hangup(function(err) {
  84.       // ignore error since dialed channel could have hung up, causing the
  85.       // original channel to exit Stasis
  86.     });
  87.   }
  88.  
  89.   // handler for the dialed channel hanging up so we can gracefully hangup the
  90.   // other end
  91.   function hangupOriginal(channel, dialed) {
  92.     console.log('Dialed channel %s has been hung up, hanging up channel %s',
  93.       dialed.name, channel.name);
  94.  
  95.     // hangup the other end
  96.     channel.hangup(function(err) {
  97.       // ignore error since original channel could have hung up, causing the
  98.       // dialed channel to exit Stasis
  99.     });
  100.   }
  101.  
  102.   // handler for dialed channel entering Stasis
  103.   function joinMixingBridge(channel, dialed) {
  104.     var bridge = client.Bridge();
  105.  
  106.     dialed.on('StasisEnd', function(event, dialed) {
  107.       dialedExit(dialed, bridge);
  108.     });
  109.  
  110.     dialed.answer(function(err) {
  111.       if (err) {
  112.         throw err;
  113.       }
  114.     });
  115.  
  116.     bridge.create({type: 'mixing'}, function(err, bridge) {
  117.       if (err) {
  118.         throw err;
  119.       }
  120.  
  121.       console.log('Created bridge %s', bridge.id);
  122.  
  123.       addChannelsToBridge(channel, dialed, bridge);
  124.     });
  125.   }
  126.  
  127.   // handler for the dialed channel leaving Stasis
  128.   function dialedExit(dialed, bridge) {
  129.     console.log(
  130.         'Dialed channel %s has left our application, destroying bridge %s',
  131.         dialed.name, bridge.id);
  132.  
  133.     bridge.destroy(function(err) {
  134.       if (err) {
  135.         throw err;
  136.       }
  137.     });
  138.   }
  139.  
  140.   // handler for new mixing bridge ready for channels to be added to it
  141.   function addChannelsToBridge(channel, dialed, bridge) {
  142.     console.log('Adding channel %s and dialed channel %s to bridge %s',
  143.         channel.name, dialed.name, bridge.id);
  144.  
  145.     bridge.addChannel({channel: [channel.id, dialed.id]}, function(err) {
  146.       if (err) {
  147.         throw err;
  148.       }
  149.     });
  150.   }
  151.  
  152.   client.on('StasisStart', stasisStart);
  153.  
  154.   client.start('bridge-dial');
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement