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_RECEIVER_FRAME_RECEIVER_H_ |
6 #define MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | 6 #define MEDIA_CAST_RECEIVER_FRAME_RECEIVER_H_ |
7 | 7 |
8 #include "base/basictypes.h" | |
9 #include "base/callback.h" | |
10 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 10 #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" | 11 #include "base/time/time.h" |
16 #include "media/cast/base/clock_drift_smoother.h" | 12 #include "media/cast/base/clock_drift_smoother.h" |
17 #include "media/cast/cast_config.h" | 13 #include "media/cast/cast_config.h" |
18 #include "media/cast/cast_environment.h" | |
19 #include "media/cast/cast_receiver.h" | 14 #include "media/cast/cast_receiver.h" |
20 #include "media/cast/framer/framer.h" | 15 #include "media/cast/framer/framer.h" |
16 #include "media/cast/logging/logging_defines.h" | |
21 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" | 17 #include "media/cast/rtcp/receiver_rtcp_event_subscriber.h" |
22 #include "media/cast/rtcp/rtcp.h" | 18 #include "media/cast/rtcp/rtcp.h" |
23 #include "media/cast/rtp_receiver/rtp_receiver.h" | 19 #include "media/cast/rtp_receiver/receiver_stats.h" |
20 #include "media/cast/rtp_receiver/rtp_parser/rtp_parser.h" | |
24 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" | 21 #include "media/cast/rtp_receiver/rtp_receiver_defines.h" |
25 #include "media/cast/transport/utility/transport_encryption_handler.h" | 22 #include "media/cast/transport/utility/transport_encryption_handler.h" |
26 | 23 |
27 namespace media { | 24 namespace media { |
28 | |
29 class VideoFrame; | |
30 | |
31 namespace cast { | 25 namespace cast { |
32 | 26 |
33 class VideoDecoder; | 27 class CastEnvironment; |
34 | 28 |
35 // VideoReceiver receives packets out-of-order while clients make requests for | 29 // FrameReceiver receives packets out-of-order while clients make requests for |
36 // complete frames in-order. (A frame consists of one or more packets.) | 30 // complete frames in-order. (A frame consists of one or more packets.) |
37 // | 31 // |
38 // VideoReceiver also includes logic for computing the playout time for each | 32 // FrameReceiver also includes logic for computing the playout time for each |
39 // frame, accounting for a constant targeted playout delay. The purpose of the | 33 // 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 | 34 // 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 | 35 // 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 | 36 // 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 | 37 // 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. | 38 // frame) can vary in duration and is typically very hard to predict. |
45 // | 39 // |
46 // Two types of frames can be requested: 1) A frame of decoded video data; or 2) | 40 // Each request for a frame includes a callback which FrameReceiver guarantees |
47 // a frame of still-encoded video data, to be passed into an external video | 41 // will be called at some point in the future unless the FrameReceiver is |
48 // decoder. Each request for a frame includes a callback which VideoReceiver | 42 // destroyed. Clients should generally limit the number of outstanding requests |
49 // guarantees will be called at some point in the future unless the | 43 // (perhaps to just one or two). |
50 // VideoReceiver is destroyed. Clients should generally limit the number of | |
51 // outstanding requests (perhaps to just one or two). | |
52 // | 44 // |
53 // This class is not thread safe. Should only be called from the Main cast | 45 // This class is not thread safe. Should only be called from the Main cast |
54 // thread. | 46 // thread. |
55 class VideoReceiver : public RtpReceiver, | 47 class FrameReceiver : public RtpPayloadFeedback, |
56 public RtpPayloadFeedback, | 48 public base::SupportsWeakPtr<FrameReceiver> { |
57 public base::NonThreadSafe, | |
58 public base::SupportsWeakPtr<VideoReceiver> { | |
59 public: | 49 public: |
60 VideoReceiver(scoped_refptr<CastEnvironment> cast_environment, | 50 FrameReceiver(const scoped_refptr<CastEnvironment>& cast_environment, |
61 const FrameReceiverConfig& video_config, | 51 const FrameReceiverConfig& config, |
52 EventMediaType event_media_type, | |
62 transport::PacedPacketSender* const packet_sender); | 53 transport::PacedPacketSender* const packet_sender); |
63 | 54 |
64 virtual ~VideoReceiver(); | 55 virtual ~FrameReceiver(); |
65 | 56 |
66 // Request a decoded video frame. | 57 // Request an encoded frame. |
67 // | 58 // |
68 // The given |callback| is guaranteed to be run at some point in the future, | 59 // The given |callback| is guaranteed to be run at some point in the future, |
69 // even if to respond with NULL at shutdown time. | 60 // except for those requests still enqueued at destruction time. |
70 void GetRawVideoFrame(const VideoFrameDecodedCallback& callback); | 61 void RequestEncodedFrame(const FrameEncodedCallback& callback); |
Alpha Left Google
2014/06/02 20:16:59
nit: The type FrameEncodedCallback is misleading b
miu
2014/06/02 22:36:08
Done. Good call.
| |
71 | 62 |
72 // Request an encoded video frame. | 63 // Called to deliver another packet, possibly a duplicate, and possibly |
73 // | 64 // out-of-order. Returns true if the parsing of the packet succeeded. |
74 // The given |callback| is guaranteed to be run at some point in the future, | 65 bool ProcessPacket(scoped_ptr<Packet> packet); |
75 // even if to respond with NULL at shutdown time. | |
76 void GetEncodedVideoFrame(const FrameEncodedCallback& callback); | |
77 | 66 |
78 // Deliver another packet, possibly a duplicate, and possibly out-of-order. | 67 // TODO(miu): This is the wrong place for this, but the (de)serialization |
79 void IncomingPacket(scoped_ptr<Packet> packet); | 68 // implementation needs to be consolidated first. |
69 static bool ParseSenderSsrc(const uint8* packet, size_t length, uint32* ssrc); | |
80 | 70 |
81 protected: | 71 protected: |
82 friend class VideoReceiverTest; // Invokes OnReceivedPayloadData(). | 72 friend class FrameReceiverTest; // Invokes ProcessParsedPacket(). |
83 | 73 |
84 virtual void OnReceivedPayloadData(const uint8* payload_data, | 74 void ProcessParsedPacket(const RtpCastHeader& rtp_header, |
85 size_t payload_size, | 75 const uint8* payload_data, |
86 const RtpCastHeader& rtp_header) OVERRIDE; | 76 size_t payload_size); |
87 | 77 |
88 // RtpPayloadFeedback implementation. | 78 // RtpPayloadFeedback implementation. |
89 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; | 79 virtual void CastFeedback(const RtcpCastMessage& cast_message) OVERRIDE; |
90 | 80 |
91 private: | 81 private: |
92 // Processes ready-to-consume packets from |framer_|, decrypting each packet's | 82 // Processes ready-to-consume packets from |framer_|, decrypting each packet's |
93 // payload data, and then running the enqueued callbacks in order (one for | 83 // 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 | 84 // each packet). This method may post a delayed task to re-invoke itself in |
95 // the future to wait for missing/incomplete frames. | 85 // the future to wait for missing/incomplete frames. |
96 void EmitAvailableEncodedFrames(); | 86 void EmitAvailableEncodedFrames(); |
97 | 87 |
98 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes | 88 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes |
99 // EmitAvailableEncodedFrames(). | 89 // EmitAvailableEncodedFrames(). |
100 void EmitAvailableEncodedFramesAfterWaiting(); | 90 void EmitAvailableEncodedFramesAfterWaiting(); |
101 | 91 |
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|. | 92 // 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 | 93 // Because lip-sync info is refreshed regularly, calling this method with the |
110 // same argument may return different results. | 94 // same argument may return different results. |
111 base::TimeTicks GetPlayoutTime(uint32 rtp_timestamp) const; | 95 base::TimeTicks GetPlayoutTime(uint32 rtp_timestamp) const; |
112 | 96 |
113 // Schedule timing for the next cast message. | 97 // Schedule timing for the next cast message. |
114 void ScheduleNextCastMessage(); | 98 void ScheduleNextCastMessage(); |
115 | 99 |
116 // Schedule timing for the next RTCP report. | 100 // Schedule timing for the next RTCP report. |
117 void ScheduleNextRtcpReport(); | 101 void ScheduleNextRtcpReport(); |
118 | 102 |
119 // Actually send the next cast message. | 103 // Actually send the next cast message. |
120 void SendNextCastMessage(); | 104 void SendNextCastMessage(); |
121 | 105 |
122 // Actually send the next RTCP report. | 106 // Actually send the next RTCP report. |
123 void SendNextRtcpReport(); | 107 void SendNextRtcpReport(); |
124 | 108 |
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_; | 109 const scoped_refptr<CastEnvironment> cast_environment_; |
140 | 110 |
111 // Deserializes a packet into a RtpHeader + payload bytes. | |
112 RtpParser packet_parser_; | |
113 | |
114 // Accumulates packet statistics, including packet loss, counts, and jitter. | |
115 ReceiverStats stats_; | |
116 | |
117 // Partitions logged events by the type of media passing through. | |
118 EventMediaType event_media_type_; | |
119 | |
141 // Subscribes to raw events. | 120 // Subscribes to raw events. |
142 // Processes raw audio events to be sent over to the cast sender via RTCP. | 121 // Processes raw events to be sent over to the cast sender via RTCP. |
143 ReceiverRtcpEventSubscriber event_subscriber_; | 122 ReceiverRtcpEventSubscriber event_subscriber_; |
144 | 123 |
145 // Configured video codec. | 124 // RTP timebase: The number of RTP units advanced per one second. |
146 const transport::VideoCodec codec_; | 125 const int rtp_timebase_; |
147 | 126 |
148 // The total amount of time between a frame's capture/recording on the sender | 127 // 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 | 128 // 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, | 129 // a value large enough to give the system sufficient time to encode, |
151 // transmit/retransmit, receive, decode, and render; given its run-time | 130 // transmit/retransmit, receive, decode, and render; given its run-time |
152 // environment (sender/receiver hardware performance, network conditions, | 131 // environment (sender/receiver hardware performance, network conditions, |
153 // etc.). | 132 // etc.). |
154 const base::TimeDelta target_playout_delay_; | 133 const base::TimeDelta target_playout_delay_; |
155 | 134 |
156 // Hack: This is used in logic that determines whether to skip frames. | 135 // Hack: This is used in logic that determines whether to skip frames. |
136 // TODO(miu): Revisit this. Logic needs to also account for expected decode | |
137 // time. | |
157 const base::TimeDelta expected_frame_duration_; | 138 const base::TimeDelta expected_frame_duration_; |
158 | 139 |
159 // Set to false initially, then set to true after scheduling the periodic | 140 // 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 | 141 // sending of reports back to the sender. Reports are first scheduled just |
161 // after receiving a first packet (since the first packet identifies the | 142 // after receiving a first packet (since the first packet identifies the |
162 // sender for the remainder of the session). | 143 // sender for the remainder of the session). |
163 bool reports_are_scheduled_; | 144 bool reports_are_scheduled_; |
164 | 145 |
165 // Assembles packets into frames, providing this receiver with complete, | 146 // Assembles packets into frames, providing this receiver with complete, |
166 // decodable EncodedFrames. | 147 // decodable EncodedFrames. |
167 Framer framer_; | 148 Framer framer_; |
168 | 149 |
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 | 150 // Manages sending/receiving of RTCP packets, including sender/receiver |
173 // reports. | 151 // reports. |
174 Rtcp rtcp_; | 152 Rtcp rtcp_; |
175 | 153 |
176 // Decrypts encrypted frames. | 154 // Decrypts encrypted frames. |
177 transport::TransportEncryptionHandler decryptor_; | 155 transport::TransportEncryptionHandler decryptor_; |
178 | 156 |
179 // Outstanding callbacks to run to deliver on client requests for frames. | 157 // Outstanding callbacks to run to deliver on client requests for frames. |
180 std::list<FrameEncodedCallback> frame_request_queue_; | 158 std::list<FrameEncodedCallback> frame_request_queue_; |
181 | 159 |
182 // True while there's an outstanding task to re-invoke | 160 // True while there's an outstanding task to re-invoke |
183 // EmitAvailableEncodedFrames(). | 161 // EmitAvailableEncodedFrames(). |
184 bool is_waiting_for_consecutive_frame_; | 162 bool is_waiting_for_consecutive_frame_; |
185 | 163 |
186 // 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 |
187 // it allows the event to be transmitted via RTCP. | 165 // it allows the event to be transmitted via RTCP. |
188 RtpTimestamp frame_id_to_rtp_timestamp_[256]; | 166 RtpTimestamp frame_id_to_rtp_timestamp_[256]; |
189 | 167 |
190 // Lip-sync values used to compute the playout time of each frame from its RTP | 168 // 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 | 169 // timestamp. These are updated each time the first packet of a frame is |
192 // received. | 170 // received. |
193 RtpTimestamp lip_sync_rtp_timestamp_; | 171 RtpTimestamp lip_sync_rtp_timestamp_; |
194 base::TimeTicks lip_sync_reference_time_; | 172 base::TimeTicks lip_sync_reference_time_; |
195 ClockDriftSmoother lip_sync_drift_; | 173 ClockDriftSmoother lip_sync_drift_; |
196 | 174 |
197 // NOTE: Weak pointers must be invalidated before all other member variables. | 175 // NOTE: Weak pointers must be invalidated before all other member variables. |
198 base::WeakPtrFactory<VideoReceiver> weak_factory_; | 176 base::WeakPtrFactory<FrameReceiver> weak_factory_; |
199 | 177 |
200 DISALLOW_COPY_AND_ASSIGN(VideoReceiver); | 178 DISALLOW_COPY_AND_ASSIGN(FrameReceiver); |
201 }; | 179 }; |
202 | 180 |
203 } // namespace cast | 181 } // namespace cast |
204 } // namespace media | 182 } // namespace media |
205 | 183 |
206 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_ | 184 #endif // MEDIA_CAST_RECEIVER_FRAME_RECEIVER_H_ |
OLD | NEW |