summaryrefslogtreecommitdiffstats
path: root/widget/gtk/nsSound.cpp
blob: 4d685d856600d1b048e4215ac9c0d4347ef17b62 (plain)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=4:tabstop=4:
 */
/* 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 <string.h>

#include "nscore.h"
#include "plstr.h"
#include "prlink.h"

#include "nsSound.h"

#include "HeadlessSound.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsIChannel.h"
#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsDirectoryService.h"
#include "nsDirectoryServiceDefs.h"
#include "mozilla/FileUtils.h"
#include "mozilla/Unused.h"
#include "mozilla/WidgetUtils.h"
#include "nsIXULAppInfo.h"
#include "nsContentUtils.h"
#include "gfxPlatform.h"
#include "mozilla/ClearOnShutdown.h"

#include <stdio.h>
#include <unistd.h>

#include <gtk/gtk.h>
static PRLibrary* libcanberra = nullptr;

/* used to play sounds with libcanberra. */
typedef struct _ca_context ca_context;
typedef struct _ca_proplist ca_proplist;

typedef void (*ca_finish_callback_t)(ca_context* c, uint32_t id, int error_code,
                                     void* userdata);

typedef int (*ca_context_create_fn)(ca_context**);
typedef int (*ca_context_destroy_fn)(ca_context*);
typedef int (*ca_context_play_fn)(ca_context* c, uint32_t id, ...);
typedef int (*ca_context_change_props_fn)(ca_context* c, ...);
typedef int (*ca_proplist_create_fn)(ca_proplist**);
typedef int (*ca_proplist_destroy_fn)(ca_proplist*);
typedef int (*ca_proplist_sets_fn)(ca_proplist* c, const char* key,
                                   const char* value);
typedef int (*ca_context_play_full_fn)(ca_context* c, uint32_t id,
                                       ca_proplist* p, ca_finish_callback_t cb,
                                       void* userdata);

static ca_context_create_fn ca_context_create;
static ca_context_destroy_fn ca_context_destroy;
static ca_context_play_fn ca_context_play;
static ca_context_change_props_fn ca_context_change_props;
static ca_proplist_create_fn ca_proplist_create;
static ca_proplist_destroy_fn ca_proplist_destroy;
static ca_proplist_sets_fn ca_proplist_sets;
static ca_context_play_full_fn ca_context_play_full;

struct ScopedCanberraFile {
  explicit ScopedCanberraFile(nsIFile* file) : mFile(file){};

  ~ScopedCanberraFile() {
    if (mFile) {
      mFile->Remove(false);
    }
  }

  void forget() { mozilla::Unused << mFile.forget(); }
  nsIFile* operator->() { return mFile; }
  operator nsIFile*() { return mFile; }

  nsCOMPtr<nsIFile> mFile;
};

static ca_context* ca_context_get_default() {
  // This allows us to avoid race conditions with freeing the context by handing
  // that responsibility to Glib, and still use one context at a time
  static GPrivate ctx_private =
      G_PRIVATE_INIT((GDestroyNotify)ca_context_destroy);

  ca_context* ctx = (ca_context*)g_private_get(&ctx_private);

  if (ctx) {
    return ctx;
  }

  ca_context_create(&ctx);
  if (!ctx) {
    return nullptr;
  }

  g_private_set(&ctx_private, ctx);

  GtkSettings* settings = gtk_settings_get_default();
  if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings),
                                   "gtk-sound-theme-name")) {
    gchar* sound_theme_name = nullptr;
    g_object_get(settings, "gtk-sound-theme-name", &sound_theme_name, nullptr);

    if (sound_theme_name) {
      ca_context_change_props(ctx, "canberra.xdg-theme.name", sound_theme_name,
                              nullptr);
      g_free(sound_theme_name);
    }
  }

  nsAutoString wbrand;
  mozilla::widget::WidgetUtils::GetBrandShortName(wbrand);
  ca_context_change_props(ctx, "application.name",
                          NS_ConvertUTF16toUTF8(wbrand).get(), nullptr);

  nsCOMPtr<nsIXULAppInfo> appInfo =
      do_GetService("@mozilla.org/xre/app-info;1");
  if (appInfo) {
    nsAutoCString version;
    appInfo->GetVersion(version);

    ca_context_change_props(ctx, "application.version", version.get(), nullptr);
  }

  ca_context_change_props(ctx, "application.icon_name", MOZ_APP_NAME, nullptr);

  return ctx;
}

