| OLD | NEW |
| (Empty) |
| 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/base/audio_bus.h" | |
| 16 #include "media/cast/cast_config.h" | |
| 17 #include "media/cast/cast_environment.h" | |
| 18 #include "media/cast/logging/logging_defines.h" | |
| 19 #include "media/cast/rtcp/rtcp.h" | |
| 20 #include "media/cast/rtp_timestamp_helper.h" | |
| 21 | |
| 22 namespace media { | |
| 23 namespace cast { | |
| 24 | |
| 25 class AudioEncoder; | |
| 26 | |
| 27 // Not thread safe. 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. | |
| 33 class AudioSender : public RtcpSenderFeedback, | |
| 34 public base::NonThreadSafe, | |
| 35 public base::SupportsWeakPtr<AudioSender> { | |
| 36 public: | |
| 37 AudioSender(scoped_refptr<CastEnvironment> cast_environment, | |
| 38 const AudioSenderConfig& audio_config, | |
| 39 transport::CastTransportSender* const transport_sender); | |
| 40 | |
| 41 virtual ~AudioSender(); | |
| 42 | |
| 43 CastInitializationStatus InitializationResult() const { | |
| 44 return cast_initialization_status_; | |
| 45 } | |
| 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 // | |
| 51 // Note: It is invalid to call this method if InitializationResult() returns | |
| 52 // anything but STATUS_AUDIO_INITIALIZED. | |
| 53 void InsertAudio(scoped_ptr<AudioBus> audio_bus, | |
| 54 const base::TimeTicks& recorded_time); | |
| 55 | |
| 56 // Only called from the main cast thread. | |
| 57 void IncomingRtcpPacket(scoped_ptr<Packet> packet); | |
| 58 | |
| 59 protected: | |
| 60 // Protected for testability. | |
| 61 virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) | |
| 62 OVERRIDE; | |
| 63 | |
| 64 private: | |
| 65 // Schedule and execute periodic sending of RTCP report. | |
| 66 void ScheduleNextRtcpReport(); | |
| 67 void SendRtcpReport(bool schedule_future_reports); | |
| 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 | |
| 84 // Called by the |audio_encoder_| with the next EncodedFrame to send. | |
| 85 void SendEncodedAudioFrame(scoped_ptr<transport::EncodedFrame> audio_frame); | |
| 86 | |
| 87 const scoped_refptr<CastEnvironment> cast_environment_; | |
| 88 | |
| 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. | |
| 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. | |
| 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. | |
| 118 RtpTimestampHelper rtp_timestamp_helper_; | |
| 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. | |
| 123 int num_aggressive_rtcp_reports_sent_; | |
| 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 | |
| 145 // If this sender is ready for use, this is STATUS_AUDIO_INITIALIZED. | |
| 146 CastInitializationStatus cast_initialization_status_; | |
| 147 | |
| 148 // This is a "good enough" mapping for finding the RTP timestamp associated | |
| 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. | |
| 151 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | |
| 152 | |
| 153 // NOTE: Weak pointers must be invalidated before all other member variables. | |
| 154 base::WeakPtrFactory<AudioSender> weak_factory_; | |
| 155 | |
| 156 DISALLOW_COPY_AND_ASSIGN(AudioSender); | |
| 157 }; | |
| 158 | |
| 159 } // namespace cast | |
| 160 } // namespace media | |
| 161 | |
| 162 #endif // MEDIA_CAST_AUDIO_SENDER_H_ | |
| OLD | NEW |