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

     1  package ari
     2  
     3  import "sync"
     4  
     5  // LiveRecording represents a communication path interacting with an Asterisk
     6  // server for live recording resources
     7  type LiveRecording interface {
     8  
     9  	// Get gets the Recording by type
    10  	Get(key *Key) *LiveRecordingHandle
    11  
    12  	// Data gets the data for the live recording
    13  	Data(key *Key) (*LiveRecordingData, error)
    14  
    15  	// Stop stops the live recording
    16  	Stop(key *Key) error
    17  
    18  	// Pause pauses the live recording
    19  	Pause(key *Key) error
    20  
    21  	// Resume resumes the live recording
    22  	Resume(key *Key) error
    23  
    24  	// Mute mutes the live recording
    25  	Mute(key *Key) error
    26  
    27  	// Unmute unmutes the live recording
    28  	Unmute(key *Key) error
    29  
    30  	// Scrap Stops and deletes the current LiveRecording
    31  	Scrap(key *Key) error
    32  
    33  	// Stored returns the StoredRecording handle for this LiveRecording
    34  	Stored(key *Key) *StoredRecordingHandle
    35  
    36  	// Subscribe subscribes to events
    37  	Subscribe(key *Key, n ...string) Subscription
    38  }
    39  
    40  // LiveRecordingData is the data for a live recording
    41  type LiveRecordingData struct {
    42  	// Key is the cluster-unique identifier for this live recording
    43  	Key *Key `json:"key"`
    44  
    45  	Cause     string      `json:"cause,omitempty"`            // If failed, the cause of the failure
    46  	Duration  DurationSec `json:"duration,omitempty"`         // Length of recording in seconds
    47  	Format    string      `json:"format"`                     // Format of recording (wav, gsm, etc)
    48  	Name      string      `json:"name"`                       // (base) name for the recording
    49  	Silence   DurationSec `json:"silence_duration,omitempty"` // If silence was detected in the recording, the duration in seconds of that silence (requires that maxSilenceSeconds be non-zero)
    50  	State     string      `json:"state"`                      // Current state of the recording
    51  	Talking   DurationSec `json:"talking_duration,omitempty"` // Duration of talking, in seconds, that has been detected in the recording (requires that maxSilenceSeconds be non-zero)
    52  	TargetURI string      `json:"target_uri"`                 // URI for the channel or bridge which is being recorded (TODO: figure out format for this)
    53  }
    54  
    55  // ID returns the identifier of the live recording
    56  func (s *LiveRecordingData) ID() string {
    57  	return s.Name
    58  }
    59  
    60  // NewLiveRecordingHandle creates a new live recording handle
    61  func NewLiveRecordingHandle(key *Key, r LiveRecording, exec func(*LiveRecordingHandle) (err error)) *LiveRecordingHandle {
    62  	return &LiveRecordingHandle{
    63  		key:  key,
    64  		r:    r,
    65  		exec: exec,
    66  	}
    67  }
    68  
    69  // A LiveRecordingHandle is a reference to a live recording that can be operated on
    70  type LiveRecordingHandle struct {
    71  	key      *Key
    72  	r        LiveRecording
    73  	exec     func(*LiveRecordingHandle) (err error)
    74  	executed bool
    75  
    76  	mu sync.Mutex
    77  }
    78  
    79  // ID returns the identifier of the live recording
    80  func (h *LiveRecordingHandle) ID() string {
    81  	return h.key.ID
    82  }
    83  
    84  // Key returns the key of the live recording
    85  func (h *LiveRecordingHandle) Key() *Key {
    86  	return h.key
    87  }
    88  
    89  // Data gets the data for the live recording
    90  func (h *LiveRecordingHandle) Data() (*LiveRecordingData, error) {
    91  	return h.r.Data(h.key)
    92  }
    93  
    94  // Stop stops and saves the recording
    95  func (h *LiveRecordingHandle) Stop() error {
    96  	return h.r.Stop(h.key)
    97  }
    98  
    99  // Scrap stops and deletes the recording
   100  func (h *LiveRecordingHandle) Scrap() error {
   101  	return h.r.Scrap(h.key)
   102  }
   103  
   104  // Resume resumes the recording
   105  func (h *LiveRecordingHandle) Resume() error {
   106  	return h.r.Resume(h.key)
   107  }
   108  
   109  // Pause pauses the recording
   110  func (h *LiveRecordingHandle) Pause() error {
   111  	return h.r.Pause(h.key)
   112  }
   113  
   114  // Mute mutes the recording
   115  func (h *LiveRecordingHandle) Mute() error {
   116  	return h.r.Mute(h.key)
   117  }
   118  
   119  // Unmute mutes the recording
   120  func (h *LiveRecordingHandle) Unmute() error {
   121  	return h.r.Unmute(h.key)
   122  }
   123  
   124  // Stored returns the StoredRecordingHandle for this LiveRecordingHandle
   125  func (h *LiveRecordingHandle) Stored() *StoredRecordingHandle {
   126  	return h.r.Stored(h.key)
   127  }
   128  
   129  // Exec executes any staged operations attached to the `LiveRecordingHandle`
   130  func (h *LiveRecordingHandle) Exec() (err error) {
   131  	h.mu.Lock()
   132  
   133  	if !h.executed {
   134  		h.executed = true
   135  		if h.exec != nil {
   136  			err = h.exec(h)
   137  			h.exec = nil
   138  		}
   139  	}
   140  
   141  	h.mu.Unlock()
   142  	return
   143  }
   144  
   145  // Subscribe subscribes the recording handle's underlying recorder to
   146  // the provided event types.
   147  func (h *LiveRecordingHandle) Subscribe(n ...string) Subscription {
   148  	return h.r.Subscribe(h.key, n...)
   149  }