static void ca_finish_cb(ca_context* c, uint32_t id, int error_code,
                         void* userdata) {
  nsIFile* file = reinterpret_cast<nsIFile*>(userdata);
  if (file) {
    file->Remove(false);
    NS_RELEASE(file);
  }
}

NS_IMPL_ISUPPORTS(nsSound, nsISound, nsIStreamLoaderObserver)

////////////////////////////////////////////////////////////////////////
nsSound::nsSound() { mInited = false; }

nsSound::~nsSound() = default;

NS_IMETHODIMP
nsSound::Init() {
  // This function is designed so that no library is compulsory, and
  // one library missing doesn't cause the other(s) to not be used.
  if (mInited) return NS_OK;

  mInited = true;

  if (!libcanberra) {
    libcanberra = PR_LoadLibrary("libcanberra.so.0");
    if (libcanberra) {
      ca_context_create = (ca_context_create_fn)PR_FindFunctionSymbol(
          libcanberra, "ca_context_create");
      if (!ca_context_create) {
#ifdef MOZ_TSAN
        // With TSan, we cannot unload libcanberra once we have loaded it
        // because TSan does not support unloading libraries that are matched
        // from its suppression list. Hence we just keep the library loaded in
        // TSan builds.
        libcanberra = nullptr;
        return NS_OK;
#endif
        PR_UnloadLibrary(libcanberra);
        libcanberra = nullptr;
      } else {
        // at this point we know we have a good libcanberra library
        ca_context_destroy = (ca_context_destroy_fn)PR_FindFunctionSymbol(
            libcanberra, "ca_context_destroy");
        ca_context_play = (ca_context_play_fn)PR_FindFunctionSymbol(
            libcanberra, "ca_context_play");
        ca_context_change_props =
            (ca_context_change_props_fn)PR_FindFunctionSymbol(
                libcanberra, "ca_context_change_props");
        ca_proplist_create = (ca_proplist_create_fn)PR_FindFunctionSymbol(
            libcanberra, "ca_proplist_create");
        ca_proplist_destroy = (ca_proplist_destroy_fn)PR_FindFunctionSymbol(
            libcanberra, "ca_proplist_destroy");
        ca_proplist_sets = (ca_proplist_sets_fn)PR_FindFunctionSymbol(
            libcanberra, "ca_proplist_sets");
        ca_context_play_full = (ca_context_play_full_fn)PR_FindFunctionSymbol(
            libcanberra, "ca_context_play_full");
      }
    }
  }

  return NS_OK;
}

/* static */
void nsSound::Shutdown() {
#ifndef MOZ_TSAN
  if (libcanberra) {
    PR_UnloadLibrary(libcanberra);
    libcanberra = nullptr;
  }
#endif
}

namespace mozilla {
namespace sound {
StaticRefPtr<nsISound> sInstance;
}
}  // namespace mozilla
/* static */
already_AddRefed<nsISound> nsSound::GetInstance() {
  using namespace mozilla::sound;

  if (!sInstance) {
    if (gfxPlatform::IsHeadless()) {
      sInstance = new mozilla::widget::HeadlessSound();
    } else {
      sInstance = new nsSound();
    }
    ClearOnShutdown(&sInstance);
  }

  RefPtr<nsISound> service = sInstance.get();
  return service.forget();
}

