Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(237)

Side by Side Diff: media/cast/audio_receiver/audio_receiver.cc

Issue 62843002: Cast: Added support for AES-CTR crypto. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "crypto/symmetric_key.h"
10 #include "media/cast/audio_receiver/audio_decoder.h" 11 #include "media/cast/audio_receiver/audio_decoder.h"
11 #include "media/cast/framer/framer.h" 12 #include "media/cast/framer/framer.h"
12 #include "media/cast/rtcp/rtcp.h" 13 #include "media/cast/rtcp/rtcp.h"
13 #include "media/cast/rtp_receiver/rtp_receiver.h" 14 #include "media/cast/rtp_receiver/rtp_receiver.h"
14 15
15 // Max time we wait until an audio frame is due to be played out is released. 16 // Max time we wait until an audio frame is due to be played out is released.
16 static const int64 kMaxAudioFrameWaitMs = 20; 17 static const int64 kMaxAudioFrameWaitMs = 20;
17 static const int64 kMinSchedulingDelayMs = 1; 18 static const int64 kMinSchedulingDelayMs = 1;
18 19
19 namespace media { 20 namespace media {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 incoming_payload_feedback_.reset(new LocalRtpAudioFeedback(this)); 93 incoming_payload_feedback_.reset(new LocalRtpAudioFeedback(this));
93 if (audio_config.use_external_decoder) { 94 if (audio_config.use_external_decoder) {
94 audio_buffer_.reset(new Framer(cast_environment->Clock(), 95 audio_buffer_.reset(new Framer(cast_environment->Clock(),
95 incoming_payload_feedback_.get(), 96 incoming_payload_feedback_.get(),
96 audio_config.incoming_ssrc, 97 audio_config.incoming_ssrc,
97 true, 98 true,
98 0)); 99 0));
99 } else { 100 } else {
100 audio_decoder_ = new AudioDecoder(audio_config); 101 audio_decoder_ = new AudioDecoder(audio_config);
101 } 102 }
103 if (audio_config.aes_iv_mask.size() == kAesKeySize &&
104 audio_config.aes_key.size() == kAesKeySize) {
105 iv_mask_ = audio_config.aes_iv_mask;
106 crypto::SymmetricKey* key = crypto::SymmetricKey::Import(
107 crypto::SymmetricKey::AES, audio_config.aes_key);
108 decryptor_.reset(new crypto::Encryptor());
109 decryptor_->Init(key, crypto::Encryptor::CTR, std::string());
110 } else if (audio_config.aes_iv_mask.size() != 0 ||
111 audio_config.aes_iv_mask.size() != 0) {
Alpha Left Google 2013/11/08 22:40:02 Do you mean audio_config.aes_key.size() here?
pwestin 2013/11/12 22:07:23 Done.
112 DCHECK(false) << "Invalid crypto configuration";
113 }
114
102 rtp_receiver_.reset(new RtpReceiver(cast_environment->Clock(), 115 rtp_receiver_.reset(new RtpReceiver(cast_environment->Clock(),
103 &audio_config, 116 &audio_config,
104 NULL, 117 NULL,
105 incoming_payload_callback_.get())); 118 incoming_payload_callback_.get()));
106 rtp_audio_receiver_statistics_.reset( 119 rtp_audio_receiver_statistics_.reset(
107 new LocalRtpReceiverStatistics(rtp_receiver_.get())); 120 new LocalRtpReceiverStatistics(rtp_receiver_.get()));
108 base::TimeDelta rtcp_interval_delta = 121 base::TimeDelta rtcp_interval_delta =
109 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval); 122 base::TimeDelta::FromMilliseconds(audio_config.rtcp_interval);
110 rtcp_.reset(new Rtcp(cast_environment->Clock(), 123 rtcp_.reset(new Rtcp(cast_environment->Clock(),
111 NULL, 124 NULL,
(...skipping 16 matching lines...) Expand all
128 size_t payload_size, 141 size_t payload_size,
129 const RtpCastHeader& rtp_header) { 142 const RtpCastHeader& rtp_header) {
130 // TODO(pwestin): update this as video to refresh over time. 143 // TODO(pwestin): update this as video to refresh over time.
131 if (time_first_incoming_packet_.is_null()) { 144 if (time_first_incoming_packet_.is_null()) {
132 first_incoming_rtp_timestamp_ = rtp_header.webrtc.header.timestamp; 145 first_incoming_rtp_timestamp_ = rtp_header.webrtc.header.timestamp;
133 time_first_incoming_packet_ = cast_environment_->Clock()->NowTicks(); 146 time_first_incoming_packet_ = cast_environment_->Clock()->NowTicks();
134 } 147 }
135 148
136 if (audio_decoder_) { 149 if (audio_decoder_) {
137 DCHECK(!audio_buffer_) << "Invalid internal state"; 150 DCHECK(!audio_buffer_) << "Invalid internal state";
138 audio_decoder_->IncomingParsedRtpPacket(payload_data, payload_size, 151 if (decryptor_) {
139 rtp_header); 152 // TODO(pwestin): we need to change to 32 bits frame id.
153 decryptor_->SetCounter(GetAesNounce(rtp_header.frame_id, iv_mask_));
154 std::string plaintext;
155 if (decryptor_->Decrypt(
156 base::StringPiece(reinterpret_cast<const char*>(payload_data),
Alpha Left Google 2013/11/08 22:40:02 nit: this should be indented by 4 spaces.
pwestin 2013/11/12 22:07:23 Done.
157 payload_size), &plaintext)) {
Alpha Left Google 2013/11/08 22:40:02 payload_size should align with reinterpret_cast ab
pwestin 2013/11/12 22:07:23 Done.
158 audio_decoder_->IncomingParsedRtpPacket(
159 reinterpret_cast<const uint8*>(plaintext.data()), plaintext.size(),
Alpha Left Google 2013/11/08 22:40:02 Can you add a new method to AudioDecoder takes a s
pwestin 2013/11/12 22:07:23 Dont think that is a good idea it will only move o
160 rtp_header);
161 } else {
162 DCHECK(false) << "Decryption error";
163 return;
164 }
165 } else {
166 audio_decoder_->IncomingParsedRtpPacket(payload_data, payload_size,
167 rtp_header);
168 }
140 return; 169 return;
141 } 170 }
142 DCHECK(audio_buffer_) << "Invalid internal state"; 171 DCHECK(audio_buffer_) << "Invalid internal state";
143 DCHECK(!audio_decoder_) << "Invalid internal state"; 172 DCHECK(!audio_decoder_) << "Invalid internal state";
144 bool complete = audio_buffer_->InsertPacket(payload_data, payload_size, 173 bool complete = audio_buffer_->InsertPacket(payload_data, payload_size,
145 rtp_header); 174 rtp_header);
146 if (!complete) return; // Audio frame not complete; wait for more packets. 175 if (!complete) return; // Audio frame not complete; wait for more packets.
147 if (queued_encoded_callbacks_.empty()) return; // No pending callback. 176 if (queued_encoded_callbacks_.empty()) return; // No pending callback.
148 177
149 AudioFrameEncodedCallback callback = queued_encoded_callbacks_.front(); 178 AudioFrameEncodedCallback callback = queued_encoded_callbacks_.front();
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 231
203 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(), 232 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(),
204 &rtp_timestamp, &next_frame)) { 233 &rtp_timestamp, &next_frame)) {
205 // We have no audio frames. Wait for new packet(s). 234 // We have no audio frames. Wait for new packet(s).
206 // Since the application can post multiple AudioFrameEncodedCallback and 235 // Since the application can post multiple AudioFrameEncodedCallback and
207 // we only check the next frame to play out we might have multiple timeout 236 // we only check the next frame to play out we might have multiple timeout
208 // events firing after each other; however this should be a rare event. 237 // events firing after each other; however this should be a rare event.
209 VLOG(1) << "Failed to retrieved a complete frame at this point in time"; 238 VLOG(1) << "Failed to retrieved a complete frame at this point in time";
210 return; 239 return;
211 } 240 }
241
242 if (decryptor_) {
243 if (!DecryptAudioFrame(&encoded_frame)) {
244 DCHECK(false) << "Decryption error";
245 return;
246 }
247 }
248
212 if (PostEncodedAudioFrame(queued_encoded_callbacks_.front(), rtp_timestamp, 249 if (PostEncodedAudioFrame(queued_encoded_callbacks_.front(), rtp_timestamp,
213 next_frame, &encoded_frame)) { 250 next_frame, &encoded_frame)) {
214 // Call succeed remove callback from list. 251 // Call succeed remove callback from list.
215 queued_encoded_callbacks_.pop_front(); 252 queued_encoded_callbacks_.pop_front();
216 } 253 }
217 } 254 }
218 255
219 void AudioReceiver::GetEncodedAudioFrame( 256 void AudioReceiver::GetEncodedAudioFrame(
220 const AudioFrameEncodedCallback& callback) { 257 const AudioFrameEncodedCallback& callback) {
221 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; 258 DCHECK(audio_buffer_) << "Invalid function call in this configuration";
222 259
223 uint32 rtp_timestamp = 0; 260 uint32 rtp_timestamp = 0;
224 bool next_frame = false; 261 bool next_frame = false;
225 scoped_ptr<EncodedAudioFrame> encoded_frame(new EncodedAudioFrame()); 262 scoped_ptr<EncodedAudioFrame> encoded_frame(new EncodedAudioFrame());
226 263
227 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(), 264 if (!audio_buffer_->GetEncodedAudioFrame(encoded_frame.get(),
228 &rtp_timestamp, &next_frame)) { 265 &rtp_timestamp, &next_frame)) {
229 // We have no audio frames. Wait for new packet(s). 266 // We have no audio frames. Wait for new packet(s).
230 VLOG(1) << "Wait for more audio packets in frame"; 267 VLOG(1) << "Wait for more audio packets in frame";
231 queued_encoded_callbacks_.push_back(callback); 268 queued_encoded_callbacks_.push_back(callback);
232 return; 269 return;
233 } 270 }
271 if (decryptor_) {
272 if (!DecryptAudioFrame(&encoded_frame)) {
273 DCHECK(false) << "Decryption error";
274 return;
275 }
276 }
234 if (!PostEncodedAudioFrame(callback, rtp_timestamp, next_frame, 277 if (!PostEncodedAudioFrame(callback, rtp_timestamp, next_frame,
235 &encoded_frame)) { 278 &encoded_frame)) {
236 // We have an audio frame; however we are missing packets and we have time 279 // We have an audio frame; however we are missing packets and we have time
237 // to wait for new packet(s). 280 // to wait for new packet(s).
238 queued_encoded_callbacks_.push_back(callback); 281 queued_encoded_callbacks_.push_back(callback);
239 } 282 }
240 } 283 }
241 284
242 bool AudioReceiver::PostEncodedAudioFrame( 285 bool AudioReceiver::PostEncodedAudioFrame(
243 const AudioFrameEncodedCallback& callback, 286 const AudioFrameEncodedCallback& callback,
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
307 base::TimeDelta()); 350 base::TimeDelta());
308 } 351 }
309 } 352 }
310 // This can fail if we have not received any RTCP packets in a long time. 353 // This can fail if we have not received any RTCP packets in a long time.
311 return rtcp_->RtpTimestampInSenderTime(frequency_, rtp_timestamp, 354 return rtcp_->RtpTimestampInSenderTime(frequency_, rtp_timestamp,
312 &rtp_timestamp_in_ticks) ? 355 &rtp_timestamp_in_ticks) ?
313 rtp_timestamp_in_ticks + time_offset_ + target_delay_delta_ : 356 rtp_timestamp_in_ticks + time_offset_ + target_delay_delta_ :
314 now; 357 now;
315 } 358 }
316 359
360 bool AudioReceiver::DecryptAudioFrame(
361 scoped_ptr<EncodedAudioFrame>* audio_frame) {
362 DCHECK(decryptor_) << "Invalid state";
363
364 scoped_ptr<EncodedAudioFrame> decrypted_audio_frame(
365 new EncodedAudioFrame());
366
367 // TODO(pwestin): the frame id must be a 32 bit number.
368 decryptor_->SetCounter(GetAesNounce((*audio_frame)->frame_id, iv_mask_));
369
370 if (!decryptor_->Decrypt((*audio_frame)->data,
371 &decrypted_audio_frame->data)) {
372 return false;
373 }
374 decrypted_audio_frame->codec = (*audio_frame)->codec;
375 decrypted_audio_frame->frame_id = (*audio_frame)->frame_id;
376
377 audio_frame->swap(decrypted_audio_frame);
378 return true;
379 }
380
317 void AudioReceiver::ScheduleNextRtcpReport() { 381 void AudioReceiver::ScheduleNextRtcpReport() {
318 base::TimeDelta time_to_send = rtcp_->TimeToSendNextRtcpReport() - 382 base::TimeDelta time_to_send = rtcp_->TimeToSendNextRtcpReport() -
319 cast_environment_->Clock()->NowTicks(); 383 cast_environment_->Clock()->NowTicks();
320 384
321 time_to_send = std::max(time_to_send, 385 time_to_send = std::max(time_to_send,
322 base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs)); 386 base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs));
323 387
324 cast_environment_->PostDelayedTask(CastEnvironment::MAIN, FROM_HERE, 388 cast_environment_->PostDelayedTask(CastEnvironment::MAIN, FROM_HERE,
325 base::Bind(&AudioReceiver::SendNextRtcpReport, 389 base::Bind(&AudioReceiver::SendNextRtcpReport,
326 weak_factory_.GetWeakPtr()), time_to_send); 390 weak_factory_.GetWeakPtr()), time_to_send);
(...skipping 22 matching lines...) Expand all
349 } 413 }
350 414
351 void AudioReceiver::SendNextCastMessage() { 415 void AudioReceiver::SendNextCastMessage() {
352 DCHECK(audio_buffer_) << "Invalid function call in this configuration"; 416 DCHECK(audio_buffer_) << "Invalid function call in this configuration";
353 audio_buffer_->SendCastMessage(); // Will only send a message if it is time. 417 audio_buffer_->SendCastMessage(); // Will only send a message if it is time.
354 ScheduleNextCastMessage(); 418 ScheduleNextCastMessage();
355 } 419 }
356 420
357 } // namespace cast 421 } // namespace cast
358 } // namespace media 422 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698