This source file includes following definitions.
- SetNextExpectedResult
- DeliverEncodedAudioFrame
- number_times_called
- Configure
- SetUp
- FeedOneFrameIntoReceiver
- TEST_F
- TEST_F
#include <stdint.h>
#include "base/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/test/simple_test_tick_clock.h"
#include "media/cast/audio_receiver/audio_receiver.h"
#include "media/cast/cast_defines.h"
#include "media/cast/cast_environment.h"
#include "media/cast/logging/simple_event_subscriber.h"
#include "media/cast/rtcp/test_rtcp_packet_builder.h"
#include "media/cast/test/fake_single_thread_task_runner.h"
#include "media/cast/transport/pacing/mock_paced_packet_sender.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace media {
namespace cast {
static const int64 kStartMillisecond = INT64_C(12345678900000);
namespace {
class FakeAudioClient {
public:
FakeAudioClient() : num_called_(0) {}
virtual ~FakeAudioClient() {}
void SetNextExpectedResult(uint8 expected_frame_id,
const base::TimeTicks& expected_playout_time) {
expected_frame_id_ = expected_frame_id;
expected_playout_time_ = expected_playout_time;
}
void DeliverEncodedAudioFrame(
scoped_ptr<transport::EncodedAudioFrame> audio_frame,
const base::TimeTicks& playout_time) {
ASSERT_FALSE(!audio_frame)
<< "If at shutdown: There were unsatisfied requests enqueued.";
EXPECT_EQ(expected_frame_id_, audio_frame->frame_id);
EXPECT_EQ(transport::kPcm16, audio_frame->codec);
EXPECT_EQ(expected_playout_time_, playout_time);
num_called_++;
}
int number_times_called() const { return num_called_; }
private:
int num_called_;
uint8 expected_frame_id_;
base::TimeTicks expected_playout_time_;
DISALLOW_COPY_AND_ASSIGN(FakeAudioClient);
};
}
class AudioReceiverTest : public ::testing::Test {
protected:
AudioReceiverTest() {
audio_config_.rtp_payload_type = 127;
audio_config_.frequency = 16000;
audio_config_.channels = 1;
audio_config_.codec = transport::kPcm16;
audio_config_.use_external_decoder = false;
audio_config_.feedback_ssrc = 1234;
testing_clock_ = new base::SimpleTestTickClock();
testing_clock_->Advance(
base::TimeDelta::FromMilliseconds(kStartMillisecond));
task_runner_ = new test::FakeSingleThreadTaskRunner(testing_clock_);
cast_environment_ = new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_).Pass(),
task_runner_,
task_runner_,
task_runner_);
}
void Configure(bool use_external_decoder) {
audio_config_.use_external_decoder = use_external_decoder;
receiver_.reset(new AudioReceiver(cast_environment_, audio_config_,
&mock_transport_));
}
virtual ~AudioReceiverTest() {}
virtual void SetUp() {
payload_.assign(kMaxIpPacketSize, 0);
rtp_header_.is_key_frame = true;
rtp_header_.frame_id = 0;
rtp_header_.packet_id = 0;
rtp_header_.max_packet_id = 0;
rtp_header_.is_reference = false;
rtp_header_.reference_frame_id = 0;
rtp_header_.webrtc.header.timestamp = 0;
}
void FeedOneFrameIntoReceiver() {
receiver_->OnReceivedPayloadData(
payload_.data(), payload_.size(), rtp_header_);
}
AudioReceiverConfig audio_config_;
std::vector<uint8> payload_;
RtpCastHeader rtp_header_;
base::SimpleTestTickClock* testing_clock_;
transport::MockPacedPacketSender mock_transport_;
scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner_;
scoped_refptr<CastEnvironment> cast_environment_;
FakeAudioClient fake_audio_client_;
scoped_ptr<AudioReceiver> receiver_;
};
TEST_F(AudioReceiverTest, GetOnePacketEncodedframe) {
SimpleEventSubscriber event_subscriber;
cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber);
Configure(true);
EXPECT_CALL(mock_transport_, SendRtcpPacket(testing::_)).Times(1);
receiver_->GetEncodedAudioFrame(
base::Bind(&FakeAudioClient::DeliverEncodedAudioFrame,
base::Unretained(&fake_audio_client_)));
task_runner_->RunTasks();
EXPECT_EQ(0, fake_audio_client_.number_times_called());
fake_audio_client_.SetNextExpectedResult(0, testing_clock_->NowTicks());
FeedOneFrameIntoReceiver();
task_runner_->RunTasks();
EXPECT_EQ(1, fake_audio_client_.number_times_called());
std::vector<FrameEvent> frame_events;
event_subscriber.GetFrameEventsAndReset(&frame_events);
ASSERT_TRUE(!frame_events.empty());
EXPECT_EQ(kAudioAckSent, frame_events.begin()->type);
EXPECT_EQ(rtp_header_.frame_id, frame_events.begin()->frame_id);
EXPECT_EQ(rtp_header_.webrtc.header.timestamp,
frame_events.begin()->rtp_timestamp);
cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber);
}
TEST_F(AudioReceiverTest, MultiplePendingGetCalls) {
Configure(true);
EXPECT_CALL(mock_transport_, SendRtcpPacket(testing::_))
.WillRepeatedly(testing::Return(true));
const AudioFrameEncodedCallback frame_encoded_callback =
base::Bind(&FakeAudioClient::DeliverEncodedAudioFrame,
base::Unretained(&fake_audio_client_));
receiver_->GetEncodedAudioFrame(frame_encoded_callback);
task_runner_->RunTasks();
EXPECT_EQ(0, fake_audio_client_.number_times_called());
fake_audio_client_.SetNextExpectedResult(0, testing_clock_->NowTicks());
FeedOneFrameIntoReceiver();
task_runner_->RunTasks();
EXPECT_EQ(1, fake_audio_client_.number_times_called());
TestRtcpPacketBuilder rtcp_packet;
uint32 ntp_high;
uint32 ntp_low;
ConvertTimeTicksToNtp(testing_clock_->NowTicks(), &ntp_high, &ntp_low);
rtcp_packet.AddSrWithNtp(audio_config_.feedback_ssrc, ntp_high, ntp_low,
rtp_header_.webrtc.header.timestamp);
testing_clock_->Advance(base::TimeDelta::FromMilliseconds(20));
receiver_->IncomingPacket(rtcp_packet.GetPacket().Pass());
receiver_->GetEncodedAudioFrame(frame_encoded_callback);
task_runner_->RunTasks();
EXPECT_EQ(1, fake_audio_client_.number_times_called());
rtp_header_.is_key_frame = false;
rtp_header_.frame_id = 2;
rtp_header_.is_reference = true;
rtp_header_.reference_frame_id = 0;
rtp_header_.webrtc.header.timestamp = 960;
fake_audio_client_.SetNextExpectedResult(
2, testing_clock_->NowTicks() + base::TimeDelta::FromMilliseconds(100));
FeedOneFrameIntoReceiver();
task_runner_->RunTasks();
EXPECT_EQ(1, fake_audio_client_.number_times_called());
receiver_->GetEncodedAudioFrame(frame_encoded_callback);
task_runner_->RunTasks();
EXPECT_EQ(1, fake_audio_client_.number_times_called());
testing_clock_->Advance(base::TimeDelta::FromMilliseconds(100));
task_runner_->RunTasks();
EXPECT_EQ(2, fake_audio_client_.number_times_called());
rtp_header_.frame_id = 3;
rtp_header_.is_reference = false;
rtp_header_.reference_frame_id = 0;
rtp_header_.webrtc.header.timestamp = 1280;
fake_audio_client_.SetNextExpectedResult(3, testing_clock_->NowTicks());
FeedOneFrameIntoReceiver();
task_runner_->RunTasks();
EXPECT_EQ(3, fake_audio_client_.number_times_called());
testing_clock_->Advance(base::TimeDelta::FromMilliseconds(100));
task_runner_->RunTasks();
EXPECT_EQ(3, fake_audio_client_.number_times_called());
}
}
}