Bug 1248513 - Fix static analysis errors in non-unified build for AudioStream.cpp. r=gerald.
MozReview-Commit-ID: 77AC9c2TKqb
--- a/dom/media/AudioStream.cpp
+++ b/dom/media/AudioStream.cpp
@@ -268,40 +268,57 @@ OpenDumpFile(AudioStream* aStream)
SetUint16LE(header + CHANNEL_OFFSET, aStream->GetChannels());
SetUint32LE(header + SAMPLE_RATE_OFFSET, aStream->GetRate());
SetUint16LE(header + BLOCK_ALIGN_OFFSET, aStream->GetChannels()*2);
fwrite(header, sizeof(header), 1, f);
return f;
}
+template <typename T>
+typename EnableIf<IsSame<T, int16_t>::value, void>::Type
+WriteDumpFileHelper(T* aInput, size_t aSamples, FILE* aFile) {
+ fwrite(aInput, sizeof(T), aSamples, aFile);
+}
+
+template <typename T>
+typename EnableIf<IsSame<T, float>::value, void>::Type
+WriteDumpFileHelper(T* aInput, size_t aSamples, FILE* aFile) {
+ AutoTArray<uint8_t, 1024*2> buf;
+ buf.SetLength(aSamples*2);
+ uint8_t* output = buf.Elements();
+ for (uint32_t i = 0; i < aSamples; ++i) {
+ SetUint16LE(output + i*2, int16_t(aInput[i]*32767.0f));
+ }
+ fwrite(output, 2, aSamples, aFile);
+ fflush(aFile);
+}
+
static void
WriteDumpFile(FILE* aDumpFile, AudioStream* aStream, uint32_t aFrames,
void* aBuffer)
{
if (!aDumpFile)
return;
uint32_t samples = aStream->GetOutChannels()*aFrames;
- if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) {
- fwrite(aBuffer, 2, samples, aDumpFile);
- return;
- }
+
+ using SampleT = AudioSampleTraits<AUDIO_OUTPUT_FORMAT>::Type;
+ WriteDumpFileHelper(reinterpret_cast<SampleT*>(aBuffer), samples, aDumpFile);
+}
- NS_ASSERTION(AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_FLOAT32, "bad format");
- AutoTArray<uint8_t, 1024*2> buf;
- buf.SetLength(samples*2);
- float* input = static_cast<float*>(aBuffer);
- uint8_t* output = buf.Elements();
- for (uint32_t i = 0; i < samples; ++i) {
- SetUint16LE(output + i*2, int16_t(input[i]*32767.0f));
- }
- fwrite(output, 2, samples, aDumpFile);
- fflush(aDumpFile);
-}
+template <AudioSampleFormat N>
+struct ToCubebFormat {
+ static const cubeb_sample_format value = CUBEB_SAMPLE_FLOAT32NE;
+};
+
+template <>
+struct ToCubebFormat<AUDIO_FORMAT_S16> {
+ static const cubeb_sample_format value = CUBEB_SAMPLE_S16NE;
+};
nsresult
AudioStream::Init(uint32_t aNumChannels, uint32_t aRate,
const dom::AudioChannel aAudioChannel)
{
mStartTime = TimeStamp::Now();
mIsFirst = CubebUtils::GetFirstStream();
@@ -328,22 +345,18 @@ AudioStream::Init(uint32_t aNumChannels,
mAudioChannel = dom::AudioChannel::Content;
params.stream_type = CUBEB_STREAM_TYPE_MUSIC;
#endif
if (params.stream_type == CUBEB_STREAM_TYPE_MAX) {
return NS_ERROR_INVALID_ARG;
}
#endif
- if (AUDIO_OUTPUT_FORMAT == AUDIO_FORMAT_S16) {
- params.format = CUBEB_SAMPLE_S16NE;
- } else {
- params.format = CUBEB_SAMPLE_FLOAT32NE;
- }
+ params.format = ToCubebFormat<AUDIO_OUTPUT_FORMAT>::value;
mAudioClock.Init();
return OpenCubeb(params);
}
// This code used to live inside AudioStream::Init(), but on Mac (others?)
// it has been known to take 300-800 (or even 8500) ms to execute(!)
nsresult