This source file includes following definitions.
- SetUp
- TEST_F
- TEST_F
- TEST_F
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
#include "content/renderer/media/webrtc_audio_capturer.h"
#include "content/renderer/media/webrtc_local_audio_source_provider.h"
#include "content/renderer/media/webrtc_local_audio_track.h"
#include "media/audio/audio_parameters.h"
#include "media/base/audio_bus.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebMediaConstraints.h"
#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
namespace content {
class WebRtcLocalAudioSourceProviderTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
source_params_.Reset(media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::CHANNEL_LAYOUT_MONO, 1, 0, 48000, 16, 480);
sink_params_.Reset(
media::AudioParameters::AUDIO_PCM_LOW_LATENCY,
media::CHANNEL_LAYOUT_STEREO, 2, 0, 44100, 16,
WebRtcLocalAudioSourceProvider::kWebAudioRenderBufferSize);
const int length =
source_params_.frames_per_buffer() * source_params_.channels();
source_data_.reset(new int16[length]);
sink_bus_ = media::AudioBus::Create(sink_params_);
blink::WebMediaConstraints constraints;
scoped_refptr<WebRtcAudioCapturer> capturer(
WebRtcAudioCapturer::CreateCapturer(-1, StreamDeviceInfo(),
constraints, NULL));
scoped_refptr<WebRtcLocalAudioTrackAdapter> adapter(
WebRtcLocalAudioTrackAdapter::Create(std::string(), NULL));
scoped_ptr<WebRtcLocalAudioTrack> native_track(
new WebRtcLocalAudioTrack(adapter, capturer, NULL));
blink::WebMediaStreamSource audio_source;
audio_source.initialize(base::UTF8ToUTF16("dummy_source_id"),
blink::WebMediaStreamSource::TypeAudio,
base::UTF8ToUTF16("dummy_source_name"));
blink_track_.initialize(blink::WebString::fromUTF8("audio_track"),
audio_source);
blink_track_.setExtraData(native_track.release());
source_provider_.reset(new WebRtcLocalAudioSourceProvider(blink_track_));
source_provider_->SetSinkParamsForTesting(sink_params_);
source_provider_->OnSetFormat(source_params_);
}
media::AudioParameters source_params_;
scoped_ptr<int16[]> source_data_;
media::AudioParameters sink_params_;
scoped_ptr<media::AudioBus> sink_bus_;
blink::WebMediaStreamTrack blink_track_;
scoped_ptr<WebRtcLocalAudioSourceProvider> source_provider_;
};
TEST_F(WebRtcLocalAudioSourceProviderTest, VerifyDataFlow) {
blink::WebVector<float*> audio_data(
static_cast<size_t>(sink_bus_->channels()));
for (size_t i = 0; i < audio_data.size(); ++i)
audio_data[i] = sink_bus_->channel(i);
source_provider_->provideInput(audio_data, sink_params_.frames_per_buffer());
EXPECT_TRUE(sink_bus_->channel(0)[0] == 0);
const int length =
source_params_.frames_per_buffer() * source_params_.channels();
std::fill(source_data_.get(), source_data_.get() + length, 1);
source_provider_->OnData(source_data_.get(),
source_params_.sample_rate(),
source_params_.channels(),
source_params_.frames_per_buffer());
for (int i = sink_params_.frames_per_buffer();
i < source_params_.frames_per_buffer();
i += sink_params_.frames_per_buffer()) {
sink_bus_->Zero();
source_provider_->provideInput(audio_data,
sink_params_.frames_per_buffer());
EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(0)[0]);
EXPECT_DOUBLE_EQ(0.0, sink_bus_->channel(1)[0]);
}
source_provider_->OnData(source_data_.get(),
source_params_.sample_rate(),
source_params_.channels(),
source_params_.frames_per_buffer());
for (int i = 0; i < source_params_.frames_per_buffer();
i += sink_params_.frames_per_buffer()) {
sink_bus_->Zero();
source_provider_->provideInput(audio_data,
sink_params_.frames_per_buffer());
EXPECT_GT(sink_bus_->channel(0)[0], 0);
EXPECT_GT(sink_bus_->channel(1)[0], 0);
EXPECT_DOUBLE_EQ(sink_bus_->channel(0)[0], sink_bus_->channel(1)[0]);
}
}
TEST_F(WebRtcLocalAudioSourceProviderTest,
DeleteSourceProviderBeforeStoppingTrack) {
source_provider_.reset();
WebRtcLocalAudioTrack* native_track = static_cast<WebRtcLocalAudioTrack*>(
MediaStreamTrack::GetTrack(blink_track_));
native_track->Stop();
}
TEST_F(WebRtcLocalAudioSourceProviderTest,
StopTrackBeforeDeletingSourceProvider) {
WebRtcLocalAudioTrack* native_track = static_cast<WebRtcLocalAudioTrack*>(
MediaStreamTrack::GetTrack(blink_track_));
native_track->Stop();
source_provider_.reset();
}
}