Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / cast / sender / audio_sender.h
1 // Copyright 2014 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_SENDER_AUDIO_SENDER_H_
6 #define MEDIA_CAST_SENDER_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/base/audio_bus.h"
16 #include "media/cast/cast_config.h"
17 #include "media/cast/sender/frame_sender.h"
18
19 namespace media {
20 namespace cast {
21
22 class AudioEncoder;
23
24 // Not thread safe. Only called from the main cast thread.
25 // This class owns all objects related to sending audio, objects that create RTP
26 // packets, congestion control, audio encoder, parsing and sending of
27 // RTCP packets.
28 // Additionally it posts a bunch of delayed tasks to the main thread for various
29 // timeouts.
30 class AudioSender : public FrameSender,
31                     public base::NonThreadSafe,
32                     public base::SupportsWeakPtr<AudioSender> {
33  public:
34   AudioSender(scoped_refptr<CastEnvironment> cast_environment,
35               const AudioSenderConfig& audio_config,
36               CastTransportSender* const transport_sender);
37
38   virtual ~AudioSender();
39
40   CastInitializationStatus InitializationResult() const {
41     return cast_initialization_status_;
42   }
43
44   // Note: It is not guaranteed that |audio_frame| will actually be encoded and
45   // sent, if AudioSender detects too many frames in flight.  Therefore, clients
46   // should be careful about the rate at which this method is called.
47   //
48   // Note: It is invalid to call this method if InitializationResult() returns
49   // anything but STATUS_AUDIO_INITIALIZED.
50   void InsertAudio(scoped_ptr<AudioBus> audio_bus,
51                    const base::TimeTicks& recorded_time);
52
53  protected:
54   // Protected for testability.
55   void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback);
56
57  private:
58   // Schedule and execute periodic checks for re-sending packets.  If no
59   // acknowledgements have been received for "too long," AudioSender will
60   // speculatively re-send certain packets of an unacked frame to kick-start
61   // re-transmission.  This is a last resort tactic to prevent the session from
62   // getting stuck after a long outage.
63   void ScheduleNextResendCheck();
64   void ResendCheck();
65   void ResendForKickstart();
66
67   // Returns true if there are too many frames in flight, as defined by the
68   // configured target playout delay plus simple logic.  When this is true,
69   // InsertAudio() will silenty drop frames instead of sending them to the audio
70   // encoder.
71   bool AreTooManyFramesInFlight() const;
72
73   // Called by the |audio_encoder_| with the next EncodedFrame to send.
74   void SendEncodedAudioFrame(scoped_ptr<EncodedFrame> audio_frame);
75
76   // The total amount of time between a frame's capture/recording on the sender
77   // and its playback on the receiver (i.e., shown to a user).  This is fixed as
78   // a value large enough to give the system sufficient time to encode,
79   // transmit/retransmit, receive, decode, and render; given its run-time
80   // environment (sender/receiver hardware performance, network conditions,
81   // etc.).
82   const base::TimeDelta target_playout_delay_;
83
84   // Maximum number of outstanding frames before the encoding and sending of
85   // new frames shall halt.
86   const int max_unacked_frames_;
87
88   // Encodes AudioBuses into EncodedFrames.
89   scoped_ptr<AudioEncoder> audio_encoder_;
90   const int configured_encoder_bitrate_;
91
92   // Counts how many RTCP reports are being "aggressively" sent (i.e., one per
93   // frame) at the start of the session.  Once a threshold is reached, RTCP
94   // reports are instead sent at the configured interval + random drift.
95   int num_aggressive_rtcp_reports_sent_;
96
97   // This is "null" until the first frame is sent.  Thereafter, this tracks the
98   // last time any frame was sent or re-sent.
99   base::TimeTicks last_send_time_;
100
101   // The ID of the last frame sent.  Logic throughout AudioSender assumes this
102   // can safely wrap-around.  This member is invalid until
103   // |!last_send_time_.is_null()|.
104   uint32 last_sent_frame_id_;
105
106   // The ID of the latest (not necessarily the last) frame that has been
107   // acknowledged.  Logic throughout AudioSender assumes this can safely
108   // wrap-around.  This member is invalid until |!last_send_time_.is_null()|.
109   uint32 latest_acked_frame_id_;
110
111   // Counts the number of duplicate ACK that are being received.  When this
112   // number reaches a threshold, the sender will take this as a sign that the
113   // receiver hasn't yet received the first packet of the next frame.  In this
114   // case, AudioSender will trigger a re-send of the next frame.
115   int duplicate_ack_counter_;
116
117   // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED.
118   CastInitializationStatus cast_initialization_status_;
119
120   // This is a "good enough" mapping for finding the RTP timestamp associated
121   // with a video frame. The key is the lowest 8 bits of frame id (which is
122   // what is sent via RTCP). This map is used for logging purposes.
123   RtpTimestamp frame_id_to_rtp_timestamp_[256];
124
125   // NOTE: Weak pointers must be invalidated before all other member variables.
126   base::WeakPtrFactory<AudioSender> weak_factory_;
127
128   DISALLOW_COPY_AND_ASSIGN(AudioSender);
129 };
130
131 }  // namespace cast
132 }  // namespace media
133
134 #endif  // MEDIA_CAST_SENDER_AUDIO_SENDER_H_