OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | |
6 #define MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/callback.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/threading/non_thread_safe.h" | |
14 #include "base/time/tick_clock.h" | |
15 #include "base/time/time.h" | |
16 #include "media/cast/base/clock_drift_smoother.h" | |
17 #include "media/cast/cast_config.h" | |
18 #include "media/cast/cast_environment.h" | |
19 #include "media/cast/cast_receiver.h" | |
20 #include "media/cast/framer/framer.h" | |
21 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" | |
22 #include "media/cast/rtcp/rtcp.h" | |
23 #include "media/cast/rtp_receiver/rtp_receiver.h" | |
24 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" | |
25 #include "media/cast/transport/utility/transport_encryption_handler.h" | |
26 | |
27 namespace media { | |
28 | |
29 class VideoFrame; | |
30 | |
31 namespace cast { | |
32 | |
33 class VideoDecoder; | |
34 | |
35 // VideoReceiver receives packets out-of-order while clients make requests for | |
36 // complete frames in-order. (A frame consists of one or more packets.) | |
37 // | |
38 // VideoReceiver also includes logic for computing the playout time for each | |
39 // frame, accounting for a constant targeted playout delay. The purpose of the | |
40 // playout delay is to provide a fixed window of time between the capture event | |
41 // on the sender and the playout on the receiver. This is important because | |
42 // each step of the pipeline (i.e., encode frame, then transmit/retransmit from | |
43 // the sender, then receive and re-order packets on the receiver, then decode | |
44 // frame) can vary in duration and is typically very hard to predict. | |
45 // | |
46 // Two types of frames can be requested: 1) A frame of decoded video data; or 2) | |
47 // a frame of still-encoded video data, to be passed into an external video | |
48 // decoder. Each request for a frame includes a callback which VideoReceiver | |
49 // guarantees will be called at some point in the future unless the | |
50 // VideoReceiver is destroyed. Clients should generally limit the number of | |
51 // outstanding requests (perhaps to just one or two). | |
52 // | |
53 // This class is not thread safe. Should only be called from the Main cast | |
54 // thread. | |
55 class VideoReceiver : public RtpReceiver, | |
56 public RtpPayloadFeedback, | |
57 public base::NonThreadSafe, | |
58 public base::SupportsWeakPtr<VideoReceiver> { | |
59 public: | |
60 VideoReceiver(scoped_refptr<CastEnvironment> cast_environment, | |
61 const FrameReceiverConfig& video_config, | |
62 transport::PacedPacketSender* const packet_sender); | |
63 | |
64 virtual ~VideoReceiver(); | |
65 | |
66 // Request a decoded video frame. | |
67 // | |
68 // The given |callback| is guaranteed to be run at some point in the future, | |
69 // even if to respond with NULL at shutdown time. | |
70 void GetRawVideoFrame(const VideoFrameDecodedCallback& callback); | |
71 | |
72 // Request an encoded video frame. | |
73 // | |
74 // The given |callback| is guaranteed to be run at some point in the future, | |
75 // even if to respond with NULL at shutdown time. | |
76 void GetEncodedVideoFrame(const FrameEncodedCallback& callback); | |
77 | |
78 // Deliver another packet, possibly a duplicate, and possibly out-of-order. | |
79 void IncomingPacket(scoped_ptr<Packet> packet); | |
80 | |
81 protected: | |
82 friend class VideoReceiverTest; // Invokes OnReceivedPayloadData(). | |
83 | |
84 virtual void OnReceivedPayloadData(const uint8* payload_data, | |
85 size_t payload_size, | |
86 const RtpCastHeader& rtp_header) OVERRIDE; | |
87 | |
88 // RtpPayloadFeedback implementation. | |
89 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; | |
90 | |
91 private: | |
92 // Processes ready-to-consume packets from |framer_|, decrypting each packet's | |
93 // payload data, and then running the enqueued callbacks in order (one for | |
94 // each packet). This method may post a delayed task to re-invoke itself in | |
95 // the future to wait for missing/incomplete frames. | |
96 void EmitAvailableEncodedFrames(); | |
97 | |
98 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes | |
99 // EmitAvailableEncodedFrames(). | |
100 void EmitAvailableEncodedFramesAfterWaiting(); | |
101 | |
102 // Feeds an EncodedFrame into |video_decoder_|. GetRawVideoFrame() uses this | |
103 // as a callback for GetEncodedVideoFrame(). | |
104 void DecodeEncodedVideoFrame( | |
105 const VideoFrameDecodedCallback& callback, | |
106 scoped_ptr<transport::EncodedFrame> encoded_frame); | |
107 | |
108 // Computes the playout time for a frame with the given |rtp_timestamp|. | |
109 // Because lip-sync info is refreshed regularly, calling this method with the | |
110 // same argument may return different results. | |
111 base::TimeTicks GetPlayoutTime(uint32 rtp_timestamp) const; | |
112 | |
113 // Schedule timing for the next cast message. | |
114 void ScheduleNextCastMessage(); | |
115 | |
116 // Schedule timing for the next RTCP report. | |
117 void ScheduleNextRtcpReport(); | |
118 | |
119 // Actually send the next cast message. | |
120 void SendNextCastMessage(); | |
121 | |
122 // Actually send the next RTCP report. | |
123 void SendNextRtcpReport(); | |
124 | |
125 // Receives a VideoFrame from |video_decoder_|, logs the event, and passes the | |
126 // data on by running the given |callback|. This method is static to ensure | |
127 // it can be called after a VideoReceiver instance is destroyed. | |
128 // DecodeEncodedVideoFrame() uses this as a callback for | |
129 // VideoDecoder::DecodeFrame(). | |
130 static void EmitRawVideoFrame( | |
131 const scoped_refptr<CastEnvironment>& cast_environment, | |
132 const VideoFrameDecodedCallback& callback, | |
133 uint32 frame_id, | |
134 uint32 rtp_timestamp, | |
135 const base::TimeTicks& playout_time, | |
136 const scoped_refptr<VideoFrame>& video_frame, | |
137 bool is_continuous); | |
138 | |
139 const scoped_refptr<CastEnvironment> cast_environment_; | |
140 | |
141 // Subscribes to raw events. | |
142 // Processes raw audio events to be sent over to the cast sender via RTCP. | |
143 ReceiverRtcpEventSubscriber event_subscriber_; | |
144 | |
145 // Configured video codec. | |
146 const transport::VideoCodec codec_; | |
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. | |
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. | |
167 Framer framer_; | |
168 | |
169 // Decodes frames into media::VideoFrame images for playback. | |
170 scoped_ptr<VideoDecoder> video_decoder_; | |
171 | |
172 // Manages sending/receiving of RTCP packets, including sender/receiver | |
173 // reports. | |
174 Rtcp rtcp_; | |
175 | |
176 // Decrypts encrypted frames. | |
177 transport::TransportEncryptionHandler decryptor_; | |
178 | |
179 // Outstanding callbacks to run to deliver on client requests for frames. | |
180 std::list<FrameEncodedCallback> frame_request_queue_; | |
181 | |
182 // True while there's an outstanding task to re-invoke | |
183 // EmitAvailableEncodedFrames(). | |
184 bool is_waiting_for_consecutive_frame_; | |
185 | |
186 // This mapping allows us to log FRAME_ACK_SENT as a frame event. In addition | |
187 // it allows the event to be transmitted via RTCP. | |
188 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | |
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 | |
197 // NOTE: Weak pointers must be invalidated before all other member variables. | |
198 base::WeakPtrFactory<VideoReceiver> weak_factory_; | |
199 | |
200 DISALLOW_COPY_AND_ASSIGN(VideoReceiver); | |
201 }; | |
202 | |
203 } // namespace cast | |
204 } // namespace media | |
205 | |
206 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | |
OLD | NEW |