Bug 1265408 - Add LogToBrowserConsole to WebAudioUtils; r?padenot draft
authorDan Minor <dminor@mozilla.com>
Fri, 13 May 2016 11:52:23 -0400
changeset 372084 7529f32d44ed10705ae4fb104d3d8af070cfafa0
parent 372083 aab08a556fcb4ef7fc083d357fe8a55033d2d2c4
child 372085 1f4838b63c46d6a6d45b599f4d0fe4a674b04335
push id19434
push userdminor@mozilla.com
push dateFri, 27 May 2016 11:00:28 +0000
reviewerspadenot
bugs1265408
milestone49.0a1
Bug 1265408 - Add LogToBrowserConsole to WebAudioUtils; r?padenot MozReview-Commit-ID: 2Zf59JjQX9u
dom/media/webaudio/WebAudioUtils.cpp
dom/media/webaudio/WebAudioUtils.h
--- a/dom/media/webaudio/WebAudioUtils.cpp
+++ b/dom/media/webaudio/WebAudioUtils.cpp
@@ -3,16 +3,18 @@
 /* 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 "WebAudioUtils.h"
 #include "AudioNodeStream.h"
 #include "blink/HRTFDatabaseLoader.h"
 
+#include "nsIConsoleService.h"
+
 namespace mozilla {
 
 LazyLogModule gWebAudioAPILog("WebAudioAPI");
 
 namespace dom {
 
 void WebAudioUtils::ConvertAudioTimelineEventToTicks(AudioTimelineEvent& aEvent,
                                                      AudioNodeStream* aDest)
@@ -84,10 +86,31 @@ WebAudioUtils::SpeexResamplerProcess(Spe
   tmp2.SetLength(*aOutLen);
   ConvertAudioSamples(aIn, tmp1.Elements(), *aInLen);
   int result = speex_resampler_process_float(aResampler, aChannel, tmp1.Elements(), aInLen, tmp2.Elements(), aOutLen);
   ConvertAudioSamples(tmp2.Elements(), aOut, *aOutLen);
   return result;
 #endif
 }
 
+void
+WebAudioUtils::LogToBrowserConsole(const nsAString& aMsg)
+{
+  // This implementation is taken from dom/media/VideoUtils.cpp
+  if (!NS_IsMainThread()) {
+    nsString msg(aMsg);
+    nsCOMPtr<nsIRunnable> task =
+      NS_NewRunnableFunction([msg]() { LogToBrowserConsole(msg); });
+    NS_DispatchToMainThread(task.forget(), NS_DISPATCH_NORMAL);
+    return;
+  }
+  nsCOMPtr<nsIConsoleService> console(
+    do_GetService("@mozilla.org/consoleservice;1"));
+  if (!console) {
+    NS_WARNING("Failed to log message to console.");
+    return;
+  }
+  nsAutoString msg(aMsg);
+  console->LogStringMessage(msg.get());
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/webaudio/WebAudioUtils.h
+++ b/dom/media/webaudio/WebAudioUtils.h
@@ -220,15 +220,19 @@ namespace WebAudioUtils {
                         const int16_t* aIn, uint32_t* aInLen,
                         float* aOut, uint32_t* aOutLen);
 
   int
   SpeexResamplerProcess(SpeexResamplerState* aResampler,
                         uint32_t aChannel,
                         const int16_t* aIn, uint32_t* aInLen,
                         int16_t* aOut, uint32_t* aOutLen);
+
+  void
+  LogToBrowserConsole(const nsAString& aMsg);
+
   } // namespace WebAudioUtils
 
 } // namespace dom
 } // namespace mozilla
 
 #endif