- add sources.
[platform/framework/web/crosswalk.git] / src / media / cast / audio_receiver / audio_receiver.h
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.
4
5 #ifndef MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_
6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_
7
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
21
22 namespace media {
23 namespace cast {
24
25 class AudioDecoder;
26 class Framer;
27 class LocalRtpAudioData;
28 class LocalRtpAudioFeedback;
29 class PacedPacketSender;
30 class RtpReceiver;
31 class RtpReceiverStatistics;
32
33 // This class is not thread safe. Should only be called from the Main cast
34 // thread.
35 class AudioReceiver : public base::NonThreadSafe,
36                       public base::SupportsWeakPtr<AudioReceiver> {
37  public:
38   AudioReceiver(scoped_refptr<CastEnvironment> cast_environment,
39                 const AudioReceiverConfig& audio_config,
40                 PacedPacketSender* const packet_sender);
41
42   virtual ~AudioReceiver();
43
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);
49
50   // Extract an encoded audio frame from the cast receiver.
51   void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback);
52
53   // Should only be called from the main cast thread.
54   void IncomingPacket(const uint8* packet, size_t length,
55                       const base::Closure callback);
56
57  protected:
58   void IncomingParsedRtpPacket(const uint8* payload_data,
59                                size_t payload_size,
60                                const RtpCastHeader& rtp_header);
61  private:
62   friend class LocalRtpAudioData;
63   friend class LocalRtpAudioFeedback;
64
65   void CastFeedback(const RtcpCastMessage& cast_message);
66
67   // Time to pull out the audio even though we are missing data.
68   void PlayoutTimeout();
69
70   bool PostEncodedAudioFrame(const AudioFrameEncodedCallback& callback,
71                              uint32 rtp_timestamp,
72                              bool next_frame,
73                              scoped_ptr<EncodedAudioFrame>* encoded_frame);
74
75   // Actual decoding implementation - should be called under the audio decoder
76   // thread.
77   void DecodeAudioFrameThread(int number_of_10ms_blocks,
78                               int desired_frequency,
79                               const AudioFrameDecodedCallback callback);
80
81   // Return the playout time based on the current time and rtp timestamp.
82   base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp);
83
84   // Schedule the next RTCP report.
85   void ScheduleNextRtcpReport();
86
87   // Actually send the next RTCP report.
88   void SendNextRtcpReport();
89
90   // Schedule timing for the next cast message.
91   void ScheduleNextCastMessage();
92
93   // Actually send the next cast message.
94   void SendNextCastMessage();
95
96   scoped_refptr<CastEnvironment> cast_environment_;
97   base::WeakPtrFactory<AudioReceiver> weak_factory_;
98
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_;
113
114   std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_;
115 };
116
117 }  // namespace cast
118 }  // namespace media
119
120 #endif  // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_