| 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 "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "media/cast/audio_receiver/audio_decoder.h" | 6 #include "media/cast/audio_receiver/audio_decoder.h" |
| 7 | 7 |
| 8 #include "third_party/webrtc/modules/audio_coding/main/interface/audio_coding_mo
dule.h" | 8 #include "third_party/webrtc/modules/audio_coding/main/interface/audio_coding_mo
dule.h" |
| 9 #include "third_party/webrtc/modules/interface/module_common_types.h" | 9 #include "third_party/webrtc/modules/interface/module_common_types.h" |
| 10 | 10 |
| 11 namespace media { | 11 namespace media { |
| 12 namespace cast { | 12 namespace cast { |
| 13 | 13 |
| 14 AudioDecoder::AudioDecoder(scoped_refptr<CastEnvironment> cast_environment, | 14 AudioDecoder::AudioDecoder(scoped_refptr<CastEnvironment> cast_environment, |
| 15 const AudioReceiverConfig& audio_config, | 15 const AudioReceiverConfig& audio_config, |
| 16 RtpPayloadFeedback* incoming_payload_feedback) | 16 RtpPayloadFeedback* incoming_payload_feedback) |
| 17 : cast_environment_(cast_environment), | 17 : cast_environment_(cast_environment), |
| 18 audio_decoder_(webrtc::AudioCodingModule::Create(0)), | 18 audio_decoder_(webrtc::AudioCodingModule::Create(0)), |
| 19 cast_message_builder_(cast_environment->Clock(), | 19 cast_message_builder_(cast_environment->Clock(), |
| 20 incoming_payload_feedback, &frame_id_map_, audio_config.incoming_ssrc, | 20 incoming_payload_feedback, |
| 21 true, 0), | 21 &frame_id_map_, |
| 22 audio_config.incoming_ssrc, |
| 23 true, |
| 24 0), |
| 22 have_received_packets_(false), | 25 have_received_packets_(false), |
| 23 last_played_out_timestamp_(0) { | 26 last_played_out_timestamp_(0) { |
| 24 audio_decoder_->InitializeReceiver(); | 27 audio_decoder_->InitializeReceiver(); |
| 25 | 28 |
| 26 webrtc::CodecInst receive_codec; | 29 webrtc::CodecInst receive_codec; |
| 27 switch (audio_config.codec) { | 30 switch (audio_config.codec) { |
| 28 case transport::kPcm16: | 31 case transport::kPcm16: |
| 29 receive_codec.pltype = audio_config.rtp_payload_type; | 32 receive_codec.pltype = audio_config.rtp_payload_type; |
| 30 strncpy(receive_codec.plname, "L16", 4); | 33 strncpy(receive_codec.plname, "L16", 4); |
| 31 receive_codec.plfreq = audio_config.frequency; | 34 receive_codec.plfreq = audio_config.frequency; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 61 uint32* rtp_timestamp) { | 64 uint32* rtp_timestamp) { |
| 62 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_DECODER)); | 65 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_DECODER)); |
| 63 // We don't care about the race case where a packet arrives at the same time | 66 // We don't care about the race case where a packet arrives at the same time |
| 64 // as this function in called. The data will be there the next time this | 67 // as this function in called. The data will be there the next time this |
| 65 // function is called. | 68 // function is called. |
| 66 lock_.Acquire(); | 69 lock_.Acquire(); |
| 67 // Get a local copy under lock. | 70 // Get a local copy under lock. |
| 68 bool have_received_packets = have_received_packets_; | 71 bool have_received_packets = have_received_packets_; |
| 69 lock_.Release(); | 72 lock_.Release(); |
| 70 | 73 |
| 71 if (!have_received_packets) return false; | 74 if (!have_received_packets) |
| 75 return false; |
| 72 | 76 |
| 73 audio_frame->samples.clear(); | 77 audio_frame->samples.clear(); |
| 74 | 78 |
| 75 for (int i = 0; i < number_of_10ms_blocks; ++i) { | 79 for (int i = 0; i < number_of_10ms_blocks; ++i) { |
| 76 webrtc::AudioFrame webrtc_audio_frame; | 80 webrtc::AudioFrame webrtc_audio_frame; |
| 77 if (0 != audio_decoder_->PlayoutData10Ms(desired_frequency, | 81 if (0 != audio_decoder_->PlayoutData10Ms(desired_frequency, |
| 78 &webrtc_audio_frame)) { | 82 &webrtc_audio_frame)) { |
| 79 return false; | 83 return false; |
| 80 } | 84 } |
| 81 if (webrtc_audio_frame.speech_type_ == webrtc::AudioFrame::kPLCCNG || | 85 if (webrtc_audio_frame.speech_type_ == webrtc::AudioFrame::kPLCCNG || |
| (...skipping 21 matching lines...) Expand all Loading... |
| 103 &webrtc_audio_frame.data_[samples_per_10ms * audio_frame->channels]); | 107 &webrtc_audio_frame.data_[samples_per_10ms * audio_frame->channels]); |
| 104 } | 108 } |
| 105 return true; | 109 return true; |
| 106 } | 110 } |
| 107 | 111 |
| 108 void AudioDecoder::IncomingParsedRtpPacket(const uint8* payload_data, | 112 void AudioDecoder::IncomingParsedRtpPacket(const uint8* payload_data, |
| 109 size_t payload_size, | 113 size_t payload_size, |
| 110 const RtpCastHeader& rtp_header) { | 114 const RtpCastHeader& rtp_header) { |
| 111 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 115 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 112 DCHECK_LE(payload_size, kMaxIpPacketSize); | 116 DCHECK_LE(payload_size, kMaxIpPacketSize); |
| 113 audio_decoder_->IncomingPacket(payload_data, static_cast<int32>(payload_size), | 117 audio_decoder_->IncomingPacket( |
| 114 rtp_header.webrtc); | 118 payload_data, static_cast<int32>(payload_size), rtp_header.webrtc); |
| 115 lock_.Acquire(); | 119 lock_.Acquire(); |
| 116 have_received_packets_ = true; | 120 have_received_packets_ = true; |
| 117 uint32 last_played_out_timestamp = last_played_out_timestamp_; | 121 uint32 last_played_out_timestamp = last_played_out_timestamp_; |
| 118 lock_.Release(); | 122 lock_.Release(); |
| 119 | 123 |
| 120 PacketType packet_type = frame_id_map_.InsertPacket(rtp_header); | 124 PacketType packet_type = frame_id_map_.InsertPacket(rtp_header); |
| 121 if (packet_type != kNewPacketCompletingFrame) return; | 125 if (packet_type != kNewPacketCompletingFrame) |
| 126 return; |
| 122 | 127 |
| 123 cast_message_builder_.CompleteFrameReceived(rtp_header.frame_id, | 128 cast_message_builder_.CompleteFrameReceived(rtp_header.frame_id, |
| 124 rtp_header.is_key_frame); | 129 rtp_header.is_key_frame); |
| 125 | 130 |
| 126 frame_id_rtp_timestamp_map_[rtp_header.frame_id] = | 131 frame_id_rtp_timestamp_map_[rtp_header.frame_id] = |
| 127 rtp_header.webrtc.header.timestamp; | 132 rtp_header.webrtc.header.timestamp; |
| 128 | 133 |
| 129 if (last_played_out_timestamp == 0) return; // Nothing is played out yet. | 134 if (last_played_out_timestamp == 0) |
| 135 return; // Nothing is played out yet. |
| 130 | 136 |
| 131 uint32 latest_frame_id_to_remove = 0; | 137 uint32 latest_frame_id_to_remove = 0; |
| 132 bool frame_to_remove = false; | 138 bool frame_to_remove = false; |
| 133 | 139 |
| 134 FrameIdRtpTimestampMap::iterator it = frame_id_rtp_timestamp_map_.begin(); | 140 FrameIdRtpTimestampMap::iterator it = frame_id_rtp_timestamp_map_.begin(); |
| 135 while (it != frame_id_rtp_timestamp_map_.end()) { | 141 while (it != frame_id_rtp_timestamp_map_.end()) { |
| 136 if (IsNewerRtpTimestamp(it->second, last_played_out_timestamp)) { | 142 if (IsNewerRtpTimestamp(it->second, last_played_out_timestamp)) { |
| 137 break; | 143 break; |
| 138 } | 144 } |
| 139 frame_to_remove = true; | 145 frame_to_remove = true; |
| 140 latest_frame_id_to_remove = it->first; | 146 latest_frame_id_to_remove = it->first; |
| 141 frame_id_rtp_timestamp_map_.erase(it); | 147 frame_id_rtp_timestamp_map_.erase(it); |
| 142 it = frame_id_rtp_timestamp_map_.begin(); | 148 it = frame_id_rtp_timestamp_map_.begin(); |
| 143 } | 149 } |
| 144 if (!frame_to_remove) return; | 150 if (!frame_to_remove) |
| 151 return; |
| 145 | 152 |
| 146 frame_id_map_.RemoveOldFrames(latest_frame_id_to_remove); | 153 frame_id_map_.RemoveOldFrames(latest_frame_id_to_remove); |
| 147 } | 154 } |
| 148 | 155 |
| 149 bool AudioDecoder::TimeToSendNextCastMessage(base::TimeTicks* time_to_send) { | 156 bool AudioDecoder::TimeToSendNextCastMessage(base::TimeTicks* time_to_send) { |
| 150 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 157 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 151 return cast_message_builder_.TimeToSendNextCastMessage(time_to_send); | 158 return cast_message_builder_.TimeToSendNextCastMessage(time_to_send); |
| 152 } | 159 } |
| 153 | 160 |
| 154 void AudioDecoder::SendCastMessage() { | 161 void AudioDecoder::SendCastMessage() { |
| 155 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 162 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 156 cast_message_builder_.UpdateCastMessage(); | 163 cast_message_builder_.UpdateCastMessage(); |
| 157 } | 164 } |
| 158 | 165 |
| 159 } // namespace cast | 166 } // namespace cast |
| 160 } // namespace media | 167 } // namespace media |
| OLD | NEW |