Index: media/cast/audio_receiver/audio_receiver.cc |
diff --git a/media/cast/audio_receiver/audio_receiver.cc b/media/cast/audio_receiver/audio_receiver.cc |
index c3dc3937b9acf176364fcc8aa5dbe3a16461da3a..bf59f36b657af43b7416ce4a5f214687ffe0cef5 100644 |
--- a/media/cast/audio_receiver/audio_receiver.cc |
+++ b/media/cast/audio_receiver/audio_receiver.cc |
@@ -7,6 +7,8 @@ |
#include "base/bind.h" |
#include "base/logging.h" |
#include "base/message_loop/message_loop.h" |
+#include "crypto/encryptor.h" |
+#include "crypto/symmetric_key.h" |
#include "media/cast/audio_receiver/audio_decoder.h" |
#include "media/cast/framer/framer.h" |
#include "media/cast/rtcp/rtcp.h" |
@@ -99,6 +101,18 @@ AudioReceiver::AudioReceiver(scoped_refptr<CastEnvironment> cast_environment, |
} else { |
audio_decoder_ = new AudioDecoder(audio_config); |
} |
+ if (audio_config.aes_iv_mask.size() == kAesKeySize && |
+ audio_config.aes_key.size() == kAesKeySize) { |
+ iv_mask_ = audio_config.aes_iv_mask; |
+ crypto::SymmetricKey* key = crypto::SymmetricKey::Import( |
+ crypto::SymmetricKey::AES, audio_config.aes_key); |
+ decryptor_.reset(new crypto::Encryptor()); |
+ decryptor_->Init(key, crypto::Encryptor::CTR, std::string()); |
+ } else if (audio_config.aes_iv_mask.size() != 0 || |
+ audio_config.aes_key.size() != 0) { |
+ DCHECK(false) << "Invalid crypto configuration"; |
+ } |
+ |
rtp_receiver_.reset(new RtpReceiver(cast_environment->Clock(), |
&audio_config, |
NULL, |
@@ -135,8 +149,22 @@ void AudioReceiver::IncomingParsedRtpPacket(const uint8* payload_data, |
if (audio_decoder_) { |
DCHECK(!audio_buffer_) << "Invalid internal state"; |
- audio_decoder_->IncomingParsedRtpPacket(payload_data, payload_size, |
- rtp_header); |
+ std::string plaintext(reinterpret_cast<const char*>(payload_data), |
+ payload_size); |
wtc
2013/11/15 20:13:37
The changes you made to these audio_xxx files shou
pwestin
2013/11/15 22:11:10
Done.
|
+ if (decryptor_) { |
+ plaintext.clear(); |
+ if (!decryptor_->SetCounter(GetAesNonce(rtp_header.frame_id, iv_mask_))) { |
+ NOTREACHED(); |
wtc
2013/11/15 20:13:37
Should we add a return statement?
pwestin
2013/11/15 22:11:10
Done.
|
+ } |
+ if (!decryptor_->Decrypt(base::StringPiece(reinterpret_cast<const char*>( |
+ payload_data), payload_size), &plaintext)) { |
+ VLOG(0) << "Decryption error"; |
+ return; |
+ } |
+ } |
+ audio_decoder_->IncomingParsedRtpPacket( |
+ reinterpret_cast<const uint8*>(plaintext.data()), plaintext.size(), |
+ rtp_header); |
return; |
} |
DCHECK(audio_buffer_) << "Invalid internal state"; |
@@ -209,6 +237,14 @@ void AudioReceiver::PlayoutTimeout() { |
VLOG(1) << "Failed to retrieved a complete frame at this point in time"; |
return; |
} |
+ |
+ if (decryptor_) { |
+ if (!DecryptAudioFrame(&encoded_frame)) { |
+ VLOG(0) << "Decryption error"; |
+ return; |
+ } |
+ } |
+ |
if (PostEncodedAudioFrame(queued_encoded_callbacks_.front(), rtp_timestamp, |
next_frame, &encoded_frame)) { |
// Call succeed remove callback from list. |
@@ -231,6 +267,13 @@ void AudioReceiver::GetEncodedAudioFrame( |
queued_encoded_callbacks_.push_back(callback); |
return; |
} |
+ if (decryptor_) { |
+ if (!DecryptAudioFrame(&encoded_frame)) { |
+ VLOG(0) << "Decryption error"; |
+ queued_encoded_callbacks_.push_back(callback); |
+ return; |
+ } |
+ } |
if (!PostEncodedAudioFrame(callback, rtp_timestamp, next_frame, |
&encoded_frame)) { |
// We have an audio frame; however we are missing packets and we have time |
@@ -314,6 +357,23 @@ base::TimeTicks AudioReceiver::GetPlayoutTime(base::TimeTicks now, |
now; |
} |
+bool AudioReceiver::DecryptAudioFrame( |
+ scoped_ptr<EncodedAudioFrame>* audio_frame) { |
+ DCHECK(decryptor_) << "Invalid state"; |
+ |
+ if (!decryptor_->SetCounter(GetAesNonce((*audio_frame)->frame_id, |
+ iv_mask_))) { |
+ NOTREACHED(); |
+ return false; |
+ } |
+ std::string decrypted_audio_data; |
+ if (!decryptor_->Decrypt((*audio_frame)->data, &decrypted_audio_data)) { |
+ return false; |
+ } |
+ (*audio_frame)->data.swap(decrypted_audio_data); |
+ return true; |
+} |
+ |
void AudioReceiver::ScheduleNextRtcpReport() { |
base::TimeDelta time_to_send = rtcp_->TimeToSendNextRtcpReport() - |
cast_environment_->Clock()->NowTicks(); |