Home | History | Annotate | Download | only in audio_sender
      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_SENDER_H_
      6 #define MEDIA_CAST_AUDIO_SENDER_H_
      7 
      8 #include "base/callback.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/memory/weak_ptr.h"
     12 #include "base/threading/non_thread_safe.h"
     13 #include "base/time/tick_clock.h"
     14 #include "base/time/time.h"
     15 #include "media/cast/cast_config.h"
     16 #include "media/cast/cast_environment.h"
     17 #include "media/cast/net/rtp_sender/rtp_sender.h"
     18 #include "media/cast/rtcp/rtcp.h"
     19 
     20 namespace crypto {
     21   class Encryptor;
     22 }
     23 
     24 namespace media {
     25 class AudioBus;
     26 }
     27 
     28 namespace media {
     29 namespace cast {
     30 
     31 class AudioEncoder;
     32 class LocalRtcpAudioSenderFeedback;
     33 class LocalRtpSenderStatistics;
     34 class PacedPacketSender;
     35 
     36 // This class is not thread safe.
     37 // It's only called from the main cast thread.
     38 class AudioSender : public base::NonThreadSafe,
     39                     public base::SupportsWeakPtr<AudioSender> {
     40  public:
     41   AudioSender(scoped_refptr<CastEnvironment> cast_environment,
     42               const AudioSenderConfig& audio_config,
     43               PacedPacketSender* const paced_packet_sender);
     44 
     45   virtual ~AudioSender();
     46 
     47   // The |audio_bus| must be valid until the |done_callback| is called.
     48   // The callback is called from the main cast thread as soon as the encoder is
     49   // done with |audio_bus|; it does not mean that the encoded data has been
     50   // sent out.
     51   void InsertAudio(const AudioBus* audio_bus,
     52                    const base::TimeTicks& recorded_time,
     53                    const base::Closure& done_callback);
     54 
     55   // The audio_frame must be valid until the closure callback is called.
     56   // The closure callback is called from the main cast thread as soon as
     57   // the cast sender is done with the frame; it does not mean that the encoded
     58   // frame has been sent out.
     59   void InsertCodedAudioFrame(const EncodedAudioFrame* audio_frame,
     60                              const base::TimeTicks& recorded_time,
     61                              const base::Closure callback);
     62 
     63   // Only called from the main cast thread.
     64   void IncomingRtcpPacket(const uint8* packet, size_t length,
     65                           const base::Closure callback);
     66 
     67  protected:
     68   void SendEncodedAudioFrame(scoped_ptr<EncodedAudioFrame> audio_frame,
     69                              const base::TimeTicks& recorded_time);
     70 
     71  private:
     72   friend class LocalRtcpAudioSenderFeedback;
     73 
     74   void ResendPackets(
     75       const MissingFramesAndPacketsMap& missing_frames_and_packets);
     76 
     77   // Caller must allocate the destination |encrypted_frame|. The data member
     78   // will be resized to hold the encrypted size.
     79   bool EncryptAudioFrame(const EncodedAudioFrame& audio_frame,
     80                          EncodedAudioFrame* encrypted_frame);
     81 
     82   void ScheduleNextRtcpReport();
     83   void SendRtcpReport();
     84 
     85   void InitializeTimers();
     86 
     87   base::WeakPtrFactory<AudioSender> weak_factory_;
     88 
     89   scoped_refptr<CastEnvironment> cast_environment_;
     90   scoped_refptr<AudioEncoder> audio_encoder_;
     91   RtpSender rtp_sender_;
     92   scoped_ptr<LocalRtpSenderStatistics> rtp_audio_sender_statistics_;
     93   scoped_ptr<LocalRtcpAudioSenderFeedback> rtcp_feedback_;
     94   Rtcp rtcp_;
     95   bool initialized_;
     96   scoped_ptr<crypto::Encryptor> encryptor_;
     97   std::string iv_mask_;
     98 
     99   DISALLOW_COPY_AND_ASSIGN(AudioSender);
    100 };
    101 
    102 }  // namespace cast
    103 }  // namespace media
    104 
    105 #endif  // MEDIA_CAST_AUDIO_SENDER_H_
    106