forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
AudioChannelServiceChild.cpp
168 lines (136 loc) · 4.64 KB
/
AudioChannelServiceChild.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AudioChannelServiceChild.h"
#include "base/basictypes.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/unused.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"
#include "nsIObserverService.h"
#include "nsThreadUtils.h"
#ifdef MOZ_WIDGET_GONK
#include "SpeakerManagerService.h"
#endif
using namespace mozilla;
using namespace mozilla::dom;
using namespace mozilla::hal;
StaticRefPtr<AudioChannelServiceChild> gAudioChannelServiceChild;
// static
AudioChannelService*
AudioChannelServiceChild::GetAudioChannelService()
{
MOZ_ASSERT(NS_IsMainThread());
return gAudioChannelServiceChild;
}
// static
AudioChannelService*
AudioChannelServiceChild::GetOrCreateAudioChannelService()
{
MOZ_ASSERT(NS_IsMainThread());
// If we already exist, exit early
if (gAudioChannelServiceChild) {
return gAudioChannelServiceChild;
}
// Create new instance, register, return
nsRefPtr<AudioChannelServiceChild> service = new AudioChannelServiceChild();
MOZ_ASSERT(service);
gAudioChannelServiceChild = service;
return gAudioChannelServiceChild;
}
void
AudioChannelServiceChild::Shutdown()
{
if (gAudioChannelServiceChild) {
gAudioChannelServiceChild = nullptr;
}
}
AudioChannelServiceChild::AudioChannelServiceChild()
{
}
AudioChannelServiceChild::~AudioChannelServiceChild()
{
}
AudioChannelState
AudioChannelServiceChild::GetState(AudioChannelAgent* aAgent, bool aElementHidden)
{
AudioChannelAgentData* data = Find(aAgent);
if (!data) {
return AUDIO_CHANNEL_STATE_MUTED;
}
AudioChannelState state = AUDIO_CHANNEL_STATE_MUTED;
bool oldElementHidden = data->mElementHidden;
UpdateChannelType(data->mChannel, CONTENT_PROCESS_ID_MAIN, aElementHidden,
oldElementHidden);
// Update visibility.
data->mElementHidden = aElementHidden;
ContentChild* cc = ContentChild::GetSingleton();
cc->SendAudioChannelGetState(data->mChannel, aElementHidden, oldElementHidden,
&state);
data->mState = state;
cc->SendAudioChannelChangedNotification();
#ifdef MOZ_WIDGET_GONK
/** Only modify the speaker status when
* (1) apps in the foreground.
* (2) apps in the backgrund and inactive.
* Notice : modify only when the visible status is stable, because there
* has lantency in passing the visibility events.
**/
bool active = AnyAudioChannelIsActive();
if (aElementHidden == oldElementHidden &&
(!aElementHidden || (aElementHidden && !active))) {
for (uint32_t i = 0; i < mSpeakerManager.Length(); i++) {
mSpeakerManager[i]->SetAudioChannelActive(active);
}
}
#endif
return state;
}
void
AudioChannelServiceChild::RegisterAudioChannelAgent(AudioChannelAgent* aAgent,
AudioChannel aChannel,
bool aWithVideo)
{
AudioChannelService::RegisterAudioChannelAgent(aAgent, aChannel, aWithVideo);
ContentChild::GetSingleton()->SendAudioChannelRegisterType(aChannel, aWithVideo);
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr);
}
}
void
AudioChannelServiceChild::UnregisterAudioChannelAgent(AudioChannelAgent* aAgent)
{
AudioChannelAgentData* pData = Find(aAgent);
if (!pData) {
return;
}
// We need to keep a copy because unregister will remove the
// AudioChannelAgentData object from the hashtable.
AudioChannelAgentData data(*pData);
AudioChannelService::UnregisterAudioChannelAgent(aAgent);
ContentChild::GetSingleton()->SendAudioChannelUnregisterType(
data.mChannel, data.mElementHidden, data.mWithVideo);
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "audio-channel-agent-changed", nullptr);
}
#ifdef MOZ_WIDGET_GONK
bool active = AnyAudioChannelIsActive();
for (uint32_t i = 0; i < mSpeakerManager.Length(); i++) {
mSpeakerManager[i]->SetAudioChannelActive(active);
}
#endif
}
void
AudioChannelServiceChild::SetDefaultVolumeControlChannel(int32_t aChannel,
bool aHidden)
{
ContentChild *cc = ContentChild::GetSingleton();
if (cc) {
cc->SendAudioChannelChangeDefVolChannel(aChannel, aHidden);
}
}