OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_CAST_AUDIO_SENDER_H_ | 5 #ifndef MEDIA_CAST_AUDIO_SENDER_H_ |
6 #define MEDIA_CAST_AUDIO_SENDER_H_ | 6 #define MEDIA_CAST_AUDIO_SENDER_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
13 #include "base/time/tick_clock.h" | 13 #include "base/time/tick_clock.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "media/base/audio_bus.h" | 15 #include "media/base/audio_bus.h" |
16 #include "media/cast/cast_config.h" | 16 #include "media/cast/cast_config.h" |
| 17 #include "media/cast/cast_environment.h" |
| 18 #include "media/cast/logging/logging_defines.h" |
17 #include "media/cast/rtcp/rtcp.h" | 19 #include "media/cast/rtcp/rtcp.h" |
18 #include "media/cast/rtp_timestamp_helper.h" | 20 #include "media/cast/rtp_timestamp_helper.h" |
19 | 21 |
20 namespace media { | 22 namespace media { |
21 namespace cast { | 23 namespace cast { |
22 | 24 |
23 class AudioEncoder; | 25 class AudioEncoder; |
24 | 26 |
25 // This class is not thread safe. | 27 // Not thread safe. Only called from the main cast thread. |
26 // It's only called from the main cast thread. | 28 // This class owns all objects related to sending audio, objects that create RTP |
| 29 // packets, congestion control, audio encoder, parsing and sending of |
| 30 // RTCP packets. |
| 31 // Additionally it posts a bunch of delayed tasks to the main thread for various |
| 32 // timeouts. |
27 class AudioSender : public RtcpSenderFeedback, | 33 class AudioSender : public RtcpSenderFeedback, |
28 public base::NonThreadSafe, | 34 public base::NonThreadSafe, |
29 public base::SupportsWeakPtr<AudioSender> { | 35 public base::SupportsWeakPtr<AudioSender> { |
30 public: | 36 public: |
31 AudioSender(scoped_refptr<CastEnvironment> cast_environment, | 37 AudioSender(scoped_refptr<CastEnvironment> cast_environment, |
32 const AudioSenderConfig& audio_config, | 38 const AudioSenderConfig& audio_config, |
33 transport::CastTransportSender* const transport_sender); | 39 transport::CastTransportSender* const transport_sender); |
34 | 40 |
35 virtual ~AudioSender(); | 41 virtual ~AudioSender(); |
36 | 42 |
37 CastInitializationStatus InitializationResult() const { | 43 CastInitializationStatus InitializationResult() const { |
38 return cast_initialization_status_; | 44 return cast_initialization_status_; |
39 } | 45 } |
40 | 46 |
| 47 // Note: It is not guaranteed that |audio_frame| will actually be encoded and |
| 48 // sent, if AudioSender detects too many frames in flight. Therefore, clients |
| 49 // should be careful about the rate at which this method is called. |
| 50 // |
41 // Note: It is invalid to call this method if InitializationResult() returns | 51 // Note: It is invalid to call this method if InitializationResult() returns |
42 // anything but STATUS_AUDIO_INITIALIZED. | 52 // anything but STATUS_AUDIO_INITIALIZED. |
43 void InsertAudio(scoped_ptr<AudioBus> audio_bus, | 53 void InsertAudio(scoped_ptr<AudioBus> audio_bus, |
44 const base::TimeTicks& recorded_time); | 54 const base::TimeTicks& recorded_time); |
45 | 55 |
46 // Only called from the main cast thread. | 56 // Only called from the main cast thread. |
47 void IncomingRtcpPacket(scoped_ptr<Packet> packet); | 57 void IncomingRtcpPacket(scoped_ptr<Packet> packet); |
48 | 58 |
| 59 protected: |
| 60 // Protected for testability. |
| 61 virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) |
| 62 OVERRIDE; |
| 63 |
49 private: | 64 private: |
50 void ResendPackets( | 65 // Schedule and execute periodic sending of RTCP report. |
51 const MissingFramesAndPacketsMap& missing_frames_and_packets); | |
52 | |
53 void ScheduleNextRtcpReport(); | 66 void ScheduleNextRtcpReport(); |
54 void SendRtcpReport(bool schedule_future_reports); | 67 void SendRtcpReport(bool schedule_future_reports); |
55 | 68 |
| 69 // Schedule and execute periodic checks for re-sending packets. If no |
| 70 // acknowledgements have been received for "too long," AudioSender will |
| 71 // speculatively re-send certain packets of an unacked frame to kick-start |
| 72 // re-transmission. This is a last resort tactic to prevent the session from |
| 73 // getting stuck after a long outage. |
| 74 void ScheduleNextResendCheck(); |
| 75 void ResendCheck(); |
| 76 void ResendForKickstart(); |
| 77 |
| 78 // Returns true if there are too many frames in flight, as defined by the |
| 79 // configured target playout delay plus simple logic. When this is true, |
| 80 // InsertAudio() will silenty drop frames instead of sending them to the audio |
| 81 // encoder. |
| 82 bool AreTooManyFramesInFlight() const; |
| 83 |
56 // Called by the |audio_encoder_| with the next EncodedFrame to send. | 84 // Called by the |audio_encoder_| with the next EncodedFrame to send. |
57 void SendEncodedAudioFrame(scoped_ptr<transport::EncodedFrame> audio_frame); | 85 void SendEncodedAudioFrame(scoped_ptr<transport::EncodedFrame> audio_frame); |
58 | 86 |
59 virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) | 87 const scoped_refptr<CastEnvironment> cast_environment_; |
60 OVERRIDE; | |
61 | 88 |
62 scoped_refptr<CastEnvironment> cast_environment_; | 89 // The total amount of time between a frame's capture/recording on the sender |
| 90 // and its playback on the receiver (i.e., shown to a user). This is fixed as |
| 91 // a value large enough to give the system sufficient time to encode, |
| 92 // transmit/retransmit, receive, decode, and render; given its run-time |
| 93 // environment (sender/receiver hardware performance, network conditions, |
| 94 // etc.). |
| 95 const base::TimeDelta target_playout_delay_; |
| 96 |
| 97 // Sends encoded frames over the configured transport (e.g., UDP). In |
| 98 // Chromium, this could be a proxy that first sends the frames from a renderer |
| 99 // process to the browser process over IPC, with the browser process being |
| 100 // responsible for "packetizing" the frames and pushing packets into the |
| 101 // network layer. |
63 transport::CastTransportSender* const transport_sender_; | 102 transport::CastTransportSender* const transport_sender_; |
| 103 |
| 104 // Maximum number of outstanding frames before the encoding and sending of |
| 105 // new frames shall halt. |
| 106 const int max_unacked_frames_; |
| 107 |
| 108 // Encodes AudioBuses into EncodedFrames. |
64 scoped_ptr<AudioEncoder> audio_encoder_; | 109 scoped_ptr<AudioEncoder> audio_encoder_; |
| 110 const int configured_encoder_bitrate_; |
| 111 |
| 112 // Manages sending/receiving of RTCP packets, including sender/receiver |
| 113 // reports. |
| 114 Rtcp rtcp_; |
| 115 |
| 116 // Records lip-sync (i.e., mapping of RTP <--> NTP timestamps), and |
| 117 // extrapolates this mapping to any other point in time. |
65 RtpTimestampHelper rtp_timestamp_helper_; | 118 RtpTimestampHelper rtp_timestamp_helper_; |
66 Rtcp rtcp_; | 119 |
| 120 // Counts how many RTCP reports are being "aggressively" sent (i.e., one per |
| 121 // frame) at the start of the session. Once a threshold is reached, RTCP |
| 122 // reports are instead sent at the configured interval + random drift. |
67 int num_aggressive_rtcp_reports_sent_; | 123 int num_aggressive_rtcp_reports_sent_; |
68 | 124 |
| 125 // This is "null" until the first frame is sent. Thereafter, this tracks the |
| 126 // last time any frame was sent or re-sent. |
| 127 base::TimeTicks last_send_time_; |
| 128 |
| 129 // The ID of the last frame sent. Logic throughout AudioSender assumes this |
| 130 // can safely wrap-around. This member is invalid until |
| 131 // |!last_send_time_.is_null()|. |
| 132 uint32 last_sent_frame_id_; |
| 133 |
| 134 // The ID of the latest (not necessarily the last) frame that has been |
| 135 // acknowledged. Logic throughout AudioSender assumes this can safely |
| 136 // wrap-around. This member is invalid until |!last_send_time_.is_null()|. |
| 137 uint32 latest_acked_frame_id_; |
| 138 |
| 139 // Counts the number of duplicate ACK that are being received. When this |
| 140 // number reaches a threshold, the sender will take this as a sign that the |
| 141 // receiver hasn't yet received the first packet of the next frame. In this |
| 142 // case, AudioSender will trigger a re-send of the next frame. |
| 143 int duplicate_ack_counter_; |
| 144 |
69 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED. | 145 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED. |
70 CastInitializationStatus cast_initialization_status_; | 146 CastInitializationStatus cast_initialization_status_; |
71 | 147 |
72 // Used to map the lower 8 bits of the frame id to a RTP timestamp. This is | 148 // This is a "good enough" mapping for finding the RTP timestamp associated |
73 // good enough as we only use it for logging. | 149 // with a video frame. The key is the lowest 8 bits of frame id (which is |
| 150 // what is sent via RTCP). This map is used for logging purposes. |
74 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 151 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
75 | 152 |
76 // NOTE: Weak pointers must be invalidated before all other member variables. | 153 // NOTE: Weak pointers must be invalidated before all other member variables. |
77 base::WeakPtrFactory<AudioSender> weak_factory_; | 154 base::WeakPtrFactory<AudioSender> weak_factory_; |
78 | 155 |
79 DISALLOW_COPY_AND_ASSIGN(AudioSender); | 156 DISALLOW_COPY_AND_ASSIGN(AudioSender); |
80 }; | 157 }; |
81 | 158 |
82 } // namespace cast | 159 } // namespace cast |
83 } // namespace media | 160 } // namespace media |
84 | 161 |
85 #endif // MEDIA_CAST_AUDIO_SENDER_H_ | 162 #endif // MEDIA_CAST_AUDIO_SENDER_H_ |
OLD | NEW |