Bug 805254. Part 7: Move SampleFormat to mozilla::AudioSampleFormat in its own file. r=kinetik
authorRobert O'Callahan <robert@ocallahan.org>
Thu, 25 Oct 2012 23:09:40 +1300
changeset 111623 569b5cf142851ddc79c169a205c21a2dc84f3acd
parent 111622 da7b1a16814662dcb2a4182e6693089571e6bec1
child 111624 882cfaba69c25be5368542dbd10675c69401c6f4
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerskinetik
bugs805254
milestone19.0a1
Bug 805254. Part 7: Move SampleFormat to mozilla::AudioSampleFormat in its own file. r=kinetik
content/html/content/src/nsHTMLAudioElement.cpp
content/media/AudioSampleFormat.h
content/media/AudioSegment.cpp
content/media/AudioSegment.h
content/media/Makefile.in
content/media/SharedBuffer.h
content/media/nsAudioStream.cpp
content/media/nsAudioStream.h
content/media/webrtc/MediaEngineWebRTCAudio.cpp
media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
media/webrtc/signaling/test/FakeMediaStreams.h
--- a/content/html/content/src/nsHTMLAudioElement.cpp
+++ b/content/html/content/src/nsHTMLAudioElement.cpp
@@ -7,16 +7,17 @@
 #include "nsIDOMHTMLAudioElement.h"
 #include "nsHTMLAudioElement.h"
 #include "nsGenericHTMLElement.h"
 #include "nsGkAtoms.h"
 #include "nsIDocument.h"
 #include "jsfriendapi.h"
 #include "nsContentUtils.h"
 #include "nsJSUtils.h"
