This source file includes following definitions.
- weak_factory_
- InitializeTimers
- OnReceivedPayloadData
- GetRawAudioFrame
- DecodeEncodedAudioFrame
- EmitRawAudioFrame
- GetEncodedAudioFrame
- EmitAvailableEncodedFrames
- EmitAvailableEncodedFramesAfterWaiting
- IncomingPacket
- SetTargetDelay
- CastFeedback
- GetPlayoutTime
- ScheduleNextRtcpReport
- SendNextRtcpReport
- ScheduleNextCastMessage
- SendNextCastMessage
#include "media/cast/audio_receiver/audio_receiver.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "media/cast/audio_receiver/audio_decoder.h"
#include "media/cast/transport/cast_transport_defines.h"
namespace {
const int kTypicalAudioFrameDurationMs = 10;
const int kMinSchedulingDelayMs = 1;
}
namespace media {
namespace cast {
AudioReceiver::AudioReceiver(scoped_refptr<CastEnvironment> cast_environment,
const AudioReceiverConfig& audio_config,
transport::PacedPacketSender* const packet_sender)
: RtpReceiver(cast_environment->Clock(), &audio_config, NULL),
cast_environment_(cast_environment),
event_subscriber_(kReceiverRtcpEventHistorySize,
ReceiverRtcpEventSubscriber::kAudioEventSubscriber),
codec_(audio_config.codec),
frequency_(audio_config.frequency),
framer_(cast_environment->Clock(),
this,
audio_config.incoming_ssrc,
true,
0),
rtcp_(cast_environment,
NULL,
NULL,
packet_sender,
GetStatistics(),
audio_config.rtcp_mode,
base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval),
audio_config.feedback_ssrc,
audio_config.incoming_ssrc,
audio_config.rtcp_c_name),
is_waiting_for_consecutive_frame_(false),
weak_factory_(this) {
target_delay_delta_ =
base::TimeDelta::FromMilliseconds(audio_config.rtp_max_delay_ms);
if (!audio_config.use_external_decoder)
audio_decoder_.reset(new AudioDecoder(cast_environment, audio_config));
decryptor_.Initialize(audio_config.aes_key, audio_config.aes_iv_mask);
rtcp_.SetTargetDelay(target_delay_delta_);
cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
memset(frame_id_to_rtp_timestamp_, 0, sizeof(frame_id_to_rtp_timestamp_));
}
AudioReceiver::~AudioReceiver() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
while (!frame_request_queue_.empty()) {
frame_request_queue_.front().Run(
make_scoped_ptr<transport::EncodedAudioFrame>(NULL), base::TimeTicks());
frame_request_queue_.pop_front();
}
cast_environment_->Logging()->RemoveRawEventSubscriber(&event_subscriber_);
}
void AudioReceiver::InitializeTimers() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
ScheduleNextRtcpReport();
ScheduleNextCastMessage();
}
void AudioReceiver::OnReceivedPayloadData(const uint8* payload_data,
size_t payload_size,
const RtpCastHeader& rtp_header) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
frame_id_to_rtp_timestamp_[rtp_header.frame_id & 0xff] =
rtp_header.webrtc.header.timestamp;
cast_environment_->Logging()->InsertPacketEvent(
now, kAudioPacketReceived, rtp_header.webrtc.header.timestamp,
rtp_header.frame_id, rtp_header.packet_id, rtp_header.max_packet_id,
payload_size);
if (time_first_incoming_packet_.is_null()) {
InitializeTimers();
first_incoming_rtp_timestamp_ = rtp_header.webrtc.header.timestamp;
time_first_incoming_packet_ = now;
}
bool duplicate = false;
const bool complete =
framer_.InsertPacket(payload_data, payload_size, rtp_header, &duplicate);
if (duplicate) {
cast_environment_->Logging()->InsertPacketEvent(
now, kDuplicateAudioPacketReceived, rtp_header.webrtc.header.timestamp,
rtp_header.frame_id, rtp_header.packet_id, rtp_header.max_packet_id,
payload_size);
return;
}
if (!complete)
return;
EmitAvailableEncodedFrames();
}
void AudioReceiver::GetRawAudioFrame(
const AudioFrameDecodedCallback& callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
DCHECK(!callback.is_null());
DCHECK(audio_decoder_.get());
GetEncodedAudioFrame(base::Bind(
&AudioReceiver::DecodeEncodedAudioFrame,
base::Unretained(this),
callback));
}
void AudioReceiver::DecodeEncodedAudioFrame(
const AudioFrameDecodedCallback& callback,
scoped_ptr<transport::EncodedAudioFrame> encoded_frame,
const base::TimeTicks& playout_time) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (!encoded_frame) {
callback.Run(make_scoped_ptr<AudioBus>(NULL), playout_time, false);
return;
}
const uint32 frame_id = encoded_frame->frame_id;
const uint32 rtp_timestamp = encoded_frame->rtp_timestamp;
audio_decoder_->DecodeFrame(encoded_frame.Pass(),
base::Bind(&AudioReceiver::EmitRawAudioFrame,
cast_environment_,
callback,
frame_id,
rtp_timestamp,
playout_time));
}
void AudioReceiver::EmitRawAudioFrame(
const scoped_refptr<CastEnvironment>& cast_environment,
const AudioFrameDecodedCallback& callback,
uint32 frame_id,
uint32 rtp_timestamp,
const base::TimeTicks& playout_time,
scoped_ptr<AudioBus> audio_bus,
bool is_continuous) {
DCHECK(cast_environment->CurrentlyOn(CastEnvironment::MAIN));
if (audio_bus.get()) {
const base::TimeTicks now = cast_environment->Clock()->NowTicks();
cast_environment->Logging()->InsertFrameEvent(
now, kAudioFrameDecoded, rtp_timestamp, frame_id);
cast_environment->Logging()->InsertFrameEventWithDelay(
now, kAudioPlayoutDelay, rtp_timestamp, frame_id,
playout_time - now);
}
callback.Run(audio_bus.Pass(), playout_time, is_continuous);
}
void AudioReceiver::GetEncodedAudioFrame(
const AudioFrameEncodedCallback& callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
frame_request_queue_.push_back(callback);
EmitAvailableEncodedFrames();
}
void AudioReceiver::EmitAvailableEncodedFrames() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
while (!frame_request_queue_.empty()) {
scoped_ptr<transport::EncodedAudioFrame> encoded_frame(
new transport::EncodedAudioFrame());
bool is_consecutively_next_frame = false;
if (!framer_.GetEncodedAudioFrame(encoded_frame.get(),
&is_consecutively_next_frame)) {
VLOG(1) << "Wait for more audio packets to produce a completed frame.";
return;
}
const base::TimeTicks now = cast_environment_->Clock()->NowTicks();
const base::TimeTicks playout_time =
GetPlayoutTime(now, encoded_frame->rtp_timestamp);
if (!is_consecutively_next_frame) {
const base::TimeTicks earliest_possible_end_time_of_missing_frame =
now + base::TimeDelta::FromMilliseconds(kTypicalAudioFrameDurationMs);
if (earliest_possible_end_time_of_missing_frame < playout_time) {
VLOG(1) << "Wait for next consecutive frame instead of skipping.";
if (!is_waiting_for_consecutive_frame_) {
is_waiting_for_consecutive_frame_ = true;
cast_environment_->PostDelayedTask(
CastEnvironment::MAIN,
FROM_HERE,
base::Bind(&AudioReceiver::EmitAvailableEncodedFramesAfterWaiting,
weak_factory_.GetWeakPtr()),
playout_time - now);
}
return;
}
}
if (decryptor_.initialized()) {
std::string decrypted_audio_data;
if (!decryptor_.Decrypt(encoded_frame->frame_id,
encoded_frame->data,
&decrypted_audio_data)) {
framer_.ReleaseFrame(encoded_frame->frame_id);
continue;
}
encoded_frame->data.swap(decrypted_audio_data);
}
encoded_frame->codec = codec_;
framer_.ReleaseFrame(encoded_frame->frame_id);
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(frame_request_queue_.front(),
base::Passed(&encoded_frame),
playout_time));
frame_request_queue_.pop_front();
}
}
void AudioReceiver::EmitAvailableEncodedFramesAfterWaiting() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
DCHECK(is_waiting_for_consecutive_frame_);
is_waiting_for_consecutive_frame_ = false;
EmitAvailableEncodedFrames();
}
void AudioReceiver::IncomingPacket(scoped_ptr<Packet> packet) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
bool rtcp_packet = Rtcp::IsRtcpPacket(&packet->front(), packet->size());
if (!rtcp_packet) {
ReceivedPacket(&packet->front(), packet->size());
} else {
rtcp_.IncomingRtcpPacket(&packet->front(), packet->size());
}
}
void AudioReceiver::SetTargetDelay(base::TimeDelta target_delay) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
target_delay_delta_ = target_delay;
rtcp_.SetTargetDelay(target_delay_delta_);
}
void AudioReceiver::CastFeedback(const RtcpCastMessage& cast_message) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
base::TimeTicks now = cast_environment_->Clock()->NowTicks();
RtpTimestamp rtp_timestamp =
frame_id_to_rtp_timestamp_[cast_message.ack_frame_id_ & 0xff];
cast_environment_->Logging()->InsertFrameEvent(
now, kAudioAckSent, rtp_timestamp, cast_message.ack_frame_id_);
ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events;
event_subscriber_.GetRtcpEventsAndReset(&rtcp_events);
rtcp_.SendRtcpFromRtpReceiver(&cast_message, &rtcp_events);
}
base::TimeTicks AudioReceiver::GetPlayoutTime(base::TimeTicks now,
uint32 rtp_timestamp) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
base::TimeTicks rtp_timestamp_in_ticks;
base::TimeTicks playout_time;
if (time_offset_ == base::TimeDelta()) {
if (rtcp_.RtpTimestampInSenderTime(frequency_,
first_incoming_rtp_timestamp_,
&rtp_timestamp_in_ticks)) {
time_offset_ = time_first_incoming_packet_ - rtp_timestamp_in_ticks;
} else {
uint32 rtp_timestamp_diff = rtp_timestamp - first_incoming_rtp_timestamp_;
int frequency_khz = frequency_ / 1000;
base::TimeDelta rtp_time_diff_delta =
base::TimeDelta::FromMilliseconds(rtp_timestamp_diff / frequency_khz);
base::TimeDelta time_diff_delta = now - time_first_incoming_packet_;
playout_time = now + std::max(rtp_time_diff_delta - time_diff_delta,
base::TimeDelta());
}
}
if (playout_time.is_null()) {
if (rtcp_.RtpTimestampInSenderTime(frequency_, rtp_timestamp,
&rtp_timestamp_in_ticks)) {
playout_time =
rtp_timestamp_in_ticks + time_offset_ + target_delay_delta_;
} else {
playout_time = now;
}
}
#if 0
if (last_playout_time_ > playout_time) playout_time = last_playout_time_;
last_playout_time_ = playout_time;
#endif
return playout_time;
}
void AudioReceiver::ScheduleNextRtcpReport() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
base::TimeDelta time_to_send = rtcp_.TimeToSendNextRtcpReport() -
cast_environment_->Clock()->NowTicks();
time_to_send = std::max(
time_to_send, base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs));
cast_environment_->PostDelayedTask(
CastEnvironment::MAIN, FROM_HERE,
base::Bind(&AudioReceiver::SendNextRtcpReport,
weak_factory_.GetWeakPtr()),
time_to_send);
}
void AudioReceiver::SendNextRtcpReport() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
rtcp_.SendRtcpFromRtpReceiver(NULL, NULL);
ScheduleNextRtcpReport();
}
void AudioReceiver::ScheduleNextCastMessage() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
base::TimeTicks send_time;
framer_.TimeToSendNextCastMessage(&send_time);
base::TimeDelta time_to_send =
send_time - cast_environment_->Clock()->NowTicks();
time_to_send = std::max(
time_to_send, base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs));
cast_environment_->PostDelayedTask(
CastEnvironment::MAIN, FROM_HERE,
base::Bind(&AudioReceiver::SendNextCastMessage,
weak_factory_.GetWeakPtr()),
time_to_send);
}
void AudioReceiver::SendNextCastMessage() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
framer_.SendCastMessage();
ScheduleNextCastMessage();
}
}
}