mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-27 12:50:09 +00:00
Bug 1129882 - create agent in telephony object. r=baku, r=hsinyi
--HG-- extra : transplant_source : %FA%40F5A%AF%93%BCl%D2%18%FB%0E%3F%26%ED%91%19%5DE
This commit is contained in:
parent
af94c6a345
commit
b5daced1c2
@ -62,8 +62,11 @@ public:
|
||||
};
|
||||
|
||||
Telephony::Telephony(nsPIDOMWindow* aOwner)
|
||||
: DOMEventTargetHelper(aOwner)
|
||||
: DOMEventTargetHelper(aOwner),
|
||||
mAudioAgentNotify(nsIAudioChannelAgent::AUDIO_AGENT_NOTIFY),
|
||||
mIsAudioStartPlaying(false)
|
||||
{
|
||||
MOZ_ASSERT(aOwner);
|
||||
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aOwner);
|
||||
MOZ_ASSERT(global);
|
||||
|
||||
@ -518,6 +521,73 @@ Telephony::StopTone(const Optional<uint32_t>& aServiceId, ErrorResult& aRv)
|
||||
aRv = mService->StopTone(serviceId);
|
||||
}
|
||||
|
||||
void
|
||||
Telephony::OwnAudioChannel(ErrorResult& aRv)
|
||||
{
|
||||
if (mAudioAgent) {
|
||||
return;
|
||||
}
|
||||
|
||||
mAudioAgent = do_CreateInstance("@mozilla.org/audiochannelagent;1");
|
||||
MOZ_ASSERT(mAudioAgent);
|
||||
aRv = mAudioAgent->Init(GetParentObject(),
|
||||
(int32_t)AudioChannel::Telephony, this);
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return;
|
||||
}
|
||||
aRv = HandleAudioAgentState();
|
||||
if (NS_WARN_IF(aRv.Failed())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
nsresult
|
||||
Telephony::HandleAudioAgentState()
|
||||
{
|
||||
if (!mAudioAgent) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
Nullable<OwningTelephonyCallOrTelephonyCallGroup> activeCall;
|
||||
GetActive(activeCall);
|
||||
nsresult rv;
|
||||
// Only stop the agent when there's no call.
|
||||
if ((!mCalls.Length() && !mGroup->CallsArray().Length()) &&
|
||||
mIsAudioStartPlaying) {
|
||||
mIsAudioStartPlaying = false;
|
||||
rv = mAudioAgent->NotifyStoppedPlaying(mAudioAgentNotify);
|
||||
mAudioAgent = nullptr;
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
} else if (!activeCall.IsNull() && !mIsAudioStartPlaying) {
|
||||
mIsAudioStartPlaying = true;
|
||||
float volume;
|
||||
bool muted;
|
||||
rv = mAudioAgent->NotifyStartedPlaying(mAudioAgentNotify, &volume, &muted);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
|
||||
// In B2G, the system app manages audio playback policy. If there is a new
|
||||
// sound want to be playback, it must wait for the permission from the
|
||||
// system app. It means that the sound would be muted first, and then be
|
||||
// unmuted. For telephony, the behaviors are hold() first, then resume().
|
||||
// However, the telephony service can't handle all these requests within a
|
||||
// short period. The telephony service would reject our resume request,
|
||||
// because the modem have not changed the call state yet. It causes that
|
||||
// the telephony can't be resumed. Therefore, we don't mute the telephony
|
||||
// at the beginning.
|
||||
volume = 1.0;
|
||||
muted = false;
|
||||
rv = WindowVolumeChanged(volume, muted);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
bool
|
||||
Telephony::GetMuted(ErrorResult& aRv) const
|
||||
{
|
||||
@ -591,13 +661,61 @@ Telephony::GetReady(ErrorResult& aRv) const
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
// nsIAudioChannelAgentCallback
|
||||
|
||||
NS_IMETHODIMP
|
||||
Telephony::WindowVolumeChanged(float aVolume, bool aMuted)
|
||||
{
|
||||
// It's impossible to put all the calls on-hold in the multi-call case.
|
||||
if (mCalls.Length() > 1 ||
|
||||
(mCalls.Length() == 1 && mGroup->CallsArray().Length())) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
ErrorResult rv;
|
||||
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetOwner());
|
||||
nsRefPtr<Promise> promise = Promise::Create(global, rv);
|
||||
if (NS_WARN_IF(rv.Failed())) {
|
||||
return rv.StealNSResult();
|
||||
}
|
||||
|
||||
bool isSingleCall = mCalls.Length();
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
if (isSingleCall) {
|
||||
rv = aMuted ? mCalls[0]->Hold(callback) : mCalls[0]->Resume(callback);
|
||||
} else {
|
||||
rv = aMuted ? mGroup->Hold(callback) : mGroup->Resume(callback);
|
||||
}
|
||||
if (NS_WARN_IF(rv.Failed())) {
|
||||
return rv.StealNSResult();
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
Telephony::WindowAudioCaptureChanged()
|
||||
{
|
||||
// Do nothing, it's useless for the telephony object.
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// nsITelephonyListener
|
||||
|
||||
NS_IMETHODIMP
|
||||
Telephony::CallStateChanged(uint32_t aLength, nsITelephonyCallInfo** aAllInfo)
|
||||
{
|
||||
nsresult rv;
|
||||
for (uint32_t i = 0; i < aLength; ++i) {
|
||||
HandleCallInfo(aAllInfo[i]);
|
||||
rv = HandleCallInfo(aAllInfo[i]);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
rv = HandleAudioAgentState();
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return rv;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
@ -633,6 +751,7 @@ Telephony::EnumerateCallStateComplete()
|
||||
mGroup->ChangeState(callState);
|
||||
}
|
||||
|
||||
HandleAudioAgentState();
|
||||
if (mReadyPromise) {
|
||||
mReadyPromise->MaybeResolve(JS::UndefinedHandleValue);
|
||||
}
|
||||
|
@ -7,6 +7,8 @@
|
||||
#ifndef mozilla_dom_telephony_telephony_h__
|
||||
#define mozilla_dom_telephony_telephony_h__
|
||||
|
||||
#include "AudioChannelService.h"
|
||||
|
||||
#include "mozilla/dom/BindingDeclarations.h"
|
||||
#include "mozilla/dom/Promise.h"
|
||||
#include "mozilla/dom/telephony/TelephonyCommon.h"
|
||||
@ -31,6 +33,7 @@ class TelephonyDialCallback;
|
||||
class OwningTelephonyCallOrTelephonyCallGroup;
|
||||
|
||||
class Telephony final : public DOMEventTargetHelper,
|
||||
public nsIAudioChannelAgentCallback,
|
||||
private nsITelephonyListener
|
||||
{
|
||||
/**
|
||||
@ -44,6 +47,8 @@ class Telephony final : public DOMEventTargetHelper,
|
||||
|
||||
friend class telephony::TelephonyDialCallback;
|
||||
|
||||
// The audio agent is needed to communicate with the audio channel service.
|
||||
nsCOMPtr<nsIAudioChannelAgent> mAudioAgent;
|
||||
nsCOMPtr<nsITelephonyService> mService;
|
||||
nsRefPtr<Listener> mListener;
|
||||
|
||||
@ -54,8 +59,12 @@ class Telephony final : public DOMEventTargetHelper,
|
||||
|
||||
nsRefPtr<Promise> mReadyPromise;
|
||||
|
||||
uint32_t mAudioAgentNotify;
|
||||
bool mIsAudioStartPlaying;
|
||||
|
||||
public:
|
||||
NS_DECL_ISUPPORTS_INHERITED
|
||||
NS_DECL_NSIAUDIOCHANNELAGENTCALLBACK
|
||||
NS_DECL_NSITELEPHONYLISTENER
|
||||
NS_REALLY_FORWARD_NSIDOMEVENTTARGET(DOMEventTargetHelper)
|
||||
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(Telephony,
|
||||
@ -94,6 +103,15 @@ public:
|
||||
void
|
||||
StopTone(const Optional<uint32_t>& aServiceId, ErrorResult& aRv);
|
||||
|
||||
// In the audio channel architecture, the system app needs to know the state
|
||||
// of every audio channel, including the telephony. Therefore, when a
|
||||
// telephony call is activated , the audio channel service would notify the
|
||||
// system app about that. And we need an agent to communicate with the audio
|
||||
// channel service. We would follow the call states to make a correct
|
||||
// notification.
|
||||
void
|
||||
OwnAudioChannel(ErrorResult& aRv);
|
||||
|
||||
bool
|
||||
GetMuted(ErrorResult& aRv) const;
|
||||
|
||||
@ -213,6 +231,10 @@ private:
|
||||
|
||||
nsresult
|
||||
HandleCallInfo(nsITelephonyCallInfo* aInfo);
|
||||
|
||||
// Check the call states to decide whether need to send the notificaiton.
|
||||
nsresult
|
||||
HandleAudioAgentState();
|
||||
};
|
||||
|
||||
} // namespace dom
|
||||
|
@ -324,34 +324,11 @@ TelephonyCall::Hold(ErrorResult& aRv)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_CONNECTED) {
|
||||
NS_WARNING(nsPrintfCString("Hold non-connected call is rejected!"
|
||||
" (State: %u)", mCallState).get());
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
if (mGroup) {
|
||||
NS_WARNING("Hold a call in conference is rejected!");
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
if (!mSwitchable) {
|
||||
NS_WARNING("Hold a non-switchable call is rejected!");
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = mTelephony->Service()->HoldCall(mServiceId, mCallIndex, callback);
|
||||
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
||||
|
||||
if (mSecondId) {
|
||||
// No state transition when we switch two numbers within one TelephonyCall
|
||||
// object. Otherwise, the state here will be inconsistent with the backend
|
||||
// RIL and will never be right.
|
||||
return promise.forget();
|
||||
aRv = Hold(callback);
|
||||
if (NS_WARN_IF(aRv.Failed() &&
|
||||
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return promise.forget();
|
||||
@ -365,28 +342,78 @@ TelephonyCall::Resume(ErrorResult& aRv)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_HELD) {
|
||||
NS_WARNING(nsPrintfCString("Resume non-held call is rejected!"
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = Resume(callback);
|
||||
if (NS_WARN_IF(aRv.Failed() &&
|
||||
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
nsresult
|
||||
TelephonyCall::Hold(nsITelephonyCallback* aCallback)
|
||||
{
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_CONNECTED) {
|
||||
NS_WARNING(nsPrintfCString("Hold non-connected call is rejected!"
|
||||
" (State: %u)", mCallState).get());
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
if (mGroup) {
|
||||
NS_WARNING("Hold a call in conference is rejected!");
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
if (!mSwitchable) {
|
||||
NS_WARNING("Hold a non-switchable call is rejected!");
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
nsresult rv = mTelephony->Service()->HoldCall(mServiceId, mCallIndex, aCallback);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
if (mSecondId) {
|
||||
// No state transition when we switch two numbers within one TelephonyCall
|
||||
// object. Otherwise, the state here will be inconsistent with the backend
|
||||
// RIL and will never be right.
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
TelephonyCall::Resume(nsITelephonyCallback* aCallback)
|
||||
{
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_HELD) {
|
||||
NS_WARNING("Resume non-held call is rejected!");
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
if (mGroup) {
|
||||
NS_WARNING("Resume a call in conference is rejected!");
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
if (!mSwitchable) {
|
||||
NS_WARNING("Resume a non-switchable call is rejected!");
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = mTelephony->Service()->ResumeCall(mServiceId, mCallIndex, callback);
|
||||
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
||||
nsresult rv = mTelephony->Service()->ResumeCall(mServiceId, mCallIndex, aCallback);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
return promise.forget();
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
@ -13,6 +13,8 @@
|
||||
#include "mozilla/dom/TelephonyCallId.h"
|
||||
#include "mozilla/dom/telephony/TelephonyCommon.h"
|
||||
|
||||
#include "nsITelephonyService.h"
|
||||
|
||||
class nsPIDOMWindow;
|
||||
|
||||
namespace mozilla {
|
||||
@ -185,6 +187,12 @@ private:
|
||||
|
||||
~TelephonyCall();
|
||||
|
||||
nsresult
|
||||
Hold(nsITelephonyCallback* aCallback);
|
||||
|
||||
nsresult
|
||||
Resume(nsITelephonyCallback* aCallback);
|
||||
|
||||
void
|
||||
ChangeStateInternal(uint16_t aCallState, bool aFireEvents);
|
||||
|
||||
|
@ -347,16 +347,13 @@ TelephonyCallGroup::Hold(ErrorResult& aRv)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_CONNECTED) {
|
||||
NS_WARNING("Holding a non-connected call is rejected!");
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = Hold(callback);
|
||||
if (NS_WARN_IF(aRv.Failed() &&
|
||||
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = mTelephony->Service()->HoldConference(mCalls[0]->ServiceId(),
|
||||
callback);
|
||||
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
@ -370,15 +367,48 @@ TelephonyCallGroup::Resume(ErrorResult& aRv)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_HELD) {
|
||||
NS_WARNING("Resuming a non-held call is rejected!");
|
||||
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
||||
return promise.forget();
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = Resume(callback);
|
||||
if (NS_WARN_IF(aRv.Failed() &&
|
||||
!aRv.ErrorCodeIs(NS_ERROR_DOM_INVALID_STATE_ERR))) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
nsCOMPtr<nsITelephonyCallback> callback = new TelephonyCallback(promise);
|
||||
aRv = mTelephony->Service()->ResumeConference(mCalls[0]->ServiceId(),
|
||||
callback);
|
||||
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
|
||||
return promise.forget();
|
||||
}
|
||||
|
||||
nsresult
|
||||
TelephonyCallGroup::Hold(nsITelephonyCallback* aCallback)
|
||||
{
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_CONNECTED) {
|
||||
NS_WARNING("Holding a non-connected call is rejected!");
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
nsresult rv = mTelephony->Service()->HoldConference(mCalls[0]->ServiceId(),
|
||||
aCallback);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
TelephonyCallGroup::Resume(nsITelephonyCallback* aCallback)
|
||||
{
|
||||
if (mCallState != nsITelephonyService::CALL_STATE_HELD) {
|
||||
NS_WARNING("Resuming a non-held call is rejected!");
|
||||
aCallback->NotifyError(NS_LITERAL_STRING("InvalidStateError"));
|
||||
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
||||
}
|
||||
|
||||
nsresult rv = mTelephony->Service()->ResumeConference(mCalls[0]->ServiceId(),
|
||||
aCallback);
|
||||
if (NS_WARN_IF(NS_FAILED(rv))) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
@ -30,6 +30,8 @@ public:
|
||||
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(TelephonyCallGroup,
|
||||
DOMEventTargetHelper)
|
||||
|
||||
friend class Telephony;
|
||||
|
||||
nsPIDOMWindow*
|
||||
GetParentObject() const
|
||||
{
|
||||
@ -108,6 +110,12 @@ private:
|
||||
explicit TelephonyCallGroup(nsPIDOMWindow* aOwner);
|
||||
~TelephonyCallGroup();
|
||||
|
||||
nsresult
|
||||
Hold(nsITelephonyCallback* aCallback);
|
||||
|
||||
nsresult
|
||||
Resume(nsITelephonyCallback* aCallback);
|
||||
|
||||
nsresult
|
||||
NotifyCallsChanged(TelephonyCall* aCall);
|
||||
|
||||
|
@ -49,6 +49,12 @@ interface Telephony : EventTarget {
|
||||
[Throws]
|
||||
void stopTone(optional unsigned long serviceId);
|
||||
|
||||
// Calling this method, the app will be treated as owner of the telephony
|
||||
// calls from the AudioChannel policy.
|
||||
[Throws,
|
||||
CheckAllPermissions="audio-channel-telephony"]
|
||||
void ownAudioChannel();
|
||||
|
||||
[Throws]
|
||||
attribute boolean muted;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user