Upstream version 5.34.92.0
[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_receiver/rtp_receiver_defines.h"  // RtpCastHeader
21
22 namespace crypto {
23 class Encryptor;
24 class SymmetricKey;
25 }
26
27 namespace media {
28 namespace cast {
29
30 class AudioDecoder;
31 class Framer;
32 class LocalRtpAudioData;
33 class LocalRtpAudioFeedback;
34 class RtpReceiver;
35 class RtpReceiverStatistics;
36
37 struct DecodedAudioCallbackData {
38   DecodedAudioCallbackData();
39   ~DecodedAudioCallbackData();
40   int number_of_10ms_blocks;
41   int desired_frequency;
42   AudioFrameDecodedCallback callback;
43 };
44
45 // This class is not thread safe. Should only be called from the Main cast
46 // thread.
47 class AudioReceiver : public base::NonThreadSafe,
48                       public base::SupportsWeakPtr<AudioReceiver> {
49  public:
50   AudioReceiver(scoped_refptr<CastEnvironment> cast_environment,
51                 const AudioReceiverConfig& audio_config,
52                 transport::PacedPacketSender* const packet_sender);
53
54   virtual ~AudioReceiver();
55
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);
61
62   // Extract an encoded audio frame from the cast receiver.
63   void GetEncodedAudioFrame(const AudioFrameEncodedCallback& callback);
64
65   // Should only be called from the main cast thread.
66   void IncomingPacket(const uint8* packet, size_t length,
67                       const base::Closure callback);
68
69  protected:
70   void IncomingParsedRtpPacket(const uint8* payload_data,
71                                size_t payload_size,
72                                const RtpCastHeader& rtp_header);
73  private:
74   friend class LocalRtpAudioData;
75   friend class LocalRtpAudioFeedback;
76
77   void CastFeedback(const RtcpCastMessage& cast_message);
78
79   // Time to pull out the audio even though we are missing data.
80   void PlayoutTimeout();
81
82   bool PostEncodedAudioFrame(
83       const AudioFrameEncodedCallback& callback,
84       uint32 rtp_timestamp,
85       bool next_frame,
86       scoped_ptr<transport::EncodedAudioFrame>* encoded_frame);
87
88   // Actual decoding implementation - should be called under the audio decoder
89   // thread.
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);
96
97   // Return the playout time based on the current time and rtp timestamp.
98   base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp);
99
100   void InitializeTimers();
101
102   // Decrypts the data within the |audio_frame| and replaces the data with the
103   // decrypted string.
104   bool DecryptAudioFrame(scoped_ptr<transport::EncodedAudioFrame>* audio_frame);
105
106   // Schedule the next RTCP report.
107   void ScheduleNextRtcpReport();
108
109   // Actually send the next RTCP report.
110   void SendNextRtcpReport();
111
112   // Schedule timing for the next cast message.
113   void ScheduleNextCastMessage();
114
115   // Actually send the next cast message.
116   void SendNextCastMessage();
117
118   scoped_refptr<CastEnvironment> cast_environment_;
119   base::WeakPtrFactory<AudioReceiver> weak_factory_;
120
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_;
138
139   std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_;
140   std::list<DecodedAudioCallbackData> queued_decoded_callbacks_;
141 };
142
143 }  // namespace cast
144 }  // namespace media
145
146 #endif  // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_