github.com/CyCoreSystems/ari@v4.8.4+incompatible/bridge.go (about)

     1  package ari
     2  
     3  // Bridge represents a communication path to an
     4  // Asterisk server for working with bridge resources
     5  type Bridge interface {
     6  
     7  	// Create creates a bridge
     8  	Create(key *Key, btype string, name string) (*BridgeHandle, error)
     9  
    10  	// StageCreate creates a new bridge handle, staged with a bridge `Create` operation.
    11  	StageCreate(key *Key, btype string, name string) (*BridgeHandle, error)
    12  
    13  	// Get gets the BridgeHandle
    14  	Get(key *Key) *BridgeHandle
    15  
    16  	// Lists returns the lists of bridges in asterisk, optionally using the key for filtering.
    17  	List(*Key) ([]*Key, error)
    18  
    19  	// Data gets the bridge data
    20  	Data(key *Key) (*BridgeData, error)
    21  
    22  	// AddChannel adds a channel to the bridge
    23  	AddChannel(key *Key, channelID string) error
    24  
    25  	// RemoveChannel removes a channel from the bridge
    26  	RemoveChannel(key *Key, channelID string) error
    27  
    28  	// Delete deletes the bridge
    29  	Delete(key *Key) error
    30  
    31  	// MOH plays music on hold
    32  	MOH(key *Key, moh string) error
    33  
    34  	// StopMOH stops music on hold
    35  	StopMOH(key *Key) error
    36  
    37  	// Play plays the media URI to the bridge
    38  	Play(key *Key, playbackID string, mediaURI string) (*PlaybackHandle, error)
    39  
    40  	// StagePlay stages a `Play` operation and returns the `PlaybackHandle`
    41  	// for invoking it.
    42  	StagePlay(key *Key, playbackID string, mediaURI string) (*PlaybackHandle, error)
    43  
    44  	// Record records the bridge
    45  	Record(key *Key, name string, opts *RecordingOptions) (*LiveRecordingHandle, error)
    46  
    47  	// StageRecord stages a `Record` operation and returns the `PlaybackHandle`
    48  	// for invoking it.
    49  	StageRecord(key *Key, name string, opts *RecordingOptions) (*LiveRecordingHandle, error)
    50  
    51  	// Subscribe subscribes the given bridge events events
    52  	Subscribe(key *Key, n ...string) Subscription
    53  }
    54  
    55  // BridgeData describes an Asterisk Bridge, the entity which merges media from
    56  // one or more channels into a common audio output
    57  type BridgeData struct {
    58  	// Key is the cluster-unique identifier for this bridge
    59  	Key *Key `json:"key"`
    60  
    61  	ID         string   `json:"id"`           // Unique Id for this bridge
    62  	Class      string   `json:"bridge_class"` // Class of the bridge
    63  	Type       string   `json:"bridge_type"`  // Type of bridge (mixing, holding, dtmf_events, proxy_media)
    64  	ChannelIDs []string `json:"channels"`     // List of pariticipating channel ids
    65  	Creator    string   `json:"creator"`      // Creating entity of the bridge
    66  	Name       string   `json:"name"`         // The name of the bridge
    67  	Technology string   `json:"technology"`   // Name of the bridging technology
    68  }
    69  
    70  // Channels returns the list of channels found in the bridge
    71  func (b *BridgeData) Channels() (list []*Key) {
    72  	for _, id := range b.ChannelIDs {
    73  		list = append(list, b.Key.New(ChannelKey, id))
    74  	}
    75  	return
    76  }
    77  
    78  // NewBridgeHandle creates a new bridge handle
    79  func NewBridgeHandle(key *Key, b Bridge, exec func(bh *BridgeHandle) error) *BridgeHandle {
    80  	return &BridgeHandle{
    81  		key:  key,
    82  		b:    b,
    83  		exec: exec,
    84  	}
    85  }
    86  
    87  // BridgeHandle is the handle to a bridge for performing operations
    88  type BridgeHandle struct {
    89  	key      *Key
    90  	b        Bridge
    91  	exec     func(bh *BridgeHandle) error
    92  	executed bool
    93  }
    94  
    95  // ID returns the identifier for the bridge
    96  func (bh *BridgeHandle) ID() string {
    97  	return bh.key.ID
    98  }
    99  
   100  // Key returns the Key of the bridge
   101  func (bh *BridgeHandle) Key() *Key {
   102  	return bh.key
   103  }
   104  
   105  // Exec executes any staged operations attached on the bridge handle
   106  func (bh *BridgeHandle) Exec() error {
   107  	if !bh.executed {
   108  		bh.executed = true
   109  		if bh.exec != nil {
   110  			err := bh.exec(bh)
   111  			bh.exec = nil
   112  			return err
   113  		}
   114  	}
   115  	return nil
   116  }
   117  
   118  // AddChannel adds a channel to the bridge
   119  func (bh *BridgeHandle) AddChannel(channelID string) error {
   120  	return bh.b.AddChannel(bh.key, channelID)
   121  }
   122  
   123  // RemoveChannel removes a channel from the bridge
   124  func (bh *BridgeHandle) RemoveChannel(channelID string) error {
   125  	return bh.b.RemoveChannel(bh.key, channelID)
   126  }
   127  
   128  // Delete deletes the bridge
   129  func (bh *BridgeHandle) Delete() (err error) {
   130  	err = bh.b.Delete(bh.key)
   131  	return
   132  }
   133  
   134  // Data gets the bridge data
   135  func (bh *BridgeHandle) Data() (*BridgeData, error) {
   136  	return bh.b.Data(bh.key)
   137  }
   138  
   139  // MOH requests that the given MusicOnHold class being played to the bridge
   140  func (bh *BridgeHandle) MOH(class string) error {
   141  	return bh.b.MOH(bh.key, class)
   142  }
   143  
   144  // StopMOH requests that any MusicOnHold which is being played to the bridge is stopped.
   145  func (bh *BridgeHandle) StopMOH() error {
   146  	return bh.b.StopMOH(bh.key)
   147  }
   148  
   149  // Play initiates playback of the specified media uri
   150  // to the bridge, returning the Playback handle
   151  func (bh *BridgeHandle) Play(id string, mediaURI string) (*PlaybackHandle, error) {
   152  	return bh.b.Play(bh.key, id, mediaURI)
   153  }
   154  
   155  // StagePlay stages a `Play` operation.
   156  func (bh *BridgeHandle) StagePlay(id string, mediaURI string) (*PlaybackHandle, error) {
   157  	return bh.b.StagePlay(bh.key, id, mediaURI)
   158  }
   159  
   160  // Record records the bridge to the given filename
   161  func (bh *BridgeHandle) Record(name string, opts *RecordingOptions) (*LiveRecordingHandle, error) {
   162  	return bh.b.Record(bh.key, name, opts)
   163  }
   164  
   165  // StageRecord stages a `Record` operation
   166  func (bh *BridgeHandle) StageRecord(name string, opts *RecordingOptions) (*LiveRecordingHandle, error) {
   167  	return bh.b.StageRecord(bh.key, name, opts)
   168  }
   169  
   170  // Subscribe creates a subscription to the list of events
   171  func (bh *BridgeHandle) Subscribe(n ...string) Subscription {
   172  	if bh == nil {
   173  		return nil
   174  	}
   175  	return bh.b.Subscribe(bh.key, n...)
   176  }