dom/audiochannel/AudioChannelServiceChild.cpp

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

mercurial