Home | History | Annotate | Download | only in audio_receiver
      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 }
     25 
     26 namespace media {
     27 namespace cast {
     28 
     29 class AudioDecoder;
     30 class Framer;
     31 class LocalRtpAudioData;
     32 class LocalRtpAudioFeedback;
     33 class PacedPacketSender;
     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                 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(const AudioFrameEncodedCallback& callback,
     83                              uint32 rtp_timestamp,
     84                              bool next_frame,
     85                              scoped_ptr<EncodedAudioFrame>* encoded_frame);
     86 
     87   // Actual decoding implementation - should be called under the audio decoder
     88   // thread.
     89   void DecodeAudioFrameThread(int number_of_10ms_blocks,
     90                               int desired_frequency,
     91                               const AudioFrameDecodedCallback callback);
     92   void ReturnDecodedFrameWithPlayoutDelay(
     93       scoped_ptr<PcmAudioFrame> audio_frame, uint32 rtp_timestamp,
     94       const AudioFrameDecodedCallback callback);
     95 
     96   // Return the playout time based on the current time and rtp timestamp.
     97   base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp);
     98 
     99   void InitializeTimers();
    100 
    101   // Decrypts the data within the |audio_frame| and replaces the data with the
    102   // decrypted string.
    103   bool DecryptAudioFrame(scoped_ptr<EncodedAudioFrame>* audio_frame);
    104 
    105   // Schedule the next RTCP report.
    106   void ScheduleNextRtcpReport();
    107 
    108   // Actually send the next RTCP report.
    109   void SendNextRtcpReport();
    110 
    111   // Schedule timing for the next cast message.
    112   void ScheduleNextCastMessage();
    113 
    114   // Actually send the next cast message.
    115   void SendNextCastMessage();
    116 
    117   scoped_refptr<CastEnvironment> cast_environment_;
    118   base::WeakPtrFactory<AudioReceiver> weak_factory_;
    119 
    120   const AudioCodec codec_;
    121   const int frequency_;
    122   base::TimeDelta target_delay_delta_;
    123   scoped_ptr<Framer> audio_buffer_;
    124   scoped_ptr<AudioDecoder> audio_decoder_;
    125   scoped_ptr<LocalRtpAudioData> incoming_payload_callback_;
    126   scoped_ptr<LocalRtpAudioFeedback> incoming_payload_feedback_;
    127   scoped_ptr<RtpReceiver> rtp_receiver_;
    128   scoped_ptr<Rtcp> rtcp_;
    129   scoped_ptr<RtpReceiverStatistics> rtp_audio_receiver_statistics_;
    130   base::TimeDelta time_offset_;
    131   base::TimeTicks time_first_incoming_packet_;
    132   uint32 first_incoming_rtp_timestamp_;
    133   scoped_ptr<crypto::Encryptor> decryptor_;
    134   std::string iv_mask_;
    135 
    136   std::list<AudioFrameEncodedCallback> queued_encoded_callbacks_;
    137   std::list<DecodedAudioCallbackData> queued_decoded_callbacks_;
    138 };
    139 
    140 }  // namespace cast
    141 }  // namespace media
    142 
    143 #endif  // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_
    144