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..1291da413b24c6b9413709769f138941aa6a90cd 100644 |
--- a/media/cast/audio_receiver/audio_receiver.cc |
+++ b/media/cast/audio_receiver/audio_receiver.cc |
@@ -7,6 +7,7 @@ |
#include "base/bind.h" |
#include "base/logging.h" |
#include "base/message_loop/message_loop.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 +100,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 +148,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); |
+ if (decryptor_) { |
+ // TODO(pwestin): we need to change to 32 bits frame id. |
+ decryptor_->SetCounter(GetAesNounce(rtp_header.frame_id, iv_mask_)); |
wtc
2013/11/13 20:57:17
GetAesNounce seems to be a typo. Should it be GetA
pwestin
2013/11/15 19:38:17
Done.
|
+ std::string plaintext; |
+ if (!decryptor_->Decrypt(base::StringPiece(reinterpret_cast<const char*>( |
+ payload_data), payload_size), &plaintext)) { |
+ DCHECK(false) << "Decryption error"; |
wtc
2013/11/13 20:57:17
This should be a LOG statement rather than a DCHEC
pwestin
2013/11/15 19:38:17
Done.
|
+ return; |
+ } |
+ audio_decoder_->IncomingParsedRtpPacket( |
+ reinterpret_cast<const uint8*>(plaintext.data()), plaintext.size(), |
+ rtp_header); |
+ } else { |
+ audio_decoder_->IncomingParsedRtpPacket(payload_data, payload_size, |
+ rtp_header); |
wtc
2013/11/13 20:57:17
Nit: ideally, this code should look like:
if (a
pwestin
2013/11/15 19:38:17
Done.
|
+ } |
return; |
} |
DCHECK(audio_buffer_) << "Invalid internal state"; |
@@ -209,6 +236,14 @@ void AudioReceiver::PlayoutTimeout() { |
VLOG(1) << "Failed to retrieved a complete frame at this point in time"; |
return; |
} |
+ |
+ if (decryptor_) { |
+ if (!DecryptAudioFrame(&encoded_frame)) { |
+ DCHECK(false) << "Decryption error"; |
wtc
2013/11/13 20:57:17
This should be a LOG statement rather than a DCHEC
pwestin
2013/11/15 19:38:17
Done.
|
+ return; |
+ } |
+ } |
+ |
if (PostEncodedAudioFrame(queued_encoded_callbacks_.front(), rtp_timestamp, |
next_frame, &encoded_frame)) { |
// Call succeed remove callback from list. |
@@ -231,6 +266,12 @@ void AudioReceiver::GetEncodedAudioFrame( |
queued_encoded_callbacks_.push_back(callback); |
return; |
} |
+ if (decryptor_) { |
+ if (!DecryptAudioFrame(&encoded_frame)) { |
+ DCHECK(false) << "Decryption error"; |
+ return; |
wtc
2013/11/13 20:57:17
Do we need to do
queued_encoded_callbacks_.pus
pwestin
2013/11/15 19:38:17
Done.
|
+ } |
+ } |
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 +355,27 @@ base::TimeTicks AudioReceiver::GetPlayoutTime(base::TimeTicks now, |
now; |
} |
+bool AudioReceiver::DecryptAudioFrame( |
+ scoped_ptr<EncodedAudioFrame>* audio_frame) { |
+ DCHECK(decryptor_) << "Invalid state"; |
+ |
+ scoped_ptr<EncodedAudioFrame> decrypted_audio_frame( |
+ new EncodedAudioFrame()); |
wtc
2013/11/13 20:57:17
I think we just need a std::string for the decrypt
pwestin
2013/11/15 19:38:17
Done.
|
+ |
+ // TODO(pwestin): the frame id must be a 32 bit number. |
+ decryptor_->SetCounter(GetAesNounce((*audio_frame)->frame_id, iv_mask_)); |
+ |
+ if (!decryptor_->Decrypt((*audio_frame)->data, |
+ &decrypted_audio_frame->data)) { |
+ return false; |
+ } |
+ decrypted_audio_frame->codec = (*audio_frame)->codec; |
+ decrypted_audio_frame->frame_id = (*audio_frame)->frame_id; |
wtc
2013/11/13 20:57:17
Do we need to copy the "samples" field?
pwestin
2013/11/15 19:38:17
Done.
|
+ |
+ audio_frame->swap(decrypted_audio_frame); |
+ return true; |
+} |
+ |
void AudioReceiver::ScheduleNextRtcpReport() { |
base::TimeDelta time_to_send = rtcp_->TimeToSendNextRtcpReport() - |
cast_environment_->Clock()->NowTicks(); |