This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / media / cast / receiver / frame_receiver.h
blobf4037dd9a1124f267c9341ccc6596c835851a953
1 // Copyright 2014 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.
5 #ifndef MEDIA_CAST_RECEIVER_FRAME_RECEIVER_H_
6 #define MEDIA_CAST_RECEIVER_FRAME_RECEIVER_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "media/cast/cast_config.h"
13 #include "media/cast/cast_receiver.h"
14 #include "media/cast/common/clock_drift_smoother.h"
15 #include "media/cast/common/transport_encryption_handler.h"
16 #include "media/cast/logging/logging_defines.h"
17 #include "media/cast/net/rtcp/receiver_rtcp_event_subscriber.h"
18 #include "media/cast/net/rtcp/rtcp.h"
19 #include "media/cast/net/rtp/framer.h"
20 #include "media/cast/net/rtp/receiver_stats.h"
21 #include "media/cast/net/rtp/rtp_parser.h"
22 #include "media/cast/net/rtp/rtp_receiver_defines.h"
24 namespace media {
25 namespace cast {
27 class CastEnvironment;
29 // FrameReceiver receives packets out-of-order while clients make requests for
30 // complete frames in-order. (A frame consists of one or more packets.)
32 // FrameReceiver also includes logic for computing the playout time for each
33 // frame, accounting for a constant targeted playout delay. The purpose of the
34 // playout delay is to provide a fixed window of time between the capture event
35 // on the sender and the playout on the receiver. This is important because
36 // each step of the pipeline (i.e., encode frame, then transmit/retransmit from
37 // the sender, then receive and re-order packets on the receiver, then decode
38 // frame) can vary in duration and is typically very hard to predict.
40 // Each request for a frame includes a callback which FrameReceiver guarantees
41 // will be called at some point in the future unless the FrameReceiver is
42 // destroyed. Clients should generally limit the number of outstanding requests
43 // (perhaps to just one or two).
45 // This class is not thread safe. Should only be called from the Main cast
46 // thread.
47 class FrameReceiver : public RtpPayloadFeedback,
48 public base::SupportsWeakPtr<FrameReceiver> {
49 public:
50 FrameReceiver(const scoped_refptr<CastEnvironment>& cast_environment,
51 const FrameReceiverConfig& config,
52 EventMediaType event_media_type,
53 CastTransportSender* const transport);
55 ~FrameReceiver() final;
57 // Request an encoded frame.
59 // The given |callback| is guaranteed to be run at some point in the future,
60 // except for those requests still enqueued at destruction time.
61 void RequestEncodedFrame(const ReceiveEncodedFrameCallback& callback);
63 // Called to deliver another packet, possibly a duplicate, and possibly
64 // out-of-order. Returns true if the parsing of the packet succeeded.
65 bool ProcessPacket(scoped_ptr<Packet> packet);
67 protected:
68 friend class FrameReceiverTest; // Invokes ProcessParsedPacket().
70 void ProcessParsedPacket(const RtpCastHeader& rtp_header,
71 const uint8* payload_data,
72 size_t payload_size);
74 // RtpPayloadFeedback implementation.
75 void CastFeedback(const RtcpCastMessage& cast_message) final;
77 private:
78 // Processes ready-to-consume packets from |framer_|, decrypting each packet's
79 // payload data, and then running the enqueued callbacks in order (one for
80 // each packet). This method may post a delayed task to re-invoke itself in
81 // the future to wait for missing/incomplete frames.
82 void EmitAvailableEncodedFrames();
84 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes
85 // EmitAvailableEncodedFrames().
86 void EmitAvailableEncodedFramesAfterWaiting();
88 // Helper that runs |callback|, passing ownership of |encoded_frame| to it.
89 // This method is used by EmitAvailableEncodedFrames() to return to the event
90 // loop, but make sure that FrameReceiver is still alive before the callback
91 // is run.
92 void EmitOneFrame(const ReceiveEncodedFrameCallback& callback,
93 scoped_ptr<EncodedFrame> encoded_frame) const;
95 // Computes the playout time for a frame with the given |rtp_timestamp|.
96 // Because lip-sync info is refreshed regularly, calling this method with the
97 // same argument may return different results.
98 base::TimeTicks GetPlayoutTime(const EncodedFrame& frame) const;
100 // Schedule timing for the next cast message.
101 void ScheduleNextCastMessage();
103 // Schedule timing for the next RTCP report.
104 void ScheduleNextRtcpReport();
106 // Actually send the next cast message.
107 void SendNextCastMessage();
109 // Actually send the next RTCP report.
110 void SendNextRtcpReport();
112 const scoped_refptr<CastEnvironment> cast_environment_;
114 // Transport used to send data back.
115 CastTransportSender* const transport_;
117 // Deserializes a packet into a RtpHeader + payload bytes.
118 RtpParser packet_parser_;
120 // Accumulates packet statistics, including packet loss, counts, and jitter.
121 ReceiverStats stats_;
123 // Partitions logged events by the type of media passing through.
124 EventMediaType event_media_type_;
126 // Subscribes to raw events.
127 // Processes raw events to be sent over to the cast sender via RTCP.
128 ReceiverRtcpEventSubscriber event_subscriber_;
130 // RTP timebase: The number of RTP units advanced per one second.
131 const int rtp_timebase_;
133 // The total amount of time between a frame's capture/recording on the sender
134 // and its playback on the receiver (i.e., shown to a user). This is fixed as
135 // a value large enough to give the system sufficient time to encode,
136 // transmit/retransmit, receive, decode, and render; given its run-time
137 // environment (sender/receiver hardware performance, network conditions,
138 // etc.).
139 base::TimeDelta target_playout_delay_;
141 // Hack: This is used in logic that determines whether to skip frames.
142 // TODO(miu): Revisit this. Logic needs to also account for expected decode
143 // time.
144 const base::TimeDelta expected_frame_duration_;
146 // Set to false initially, then set to true after scheduling the periodic
147 // sending of reports back to the sender. Reports are first scheduled just
148 // after receiving a first packet (since the first packet identifies the
149 // sender for the remainder of the session).
150 bool reports_are_scheduled_;
152 // Assembles packets into frames, providing this receiver with complete,
153 // decodable EncodedFrames.
154 Framer framer_;
156 // Manages sending/receiving of RTCP packets, including sender/receiver
157 // reports.
158 Rtcp rtcp_;
160 // Decrypts encrypted frames.
161 TransportEncryptionHandler decryptor_;
163 // Outstanding callbacks to run to deliver on client requests for frames.
164 std::list<ReceiveEncodedFrameCallback> frame_request_queue_;
166 // True while there's an outstanding task to re-invoke
167 // EmitAvailableEncodedFrames().
168 bool is_waiting_for_consecutive_frame_;
170 // This mapping allows us to log FRAME_ACK_SENT as a frame event. In addition
171 // it allows the event to be transmitted via RTCP.
172 RtpTimestamp frame_id_to_rtp_timestamp_[256];
174 // Lip-sync values used to compute the playout time of each frame from its RTP
175 // timestamp. These are updated each time the first packet of a frame is
176 // received.
177 RtpTimestamp lip_sync_rtp_timestamp_;
178 base::TimeTicks lip_sync_reference_time_;
179 ClockDriftSmoother lip_sync_drift_;
181 // NOTE: Weak pointers must be invalidated before all other member variables.
182 base::WeakPtrFactory<FrameReceiver> weak_factory_;
184 DISALLOW_COPY_AND_ASSIGN(FrameReceiver);
187 } // namespace cast
188 } // namespace media
190 #endif // MEDIA_CAST_RECEIVER_FRAME_RECEIVER_H_