1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
3  */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 
8 #include <string.h>
9 
10 #include "nscore.h"
11 #include "plstr.h"
12 #include "prlink.h"
13 
14 #include "nsSound.h"
15 
16 #include "HeadlessSound.h"
17 #include "nsIURL.h"
18 #include "nsNetUtil.h"
19 #include "nsIChannel.h"
20 #include "nsCOMPtr.h"
21 #include "nsString.h"
22 #include "nsDirectoryService.h"
23 #include "nsDirectoryServiceDefs.h"
24 #include "mozilla/FileUtils.h"
25 #include "mozilla/Unused.h"
26 #include "mozilla/WidgetUtils.h"
27 #include "nsIXULAppInfo.h"
28 #include "nsContentUtils.h"
29 #include "gfxPlatform.h"
30 #include "mozilla/ClearOnShutdown.h"
31 
32 #include <stdio.h>
33 #include <unistd.h>
34 
35 #include <gtk/gtk.h>
36 static PRLibrary* libcanberra = nullptr;
37 
38 /* used to play sounds with libcanberra. */
39 typedef struct _ca_context ca_context;
40 typedef struct _ca_proplist ca_proplist;
41 
42 typedef void (*ca_finish_callback_t)(ca_context* c, uint32_t id, int error_code,
43                                      void* userdata);
44 
45 typedef int (*ca_context_create_fn)(ca_context**);
46 typedef int (*ca_context_destroy_fn)(ca_context*);
47 typedef int (*ca_context_play_fn)(ca_context* c, uint32_t id, ...);
48 typedef int (*ca_context_change_props_fn)(ca_context* c, ...);
49 typedef int (*ca_proplist_create_fn)(ca_proplist**);
50 typedef int (*ca_proplist_destroy_fn)(ca_proplist*);
51 typedef int (*ca_proplist_sets_fn)(ca_proplist* c, const char* key,
52                                    const char* value);
53 typedef int (*ca_context_play_full_fn)(ca_context* c, uint32_t id,
54                                        ca_proplist* p, ca_finish_callback_t cb,
55                                        void* userdata);
56 
57 static ca_context_create_fn ca_context_create;
58 static ca_context_destroy_fn ca_context_destroy;
59 static ca_context_play_fn ca_context_play;
60 static ca_context_change_props_fn ca_context_change_props;
61 static ca_proplist_create_fn ca_proplist_create;
62 static ca_proplist_destroy_fn ca_proplist_destroy;
63 static ca_proplist_sets_fn ca_proplist_sets;
64 static ca_context_play_full_fn ca_context_play_full;
65 
66 struct ScopedCanberraFile {
ScopedCanberraFileScopedCanberraFile67   explicit ScopedCanberraFile(nsIFile* file) : mFile(file){};
68 
~ScopedCanberraFileScopedCanberraFile69   ~ScopedCanberraFile() {
70     if (mFile) {
71       mFile->Remove(false);
72     }
73   }
74 
forgetScopedCanberraFile75   void forget() { mozilla::Unused << mFile.forget(); }
operator ->ScopedCanberraFile76   nsIFile* operator->() { return mFile; }
operator nsIFile*ScopedCanberraFile77   operator nsIFile*() { return mFile; }
78 
79   nsCOMPtr<nsIFile> mFile;
80 };
81 
ca_context_get_default()82 static ca_context* ca_context_get_default() {
83   // This allows us to avoid race conditions with freeing the context by handing
84   // that responsibility to Glib, and still use one context at a time
85   static GPrivate ctx_private =
86       G_PRIVATE_INIT((GDestroyNotify)ca_context_destroy);
87 
88   ca_context* ctx = (ca_context*)g_private_get(&ctx_private);
89 
90   if (ctx) {
91     return ctx;
92   }
93 
94   ca_context_create(&ctx);
95   if (!ctx) {
96     return nullptr;
97   }
98 
99   g_private_set(&ctx_private, ctx);
100 
101   GtkSettings* settings = gtk_settings_get_default();
102   if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings),
103                                    "gtk-sound-theme-name")) {
104     gchar* sound_theme_name = nullptr;
105     g_object_get(settings, "gtk-sound-theme-name", &sound_theme_name, nullptr);
106 
107     if (sound_theme_name) {
108       ca_context_change_props(ctx, "canberra.xdg-theme.name", sound_theme_name,
109                               nullptr);
110       g_free(sound_theme_name);
111     }
112   }
113 
114   nsAutoString wbrand;
115   mozilla::widget::WidgetUtils::GetBrandShortName(wbrand);
116   ca_context_change_props(ctx, "application.name",
117                           NS_ConvertUTF16toUTF8(wbrand).get(), nullptr);
118 
119   nsCOMPtr<nsIXULAppInfo> appInfo =
120       do_GetService("@mozilla.org/xre/app-info;1");
121   if (appInfo) {
122     nsAutoCString version;
123     appInfo->GetVersion(version);
124 
125     ca_context_change_props(ctx, "application.version", version.get(), nullptr);
126   }
127 
128   ca_context_change_props(ctx, "application.icon_name", MOZ_APP_NAME, nullptr);
129 
130   return ctx;
131 }
132 
ca_finish_cb(ca_context * c,uint32_t id,int error_code,void * userdata)133 static void ca_finish_cb(ca_context* c, uint32_t id, int error_code,
134                          void* userdata) {
135   nsIFile* file = reinterpret_cast<nsIFile*>(userdata);
136   if (file) {
137     file->Remove(false);
138     NS_RELEASE(file);
139   }
140 }
141 
NS_IMPL_ISUPPORTS(nsSound,nsISound,nsIStreamLoaderObserver)142 NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver)
143 
144 ////////////////////////////////////////////////////////////////////////
145 nsSound::nsSound() { mInited = false; }
146 
147 nsSound::~nsSound() = default;
148 
149 NS_IMETHODIMP
Init()150 nsSound::Init() {
151   // This function is designed so that no library is compulsory, and
152   // one library missing doesn't cause the other(s) to not be used.
153   if (mInited) return NS_OK;
154 
155   mInited = true;
156 
157   if (!libcanberra) {
158     libcanberra = PR_LoadLibrary("libcanberra.so.0");
159     if (libcanberra) {
160       ca_context_create = (ca_context_create_fn)PR_FindFunctionSymbol(
161           libcanberra, "ca_context_create");
162       if (!ca_context_create) {
163 #ifdef MOZ_TSAN
164         // With TSan, we cannot unload libcanberra once we have loaded it
165         // because TSan does not support unloading libraries that are matched
166         // from its suppression list. Hence we just keep the library loaded in
167         // TSan builds.
168         libcanberra = nullptr;
169         return NS_OK;
170 #endif
171         PR_UnloadLibrary(libcanberra);
172         libcanberra = nullptr;
173       } else {
174         // at this point we know we have a good libcanberra library
175         ca_context_destroy = (ca_context_destroy_fn)PR_FindFunctionSymbol(
176             libcanberra, "ca_context_destroy");
177         ca_context_play = (ca_context_play_fn)PR_FindFunctionSymbol(
178             libcanberra, "ca_context_play");
179         ca_context_change_props =
180             (ca_context_change_props_fn)PR_FindFunctionSymbol(
181                 libcanberra, "ca_context_change_props");
182         ca_proplist_create = (ca_proplist_create_fn)PR_FindFunctionSymbol(
183             libcanberra, "ca_proplist_create");
184         ca_proplist_destroy = (ca_proplist_destroy_fn)PR_FindFunctionSymbol(
185             libcanberra, "ca_proplist_destroy");
186         ca_proplist_sets = (ca_proplist_sets_fn)PR_FindFunctionSymbol(
187             libcanberra, "ca_proplist_sets");
188         ca_context_play_full = (ca_context_play_full_fn)PR_FindFunctionSymbol(
189             libcanberra, "ca_context_play_full");
190       }
191     }
192   }
193 
194   return NS_OK;
195 }
196 
197 /* static */
Shutdown()198 void nsSound::Shutdown() {
199 #ifndef MOZ_TSAN
200   if (libcanberra) {
201     PR_UnloadLibrary(libcanberra);
202     libcanberra = nullptr;
203   }
204 #endif
205 }
206 
207 namespace mozilla {
208 namespace sound {
209 StaticRefPtr<nsISound> sInstance;
210 }
211 }  // namespace mozilla
212 /* static */
GetInstance()213 already_AddRefed<nsISound> nsSound::GetInstance() {
214   using namespace mozilla::sound;
215 
216   if (!sInstance) {
217     if (gfxPlatform::IsHeadless()) {
218       sInstance = new mozilla::widget::HeadlessSound();
219     } else {
220       sInstance = new nsSound();
221     }
222     ClearOnShutdown(&sInstance);
223   }
224 
225   RefPtr<nsISound> service = sInstance.get();
226   return service.forget();
227 }
228 
OnStreamComplete(nsIStreamLoader * aLoader,nsISupports * context,nsresult aStatus,uint32_t dataLen,const uint8_t * data)229 NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader* aLoader,
230                                         nsISupports* context, nsresult aStatus,
231                                         uint32_t dataLen, const uint8_t* data) {
232   // print a load error on bad status, and return
233   if (NS_FAILED(aStatus)) {
234 #ifdef DEBUG
235     if (aLoader) {
236       nsCOMPtr<nsIRequest> request;
237       aLoader->GetRequest(getter_AddRefs(request));
238       if (request) {
239         nsCOMPtr<nsIURI> uri;
240         nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
241         if (channel) {
242           channel->GetURI(getter_AddRefs(uri));
243           if (uri) {
244             printf("Failed to load %s\n", uri->GetSpecOrDefault().get());
245           }
246         }
247       }
248     }
249 #endif
250     return aStatus;
251   }
252 
253   nsCOMPtr<nsIFile> tmpFile;
254   nsDirectoryService::gService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile),
255                                     getter_AddRefs(tmpFile));
256 
257   nsresult rv =
258       tmpFile->AppendNative(nsDependentCString("mozilla_audio_sample"));
259   if (NS_FAILED(rv)) {
260     return rv;
261   }
262 
263   rv = tmpFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, PR_IRUSR | PR_IWUSR);
264   if (NS_FAILED(rv)) {
265     return rv;
266   }
267 
268   ScopedCanberraFile canberraFile(tmpFile);
269 
270   mozilla::AutoFDClose fd;
271   rv = canberraFile->OpenNSPRFileDesc(PR_WRONLY, PR_IRUSR | PR_IWUSR,
272                                       &fd.rwget());
273   if (NS_FAILED(rv)) {
274     return rv;
275   }
276 
277   // XXX: Should we do this on another thread?
278   uint32_t length = dataLen;
279   while (length > 0) {
280     int32_t amount = PR_Write(fd, data, length);
281     if (amount < 0) {
282       return NS_ERROR_FAILURE;
283     }
284     length -= amount;
285     data += amount;
286   }
287 
288   ca_context* ctx = ca_context_get_default();
289   if (!ctx) {
290     return NS_ERROR_OUT_OF_MEMORY;
291   }
292 
293   ca_proplist* p;
294   ca_proplist_create(&p);
295   if (!p) {
296     return NS_ERROR_OUT_OF_MEMORY;
297   }
298 
299   nsAutoCString path;
300   rv = canberraFile->GetNativePath(path);
301   if (NS_FAILED(rv)) {
302     return rv;
303   }
304 
305   ca_proplist_sets(p, "media.filename", path.get());
306   if (ca_context_play_full(ctx, 0, p, ca_finish_cb, canberraFile) >= 0) {
307     // Don't delete the temporary file here if ca_context_play_full succeeds
308     canberraFile.forget();
309   }
310   ca_proplist_destroy(p);
311 
312   return NS_OK;
313 }
314 
Beep()315 NS_IMETHODIMP nsSound::Beep() {
316   ::gdk_beep();
317   return NS_OK;
318 }
319 
Play(nsIURL * aURL)320 NS_IMETHODIMP nsSound::Play(nsIURL* aURL) {
321   if (!mInited) Init();
322 
323   if (!libcanberra) return NS_ERROR_NOT_AVAILABLE;
324 
325   nsresult rv;
326   if (aURL->SchemeIs("file")) {
327     ca_context* ctx = ca_context_get_default();
328     if (!ctx) {
329       return NS_ERROR_OUT_OF_MEMORY;
330     }
331 
332     nsAutoCString spec;
333     rv = aURL->GetSpec(spec);
334     if (NS_FAILED(rv)) {
335       return rv;
336     }
337     gchar* path = g_filename_from_uri(spec.get(), nullptr, nullptr);
338     if (!path) {
339       return NS_ERROR_FILE_UNRECOGNIZED_PATH;
340     }
341 
342     ca_context_play(ctx, 0, "media.filename", path, nullptr);
343     g_free(path);
344   } else {
345     nsCOMPtr<nsIStreamLoader> loader;
346     rv = NS_NewStreamLoader(
347         getter_AddRefs(loader), aURL,
348         this,  // aObserver
349         nsContentUtils::GetSystemPrincipal(),
350         nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
351         nsIContentPolicy::TYPE_OTHER);
352   }
353 
354   return rv;
355 }
356 
PlayEventSound(uint32_t aEventId)357 NS_IMETHODIMP nsSound::PlayEventSound(uint32_t aEventId) {
358   if (!mInited) Init();
359 
360   if (!libcanberra) return NS_OK;
361 
362   // Do we even want alert sounds?
363   GtkSettings* settings = gtk_settings_get_default();
364 
365   if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings),
366                                    "gtk-enable-event-sounds")) {
367     gboolean enable_sounds = TRUE;
368     g_object_get(settings, "gtk-enable-event-sounds", &enable_sounds, nullptr);
369 
370     if (!enable_sounds) {
371       return NS_OK;
372     }
373   }
374 
375   ca_context* ctx = ca_context_get_default();
376   if (!ctx) {
377     return NS_ERROR_OUT_OF_MEMORY;
378   }
379 
380   switch (aEventId) {
381     case EVENT_ALERT_DIALOG_OPEN:
382       ca_context_play(ctx, 0, "event.id", "dialog-warning", nullptr);
383       break;
384     case EVENT_CONFIRM_DIALOG_OPEN:
385       ca_context_play(ctx, 0, "event.id", "dialog-question", nullptr);
386       break;
387     case EVENT_NEW_MAIL_RECEIVED:
388       ca_context_play(ctx, 0, "event.id", "message-new-email", nullptr);
389       break;
390     case EVENT_MENU_EXECUTE:
391       ca_context_play(ctx, 0, "event.id", "menu-click", nullptr);
392       break;
393     case EVENT_MENU_POPUP:
394       ca_context_play(ctx, 0, "event.id", "menu-popup", nullptr);
395       break;
396   }
397   return NS_OK;
398 }
399