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_RECEIVER_AUDIO_RECEIVER_H_ | 5 #ifndef MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | 6 #define MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
15 #include "base/time/tick_clock.h" | 15 #include "base/time/tick_clock.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "media/cast/base/clock_drift_smoother.h" |
17 #include "media/cast/cast_config.h" | 18 #include "media/cast/cast_config.h" |
18 #include "media/cast/cast_environment.h" | 19 #include "media/cast/cast_environment.h" |
19 #include "media/cast/cast_receiver.h" | 20 #include "media/cast/cast_receiver.h" |
20 #include "media/cast/framer/framer.h" | 21 #include "media/cast/framer/framer.h" |
21 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" | 22 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" |
22 #include "media/cast/rtcp/rtcp.h" | 23 #include "media/cast/rtcp/rtcp.h" |
23 #include "media/cast/rtp_receiver/rtp_receiver.h" | 24 #include "media/cast/rtp_receiver/rtp_receiver.h" |
24 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" | 25 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" |
25 #include "media/cast/transport/utility/transport_encryption_handler.h" | 26 #include "media/cast/transport/utility/transport_encryption_handler.h" |
26 | 27 |
27 namespace media { | 28 namespace media { |
28 namespace cast { | 29 namespace cast { |
29 | 30 |
30 class AudioDecoder; | 31 class AudioDecoder; |
31 | 32 |
32 // AudioReceiver receives packets out-of-order while clients make requests for | 33 // AudioReceiver receives packets out-of-order while clients make requests for |
33 // complete frames in-order. (A frame consists of one or more packets.) | 34 // complete frames in-order. (A frame consists of one or more packets.) |
34 // | 35 // |
35 // AudioReceiver also includes logic for computing the playout time for each | 36 // AudioReceiver also includes logic for computing the playout time for each |
36 // frame, accounting for a constant targeted playout delay. The purpose of the | 37 // frame, accounting for a constant targeted playout delay. The purpose of the |
37 // playout delay is to provide a fixed window of time between the capture event | 38 // playout delay is to provide a fixed window of time between the capture event |
38 // on the sender and the playout on the receiver. This is important because | 39 // on the sender and the playout on the receiver. This is important because |
39 // each step of the pipeline (i.e., encode frame, then transmit/retransmit from | 40 // each step of the pipeline (i.e., encode frame, then transmit/retransmit from |
40 // the sender, then receive and re-order packets on the receiver, then decode | 41 // the sender, then receive and re-order packets on the receiver, then decode |
41 // frame) can vary in duration and is typically very hard to predict. | 42 // frame) can vary in duration and is typically very hard to predict. |
42 // Heuristics will determine when the targeted playout delay is insufficient in | |
43 // the current environment; and the receiver can then increase the playout | |
44 // delay, notifying the sender, to account for the extra variance. | |
45 // TODO(miu): Make the last sentence true. http://crbug.com/360111 | |
46 // | 43 // |
47 // Two types of frames can be requested: 1) A frame of decoded audio data; or 2) | 44 // Two types of frames can be requested: 1) A frame of decoded audio data; or 2) |
48 // a frame of still-encoded audio data, to be passed into an external audio | 45 // a frame of still-encoded audio data, to be passed into an external audio |
49 // decoder. Each request for a frame includes a callback which AudioReceiver | 46 // decoder. Each request for a frame includes a callback which AudioReceiver |
50 // guarantees will be called at some point in the future unless the | 47 // guarantees will be called at some point in the future unless the |
51 // AudioReceiver is destroyed. Clients should generally limit the number of | 48 // AudioReceiver is destroyed. Clients should generally limit the number of |
52 // outstanding requests (perhaps to just one or two). | 49 // outstanding requests (perhaps to just one or two). |
53 // | 50 // |
54 // This class is not thread safe. Should only be called from the Main cast | 51 // This class is not thread safe. Should only be called from the Main cast |
55 // thread. | 52 // thread. |
(...skipping 18 matching lines...) Expand all Loading... |
74 | 71 |
75 // Request an encoded audio frame. | 72 // Request an encoded audio frame. |
76 // | 73 // |
77 // The given |callback| is guaranteed to be run at some point in the future, | 74 // The given |callback| is guaranteed to be run at some point in the future, |
78 // even if to respond with NULL at shutdown time. | 75 // even if to respond with NULL at shutdown time. |
79 void GetEncodedAudioFrame(const FrameEncodedCallback& callback); | 76 void GetEncodedAudioFrame(const FrameEncodedCallback& callback); |
80 | 77 |
81 // Deliver another packet, possibly a duplicate, and possibly out-of-order. | 78 // Deliver another packet, possibly a duplicate, and possibly out-of-order. |
82 void IncomingPacket(scoped_ptr<Packet> packet); | 79 void IncomingPacket(scoped_ptr<Packet> packet); |
83 | 80 |
84 // Update target audio delay used to compute the playout time. Rtcp | |
85 // will also be updated (will be included in all outgoing reports). | |
86 void SetTargetDelay(base::TimeDelta target_delay); | |
87 | |
88 protected: | 81 protected: |
89 friend class AudioReceiverTest; // Invokes OnReceivedPayloadData(). | 82 friend class AudioReceiverTest; // Invokes OnReceivedPayloadData(). |
90 | 83 |
91 virtual void OnReceivedPayloadData(const uint8* payload_data, | 84 virtual void OnReceivedPayloadData(const uint8* payload_data, |
92 size_t payload_size, | 85 size_t payload_size, |
93 const RtpCastHeader& rtp_header) OVERRIDE; | 86 const RtpCastHeader& rtp_header) OVERRIDE; |
94 | 87 |
95 // RtpPayloadFeedback implementation. | 88 // RtpPayloadFeedback implementation. |
96 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; | 89 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; |
97 | 90 |
98 private: | 91 private: |
99 // Processes ready-to-consume packets from |framer_|, decrypting each packet's | 92 // Processes ready-to-consume packets from |framer_|, decrypting each packet's |
100 // payload data, and then running the enqueued callbacks in order (one for | 93 // payload data, and then running the enqueued callbacks in order (one for |
101 // each packet). This method may post a delayed task to re-invoke itself in | 94 // each packet). This method may post a delayed task to re-invoke itself in |
102 // the future to wait for missing/incomplete frames. | 95 // the future to wait for missing/incomplete frames. |
103 void EmitAvailableEncodedFrames(); | 96 void EmitAvailableEncodedFrames(); |
104 | 97 |
105 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes | 98 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes |
106 // EmitAvailableEncodedFrames(). | 99 // EmitAvailableEncodedFrames(). |
107 void EmitAvailableEncodedFramesAfterWaiting(); | 100 void EmitAvailableEncodedFramesAfterWaiting(); |
108 | 101 |
109 // Feeds an EncodedFrame into |audio_decoder_|. GetRawAudioFrame() uses this | 102 // Feeds an EncodedFrame into |audio_decoder_|. GetRawAudioFrame() uses this |
110 // as a callback for GetEncodedAudioFrame(). | 103 // as a callback for GetEncodedAudioFrame(). |
111 void DecodeEncodedAudioFrame( | 104 void DecodeEncodedAudioFrame( |
112 const AudioFrameDecodedCallback& callback, | 105 const AudioFrameDecodedCallback& callback, |
113 scoped_ptr<transport::EncodedFrame> encoded_frame); | 106 scoped_ptr<transport::EncodedFrame> encoded_frame); |
114 | 107 |
115 // Return the playout time based on the current time and rtp timestamp. | 108 // Computes the playout time for a frame with the given |rtp_timestamp|. |
116 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); | 109 // Because lip-sync info is refreshed regularly, calling this method with the |
117 | 110 // same argument may return different results. |
118 void InitializeTimers(); | 111 base::TimeTicks GetPlayoutTime(uint32 rtp_timestamp) const; |
119 | 112 |
120 // Schedule the next RTCP report. | 113 // Schedule the next RTCP report. |
121 void ScheduleNextRtcpReport(); | 114 void ScheduleNextRtcpReport(); |
122 | 115 |
123 // Actually send the next RTCP report. | 116 // Actually send the next RTCP report. |
124 void SendNextRtcpReport(); | 117 void SendNextRtcpReport(); |
125 | 118 |
126 // Schedule timing for the next cast message. | 119 // Schedule timing for the next cast message. |
127 void ScheduleNextCastMessage(); | 120 void ScheduleNextCastMessage(); |
128 | 121 |
(...skipping 13 matching lines...) Expand all Loading... |
142 const base::TimeTicks& playout_time, | 135 const base::TimeTicks& playout_time, |
143 scoped_ptr<AudioBus> audio_bus, | 136 scoped_ptr<AudioBus> audio_bus, |
144 bool is_continuous); | 137 bool is_continuous); |
145 | 138 |
146 const scoped_refptr<CastEnvironment> cast_environment_; | 139 const scoped_refptr<CastEnvironment> cast_environment_; |
147 | 140 |
148 // Subscribes to raw events. | 141 // Subscribes to raw events. |
149 // Processes raw audio events to be sent over to the cast sender via RTCP. | 142 // Processes raw audio events to be sent over to the cast sender via RTCP. |
150 ReceiverRtcpEventSubscriber event_subscriber_; | 143 ReceiverRtcpEventSubscriber event_subscriber_; |
151 | 144 |
| 145 // Configured audio codec. |
152 const transport::AudioCodec codec_; | 146 const transport::AudioCodec codec_; |
| 147 |
| 148 // RTP timebase: The number of RTP units advanced per one second. For audio, |
| 149 // this is the sampling rate. |
153 const int frequency_; | 150 const int frequency_; |
154 base::TimeDelta target_delay_delta_; | 151 |
| 152 // The total amount of time between a frame's capture/recording on the sender |
| 153 // and its playback on the receiver (i.e., shown to a user). This is fixed as |
| 154 // a value large enough to give the system sufficient time to encode, |
| 155 // transmit/retransmit, receive, decode, and render; given its run-time |
| 156 // environment (sender/receiver hardware performance, network conditions, |
| 157 // etc.). |
| 158 const base::TimeDelta target_playout_delay_; |
| 159 |
| 160 // Set to false initially, then set to true after scheduling the periodic |
| 161 // sending of reports back to the sender. Reports are first scheduled just |
| 162 // after receiving a first packet (since the first packet identifies the |
| 163 // sender for the remainder of the session). |
| 164 bool reports_are_scheduled_; |
| 165 |
| 166 // Assembles packets into frames, providing this receiver with complete, |
| 167 // decodable EncodedFrames. |
155 Framer framer_; | 168 Framer framer_; |
| 169 |
| 170 // Decodes frames into raw audio for playback. |
156 scoped_ptr<AudioDecoder> audio_decoder_; | 171 scoped_ptr<AudioDecoder> audio_decoder_; |
| 172 |
| 173 // Manages sending/receiving of RTCP packets, including sender/receiver |
| 174 // reports. |
157 Rtcp rtcp_; | 175 Rtcp rtcp_; |
158 base::TimeDelta time_offset_; | 176 |
159 base::TimeTicks time_first_incoming_packet_; | 177 // Decrypts encrypted frames. |
160 uint32 first_incoming_rtp_timestamp_; | |
161 transport::TransportEncryptionHandler decryptor_; | 178 transport::TransportEncryptionHandler decryptor_; |
162 | 179 |
163 // Outstanding callbacks to run to deliver on client requests for frames. | 180 // Outstanding callbacks to run to deliver on client requests for frames. |
164 std::list<FrameEncodedCallback> frame_request_queue_; | 181 std::list<FrameEncodedCallback> frame_request_queue_; |
165 | 182 |
166 // True while there's an outstanding task to re-invoke | 183 // True while there's an outstanding task to re-invoke |
167 // EmitAvailableEncodedFrames(). | 184 // EmitAvailableEncodedFrames(). |
168 bool is_waiting_for_consecutive_frame_; | 185 bool is_waiting_for_consecutive_frame_; |
169 | 186 |
170 // This mapping allows us to log AUDIO_ACK_SENT as a frame event. In addition | 187 // This mapping allows us to log AUDIO_ACK_SENT as a frame event. In addition |
171 // it allows the event to be transmitted via RTCP. | 188 // it allows the event to be transmitted via RTCP. |
172 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 189 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
173 | 190 |
| 191 // Lip-sync values used to compute the playout time of each frame from its RTP |
| 192 // timestamp. These are updated each time the first packet of a frame is |
| 193 // received. |
| 194 RtpTimestamp lip_sync_rtp_timestamp_; |
| 195 base::TimeTicks lip_sync_reference_time_; |
| 196 ClockDriftSmoother lip_sync_drift_; |
| 197 |
174 // NOTE: Weak pointers must be invalidated before all other member variables. | 198 // NOTE: Weak pointers must be invalidated before all other member variables. |
175 base::WeakPtrFactory<AudioReceiver> weak_factory_; | 199 base::WeakPtrFactory<AudioReceiver> weak_factory_; |
176 | 200 |
177 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); | 201 DISALLOW_COPY_AND_ASSIGN(AudioReceiver); |
178 }; | 202 }; |
179 | 203 |
180 } // namespace cast | 204 } // namespace cast |
181 } // namespace media | 205 } // namespace media |
182 | 206 |
183 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ | 207 #endif // MEDIA_CAST_AUDIO_RECEIVER_AUDIO_RECEIVER_H_ |
OLD | NEW |