| 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/cast_config.h" | 16 #include "media/cast/cast_config.h" |
| 17 #include "media/cast/cast_environment.h" | 17 #include "media/cast/cast_environment.h" |
| 18 #include "media/cast/cast_receiver.h" | 18 #include "media/cast/cast_receiver.h" |
| 19 #include "media/cast/framer/framer.h" |
| 19 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" | 20 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" |
| 20 #include "media/cast/rtcp/rtcp.h" | 21 #include "media/cast/rtcp/rtcp.h" |
| 21 #include "media/cast/rtp_receiver/rtp_receiver.h" | 22 #include "media/cast/rtp_receiver/rtp_receiver.h" |
| 22 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" | 23 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" |
| 23 #include "media/cast/transport/utility/transport_encryption_handler.h" | 24 #include "media/cast/transport/utility/transport_encryption_handler.h" |
| 24 | 25 |
| 25 namespace media { | 26 namespace media { |
| 27 |
| 28 class VideoFrame; |
| 29 |
| 26 namespace cast { | 30 namespace cast { |
| 27 | 31 |
| 28 class Framer; | |
| 29 class LocalRtpVideoFeedback; | |
| 30 class PeerVideoReceiver; | |
| 31 class Rtcp; | |
| 32 class RtpReceiverStatistics; | |
| 33 class VideoDecoder; | 32 class VideoDecoder; |
| 34 | 33 |
| 35 // Callback used by the video receiver to inform the audio receiver of the new | 34 // VideoReceiver receives packets out-of-order while clients make requests for |
| 36 // delay used to compute the playout and render times. | 35 // complete frames in-order. (A frame consists of one or more packets.) |
| 37 typedef base::Callback<void(base::TimeDelta)> SetTargetDelayCallback; | 36 // |
| 38 | 37 // VideoReceiver also includes logic for computing the playout time for each |
| 39 // Should only be called from the Main cast thread. | 38 // frame, accounting for a constant targeted playout delay. The purpose of the |
| 40 class VideoReceiver : public base::NonThreadSafe, | 39 // playout delay is to provide a fixed window of time between the capture event |
| 41 public base::SupportsWeakPtr<VideoReceiver>, | 40 // on the sender and the playout on the receiver. This is important because |
| 42 public RtpReceiver { | 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 |
| 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 // |
| 49 // 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 |
| 51 // decoder. Each request for a frame includes a callback which VideoReceiver |
| 52 // guarantees will be called at some point in the future. Clients should |
| 53 // generally limit the number of outstanding requests (perhaps to just one or |
| 54 // two). When VideoReceiver is destroyed, any outstanding requests will be |
| 55 // immediately invoked with a NULL frame. |
| 56 // |
| 57 // This class is not thread safe. Should only be called from the Main cast |
| 58 // thread. |
| 59 class VideoReceiver : public RtpReceiver, |
| 60 public RtpPayloadFeedback, |
| 61 public base::NonThreadSafe, |
| 62 public base::SupportsWeakPtr<VideoReceiver> { |
| 43 public: | 63 public: |
| 44 VideoReceiver(scoped_refptr<CastEnvironment> cast_environment, | 64 VideoReceiver(scoped_refptr<CastEnvironment> cast_environment, |
| 45 const VideoReceiverConfig& video_config, | 65 const VideoReceiverConfig& video_config, |
| 46 transport::PacedPacketSender* const packet_sender, | 66 transport::PacedPacketSender* const packet_sender); |
| 47 const SetTargetDelayCallback& target_delay_cb); | |
| 48 | 67 |
| 49 virtual ~VideoReceiver(); | 68 virtual ~VideoReceiver(); |
| 50 | 69 |
| 51 // Request a raw frame. Will return frame via callback when available. | 70 // Request a decoded video frame. |
| 71 // |
| 72 // The given |callback| is guaranteed to be run at some point in the future, |
| 73 // even if to respond with NULL at shutdown time. |
| 52 void GetRawVideoFrame(const VideoFrameDecodedCallback& callback); | 74 void GetRawVideoFrame(const VideoFrameDecodedCallback& callback); |
| 53 | 75 |
| 54 // Request an encoded frame. Will return frame via callback when available. | 76 // Request an encoded video frame. |
| 77 // |
| 78 // The given |callback| is guaranteed to be run at some point in the future, |
| 79 // even if to respond with NULL at shutdown time. |
| 55 void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback); | 80 void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback); |
| 56 | 81 |
| 57 // Insert a RTP packet to the video receiver. | 82 // Deliver another packet, possibly a duplicate, and possibly out-of-order. |
| 58 void IncomingPacket(scoped_ptr<Packet> packet); | 83 void IncomingPacket(scoped_ptr<Packet> packet); |
| 59 | 84 |
| 85 protected: |
| 86 friend class VideoReceiverTest; // Invoked OnReceivedPayloadData(). |
| 87 |
| 60 virtual void OnReceivedPayloadData(const uint8* payload_data, | 88 virtual void OnReceivedPayloadData(const uint8* payload_data, |
| 61 size_t payload_size, | 89 size_t payload_size, |
| 62 const RtpCastHeader& rtp_header) OVERRIDE; | 90 const RtpCastHeader& rtp_header) OVERRIDE; |
| 63 | 91 |
| 64 protected: | 92 // RtpPayloadFeedback implementation. |
| 65 void DecodeVideoFrameThread( | 93 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; |
| 66 scoped_ptr<transport::EncodedVideoFrame> encoded_frame, | |
| 67 const base::TimeTicks render_time, | |
| 68 const VideoFrameDecodedCallback& frame_decoded_callback); | |
| 69 | 94 |
| 70 private: | 95 private: |
| 71 friend class LocalRtpVideoFeedback; | 96 // Processes ready-to-consume packets from |framer_|, decrypting each packet's |
| 97 // payload data, and then running the enqueued callbacks in order (one for |
| 98 // each packet). This method may post a delayed task to re-invoke itself in |
| 99 // the future to wait for missing/incomplete frames. |
| 100 void EmitAvailableEncodedFrames(); |
| 72 | 101 |
| 73 void CastFeedback(const RtcpCastMessage& cast_message); | 102 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes |
| 103 // EmitAvailableEncodedFrames(). |
| 104 void EmitAvailableEncodedFramesAfterWaiting(); |
| 74 | 105 |
| 75 void DecodeVideoFrame(const VideoFrameDecodedCallback& callback, | 106 // Feeds an EncodedVideoFrame into |video_decoder_|. GetRawVideoFrame() uses |
| 76 scoped_ptr<transport::EncodedVideoFrame> encoded_frame, | 107 // this as a callback for GetEncodedVideoFrame(). |
| 77 const base::TimeTicks& render_time); | 108 void DecodeEncodedVideoFrame( |
| 109 const VideoFrameDecodedCallback& callback, |
| 110 scoped_ptr<transport::EncodedVideoFrame> encoded_frame, |
| 111 const base::TimeTicks& playout_time); |
| 78 | 112 |
| 79 bool DecryptVideoFrame(scoped_ptr<transport::EncodedVideoFrame>* video_frame); | 113 // Return the playout time based on the current time and rtp timestamp. |
| 80 | 114 base::TimeTicks GetPlayoutTime(base::TimeTicks now, uint32 rtp_timestamp); |
| 81 bool PullEncodedVideoFrame( | |
| 82 bool next_frame, | |
| 83 scoped_ptr<transport::EncodedVideoFrame>* encoded_frame, | |
| 84 base::TimeTicks* render_time); | |
| 85 | |
| 86 void PlayoutTimeout(); | |
| 87 | |
| 88 // Returns Render time based on current time and the rtp timestamp. | |
| 89 base::TimeTicks GetRenderTime(base::TimeTicks now, uint32 rtp_timestamp); | |
| 90 | 115 |
| 91 void InitializeTimers(); | 116 void InitializeTimers(); |
| 92 | 117 |
| 93 // Schedule timing for the next cast message. | 118 // Schedule timing for the next cast message. |
| 94 void ScheduleNextCastMessage(); | 119 void ScheduleNextCastMessage(); |
| 95 | 120 |
| 96 // Schedule timing for the next RTCP report. | 121 // Schedule timing for the next RTCP report. |
| 97 void ScheduleNextRtcpReport(); | 122 void ScheduleNextRtcpReport(); |
| 98 | 123 |
| 99 // Actually send the next cast message. | 124 // Actually send the next cast message. |
| 100 void SendNextCastMessage(); | 125 void SendNextCastMessage(); |
| 101 | 126 |
| 102 // Actually send the next RTCP report. | 127 // Actually send the next RTCP report. |
| 103 void SendNextRtcpReport(); | 128 void SendNextRtcpReport(); |
| 104 | 129 |
| 105 // Update the target delay based on past information. Will also update the | 130 // Receives a VideoFrame from |video_decoder_|, logs the event, and passes the |
| 106 // rtcp module and the audio receiver. | 131 // data on by running the given |callback|. This method is static to ensure |
| 107 void UpdateTargetDelay(); | 132 // it can be called after a VideoReceiver instance is destroyed. |
| 133 // DecodeEncodedVideoFrame() uses this as a callback for |
| 134 // VideoDecoder::DecodeFrame(). |
| 135 static void EmitRawVideoFrame( |
| 136 const scoped_refptr<CastEnvironment>& cast_environment, |
| 137 const VideoFrameDecodedCallback& callback, |
| 138 uint32 frame_id, |
| 139 uint32 rtp_timestamp, |
| 140 const base::TimeTicks& playout_time, |
| 141 const scoped_refptr<VideoFrame>& video_frame, |
| 142 bool is_continuous); |
| 108 | 143 |
| 109 scoped_ptr<VideoDecoder> video_decoder_; | 144 const scoped_refptr<CastEnvironment> cast_environment_; |
| 110 scoped_refptr<CastEnvironment> cast_environment_; | |
| 111 | 145 |
| 112 // Subscribes to raw events. | 146 // Subscribes to raw events. |
| 113 // Processes raw audio events to be sent over to the cast sender via RTCP. | 147 // Processes raw audio events to be sent over to the cast sender via RTCP. |
| 114 ReceiverRtcpEventSubscriber event_subscriber_; | 148 ReceiverRtcpEventSubscriber event_subscriber_; |
| 115 | 149 |
| 116 scoped_ptr<Framer> framer_; | |
| 117 const transport::VideoCodec codec_; | 150 const transport::VideoCodec codec_; |
| 118 base::TimeDelta target_delay_delta_; | 151 const base::TimeDelta target_delay_delta_; |
| 119 base::TimeDelta frame_delay_; | 152 const base::TimeDelta expected_frame_duration_; |
| 120 scoped_ptr<LocalRtpVideoFeedback> incoming_payload_feedback_; | 153 Framer framer_; |
| 121 scoped_ptr<Rtcp> rtcp_; | 154 scoped_ptr<VideoDecoder> video_decoder_; |
| 155 Rtcp rtcp_; |
| 122 base::TimeDelta time_offset_; // Sender-receiver offset estimation. | 156 base::TimeDelta time_offset_; // Sender-receiver offset estimation. |
| 123 int time_offset_counter_; | 157 int time_offset_counter_; |
| 124 transport::TransportEncryptionHandler decryptor_; | |
| 125 std::list<VideoFrameEncodedCallback> queued_encoded_callbacks_; | |
| 126 bool time_incoming_packet_updated_; | 158 bool time_incoming_packet_updated_; |
| 127 base::TimeTicks time_incoming_packet_; | 159 base::TimeTicks time_incoming_packet_; |
| 128 uint32 incoming_rtp_timestamp_; | 160 uint32 incoming_rtp_timestamp_; |
| 129 base::TimeTicks last_render_time_; | 161 transport::TransportEncryptionHandler decryptor_; |
| 130 SetTargetDelayCallback target_delay_cb_; | 162 |
| 163 // Outstanding callbacks to run to deliver on client requests for frames. |
| 164 std::list<VideoFrameEncodedCallback> frame_request_queue_; |
| 165 |
| 166 // True while there's an outstanding task to re-invoke |
| 167 // EmitAvailableEncodedFrames(). |
| 168 bool is_waiting_for_consecutive_frame_; |
| 131 | 169 |
| 132 // This mapping allows us to log kVideoAckSent as a frame event. In addition | 170 // This mapping allows us to log kVideoAckSent as a frame event. In addition |
| 133 // it allows the event to be transmitted via RTCP. | 171 // it allows the event to be transmitted via RTCP. |
| 134 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 172 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
| 135 | 173 |
| 136 // NOTE: Weak pointers must be invalidated before all other member variables. | 174 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 137 base::WeakPtrFactory<VideoReceiver> weak_factory_; | 175 base::WeakPtrFactory<VideoReceiver> weak_factory_; |
| 138 | 176 |
| 139 DISALLOW_COPY_AND_ASSIGN(VideoReceiver); | 177 DISALLOW_COPY_AND_ASSIGN(VideoReceiver); |
| 140 }; | 178 }; |
| 141 | 179 |
| 142 } // namespace cast | 180 } // namespace cast |
| 143 } // namespace media | 181 } // namespace media |
| 144 | 182 |
| 145 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | 183 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ |
| OLD | NEW |