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" |
(...skipping 23 matching lines...) Expand all Loading... |
34 // VideoReceiver receives packets out-of-order while clients make requests for | 34 // VideoReceiver receives packets out-of-order while clients make requests for |
35 // complete frames in-order. (A frame consists of one or more packets.) | 35 // complete frames in-order. (A frame consists of one or more packets.) |
36 // | 36 // |
37 // VideoReceiver also includes logic for computing the playout time for each | 37 // VideoReceiver also includes logic for computing the playout time for each |
38 // frame, accounting for a constant targeted playout delay. The purpose of the | 38 // 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 | 39 // 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 | 40 // 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 | 41 // 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 | 42 // 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. | 43 // 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 // | 44 // |
49 // Two types of frames can be requested: 1) A frame of decoded video data; or 2) | 45 // 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 | 46 // 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 | 47 // decoder. Each request for a frame includes a callback which VideoReceiver |
52 // guarantees will be called at some point in the future unless the | 48 // guarantees will be called at some point in the future unless the |
53 // VideoReceiver is destroyed. Clients should generally limit the number of | 49 // VideoReceiver is destroyed. Clients should generally limit the number of |
54 // outstanding requests (perhaps to just one or two). | 50 // outstanding requests (perhaps to just one or two). |
55 // | 51 // |
56 // This class is not thread safe. Should only be called from the Main cast | 52 // This class is not thread safe. Should only be called from the Main cast |
57 // thread. | 53 // thread. |
(...skipping 17 matching lines...) Expand all Loading... |
75 // Request an encoded video frame. | 71 // Request an encoded video frame. |
76 // | 72 // |
77 // The given |callback| is guaranteed to be run at some point in the future, | 73 // The given |callback| is guaranteed to be run at some point in the future, |
78 // even if to respond with NULL at shutdown time. | 74 // even if to respond with NULL at shutdown time. |
79 void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback); | 75 void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback); |
80 | 76 |
81 // Deliver another packet, possibly a duplicate, and possibly out-of-order. | 77 // Deliver another packet, possibly a duplicate, and possibly out-of-order. |
82 void IncomingPacket(scoped_ptr<Packet> packet); | 78 void IncomingPacket(scoped_ptr<Packet> packet); |
83 | 79 |
84 protected: | 80 protected: |
85 friend class VideoReceiverTest; // Invoked OnReceivedPayloadData(). | 81 friend class VideoReceiverTest; // Invokes OnReceivedPayloadData(). |
86 | 82 |
87 virtual void OnReceivedPayloadData(const uint8* payload_data, | 83 virtual void OnReceivedPayloadData(const uint8* payload_data, |
88 size_t payload_size, | 84 size_t payload_size, |
89 const RtpCastHeader& rtp_header) OVERRIDE; | 85 const RtpCastHeader& rtp_header) OVERRIDE; |
90 | 86 |
91 // RtpPayloadFeedback implementation. | 87 // RtpPayloadFeedback implementation. |
92 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; | 88 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; |
93 | 89 |
94 private: | 90 private: |
95 // Processes ready-to-consume packets from |framer_|, decrypting each packet's | 91 // Processes ready-to-consume packets from |framer_|, decrypting each packet's |
96 // payload data, and then running the enqueued callbacks in order (one for | 92 // 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 | 93 // each packet). This method may post a delayed task to re-invoke itself in |
98 // the future to wait for missing/incomplete frames. | 94 // the future to wait for missing/incomplete frames. |
99 void EmitAvailableEncodedFrames(); | 95 void EmitAvailableEncodedFrames(); |
100 | 96 |
| 97 // Helper used by EmitAvailableEncodedFrames() to schedule itself to be called |
| 98 // again after |wait_time| has elapsed. |
| 99 void RetryEmitAfterWaiting(base::TimeDelta wait_time); |
| 100 |
101 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes | 101 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes |
102 // EmitAvailableEncodedFrames(). | 102 // EmitAvailableEncodedFrames(). |
103 void EmitAvailableEncodedFramesAfterWaiting(); | 103 void EmitAvailableEncodedFramesAfterWaiting(); |
104 | 104 |
105 // Feeds an EncodedVideoFrame into |video_decoder_|. GetRawVideoFrame() uses | 105 // Feeds an EncodedVideoFrame into |video_decoder_|. GetRawVideoFrame() uses |
106 // this as a callback for GetEncodedVideoFrame(). | 106 // this as a callback for GetEncodedVideoFrame(). |
107 void DecodeEncodedVideoFrame( | 107 void DecodeEncodedVideoFrame( |
108 const VideoFrameDecodedCallback& callback, | 108 const VideoFrameDecodedCallback& callback, |
109 scoped_ptr<transport::EncodedVideoFrame> encoded_frame, | 109 scoped_ptr<transport::EncodedVideoFrame> encoded_frame, |
110 const base::TimeTicks& playout_time); | 110 const base::TimeTicks& playout_time); |
111 | 111 |
112 // Return the playout time based on the current time and rtp timestamp. | 112 // Computes the playout time for a frame with the given |rtp_timestamp|. If |
113 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); | 113 // lip-sync info is not available, a best-guess is returned (a hack). |
114 | 114 base::TimeTicks GetPlayoutTime(uint32 rtp_timestamp) const; |
115 void InitializeTimers(); | |
116 | 115 |
117 // Schedule timing for the next cast message. | 116 // Schedule timing for the next cast message. |
118 void ScheduleNextCastMessage(); | 117 void ScheduleNextCastMessage(); |
119 | 118 |
120 // Schedule timing for the next RTCP report. | 119 // Schedule timing for the next RTCP report. |
121 void ScheduleNextRtcpReport(); | 120 void ScheduleNextRtcpReport(); |
122 | 121 |
123 // Actually send the next cast message. | 122 // Actually send the next cast message. |
124 void SendNextCastMessage(); | 123 void SendNextCastMessage(); |
125 | 124 |
(...skipping 14 matching lines...) Expand all Loading... |
140 const scoped_refptr<VideoFrame>& video_frame, | 139 const scoped_refptr<VideoFrame>& video_frame, |
141 bool is_continuous); | 140 bool is_continuous); |
142 | 141 |
143 const scoped_refptr<CastEnvironment> cast_environment_; | 142 const scoped_refptr<CastEnvironment> cast_environment_; |
144 | 143 |
145 // Subscribes to raw events. | 144 // Subscribes to raw events. |
146 // Processes raw audio events to be sent over to the cast sender via RTCP. | 145 // Processes raw audio events to be sent over to the cast sender via RTCP. |
147 ReceiverRtcpEventSubscriber event_subscriber_; | 146 ReceiverRtcpEventSubscriber event_subscriber_; |
148 | 147 |
149 const transport::VideoCodec codec_; | 148 const transport::VideoCodec codec_; |
150 const base::TimeDelta target_delay_delta_; | 149 const base::TimeDelta target_playout_delay_; |
151 const base::TimeDelta expected_frame_duration_; | 150 const base::TimeDelta expected_frame_duration_; |
| 151 bool reports_are_scheduled_; |
152 Framer framer_; | 152 Framer framer_; |
153 scoped_ptr<VideoDecoder> video_decoder_; | 153 scoped_ptr<VideoDecoder> video_decoder_; |
154 Rtcp rtcp_; | 154 Rtcp rtcp_; |
155 base::TimeDelta time_offset_; // Sender-receiver offset estimation. | |
156 int time_offset_counter_; | |
157 bool time_incoming_packet_updated_; | |
158 base::TimeTicks time_incoming_packet_; | |
159 uint32 incoming_rtp_timestamp_; | |
160 transport::TransportEncryptionHandler decryptor_; | 155 transport::TransportEncryptionHandler decryptor_; |
161 | 156 |
162 // Outstanding callbacks to run to deliver on client requests for frames. | 157 // Outstanding callbacks to run to deliver on client requests for frames. |
163 std::list<VideoFrameEncodedCallback> frame_request_queue_; | 158 std::list<VideoFrameEncodedCallback> frame_request_queue_; |
164 | 159 |
165 // True while there's an outstanding task to re-invoke | 160 // True while there's an outstanding task to re-invoke |
166 // EmitAvailableEncodedFrames(). | 161 // EmitAvailableEncodedFrames(). |
167 bool is_waiting_for_consecutive_frame_; | 162 bool is_waiting_to_emit_frames_; |
168 | 163 |
169 // This mapping allows us to log FRAME_ACK_SENT as a frame event. In addition | 164 // This mapping allows us to log FRAME_ACK_SENT as a frame event. In addition |
170 // it allows the event to be transmitted via RTCP. | 165 // it allows the event to be transmitted via RTCP. |
171 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 166 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
172 | 167 |
173 // NOTE: Weak pointers must be invalidated before all other member variables. | 168 // NOTE: Weak pointers must be invalidated before all other member variables. |
174 base::WeakPtrFactory<VideoReceiver> weak_factory_; | 169 base::WeakPtrFactory<VideoReceiver> weak_factory_; |
175 | 170 |
176 DISALLOW_COPY_AND_ASSIGN(VideoReceiver); | 171 DISALLOW_COPY_AND_ASSIGN(VideoReceiver); |
177 }; | 172 }; |
178 | 173 |
179 } // namespace cast | 174 } // namespace cast |
180 } // namespace media | 175 } // namespace media |
181 | 176 |
182 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | 177 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ |
OLD | NEW |