1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set sw=2 ts=8 et tw=80 : */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla Audio IPC
17 : *
18 : * The Initial Developer of the Original Code is
19 : * The Mozilla Foundation.
20 : * Portions created by the Initial Developer are Copyright (C) 2010
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Doug Turner <dougt@mozilla.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "mozilla/dom/AudioParent.h"
41 : #include "mozilla/unused.h"
42 : #include "nsThreadUtils.h"
43 :
44 : // C++ file contents
45 : namespace mozilla {
46 : namespace dom {
47 :
48 : class AudioWriteEvent : public nsRunnable
49 0 : {
50 : public:
51 0 : AudioWriteEvent(nsAudioStream* owner, nsCString data, PRUint32 frames)
52 0 : {
53 0 : mOwner = owner;
54 0 : mData = data;
55 0 : mFrames = frames;
56 0 : }
57 :
58 0 : NS_IMETHOD Run()
59 : {
60 0 : mOwner->Write(mData.get(), mFrames);
61 0 : return NS_OK;
62 : }
63 :
64 : private:
65 : nsRefPtr<nsAudioStream> mOwner;
66 : nsCString mData;
67 : PRUint32 mFrames;
68 : };
69 :
70 : class AudioPauseEvent : public nsRunnable
71 0 : {
72 : public:
73 0 : AudioPauseEvent(nsAudioStream* owner, bool aPause)
74 0 : {
75 0 : mOwner = owner;
76 0 : mPause = aPause;
77 0 : }
78 :
79 0 : NS_IMETHOD Run()
80 : {
81 0 : if (mPause)
82 0 : mOwner->Pause();
83 : else
84 0 : mOwner->Resume();
85 0 : return NS_OK;
86 : }
87 :
88 : private:
89 : nsRefPtr<nsAudioStream> mOwner;
90 : bool mPause;
91 : };
92 :
93 : class AudioStreamShutdownEvent : public nsRunnable
94 0 : {
95 : public:
96 0 : AudioStreamShutdownEvent(nsAudioStream* owner)
97 0 : {
98 0 : mOwner = owner;
99 0 : }
100 :
101 0 : NS_IMETHOD Run()
102 : {
103 0 : mOwner->Shutdown();
104 0 : return NS_OK;
105 : }
106 :
107 : private:
108 : nsRefPtr<nsAudioStream> mOwner;
109 : };
110 :
111 :
112 : class AudioMinWriteSizeDone : public nsRunnable
113 0 : {
114 : public:
115 0 : AudioMinWriteSizeDone(AudioParent* owner, PRInt32 minFrames)
116 0 : {
117 0 : mOwner = owner;
118 0 : mMinFrames = minFrames;
119 0 : }
120 :
121 0 : NS_IMETHOD Run()
122 : {
123 0 : mOwner->SendMinWriteSizeDone(mMinFrames);
124 0 : return NS_OK;
125 : }
126 :
127 : private:
128 : nsRefPtr<AudioParent> mOwner;
129 : PRInt32 mMinFrames;
130 : };
131 :
132 : class AudioMinWriteSizeEvent : public nsRunnable
133 0 : {
134 : public:
135 0 : AudioMinWriteSizeEvent(AudioParent* parent, nsAudioStream* owner)
136 0 : {
137 0 : mParent = parent;
138 0 : mOwner = owner;
139 0 : }
140 :
141 0 : NS_IMETHOD Run()
142 : {
143 0 : PRInt32 minFrames = mOwner->GetMinWriteSize();
144 0 : nsCOMPtr<nsIRunnable> event = new AudioMinWriteSizeDone(mParent, minFrames);
145 0 : NS_DispatchToMainThread(event);
146 0 : return NS_OK;
147 : }
148 :
149 : private:
150 : nsRefPtr<nsAudioStream> mOwner;
151 : nsRefPtr<AudioParent> mParent;
152 : };
153 :
154 : class AudioDrainDoneEvent : public nsRunnable
155 0 : {
156 : public:
157 0 : AudioDrainDoneEvent(AudioParent* owner)
158 0 : {
159 0 : mOwner = owner;
160 0 : }
161 :
162 0 : NS_IMETHOD Run()
163 : {
164 0 : mOwner->SendDrainDone();
165 0 : return NS_OK;
166 : }
167 :
168 : private:
169 : nsRefPtr<AudioParent> mOwner;
170 : };
171 :
172 : class AudioDrainEvent : public nsRunnable
173 0 : {
174 : public:
175 0 : AudioDrainEvent(AudioParent* parent, nsAudioStream* owner)
176 0 : {
177 0 : mParent = parent;
178 0 : mOwner = owner;
179 0 : }
180 :
181 0 : NS_IMETHOD Run()
182 : {
183 0 : mOwner->Drain();
184 0 : nsCOMPtr<nsIRunnable> event = new AudioDrainDoneEvent(mParent);
185 0 : NS_DispatchToMainThread(event);
186 0 : return NS_OK;
187 : }
188 :
189 : private:
190 : nsRefPtr<nsAudioStream> mOwner;
191 : nsRefPtr<AudioParent> mParent;
192 : };
193 :
194 0 : NS_IMPL_THREADSAFE_ISUPPORTS1(AudioParent, nsITimerCallback)
195 :
196 : nsresult
197 0 : AudioParent::Notify(nsITimer* timer)
198 : {
199 0 : if (!mIPCOpen) {
200 0 : timer->Cancel();
201 0 : return NS_ERROR_FAILURE;
202 : }
203 :
204 0 : NS_ASSERTION(mStream, "AudioStream not initialized.");
205 0 : PRInt64 position = mStream->GetPositionInFrames();
206 0 : unused << SendPositionInFramesUpdate(position, PR_IntervalNow());
207 0 : return NS_OK;
208 : }
209 :
210 : bool
211 0 : AudioParent::RecvWrite(const nsCString& data, const PRUint32& frames)
212 : {
213 0 : if (!mStream)
214 0 : return false;
215 0 : nsCOMPtr<nsIRunnable> event = new AudioWriteEvent(mStream, data, frames);
216 0 : nsCOMPtr<nsIThread> thread = mStream->GetThread();
217 0 : thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
218 0 : return true;
219 : }
220 :
221 : bool
222 0 : AudioParent::RecvSetVolume(const float& aVolume)
223 : {
224 0 : if (!mStream)
225 0 : return false;
226 0 : mStream->SetVolume(aVolume);
227 0 : return true;
228 : }
229 :
230 : bool
231 0 : AudioParent::RecvMinWriteSize()
232 : {
233 0 : if (!mStream)
234 0 : return false;
235 0 : nsCOMPtr<nsIRunnable> event = new AudioMinWriteSizeEvent(this, mStream);
236 0 : nsCOMPtr<nsIThread> thread = mStream->GetThread();
237 0 : thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
238 0 : return true;
239 : }
240 :
241 : bool
242 0 : AudioParent::RecvDrain()
243 : {
244 0 : if (!mStream)
245 0 : return false;
246 0 : nsCOMPtr<nsIRunnable> event = new AudioDrainEvent(this, mStream);
247 0 : nsCOMPtr<nsIThread> thread = mStream->GetThread();
248 0 : thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
249 0 : return true;
250 : }
251 :
252 : bool
253 0 : AudioParent::RecvPause()
254 : {
255 0 : if (!mStream)
256 0 : return false;
257 0 : nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mStream, true);
258 0 : nsCOMPtr<nsIThread> thread = mStream->GetThread();
259 0 : thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
260 0 : return true;
261 : }
262 :
263 : bool
264 0 : AudioParent::RecvResume()
265 : {
266 0 : if (!mStream)
267 0 : return false;
268 0 : nsCOMPtr<nsIRunnable> event = new AudioPauseEvent(mStream, false);
269 0 : nsCOMPtr<nsIThread> thread = mStream->GetThread();
270 0 : thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
271 0 : return true;
272 : }
273 :
274 : bool
275 0 : AudioParent::RecvShutdown()
276 : {
277 0 : Shutdown();
278 0 : unused << PAudioParent::Send__delete__(this);
279 0 : return true;
280 : }
281 :
282 : bool
283 0 : AudioParent::SendMinWriteSizeDone(PRInt32 minFrames)
284 : {
285 0 : if (mIPCOpen)
286 0 : return PAudioParent::SendMinWriteSizeDone(minFrames);
287 0 : return true;
288 : }
289 :
290 : bool
291 0 : AudioParent::SendDrainDone()
292 : {
293 0 : if (mIPCOpen)
294 0 : return PAudioParent::SendDrainDone();
295 0 : return true;
296 : }
297 :
298 0 : AudioParent::AudioParent(PRInt32 aNumChannels, PRInt32 aRate, PRInt32 aFormat)
299 0 : : mIPCOpen(true)
300 : {
301 0 : mStream = nsAudioStream::AllocateStream();
302 0 : NS_ASSERTION(mStream, "AudioStream allocation failed.");
303 0 : if (NS_FAILED(mStream->Init(aNumChannels,
304 : aRate,
305 : (nsAudioStream::SampleFormat) aFormat))) {
306 0 : NS_WARNING("AudioStream initialization failed.");
307 0 : mStream = nsnull;
308 0 : return;
309 : }
310 :
311 0 : mTimer = do_CreateInstance("@mozilla.org/timer;1");
312 0 : mTimer->InitWithCallback(this, 1000, nsITimer::TYPE_REPEATING_SLACK);
313 : }
314 :
315 0 : AudioParent::~AudioParent()
316 : {
317 0 : }
318 :
319 : void
320 0 : AudioParent::ActorDestroy(ActorDestroyReason aWhy)
321 : {
322 0 : mIPCOpen = false;
323 :
324 0 : Shutdown();
325 0 : }
326 :
327 : void
328 0 : AudioParent::Shutdown()
329 : {
330 0 : if (mTimer) {
331 0 : mTimer->Cancel();
332 0 : mTimer = nsnull;
333 : }
334 :
335 0 : if (mStream) {
336 0 : nsCOMPtr<nsIRunnable> event = new AudioStreamShutdownEvent(mStream);
337 0 : nsCOMPtr<nsIThread> thread = mStream->GetThread();
338 0 : thread->Dispatch(event, nsIEventTarget::DISPATCH_NORMAL);
339 0 : mStream = nsnull;
340 : }
341 0 : }
342 :
343 : } // namespace dom
344 : } // namespace mozilla
|