NS_IMETHODIMP nsSound::OnStreamComplete(nsIStreamLoader* aLoader,
                                        nsISupports* context, nsresult aStatus,
                                        uint32_t dataLen, const uint8_t* data) {
  // print a load error on bad status, and return
  if (NS_FAILED(aStatus)) {
#ifdef DEBUG
    if (aLoader) {
      nsCOMPtr<nsIRequest> request;
      aLoader->GetRequest(getter_AddRefs(request));
      if (request) {
        nsCOMPtr<nsIURI> uri;
        nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
        if (channel) {
          channel->GetURI(getter_AddRefs(uri));
          if (uri) {
            printf("Failed to load %s\n", uri->GetSpecOrDefault().get());
          }
        }
      }
    }
#endif
    return aStatus;
  }

  nsCOMPtr<nsIFile> tmpFile;
  nsDirectoryService::gService->Get(NS_OS_TEMP_DIR, NS_GET_IID(nsIFile),
                                    getter_AddRefs(tmpFile));

  nsresult rv =
      tmpFile->AppendNative(nsDependentCString("mozilla_audio_sample"));
  if (NS_FAILED(rv)) {
    return rv;
  }

  rv = tmpFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, PR_IRUSR | PR_IWUSR);
  if (NS_FAILED(rv)) {
    return rv;
  }

  ScopedCanberraFile canberraFile(tmpFile);

  mozilla::AutoFDClose fd;
  rv = canberraFile->OpenNSPRFileDesc(PR_WRONLY, PR_IRUSR | PR_IWUSR,
                                      &fd.rwget());
  if (NS_FAILED(rv)) {
    return rv;
  }

  // XXX: Should we do this on another thread?
  uint32_t length = dataLen;
  while (length > 0) {
    int32_t amount = PR_Write(fd, data, length);
    if (amount < 0) {
      return NS_ERROR_FAILURE;
    }
    length -= amount;
    data += amount;
  }

  ca_context* ctx = ca_context_get_default();
  if (!ctx) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  ca_proplist* p;
  ca_proplist_create(&p);
  if (!p) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  nsAutoCString path;
  rv = canberraFile->GetNativePath(path);
  if (NS_FAILED(rv)) {
    return rv;
  }

  ca_proplist_sets(p, "media.filename", path.get());
  if (ca_context_play_full(ctx, 0, p, ca_finish_cb, canberraFile) >= 0) {
    // Don't delete the temporary file here if ca_context_play_full succeeds
    canberraFile.forget();
  }
  ca_proplist_destroy(p);

  return NS_OK;
}

NS_IMETHODIMP nsSound::Beep() {
  ::gdk_beep();
  return NS_OK;
}

NS_IMETHODIMP nsSound::Play(nsIURL* aURL) {
  if (!mInited) Init();

  if (!libcanberra) return NS_ERROR_NOT_AVAILABLE;

  nsresult rv;
  if (aURL->SchemeIs("file")) {
    ca_context* ctx = ca_context_get_default();
    if (!ctx) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    nsAutoCString spec;
    rv = aURL->GetSpec(spec);
    if (NS_FAILED(rv)) {
      return rv;
    }
    gchar* path = g_filename_from_uri(spec.get(), nullptr, nullptr);
    if (!path) {
      return NS_ERROR_FILE_UNRECOGNIZED_PATH;
    }

    ca_context_play(ctx, 0, "media.filename", path, nullptr);
    g_free(path);
  } else {
    nsCOMPtr<nsIStreamLoader> loader;
    rv = NS_NewStreamLoader(
        getter_AddRefs(loader), aURL,
        this,  // aObserver
        nsContentUtils::GetSystemPrincipal(),
        nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
        nsIContentPolicy::TYPE_OTHER);
  }

  return rv;
}

NS_IMETHODIMP nsSound::PlayEventSound(uint32_t aEventId) {
  if (!mInited) Init();

  if (!libcanberra) return NS_OK;

  // Do we even want alert sounds?
  GtkSettings* settings = gtk_settings_get_default();

  if (g_object_class_find_property(G_OBJECT_GET_CLASS(settings),
                                   "gtk-enable-event-sounds")) {
    gboolean enable_sounds = TRUE;
    g_object_get(settings, "gtk-enable-event-sounds", &enable_sounds, nullptr);

    if (!enable_sounds) {
      return NS_OK;
    }
  }

  ca_context* ctx = ca_context_get_default();
  if (!ctx) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  switch (aEventId) {
    case EVENT_ALERT_DIALOG_OPEN:
      ca_context_play(ctx, 0, "event.id", "dialog-warning", nullptr);
      break;
    case EVENT_CONFIRM_DIALOG_OPEN:
      ca_context_play(ctx, 0, "event.id", "dialog-question", nullptr);
      break;
    case EVENT_NEW_MAIL_RECEIVED:
      ca_context_play(ctx, 0, "event.id", "message-new-email", nullptr);
      break;
    case EVENT_MENU_EXECUTE:
      ca_context_play(ctx, 0, "event.id", "menu-click", nullptr);
      break;
    case EVENT_MENU_POPUP:
      ca_context_play(ctx, 0, "event.id", "menu-popup", nullptr);
      break;
  }
  return NS_OK;
}