using System;
using System.Collections.Generic;
using System.Linq;
using PepperDash.Essentials.Core;
using PepperDash.Essentials.Devices.Common.Codec;
namespace PepperDash.Essentials.Devices.Common.AudioCodec;
///
/// Base class for audio codecs. Provides common properties and methods for audio codecs,
/// as well as a common implementation of IDialerCallStatus to allow the AudioCodecBaseMessenger
/// to get call status information without requiring the full AudioCodecBase class.
/// This is useful for devices that have dialer call status information but do not need to implement
/// the full AudioCodecBase class.
///
public abstract class AudioCodecBase : EssentialsDevice, IDialerCallStatus, IUsageTracking, IAudioCodecInfo
{
///
public event EventHandler CallStatusChange;
///
public AudioCodecInfo CodecInfo { get; protected set; }
#region IUsageTracking Members
///
/// This object can be added by outside users of this class to provide usage tracking
/// for various services
///
public UsageTracking UsageTracker { get; set; }
#endregion
///
/// Returns true when any call is not in state Unknown, Disconnecting, Disconnected
///
public bool IsInCall
{
get
{
bool value;
if (ActiveCalls != null)
value = ActiveCalls.Any(c => c.IsActiveCall);
else
value = false;
return value;
}
}
// In most cases only a single call can be active
///
public List ActiveCalls { get; set; }
///
/// Constructor
///
///
///
public AudioCodecBase(string key, string name)
: base(key, name)
{
ActiveCalls = new List();
}
///
/// Helper method to fire CallStatusChange event with old and new status
///
protected void SetNewCallStatusAndFireCallStatusChange(eCodecCallStatus newStatus, CodecActiveCallItem call)
{
call.Status = newStatus;
OnCallStatusChange(call);
}
///
///
///
///
///
///
protected void OnCallStatusChange(CodecActiveCallItem item)
{
var handler = CallStatusChange;
if (handler != null)
handler(this, new CodecCallStatusItemChangeEventArgs(item));
if (UsageTracker != null)
{
if (IsInCall && !UsageTracker.UsageTrackingStarted)
UsageTracker.StartDeviceUsage();
else if (UsageTracker.UsageTrackingStarted && !IsInCall)
UsageTracker.EndDeviceUsage();
}
}
#region IHasDialer Members
///
public abstract void Dial(string number);
///
public abstract void EndCall(CodecActiveCallItem activeCall);
///
public abstract void EndAllCalls();
///
public abstract void AcceptCall(CodecActiveCallItem item);
public abstract void RejectCall(CodecActiveCallItem item);
public abstract void SendDtmf(string digit);
#endregion
}