github.com/CyCoreSystems/ari@v4.8.4+incompatible/_examples/play/main.go (about) 1 package main 2 3 import ( 4 "context" 5 6 "github.com/inconshreveable/log15" 7 8 "github.com/CyCoreSystems/ari" 9 "github.com/CyCoreSystems/ari/client/native" 10 "github.com/CyCoreSystems/ari/ext/play" 11 ) 12 13 var log = log15.New() 14 15 func main() { 16 ctx, cancel := context.WithCancel(context.Background()) 17 defer cancel() 18 19 // connect 20 native.Logger = log 21 22 log.Info("Connecting to ARI") 23 cl, err := native.Connect(&native.Options{ 24 Application: "test", 25 Username: "admin", 26 Password: "admin", 27 URL: "http://localhost:8088/ari", 28 WebsocketURL: "ws://localhost:8088/ari/events", 29 }) 30 if err != nil { 31 log.Error("Failed to build ARI client", "error", err) 32 return 33 } 34 35 // setup app 36 37 log.Info("Listening for new calls") 38 sub := cl.Bus().Subscribe(nil, "StasisStart") 39 40 for { 41 select { 42 case e := <-sub.Events(): 43 v := e.(*ari.StasisStart) 44 log.Info("Got stasis start", "channel", v.Channel.ID) 45 go app(ctx, cl.Channel().Get(v.Key(ari.ChannelKey, v.Channel.ID))) 46 case <-ctx.Done(): 47 return 48 } 49 } 50 } 51 52 func app(ctx context.Context, h *ari.ChannelHandle) { 53 defer h.Hangup() 54 55 ctx, cancel := context.WithCancel(ctx) 56 defer cancel() 57 58 log.Info("Running app", "channel", h.ID()) 59 60 end := h.Subscribe(ari.Events.StasisEnd) 61 defer end.Cancel() 62 63 // End the app when the channel goes away 64 go func() { 65 <-end.Events() 66 cancel() 67 }() 68 69 if err := h.Answer(); err != nil { 70 log.Error("failed to answer call", "error", err) 71 return 72 } 73 74 if err := play.Play(ctx, h, play.URI("sound:tt-monkeys")).Err(); err != nil { 75 log.Error("failed to play sound", "error", err) 76 return 77 } 78 79 log.Info("completed playback") 80 return 81 82 }