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

     1  package ari
     2  
     3  // Playback represents a communication path for interacting
     4  // with an Asterisk server for playback resources
     5  type Playback interface {
     6  
     7  	// Get gets the handle to the given playback ID
     8  	Get(key *Key) *PlaybackHandle
     9  
    10  	// Data gets the playback data
    11  	Data(key *Key) (*PlaybackData, error)
    12  
    13  	// Control performs the given operation on the current playback.  Available operations are:
    14  	//   - restart
    15  	//   - pause
    16  	//   - unpause
    17  	//   - reverse
    18  	//   - forward
    19  	Control(key *Key, op string) error
    20  
    21  	// Stop stops the playback
    22  	Stop(key *Key) error
    23  
    24  	// Subscribe subscribes on the playback events
    25  	Subscribe(key *Key, n ...string) Subscription
    26  }
    27  
    28  // A Player is an entity which can play an audio URI
    29  type Player interface {
    30  	// Play plays the audio using the given playback ID and media URI
    31  	Play(string, string) (*PlaybackHandle, error)
    32  
    33  	// StagePlay stages a `Play` operation
    34  	StagePlay(string, string) (*PlaybackHandle, error)
    35  
    36  	// Subscribe subscribes the player to events
    37  	Subscribe(n ...string) Subscription
    38  }
    39  
    40  // PlaybackData represents the state of a playback
    41  type PlaybackData struct {
    42  	// Key is the cluster-unique identifier for this playback
    43  	Key *Key `json:"key"`
    44  
    45  	ID        string `json:"id"` // Unique ID for this playback session
    46  	Language  string `json:"language,omitempty"`
    47  	MediaURI  string `json:"media_uri"`  // URI for the media which is to be played
    48  	State     string `json:"state"`      // State of the playback operation
    49  	TargetURI string `json:"target_uri"` // URI of the channel or bridge on which the media should be played (follows format of 'type':'name')
    50  }
    51  
    52  // PlaybackHandle is the handle for performing playback operations
    53  type PlaybackHandle struct {
    54  	key      *Key
    55  	p        Playback
    56  	exec     func(pb *PlaybackHandle) error
    57  	executed bool
    58  }
    59  
    60  // NewPlaybackHandle builds a handle to the playback id
    61  func NewPlaybackHandle(key *Key, pb Playback, exec func(pb *PlaybackHandle) error) *PlaybackHandle {
    62  	return &PlaybackHandle{
    63  		key:  key,
    64  		p:    pb,
    65  		exec: exec,
    66  	}
    67  }
    68  
    69  // ID returns the identifier for the playback
    70  func (ph *PlaybackHandle) ID() string {
    71  	return ph.key.ID
    72  }
    73  
    74  // Key returns the Key for the playback
    75  func (ph *PlaybackHandle) Key() *Key {
    76  	return ph.key
    77  }
    78  
    79  // Data gets the playback data
    80  func (ph *PlaybackHandle) Data() (*PlaybackData, error) {
    81  	return ph.p.Data(ph.key)
    82  }
    83  
    84  // Control performs the given operation
    85  func (ph *PlaybackHandle) Control(op string) error {
    86  	return ph.p.Control(ph.key, op)
    87  }
    88  
    89  // Stop stops the playback
    90  func (ph *PlaybackHandle) Stop() error {
    91  	return ph.p.Stop(ph.key)
    92  }
    93  
    94  // Subscribe subscribes the list of channel events
    95  func (ph *PlaybackHandle) Subscribe(n ...string) Subscription {
    96  	if ph == nil {
    97  		return nil
    98  	}
    99  	return ph.p.Subscribe(ph.key, n...)
   100  }
   101  
   102  // Exec executes any staged operations
   103  func (ph *PlaybackHandle) Exec() (err error) {
   104  	if !ph.executed {
   105  		ph.executed = true
   106  		if ph.exec != nil {
   107  			err = ph.exec(ph)
   108  			ph.exec = nil
   109  		}
   110  	}
   111  	return
   112  }