OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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_SENDER_AUDIO_SENDER_H_ | 5 #ifndef MEDIA_CAST_SENDER_AUDIO_SENDER_H_ |
6 #define MEDIA_CAST_SENDER_AUDIO_SENDER_H_ | 6 #define MEDIA_CAST_SENDER_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" | 17 #include "media/cast/sender/frame_sender.h" |
18 #include "media/cast/logging/logging_defines.h" | |
19 #include "media/cast/net/rtcp/rtcp.h" | |
20 #include "media/cast/sender/rtp_timestamp_helper.h" | |
21 | 18 |
22 namespace media { | 19 namespace media { |
23 namespace cast { | 20 namespace cast { |
24 | 21 |
25 class AudioEncoder; | 22 class AudioEncoder; |
26 | 23 |
27 // Not thread safe. Only called from the main cast thread. | 24 // Not thread safe. Only called from the main cast thread. |
28 // This class owns all objects related to sending audio, objects that create RTP | 25 // This class owns all objects related to sending audio, objects that create RTP |
29 // packets, congestion control, audio encoder, parsing and sending of | 26 // packets, congestion control, audio encoder, parsing and sending of |
30 // RTCP packets. | 27 // RTCP packets. |
31 // Additionally it posts a bunch of delayed tasks to the main thread for various | 28 // Additionally it posts a bunch of delayed tasks to the main thread for various |
32 // timeouts. | 29 // timeouts. |
33 class AudioSender : public RtcpSenderFeedback, | 30 class AudioSender : public FrameSender, |
34 public base::NonThreadSafe, | 31 public base::NonThreadSafe, |
35 public base::SupportsWeakPtr<AudioSender> { | 32 public base::SupportsWeakPtr<AudioSender> { |
36 public: | 33 public: |
37 AudioSender(scoped_refptr<CastEnvironment> cast_environment, | 34 AudioSender(scoped_refptr<CastEnvironment> cast_environment, |
38 const AudioSenderConfig& audio_config, | 35 const AudioSenderConfig& audio_config, |
39 CastTransportSender* const transport_sender); | 36 CastTransportSender* const transport_sender); |
40 | 37 |
41 virtual ~AudioSender(); | 38 virtual ~AudioSender(); |
42 | 39 |
43 CastInitializationStatus InitializationResult() const { | 40 CastInitializationStatus InitializationResult() const { |
44 return cast_initialization_status_; | 41 return cast_initialization_status_; |
45 } | 42 } |
46 | 43 |
47 // Note: It is not guaranteed that |audio_frame| will actually be encoded and | 44 // 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 | 45 // sent, if AudioSender detects too many frames in flight. Therefore, clients |
49 // should be careful about the rate at which this method is called. | 46 // should be careful about the rate at which this method is called. |
50 // | 47 // |
51 // Note: It is invalid to call this method if InitializationResult() returns | 48 // Note: It is invalid to call this method if InitializationResult() returns |
52 // anything but STATUS_AUDIO_INITIALIZED. | 49 // anything but STATUS_AUDIO_INITIALIZED. |
53 void InsertAudio(scoped_ptr<AudioBus> audio_bus, | 50 void InsertAudio(scoped_ptr<AudioBus> audio_bus, |
54 const base::TimeTicks& recorded_time); | 51 const base::TimeTicks& recorded_time); |
55 | 52 |
56 // Only called from the main cast thread. | |
57 void IncomingRtcpPacket(scoped_ptr<Packet> packet); | |
58 | |
59 protected: | 53 protected: |
60 // Protected for testability. | 54 // Protected for testability. |
61 virtual void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) | 55 void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback); |
62 OVERRIDE; | |
63 | 56 |
64 private: | 57 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 | 58 // Schedule and execute periodic checks for re-sending packets. If no |
70 // acknowledgements have been received for "too long," AudioSender will | 59 // acknowledgements have been received for "too long," AudioSender will |
71 // speculatively re-send certain packets of an unacked frame to kick-start | 60 // 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 | 61 // re-transmission. This is a last resort tactic to prevent the session from |
73 // getting stuck after a long outage. | 62 // getting stuck after a long outage. |
74 void ScheduleNextResendCheck(); | 63 void ScheduleNextResendCheck(); |
75 void ResendCheck(); | 64 void ResendCheck(); |
76 void ResendForKickstart(); | 65 void ResendForKickstart(); |
77 | 66 |
78 // Returns true if there are too many frames in flight, as defined by the | 67 // 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, | 68 // configured target playout delay plus simple logic. When this is true, |
80 // InsertAudio() will silenty drop frames instead of sending them to the audio | 69 // InsertAudio() will silenty drop frames instead of sending them to the audio |
81 // encoder. | 70 // encoder. |
82 bool AreTooManyFramesInFlight() const; | 71 bool AreTooManyFramesInFlight() const; |
83 | 72 |
84 // Called by the |audio_encoder_| with the next EncodedFrame to send. | 73 // Called by the |audio_encoder_| with the next EncodedFrame to send. |
85 void SendEncodedAudioFrame(scoped_ptr<EncodedFrame> audio_frame); | 74 void SendEncodedAudioFrame(scoped_ptr<EncodedFrame> audio_frame); |
86 | 75 |
87 const scoped_refptr<CastEnvironment> cast_environment_; | |
88 | |
89 // The total amount of time between a frame's capture/recording on the sender | 76 // 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 | 77 // 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, | 78 // a value large enough to give the system sufficient time to encode, |
92 // transmit/retransmit, receive, decode, and render; given its run-time | 79 // transmit/retransmit, receive, decode, and render; given its run-time |
93 // environment (sender/receiver hardware performance, network conditions, | 80 // environment (sender/receiver hardware performance, network conditions, |
94 // etc.). | 81 // etc.). |
95 const base::TimeDelta target_playout_delay_; | 82 const base::TimeDelta target_playout_delay_; |
96 | 83 |
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 CastTransportSender* const transport_sender_; | |
103 | |
104 // Maximum number of outstanding frames before the encoding and sending of | 84 // Maximum number of outstanding frames before the encoding and sending of |
105 // new frames shall halt. | 85 // new frames shall halt. |
106 const int max_unacked_frames_; | 86 const int max_unacked_frames_; |
107 | 87 |
108 // Encodes AudioBuses into EncodedFrames. | 88 // Encodes AudioBuses into EncodedFrames. |
109 scoped_ptr<AudioEncoder> audio_encoder_; | 89 scoped_ptr<AudioEncoder> audio_encoder_; |
110 const int configured_encoder_bitrate_; | 90 const int configured_encoder_bitrate_; |
111 | 91 |
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 | 92 // 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 | 93 // frame) at the start of the session. Once a threshold is reached, RTCP |
122 // reports are instead sent at the configured interval + random drift. | 94 // reports are instead sent at the configured interval + random drift. |
123 int num_aggressive_rtcp_reports_sent_; | 95 int num_aggressive_rtcp_reports_sent_; |
124 | 96 |
125 // This is "null" until the first frame is sent. Thereafter, this tracks the | 97 // This is "null" until the first frame is sent. Thereafter, this tracks the |
126 // last time any frame was sent or re-sent. | 98 // last time any frame was sent or re-sent. |
127 base::TimeTicks last_send_time_; | 99 base::TimeTicks last_send_time_; |
128 | 100 |
129 // The ID of the last frame sent. Logic throughout AudioSender assumes this | 101 // The ID of the last frame sent. Logic throughout AudioSender assumes this |
(...skipping 23 matching lines...) Expand all Loading... |
153 // NOTE: Weak pointers must be invalidated before all other member variables. | 125 // NOTE: Weak pointers must be invalidated before all other member variables. |
154 base::WeakPtrFactory<AudioSender> weak_factory_; | 126 base::WeakPtrFactory<AudioSender> weak_factory_; |
155 | 127 |
156 DISALLOW_COPY_AND_ASSIGN(AudioSender); | 128 DISALLOW_COPY_AND_ASSIGN(AudioSender); |
157 }; | 129 }; |
158 | 130 |
159 } // namespace cast | 131 } // namespace cast |
160 } // namespace media | 132 } // namespace media |
161 | 133 |
162 #endif // MEDIA_CAST_SENDER_AUDIO_SENDER_H_ | 134 #endif // MEDIA_CAST_SENDER_AUDIO_SENDER_H_ |
OLD | NEW |