Bug 901633 - Part 8 - Use our new generic packetizer in the MediaPipeline so that we can packetize stereo easily. r=jesup

This commit is contained in:
Paul Adenot 2015-08-11 13:49:29 +02:00
parent a757399b09
commit a7034137c7
2 changed files with 24 additions and 69 deletions

View File

@ -979,64 +979,29 @@ void MediaPipelineTransmit::PipelineListener::ProcessAudioChunk(
MOZ_ASSERT(!(rate%100)); // rate should be a multiple of 100
// Check if the rate has changed since the last time we came through
// I realize it may be overkill to check if the rate has changed, but
// I believe it is possible (e.g. if we change sources) and it costs us
// very little to handle this case
// Check if the rate or the number of channels has changed since the last time
// we came through I realize it may be overkill to check if the rate has
// changed, but I believe it is possible (e.g. if we change sources) and it
// costs us very little to handle this case
if (samplenum_10ms_ != rate/100) {
// Determine number of samples in 10 ms from the rate:
samplenum_10ms_ = rate/100;
// If we switch sample rates (e.g. if we switch codecs),
// we throw away what was in the sample_10ms_buffer at the old rate
samples_10ms_buffer_ = new int16_t[samplenum_10ms_];
buffer_current_ = 0;
uint32_t audio_10ms = rate / 100;
if (!packetizer_ ||
packetizer_->PacketSize() != audio_10ms ||
packetizer_->Channels() != outputChannels) {
// It's ok to drop the audio still in the packetizer here.
packetizer_ = new AudioPacketizer<int16_t, int16_t>(audio_10ms, outputChannels);
}
packetizer_->Input(convertedSamples, chunk.mDuration);
while (packetizer_->PacketsAvailable()) {
uint32_t samplesPerPacket = packetizer_->PacketSize() *
packetizer_->Channels();
conduit->SendAudioFrame(packetizer_->Output(),
samplesPerPacket ,
rate, 0);
}
// Vars to handle the non-sunny-day case (where the audio chunks
// we got are not multiples of 10ms OR there were samples left over
// from the last run)
int64_t chunk_remaining;
int64_t tocpy;
int16_t *samples_tmp = convertedSamples.get();
chunk_remaining = chunk.mDuration;
MOZ_ASSERT(chunk_remaining >= 0);
if (buffer_current_) {
tocpy = std::min(chunk_remaining, samplenum_10ms_ - buffer_current_);
memcpy(&samples_10ms_buffer_[buffer_current_], samples_tmp, tocpy * sizeof(int16_t));
buffer_current_ += tocpy;
samples_tmp += tocpy;
chunk_remaining -= tocpy;
if (buffer_current_ == samplenum_10ms_) {
// Send out the audio buffer we just finished filling
conduit->SendAudioFrame(samples_10ms_buffer_, samplenum_10ms_, rate, 0);
buffer_current_ = 0;
} else {
// We still don't have enough data to send a buffer
return;
}
}
// Now send (more) frames if there is more than 10ms of input left
tocpy = (chunk_remaining / samplenum_10ms_) * samplenum_10ms_;
if (tocpy > 0) {
conduit->SendAudioFrame(samples_tmp, tocpy, rate, 0);
samples_tmp += tocpy;
chunk_remaining -= tocpy;
}
// Copy what remains for the next run
MOZ_ASSERT(chunk_remaining < samplenum_10ms_);
if (chunk_remaining) {
memcpy(samples_10ms_buffer_, samples_tmp, chunk_remaining * sizeof(int16_t));
buffer_current_ = chunk_remaining;
}
}
#if !defined(MOZILLA_EXTERNAL_LINKAGE)

View File

@ -26,6 +26,7 @@
#include "databuffer.h"
#include "runnable_utils.h"
#include "transportflow.h"
#include "AudioPacketizer.h"
#if defined(MOZILLA_INTERNAL_API)
#include "VideoSegment.h"
@ -446,9 +447,7 @@ public:
active_(false),
enabled_(false),
direct_connect_(false),
samples_10ms_buffer_(nullptr),
buffer_current_(0),
samplenum_10ms_(0)
packetizer_(nullptr)
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
, last_img_(-1)
#endif // MOZILLA_INTERNAL_API
@ -526,16 +525,7 @@ public:
bool direct_connect_;
// These vars handle breaking audio samples into exact 10ms chunks:
// The buffer of 10ms audio samples that we will send once full
// (can be carried over from one call to another).
nsAutoArrayPtr<int16_t> samples_10ms_buffer_;
// The location of the pointer within that buffer (in units of samples).
int64_t buffer_current_;
// The number of samples in a 10ms audio chunk.
int64_t samplenum_10ms_;
nsAutoPtr<AudioPacketizer<int16_t, int16_t>> packetizer_;
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
int32_t last_img_; // serial number of last Image
#endif // MOZILLA_INTERNAL_API