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