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_common/rtp_defines.h" // RtpCastHeader
27 class LocalRtpAudioData;
28 class LocalRtpAudioFeedback;
29 class PacedPacketSender;
31 class RtpReceiverStatistics;
33 // This class is not thread safe. Should only be called from the Main cast
35 class AudioReceiver : public base::NonThreadSafe,
36 public base::SupportsWeakPtr<AudioReceiver> {
38 AudioReceiver(scoped_refptr<CastEnvironment> cast_environment,
39 const AudioReceiverConfig& audio_config,
40 PacedPacketSender* const packet_sender);
42 virtual ~AudioReceiver();
44 // Extract a raw audio frame from the cast receiver.
45 // Actual decoding will be preformed on a designated audio_decoder thread.
46 void GetRawAudioFrame(int number_of_10ms_blocks,
47 int desired_frequency,
48 const AudioFrameDecodedCallback& callback);
50 // Extract an encoded audio frame from the cast receiver.
51 void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback);
53 // Should only be called from the main cast thread.
54 void IncomingPacket(const uint8* packet, size_t length,
55 const base::Closure callback);
58 void IncomingParsedRtpPacket(const uint8* payload_data,
60 const RtpCastHeader& rtp_header);
62 friend class LocalRtpAudioData;
63 friend class LocalRtpAudioFeedback;
65 void CastFeedback(const RtcpCastMessage& cast_message);
67 // Time to pull out the audio even though we are missing data.
68 void PlayoutTimeout();
70 bool PostEncodedAudioFrame(const AudioFrameEncodedCallback& callback,
73 scoped_ptr<EncodedAudioFrame>* encoded_frame);
75 // Actual decoding implementation - should be called under the audio decoder
77 void DecodeAudioFrameThread(int number_of_10ms_blocks,
78 int desired_frequency,
79 const AudioFrameDecodedCallback callback);
81 // Return the playout time based on the current time and rtp timestamp.
82 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp);
84 // Schedule the next RTCP report.
85 void ScheduleNextRtcpReport();
87 // Actually send the next RTCP report.
88 void SendNextRtcpReport();
90 // Schedule timing for the next cast message.
91 void ScheduleNextCastMessage();
93 // Actually send the next cast message.
94 void SendNextCastMessage();
96 scoped_refptr<CastEnvironment> cast_environment_;
97 base::WeakPtrFactory<AudioReceiver> weak_factory_;
99 const AudioCodec codec_;
100 const uint32 incoming_ssrc_;
101 const int frequency_;
102 base::TimeDelta target_delay_delta_;
103 scoped_ptr<Framer> audio_buffer_;
104 scoped_refptr<AudioDecoder> audio_decoder_;
105 scoped_ptr<LocalRtpAudioData> incoming_payload_callback_;
106 scoped_ptr<LocalRtpAudioFeedback> incoming_payload_feedback_;
107 scoped_ptr<RtpReceiver> rtp_receiver_;
108 scoped_ptr<Rtcp> rtcp_;
109 scoped_ptr<RtpReceiverStatistics> rtp_audio_receiver_statistics_;
110 base::TimeDelta time_offset_;
111 base::TimeTicks time_first_incoming_packet_;
112 uint32 first_incoming_rtp_timestamp_;
114 std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_;
120 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_