+#include "AudioSampleFormat.h"
 
 using namespace mozilla::dom;
 
 nsGenericHTMLElement*
 NS_NewHTMLAudioElement(already_AddRefed<nsINodeInfo> aNodeInfo,
                        FromParser aFromParser)
 {
   /*
@@ -175,17 +176,17 @@ nsHTMLAudioElement::MozWriteAudio(const 
     return NS_ERROR_DOM_INDEX_SIZE_ERR;
   }
 
   // Don't write more than can be written without blocking.
   uint32_t writeLen = NS_MIN(mAudioStream->Available(), dataLength / mChannels);
 
   float* frames = JS_GetFloat32ArrayData(tsrc, aCx);
   nsresult rv;
-  if (nsAudioStream::Format() == FORMAT_S16) {
+  if (nsAudioStream::Format() == AUDIO_FORMAT_S16) {
     // Convert the samples back to integers as we are using fixed point audio in
     // the nsAudioStream.
     nsAutoArrayPtr<short> shortsArray(new short[writeLen * mChannels]);
     // Hard clip the samples.
     for (uint32_t i = 0; i <  writeLen * mChannels; ++i) {
       float scaled_value = floorf(0.5 + 32768 * frames[i]);
       if (frames[i] < 0.0) {
         shortsArray[i] = (scaled_value < -32768.0) ?
new file mode 100644
--- /dev/null
+++ b/content/media/AudioSampleFormat.h
@@ -0,0 +1,28 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et cindent: */
+/* 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/. */
+#ifndef MOZILLA_AUDIOSAMPLEFORMAT_H_
+#define MOZILLA_AUDIOSAMPLEFORMAT_H_
+
+namespace mozilla {
+
+/**
+ * Audio formats supported in MediaStreams and media elements.
+ *
+ * Only one of these is supported by nsAudioStream, and that is determined
+ * at compile time (roughly, FLOAT32 on desktops, S16 on mobile). That format
+ * is returned by nsAudioStream::Format().
+ */
+enum AudioSampleFormat
+{
+  // Native-endian signed 16-bit audio samples
+  AUDIO_FORMAT_S16,
+  // Signed 32-bit float samples
+  AUDIO_FORMAT_FLOAT32
+};
+
+}
+
+#endif /* MOZILLA_AUDIOSAMPLEFORMAT_H_ */
--- a/content/media/AudioSegment.cpp
+++ b/content/media/AudioSegment.cpp
@@ -1,15 +1,17 @@
 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* 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 "AudioSegment.h"
 
+#include "nsAudioStream.h"
+
 namespace mozilla {
 
 /*
  * Use "2^N" conversion since it's simple, fast, "bit transparent", used by
  * many other libraries and apparently behaves reasonably.
  * http://blog.bjornroche.com/2009/12/int-float-int-its-jungle-out-there.html
  * http://blog.bjornroche.com/2009/12/linearity-and-dynamic-range-in-int.html
  */
@@ -75,47 +77,47 @@ InterleaveAndConvertBuffer(const int16_t
 }
 
 template <class SrcT>
 static void
 InterleaveAndConvertBuffer(const SrcT* aSource, int32_t aSourceLength,
                            int32_t aLength,
                            float aVolume,
                            int32_t aChannels,
-                           void* aOutput, nsAudioStream::SampleFormat aOutputFormat)
+                           void* aOutput, AudioSampleFormat aOutputFormat)
 {
   switch (aOutputFormat) {
-  case nsAudioStream::FORMAT_FLOAT32:
+  case AUDIO_FORMAT_FLOAT32:
     InterleaveAndConvertBuffer(aSource, aSourceLength, aLength, aVolume,
                                aChannels, static_cast<float*>(aOutput));
     break;
-  case nsAudioStream::FORMAT_S16:
+  case AUDIO_FORMAT_S16:
     InterleaveAndConvertBuffer(aSource, aSourceLength, aLength, aVolume,
                                aChannels, static_cast<int16_t*>(aOutput));
     break;
   }
 }
 
 static void
-InterleaveAndConvertBuffer(const void* aSource, nsAudioStream::SampleFormat aSourceFormat,
+InterleaveAndConvertBuffer(const void* aSource, AudioSampleFormat aSourceFormat,
                            int32_t aSourceLength,
                            int32_t aOffset, int32_t aLength,
                            float aVolume,
                            int32_t aChannels,
-                           void* aOutput, nsAudioStream::SampleFormat aOutputFormat)
+                           void* aOutput, AudioSampleFormat aOutputFormat)
 {
   switch (aSourceFormat) {
-  case nsAudioStream::FORMAT_FLOAT32:
+  case AUDIO_FORMAT_FLOAT32:
     InterleaveAndConvertBuffer(static_cast<const float*>(aSource) + aOffset, aSourceLength,
                                aLength,
                                aVolume,
                                aChannels,
                                aOutput, aOutputFormat);
     break;
-  case nsAudioStream::FORMAT_S16:
+  case AUDIO_FORMAT_S16:
     InterleaveAndConvertBuffer(static_cast<const int16_t*>(aSource) + aOffset, aSourceLength,
                                aLength,
                                aVolume,
                                aChannels,
                                aOutput, aOutputFormat);
     break;
   }
 }
--- a/content/media/AudioSegment.h
+++ b/content/media/AudioSegment.h
@@ -3,23 +3,25 @@
  * 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/. */
 
 #ifndef MOZILLA_AUDIOSEGMENT_H_
 #define MOZILLA_AUDIOSEGMENT_H_
 
 #include "MediaSegment.h"
 #include "nsISupportsImpl.h"
-#include "nsAudioStream.h"
+#include "AudioSampleFormat.h"
 #include "SharedBuffer.h"
 
+class nsAudioStream;
+
 namespace mozilla {
 
 struct AudioChunk {
-  typedef nsAudioStream::SampleFormat SampleFormat;
+  typedef mozilla::AudioSampleFormat SampleFormat;
 
   // Generic methods
   void SliceTo(TrackTicks aStart, TrackTicks aEnd)
   {
     NS_ASSERTION(aStart >= 0 && aStart < aEnd && aEnd <= mDuration,
                  "Slice out of bounds");
     if (mBuffer) {
       mOffset += int32_t(aStart);
@@ -57,23 +59,23 @@ struct AudioChunk {
 };
 
 /**
  * A list of audio samples consisting of a sequence of slices of SharedBuffers.
  * The audio rate is determined by the track, not stored in this class.
  */
 class AudioSegment : public MediaSegmentBase<AudioSegment, AudioChunk> {
 public:
-  typedef nsAudioStream::SampleFormat SampleFormat;
+  typedef mozilla::AudioSampleFormat SampleFormat;
 
   static int GetSampleSize(SampleFormat aFormat)
   {
     switch (aFormat) {
-    case nsAudioStream::FORMAT_S16: return 2;
-    case nsAudioStream::FORMAT_FLOAT32: return 4;
+    case AUDIO_FORMAT_S16: return 2;
+    case AUDIO_FORMAT_FLOAT32: return 4;
     }
     NS_ERROR("Bad format");
     return 0;
   }
 
   AudioSegment() : MediaSegmentBase<AudioSegment, AudioChunk>(AUDIO), mChannels(0) {}
 
   bool IsInitialized()
@@ -88,26 +90,26 @@ public:
   }
   int32_t GetChannels()
   {
     NS_ASSERTION(IsInitialized(), "Not initialized");
     return mChannels;
   }
   /**
    * Returns the format of the first audio frame that has data, or
-   * FORMAT_FLOAT32 if there is none.
+   * AUDIO_FORMAT_FLOAT32 if there is none.
    */
   SampleFormat GetFirstFrameFormat()
   {
     for (ChunkIterator ci(*this); !ci.IsEnded(); ci.Next()) {
       if (ci->mBuffer) {
         return ci->mBufferFormat;
       }
     }
-    return nsAudioStream::FORMAT_FLOAT32;
+    return AUDIO_FORMAT_FLOAT32;
   }
   void AppendFrames(already_AddRefed<SharedBuffer> aBuffer, int32_t aBufferLength,
                     int32_t aStart, int32_t aEnd, SampleFormat aFormat)
   {
     NS_ASSERTION(mChannels > 0, "Not initialized");
     AudioChunk* chunk = AppendChunk(aEnd - aStart);
     chunk->mBuffer = aBuffer;
     chunk->mBufferFormat = aFormat;
--- a/content/media/Makefile.in
+++ b/content/media/Makefile.in
@@ -10,16 +10,17 @@ FAIL_ON_WARNINGS := 1
 
 include $(DEPTH)/config/autoconf.mk
 
 MODULE = content
 LIBRARY_NAME = gkconmedia_s
 LIBXUL_LIBRARY = 1
 
 EXPORTS = \
+  AudioSampleFormat.h \
   AudioSegment.h \
   FileBlockCache.h \
   MediaResource.h \
   MediaSegment.h \
   MediaStreamGraph.h \
   nsAudioAvailableEventManager.h \
   nsBuiltinDecoder.h \
   nsBuiltinDecoderStateMachine.h \
--- a/content/media/SharedBuffer.h
+++ b/content/media/SharedBuffer.h
@@ -2,16 +2,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/. */
 
 #ifndef MOZILLA_SHAREDBUFFER_H_
 #define MOZILLA_SHAREDBUFFER_H_
 
 #include "mozilla/mozalloc.h"
+#include "nsCOMPtr.h"
+#include "nsAutoPtr.h"
 
 namespace mozilla {
 
 /**
  * Heap-allocated chunk of arbitrary data with threadsafe refcounting.
  * Typically you would allocate one of these, fill it in, and then treat it as
  * immutable while it's shared.
  * This only guarantees 4-byte alignment of the data. For alignment we
--- a/content/media/nsAudioStream.cpp
+++ b/content/media/nsAudioStream.cpp
@@ -471,17 +471,17 @@ nsresult nsNativeAudioStream::Write(cons
   if (mInError)
     return NS_ERROR_FAILURE;
 
   uint32_t samples = aFrames * mChannels;
   nsAutoArrayPtr<short> s_data(new short[samples]);
 
   if (s_data) {
     double scaled_volume = GetVolumeScale() * mVolume;
-    if (Format() == FORMAT_S16) {
+    if (Format() == AUDIO_FORMAT_S16) {
       const short* buf = static_cast<const short*>(aBuf);
       int32_t volume = int32_t((1 << 16) * scaled_volume);
       for (uint32_t i = 0; i < samples; ++i) {
         s_data[i] = short((int32_t(buf[i]) * volume) >> 16);
       }
     } else {
       const float* buf = static_cast<const float*>(aBuf);
       for (uint32_t i = 0; i <  samples; ++i) {
@@ -942,17 +942,17 @@ nsBufferedAudioStream::Init(int32_t aNum
   }
 
   mRate = aRate;
   mChannels = aNumChannels;
 
   cubeb_stream_params params;
   params.rate = aRate;
   params.channels = aNumChannels;
-  if (Format() == FORMAT_S16) {
+  if (Format() == AUDIO_FORMAT_S16) {
     params.format =  CUBEB_SAMPLE_S16NE;
     mBytesPerFrame = sizeof(int16_t) * aNumChannels;
   } else {
     params.format = CUBEB_SAMPLE_FLOAT32NE;
     mBytesPerFrame = sizeof(float) * aNumChannels;
   }
 
   {
@@ -1178,17 +1178,17 @@ nsBufferedAudioStream::DataCallback(void
     mBuffer.PopElements(available, &input[0], &input_size[0], &input[1], &input_size[1]);
 
     uint8_t* output = reinterpret_cast<uint8_t*>(aBuffer);
     for (int i = 0; i < 2; ++i) {
       // Fast path for unity volume case.
       if (scaled_volume == 1.0) {
         memcpy(output, input[i], input_size[i]);
         output += input_size[i];
-      } else if (Format() == FORMAT_S16) {
+      } else if (Format() == AUDIO_FORMAT_S16) {
         // Adjust volume as each sample is copied out.
         int32_t volume = int32_t(1 << 16) * scaled_volume;
 
         const short* src = static_cast<const short*>(input[i]);
         short* dst = reinterpret_cast<short*>(output);
         for (uint32_t j = 0; j < input_size[i] / (mBytesPerFrame / mChannels); ++j) {
           dst[j] = short((int32_t(src[j]) * volume) >> 16);
         }
--- a/content/media/nsAudioStream.h
+++ b/content/media/nsAudioStream.h
@@ -5,33 +5,25 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 #if !defined(nsAudioStream_h_)
 #define nsAudioStream_h_
 
 #include "nscore.h"
 #include "nsISupportsImpl.h"
 #include "nsIThread.h"
 #include "nsAutoPtr.h"
+#include "AudioSampleFormat.h"
 
 // Access to a single instance of this class must be synchronized by
 // callers, or made from a single thread.  One exception is that access to
 // GetPosition, GetPositionInFrames, SetVolume, and Get{Rate,Channels}
 // is thread-safe without external synchronization.
 class nsAudioStream : public nsISupports
 {
 public:
-
-  enum SampleFormat
-  {
-    // Native-endian signed 16-bit audio samples
-    FORMAT_S16,
-    // Signed 32-bit float samples
-    FORMAT_FLOAT32
-  };
-
   nsAudioStream()
     : mRate(0),
       mChannels(0)
   {}
 
   virtual ~nsAudioStream();
 
   // Initialize Audio Library. Some Audio backends require initializing the
@@ -102,21 +94,21 @@ public:
   // you can be sure that something will be played.
   // Unsafe to call with a monitor held due to synchronous event execution
   // on the main thread, which may attempt to acquire any held monitor.
   virtual int32_t GetMinWriteSize() = 0;
 
   int GetRate() { return mRate; }
   int GetChannels() { return mChannels; }
 
-  static SampleFormat Format() {
+  static mozilla::AudioSampleFormat Format() {
 #ifdef MOZ_SAMPLE_TYPE_S16
-    return nsAudioStream::FORMAT_S16;
+    return mozilla::AUDIO_FORMAT_S16;
 #else
-    return nsAudioStream::FORMAT_FLOAT32;
+    return mozilla::AUDIO_FORMAT_FLOAT32;
 #endif
   }
 
 protected:
   nsCOMPtr<nsIThread> mAudioPlaybackThread;
   int mRate;
   int mChannels;
 };
--- a/content/media/webrtc/MediaEngineWebRTCAudio.cpp
+++ b/content/media/webrtc/MediaEngineWebRTCAudio.cpp
@@ -242,16 +242,16 @@ MediaEngineWebRTCAudioSource::Process(co
   nsRefPtr<SharedBuffer> buffer = SharedBuffer::Create(length * sizeof(sample));
 
   sample* dest = static_cast<sample*>(buffer->Data());
   memcpy(dest, audio10ms, length * sizeof(sample));
 
   AudioSegment segment;
   segment.Init(CHANNELS);
   segment.AppendFrames(
-    buffer.forget(), length, 0, length, nsAudioStream::FORMAT_S16
+    buffer.forget(), length, 0, length, AUDIO_FORMAT_S16
   );
   mSource->AppendToTrack(mTrackID, &segment);
 
   return;
 }
 
 }
--- a/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
+++ b/media/webrtc/signaling/src/mediapipeline/MediaPipeline.cpp
@@ -547,22 +547,22 @@ NotifyQueuedTrackChanges(MediaStreamGrap
 void MediaPipelineTransmit::ProcessAudioChunk(AudioSessionConduit *conduit,
                                               TrackRate rate,
                                               AudioChunk& chunk) {
   // TODO(ekr@rtfm.com): Do more than one channel
   nsAutoArrayPtr<int16_t> samples(new int16_t[chunk.mDuration]);
 
   if (chunk.mBuffer) {
     switch(chunk.mBufferFormat) {
-      case nsAudioStream::FORMAT_FLOAT32:
+      case AUDIO_FORMAT_FLOAT32:
         MOZ_MTLOG(PR_LOG_ERROR, "Can't process audio except in 16-bit PCM yet");
         MOZ_ASSERT(PR_FALSE);
         return;
         break;
-      case nsAudioStream::FORMAT_S16:
+      case AUDIO_FORMAT_S16:
         {
           // Code based on nsAudioStream
           const short* buf = static_cast<const short *>(chunk.mBuffer->Data());
 
           int32_t volume = int32_t((1 << 16) * chunk.mVolume);
           for (uint32_t i = 0; i < chunk.mDuration; ++i) {
             int16_t s = buf[i];
 #if defined(IS_BIG_ENDIAN)
@@ -703,17 +703,17 @@ NotifyPull(MediaStreamGraph* graph, Stre
     if (err != kMediaConduitNoError)
       return;
 
     MOZ_MTLOG(PR_LOG_DEBUG, "Audio conduit returned buffer of length " << samples_length);
 
     AudioSegment segment;
     segment.Init(1);
     segment.AppendFrames(samples.forget(), samples_length,
-                         0, samples_length, nsAudioStream::FORMAT_S16);
+                         0, samples_length, AUDIO_FORMAT_S16);
 
     char buf[32];
     PR_snprintf(buf, 32, "%p", source);
     source->AppendToTrack(1,  // TODO(ekr@rtfm.com): Track ID
                           &segment);
   }
 }
 
--- a/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
+++ b/media/webrtc/signaling/src/peerconnection/PeerConnectionImpl.h
@@ -90,17 +90,17 @@ Fake_AudioGenerator(nsDOMMediaStream* aS
     for (int i=0; i<1600*2; i++) {
       reinterpret_cast<int16_t *>(samples->Data())[i] = ((gen->mCount % 8) * 4000) - (7*4000)/2;
       ++gen->mCount;
     }
 
     mozilla::AudioSegment segment;
     segment.Init(1);
     segment.AppendFrames(samples.forget(), 1600,
-      0, 1600, nsAudioStream::FORMAT_S16);
+      0, 1600, AUDIO_FORMAT_S16);
 
     gen->mStream->GetStream()->AsSourceStream()->AppendToTrack(1, &segment);
   }
 
  private:
   nsCOMPtr<nsITimer> mTimer;
   nsRefPtr<nsDOMMediaStream> mStream;
   int mCount;
--- a/media/webrtc/signaling/test/FakeMediaStreams.h
+++ b/media/webrtc/signaling/test/FakeMediaStreams.h
@@ -13,17 +13,16 @@
 #include "nsIComponentManager.h"
 #include "nsIComponentRegistrar.h"
 
 // #includes from MediaStream.h
 #include "mozilla/Mutex.h"
 #include "AudioSegment.h"
 #include "MediaSegment.h"
 #include "StreamBuffer.h"
-#include "nsAudioStream.h"
 #include "nsTArray.h"
 #include "nsIRunnable.h"
 #include "nsISupportsImpl.h"
 #include "nsIDOMMediaStream.h"
 
 namespace mozilla {
    class MediaStreamGraph;
    class MediaSegment;