| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/cast/audio_receiver/audio_receiver.h" | 5 #include "media/cast/audio_receiver/audio_receiver.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "media/cast/audio_receiver/audio_decoder.h" | 12 #include "media/cast/audio_receiver/audio_decoder.h" |
| 13 #include "media/cast/transport/cast_transport_defines.h" | 13 #include "media/cast/transport/cast_transport_defines.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 const int kMinSchedulingDelayMs = 1; | 16 const int kMinSchedulingDelayMs = 1; |
| 17 // TODO(miu): This should go in AudioReceiverConfig. | 17 // TODO(miu): This should go in AudioReceiverConfig. |
| 18 const int kTypicalAudioFrameDurationMs = 10; | 18 const int kTypicalAudioFrameDurationMs = 10; |
| 19 } // namespace | 19 } // namespace |
| 20 | 20 |
| 21 namespace media { | 21 namespace media { |
| 22 namespace cast { | 22 namespace cast { |
| 23 | 23 |
| 24 AudioReceiver::AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, | 24 AudioReceiver::AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, |
| 25 const AudioReceiverConfig& audio_config, | 25 const AudioReceiverConfig& audio_config, |
| 26 transport::PacedPacketSender* const packet_sender) | 26 transport::PacedPacketSender* const packet_sender) |
| 27 : RtpReceiver(cast_environment->Clock(), &audio_config, NULL), | 27 : RtpReceiver(cast_environment->Clock(), &audio_config, NULL), |
| 28 cast_environment_(cast_environment), | 28 cast_environment_(cast_environment), |
| 29 event_subscriber_(kReceiverRtcpEventHistorySize, | 29 event_subscriber_(kReceiverRtcpEventHistorySize, AUDIO_EVENT), |
| 30 ReceiverRtcpEventSubscriber::kAudioEventSubscriber), | |
| 31 codec_(audio_config.codec), | 30 codec_(audio_config.codec), |
| 32 frequency_(audio_config.frequency), | 31 frequency_(audio_config.frequency), |
| 33 target_delay_delta_( | 32 target_delay_delta_( |
| 34 base::TimeDelta::FromMilliseconds(audio_config.rtp_max_delay_ms)), | 33 base::TimeDelta::FromMilliseconds(audio_config.rtp_max_delay_ms)), |
| 35 framer_(cast_environment->Clock(), | 34 framer_(cast_environment->Clock(), |
| 36 this, | 35 this, |
| 37 audio_config.incoming_ssrc, | 36 audio_config.incoming_ssrc, |
| 38 true, | 37 true, |
| 39 audio_config.rtp_max_delay_ms / kTypicalAudioFrameDurationMs), | 38 audio_config.rtp_max_delay_ms / kTypicalAudioFrameDurationMs), |
| 40 rtcp_(cast_environment, | 39 rtcp_(cast_environment, |
| 41 NULL, | 40 NULL, |
| 42 NULL, | 41 NULL, |
| 43 packet_sender, | 42 packet_sender, |
| 44 GetStatistics(), | 43 GetStatistics(), |
| 45 audio_config.rtcp_mode, | 44 audio_config.rtcp_mode, |
| 46 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval), | 45 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval), |
| 47 audio_config.feedback_ssrc, | 46 audio_config.feedback_ssrc, |
| 48 audio_config.incoming_ssrc, | 47 audio_config.incoming_ssrc, |
| 49 audio_config.rtcp_c_name), | 48 audio_config.rtcp_c_name, |
| 49 true), |
| 50 is_waiting_for_consecutive_frame_(false), | 50 is_waiting_for_consecutive_frame_(false), |
| 51 weak_factory_(this) { | 51 weak_factory_(this) { |
| 52 if (!audio_config.use_external_decoder) | 52 if (!audio_config.use_external_decoder) |
| 53 audio_decoder_.reset(new AudioDecoder(cast_environment, audio_config)); | 53 audio_decoder_.reset(new AudioDecoder(cast_environment, audio_config)); |
| 54 decryptor_.Initialize(audio_config.aes_key, audio_config.aes_iv_mask); | 54 decryptor_.Initialize(audio_config.aes_key, audio_config.aes_iv_mask); |
| 55 rtcp_.SetTargetDelay(target_delay_delta_); | 55 rtcp_.SetTargetDelay(target_delay_delta_); |
| 56 cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_); | 56 cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_); |
| 57 memset(frame_id_to_rtp_timestamp_, 0, sizeof(frame_id_to_rtp_timestamp_)); | 57 memset(frame_id_to_rtp_timestamp_, 0, sizeof(frame_id_to_rtp_timestamp_)); |
| 58 } | 58 } |
| 59 | 59 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 77 // TODO(pwestin): update this as video to refresh over time. | 77 // TODO(pwestin): update this as video to refresh over time. |
| 78 if (time_first_incoming_packet_.is_null()) { | 78 if (time_first_incoming_packet_.is_null()) { |
| 79 InitializeTimers(); | 79 InitializeTimers(); |
| 80 first_incoming_rtp_timestamp_ = rtp_header.rtp_timestamp; | 80 first_incoming_rtp_timestamp_ = rtp_header.rtp_timestamp; |
| 81 time_first_incoming_packet_ = now; | 81 time_first_incoming_packet_ = now; |
| 82 } | 82 } |
| 83 | 83 |
| 84 frame_id_to_rtp_timestamp_[rtp_header.frame_id & 0xff] = | 84 frame_id_to_rtp_timestamp_[rtp_header.frame_id & 0xff] = |
| 85 rtp_header.rtp_timestamp; | 85 rtp_header.rtp_timestamp; |
| 86 cast_environment_->Logging()->InsertPacketEvent( | 86 cast_environment_->Logging()->InsertPacketEvent( |
| 87 now, kAudioPacketReceived, rtp_header.rtp_timestamp, | 87 now, PACKET_RECEIVED, AUDIO_EVENT, rtp_header.rtp_timestamp, |
| 88 rtp_header.frame_id, rtp_header.packet_id, rtp_header.max_packet_id, | 88 rtp_header.frame_id, rtp_header.packet_id, rtp_header.max_packet_id, |
| 89 payload_size); | 89 payload_size); |
| 90 | 90 |
| 91 bool duplicate = false; | 91 bool duplicate = false; |
| 92 const bool complete = | 92 const bool complete = |
| 93 framer_.InsertPacket(payload_data, payload_size, rtp_header, &duplicate); | 93 framer_.InsertPacket(payload_data, payload_size, rtp_header, &duplicate); |
| 94 if (duplicate) { | 94 |
| 95 cast_environment_->Logging()->InsertPacketEvent( | 95 // Duplicate packets are ignored. |
| 96 now, | 96 if (duplicate || !complete) |
| 97 kDuplicateAudioPacketReceived, | |
| 98 rtp_header.rtp_timestamp, | |
| 99 rtp_header.frame_id, | |
| 100 rtp_header.packet_id, | |
| 101 rtp_header.max_packet_id, | |
| 102 payload_size); | |
| 103 // Duplicate packets are ignored. | |
| 104 return; | |
| 105 } | |
| 106 if (!complete) | |
| 107 return; | 97 return; |
| 108 | 98 |
| 109 EmitAvailableEncodedFrames(); | 99 EmitAvailableEncodedFrames(); |
| 110 } | 100 } |
| 111 | 101 |
| 112 void AudioReceiver::GetRawAudioFrame( | 102 void AudioReceiver::GetRawAudioFrame( |
| 113 const AudioFrameDecodedCallback& callback) { | 103 const AudioFrameDecodedCallback& callback) { |
| 114 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 104 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 115 DCHECK(!callback.is_null()); | 105 DCHECK(!callback.is_null()); |
| 116 DCHECK(audio_decoder_.get()); | 106 DCHECK(audio_decoder_.get()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 const AudioFrameDecodedCallback& callback, | 138 const AudioFrameDecodedCallback& callback, |
| 149 uint32 frame_id, | 139 uint32 frame_id, |
| 150 uint32 rtp_timestamp, | 140 uint32 rtp_timestamp, |
| 151 const base::TimeTicks& playout_time, | 141 const base::TimeTicks& playout_time, |
| 152 scoped_ptr<AudioBus> audio_bus, | 142 scoped_ptr<AudioBus> audio_bus, |
| 153 bool is_continuous) { | 143 bool is_continuous) { |
| 154 DCHECK(cast_environment->CurrentlyOn(CastEnvironment::MAIN)); | 144 DCHECK(cast_environment->CurrentlyOn(CastEnvironment::MAIN)); |
| 155 if (audio_bus.get()) { | 145 if (audio_bus.get()) { |
| 156 const base::TimeTicks now = cast_environment->Clock()->NowTicks(); | 146 const base::TimeTicks now = cast_environment->Clock()->NowTicks(); |
| 157 cast_environment->Logging()->InsertFrameEvent( | 147 cast_environment->Logging()->InsertFrameEvent( |
| 158 now, kAudioFrameDecoded, rtp_timestamp, frame_id); | 148 now, FRAME_DECODED, AUDIO_EVENT, rtp_timestamp, frame_id); |
| 159 cast_environment->Logging()->InsertFrameEventWithDelay( | 149 cast_environment->Logging()->InsertFrameEventWithDelay( |
| 160 now, kAudioPlayoutDelay, rtp_timestamp, frame_id, | 150 now, FRAME_PLAYOUT, AUDIO_EVENT, rtp_timestamp, frame_id, |
| 161 playout_time - now); | 151 playout_time - now); |
| 162 } | 152 } |
| 163 callback.Run(audio_bus.Pass(), playout_time, is_continuous); | 153 callback.Run(audio_bus.Pass(), playout_time, is_continuous); |
| 164 } | 154 } |
| 165 | 155 |
| 166 void AudioReceiver::GetEncodedAudioFrame( | 156 void AudioReceiver::GetEncodedAudioFrame( |
| 167 const AudioFrameEncodedCallback& callback) { | 157 const AudioFrameEncodedCallback& callback) { |
| 168 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 158 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 169 frame_request_queue_.push_back(callback); | 159 frame_request_queue_.push_back(callback); |
| 170 EmitAvailableEncodedFrames(); | 160 EmitAvailableEncodedFrames(); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 target_delay_delta_ = target_delay; | 249 target_delay_delta_ = target_delay; |
| 260 rtcp_.SetTargetDelay(target_delay_delta_); | 250 rtcp_.SetTargetDelay(target_delay_delta_); |
| 261 } | 251 } |
| 262 | 252 |
| 263 void AudioReceiver::CastFeedback(const RtcpCastMessage& cast_message) { | 253 void AudioReceiver::CastFeedback(const RtcpCastMessage& cast_message) { |
| 264 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 254 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 265 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); | 255 base::TimeTicks now = cast_environment_->Clock()->NowTicks(); |
| 266 RtpTimestamp rtp_timestamp = | 256 RtpTimestamp rtp_timestamp = |
| 267 frame_id_to_rtp_timestamp_[cast_message.ack_frame_id_ & 0xff]; | 257 frame_id_to_rtp_timestamp_[cast_message.ack_frame_id_ & 0xff]; |
| 268 cast_environment_->Logging()->InsertFrameEvent( | 258 cast_environment_->Logging()->InsertFrameEvent( |
| 269 now, kAudioAckSent, rtp_timestamp, cast_message.ack_frame_id_); | 259 now, FRAME_ACK_SENT, AUDIO_EVENT, rtp_timestamp, |
| 260 cast_message.ack_frame_id_); |
| 270 | 261 |
| 271 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events; | 262 ReceiverRtcpEventSubscriber::RtcpEventMultiMap rtcp_events; |
| 272 event_subscriber_.GetRtcpEventsAndReset(&rtcp_events); | 263 event_subscriber_.GetRtcpEventsAndReset(&rtcp_events); |
| 273 rtcp_.SendRtcpFromRtpReceiver(&cast_message, &rtcp_events); | 264 rtcp_.SendRtcpFromRtpReceiver(&cast_message, &rtcp_events); |
| 274 } | 265 } |
| 275 | 266 |
| 276 base::TimeTicks AudioReceiver::GetPlayoutTime(base::TimeTicks now, | 267 base::TimeTicks AudioReceiver::GetPlayoutTime(base::TimeTicks now, |
| 277 uint32 rtp_timestamp) { | 268 uint32 rtp_timestamp) { |
| 278 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 269 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 279 // Senders time in ms when this frame was recorded. | 270 // Senders time in ms when this frame was recorded. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 373 } | 364 } |
| 374 | 365 |
| 375 void AudioReceiver::SendNextCastMessage() { | 366 void AudioReceiver::SendNextCastMessage() { |
| 376 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 367 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 377 framer_.SendCastMessage(); // Will only send a message if it is time. | 368 framer_.SendCastMessage(); // Will only send a message if it is time. |
| 378 ScheduleNextCastMessage(); | 369 ScheduleNextCastMessage(); |
| 379 } | 370 } |
| 380 | 371 |
| 381 } // namespace cast | 372 } // namespace cast |
| 382 } // namespace media | 373 } // namespace media |
| OLD | NEW |