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
3 // found in the LICENSE file.
5 #ifndef MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_
6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "base/time/tick_clock.h"
15 #include "base/time/time.h"
16 #include "media/cast/cast_config.h"
17 #include "media/cast/cast_environment.h"
18 #include "media/cast/cast_receiver.h"
19 #include "media/cast/rtcp/rtcp.h" // RtcpCastMessage
20 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" // RtpCastHeader
32 class LocalRtpAudioData;
33 class LocalRtpAudioFeedback;
35 class RtpReceiverStatistics;
37 struct DecodedAudioCallbackData {
38 DecodedAudioCallbackData();
39 ~DecodedAudioCallbackData();
40 int number_of_10ms_blocks;
41 int desired_frequency;
42 AudioFrameDecodedCallback callback;
45 // This class is not thread safe. Should only be called from the Main cast
47 class AudioReceiver : public base::NonThreadSafe,
48 public base::SupportsWeakPtr<AudioReceiver> {
50 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment,
51 const AudioReceiverConfig& audio_config,
52 transport::PacedPacketSender* const packet_sender);
54 virtual ~AudioReceiver();
56 // Extract a raw audio frame from the cast receiver.
57 // Actual decoding will be preformed on a designated audio_decoder thread.
58 void GetRawAudioFrame(int number_of_10ms_blocks,
59 int desired_frequency,
60 const AudioFrameDecodedCallback& callback);
62 // Extract an encoded audio frame from the cast receiver.
63 void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback);
65 // Should only be called from the main cast thread.
66 void IncomingPacket(const uint8* packet, size_t length,
67 const base::Closure callback);
70 void IncomingParsedRtpPacket(const uint8* payload_data,
72 const RtpCastHeader& rtp_header);
74 friend class LocalRtpAudioData;
75 friend class LocalRtpAudioFeedback;
77 void CastFeedback(const RtcpCastMessage& cast_message);
79 // Time to pull out the audio even though we are missing data.
80 void PlayoutTimeout();
82 bool PostEncodedAudioFrame(
83 const AudioFrameEncodedCallback& callback,
86 scoped_ptr<transport::EncodedAudioFrame>* encoded_frame);
88 // Actual decoding implementation - should be called under the audio decoder
90 void DecodeAudioFrameThread(int number_of_10ms_blocks,
91 int desired_frequency,
92 const AudioFrameDecodedCallback callback);
93 void ReturnDecodedFrameWithPlayoutDelay(
94 scoped_ptr<PcmAudioFrame> audio_frame, uint32 rtp_timestamp,
95 const AudioFrameDecodedCallback callback);
97 // Return the playout time based on the current time and rtp timestamp.
98 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp);
100 void InitializeTimers();
102 // Decrypts the data within the |audio_frame| and replaces the data with the
104 bool DecryptAudioFrame(scoped_ptr<transport::EncodedAudioFrame>* audio_frame);
106 // Schedule the next RTCP report.
107 void ScheduleNextRtcpReport();
109 // Actually send the next RTCP report.
110 void SendNextRtcpReport();
112 // Schedule timing for the next cast message.
113 void ScheduleNextCastMessage();
115 // Actually send the next cast message.
116 void SendNextCastMessage();
118 scoped_refptr<CastEnvironment> cast_environment_;
119 base::WeakPtrFactory<AudioReceiver> weak_factory_;
121 const transport::AudioCodec codec_;
122 const int frequency_;
123 base::TimeDelta target_delay_delta_;
124 scoped_ptr<Framer> audio_buffer_;
125 scoped_ptr<AudioDecoder> audio_decoder_;
126 scoped_ptr<LocalRtpAudioData> incoming_payload_callback_;
127 scoped_ptr<LocalRtpAudioFeedback> incoming_payload_feedback_;
128 scoped_ptr<RtpReceiver> rtp_receiver_;
129 scoped_ptr<Rtcp> rtcp_;
130 scoped_ptr<RtpReceiverStatistics> rtp_audio_receiver_statistics_;
131 base::TimeDelta time_offset_;
132 base::TimeTicks time_first_incoming_packet_;
133 uint32 first_incoming_rtp_timestamp_;
134 scoped_ptr<crypto::Encryptor> decryptor_;
135 scoped_ptr<crypto::SymmetricKey> decryption_key_;
136 std::string iv_mask_;
137 base::TimeTicks last_playout_time_;
139 std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_;
140 std::list<DecodedAudioCallbackData> queued_decoded_callbacks_;
146 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_