Chromium Code Reviews| Index: chrome/renderer/media/cast_rtp_stream.cc |
| diff --git a/chrome/renderer/media/cast_rtp_stream.cc b/chrome/renderer/media/cast_rtp_stream.cc |
| index 36af2795018cf4dbe0760d444253cb8984be8602..6be3016ae3c224f2a75fd62d8defbaf184d7180d 100644 |
| --- a/chrome/renderer/media/cast_rtp_stream.cc |
| +++ b/chrome/renderer/media/cast_rtp_stream.cc |
| @@ -12,8 +12,10 @@ |
| #include "content/public/renderer/media_stream_audio_sink.h" |
| #include "content/public/renderer/media_stream_video_sink.h" |
| #include "content/public/renderer/render_thread.h" |
| +#include "media/audio/audio_parameters.h" |
| #include "media/base/audio_bus.h" |
| #include "media/base/bind_to_current_loop.h" |
| +#include "media/base/multi_channel_resampler.h" |
| #include "media/cast/cast_config.h" |
| #include "media/cast/cast_defines.h" |
| #include "media/cast/cast_sender.h" |
| @@ -174,14 +176,21 @@ class CastAudioSink : public base::SupportsWeakPtr<CastAudioSink>, |
| // |track| provides data for this sink. |
| // |error_callback| is called if audio formats don't match. |
| CastAudioSink(const blink::WebMediaStreamTrack& track, |
| - const CastRtpStream::ErrorCallback& error_callback) |
| + const CastRtpStream::ErrorCallback& error_callback, |
| + int output_channels, |
| + int output_sample_rate) |
| : track_(track), |
| sink_added_(false), |
| error_callback_(error_callback), |
| weak_factory_(this), |
| render_thread_task_runner_(content::RenderThread::Get() |
| - ->GetMessageLoop() |
| - ->message_loop_proxy()) {} |
| + ->GetMessageLoop() |
| + ->message_loop_proxy()), |
| + output_channels_(output_channels), |
| + output_sample_rate_(output_sample_rate), |
| + input_data_(NULL), |
| + input_frames_(0), |
| + input_bytes_per_frame_(0) {} |
| virtual ~CastAudioSink() { |
| if (sink_added_) |
| @@ -194,9 +203,17 @@ class CastAudioSink : public base::SupportsWeakPtr<CastAudioSink>, |
| int sample_rate, |
| int number_of_channels, |
| int number_of_frames) OVERRIDE { |
| - scoped_ptr<media::AudioBus> audio_bus( |
| - media::AudioBus::Create(number_of_channels, number_of_frames)); |
| - audio_bus->FromInterleaved(audio_data, number_of_frames, 2); |
| + input_data_ = audio_data; |
| + input_frames_ = number_of_frames; |
| + |
| + DCHECK_EQ(number_of_channels, output_channels_); |
| + scoped_ptr<media::AudioBus> output_bus( |
| + media::AudioBus::Create( |
| + output_channels_, |
| + output_sample_rate_ * number_of_frames / sample_rate)); |
| + resampler_->Resample(output_bus->frames(), output_bus.get()); |
|
miu
2014/03/05 20:59:00
nit: Add a comment that the resampler will invoke
Alpha Left Google
2014/03/06 00:14:56
Done.
|
| + input_data_ = NULL; |
| + input_frames_ = 0; |
| // TODO(hclam): Pass in the accurate capture time to have good |
| // audio / video sync. |
| @@ -204,27 +221,32 @@ class CastAudioSink : public base::SupportsWeakPtr<CastAudioSink>, |
| // TODO(hclam): We shouldn't hop through the render thread. |
| // Bounce the call from the real-time audio thread to the render thread. |
| // Needed since frame_input_ can be changed runtime by the render thread. |
| - media::AudioBus* const audio_bus_ptr = audio_bus.get(); |
| + media::AudioBus* const output_bus_ptr = output_bus.get(); |
| render_thread_task_runner_->PostTask( |
| FROM_HERE, |
| base::Bind(&CastAudioSink::SendAudio, |
| weak_factory_.GetWeakPtr(), |
| - audio_bus_ptr, |
| + output_bus_ptr, |
| base::TimeTicks::Now(), |
| - base::Bind(&DeleteAudioBus, base::Passed(&audio_bus)))); |
| + base::Bind(&DeleteAudioBus, base::Passed(&output_bus)))); |
| } |
| - void SendAudio(const media::AudioBus* audio_bus_ptr, |
| + void SendAudio(const media::AudioBus* audio_bus, |
| const base::TimeTicks& recorded_time, |
| const base::Closure& done_callback) { |
| DCHECK(render_thread_task_runner_->BelongsToCurrentThread()); |
| DCHECK(frame_input_); |
| - frame_input_->InsertAudio(audio_bus_ptr, recorded_time, done_callback); |
| + frame_input_->InsertAudio(audio_bus, recorded_time, done_callback); |
| } |
| // Called on real-time audio thread. |
| virtual void OnSetFormat(const media::AudioParameters& params) OVERRIDE { |
| - NOTIMPLEMENTED(); |
| + resampler_.reset(new media::MultiChannelResampler( |
| + output_channels_, |
| + static_cast<double>(params.sample_rate()) / output_sample_rate_, |
| + params.frames_per_buffer(), |
| + base::Bind(&CastAudioSink::ProvideData, base::Unretained(this)))); |
| + input_bytes_per_frame_ = params.bits_per_sample() / 8; |
| } |
| // See CastVideoSink for details. |
| @@ -237,6 +259,12 @@ class CastAudioSink : public base::SupportsWeakPtr<CastAudioSink>, |
| } |
| } |
| + void ProvideData(int frame_delay, media::AudioBus* output_bus) { |
| + DCHECK_EQ(input_frames_, output_bus->frames()); |
| + output_bus->FromInterleaved(input_data_, input_frames_, |
| + input_bytes_per_frame_); |
| + } |
| + |
| private: |
| blink::WebMediaStreamTrack track_; |
| scoped_refptr<media::cast::FrameInput> frame_input_; |
| @@ -245,6 +273,13 @@ class CastAudioSink : public base::SupportsWeakPtr<CastAudioSink>, |
| base::WeakPtrFactory<CastAudioSink> weak_factory_; |
| scoped_refptr<base::SingleThreadTaskRunner> render_thread_task_runner_; |
| + scoped_ptr<media::MultiChannelResampler> resampler_; |
| + int output_channels_; |
|
miu
2014/03/05 20:59:00
nit: The two output_xxx_ members should be const.
Alpha Left Google
2014/03/06 00:14:56
Done.
|
| + int output_sample_rate_; |
| + const void* input_data_; |
| + int input_frames_; |
| + int input_bytes_per_frame_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(CastAudioSink); |
| }; |
| @@ -309,12 +344,15 @@ void CastRtpStream::Start(const CastRtpParams& params, |
| DidEncounterError("Invalid parameters for audio."); |
| return; |
| } |
| + |
| // In case of error we have to go through DidEncounterError() to stop |
| // the streaming after reporting the error. |
| audio_sink_.reset(new CastAudioSink( |
| track_, |
| media::BindToCurrentLoop(base::Bind(&CastRtpStream::DidEncounterError, |
| - weak_factory_.GetWeakPtr())))); |
| + weak_factory_.GetWeakPtr())), |
| + params.payload.channels, |
| + params.payload.clock_rate)); |
| cast_session_->StartAudio( |
| config, |
| base::Bind(&CastAudioSink::AddToTrack, |