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"
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
47 class FrameReceiver
: public RtpPayloadFeedback
,
48 public base::SupportsWeakPtr
<FrameReceiver
> {
50 FrameReceiver(const scoped_refptr
<CastEnvironment
>& cast_environment
,
51 const FrameReceiverConfig
& config
,
52 EventMediaType event_media_type
,
53 PacedPacketSender
* const packet_sender
);
55 ~FrameReceiver() override
;
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 // TODO(miu): This is the wrong place for this, but the (de)serialization
68 // implementation needs to be consolidated first.
69 static bool ParseSenderSsrc(const uint8
* packet
, size_t length
, uint32
* ssrc
);
72 friend class FrameReceiverTest
; // Invokes ProcessParsedPacket().
74 void ProcessParsedPacket(const RtpCastHeader
& rtp_header
,
75 const uint8
* payload_data
,
78 // RtpPayloadFeedback implementation.
79 void CastFeedback(const RtcpCastMessage
& cast_message
) override
;
82 // Processes ready-to-consume packets from |framer_|, decrypting each packet's
83 // payload data, and then running the enqueued callbacks in order (one for
84 // each packet). This method may post a delayed task to re-invoke itself in
85 // the future to wait for missing/incomplete frames.
86 void EmitAvailableEncodedFrames();
88 // Clears the |is_waiting_for_consecutive_frame_| flag and invokes
89 // EmitAvailableEncodedFrames().
90 void EmitAvailableEncodedFramesAfterWaiting();
92 // Helper that runs |callback|, passing ownership of |encoded_frame| to it.
93 // This method is used by EmitAvailableEncodedFrames() to return to the event
94 // loop, but make sure that FrameReceiver is still alive before the callback
96 void EmitOneFrame(const ReceiveEncodedFrameCallback
& callback
,
97 scoped_ptr
<EncodedFrame
> encoded_frame
) const;
99 // Computes the playout time for a frame with the given |rtp_timestamp|.
100 // Because lip-sync info is refreshed regularly, calling this method with the
101 // same argument may return different results.
102 base::TimeTicks
GetPlayoutTime(const EncodedFrame
& frame
) const;
104 // Schedule timing for the next cast message.
105 void ScheduleNextCastMessage();
107 // Schedule timing for the next RTCP report.
108 void ScheduleNextRtcpReport();
110 // Actually send the next cast message.
111 void SendNextCastMessage();
113 // Actually send the next RTCP report.
114 void SendNextRtcpReport();
116 const scoped_refptr
<CastEnvironment
> cast_environment_
;
118 // Deserializes a packet into a RtpHeader + payload bytes.
119 RtpParser packet_parser_
;
121 // Accumulates packet statistics, including packet loss, counts, and jitter.
122 ReceiverStats stats_
;
124 // Partitions logged events by the type of media passing through.
125 EventMediaType event_media_type_
;
127 // Subscribes to raw events.
128 // Processes raw events to be sent over to the cast sender via RTCP.
129 ReceiverRtcpEventSubscriber event_subscriber_
;
131 // RTP timebase: The number of RTP units advanced per one second.
132 const int rtp_timebase_
;
134 // The total amount of time between a frame's capture/recording on the sender
135 // and its playback on the receiver (i.e., shown to a user). This is fixed as
136 // a value large enough to give the system sufficient time to encode,
137 // transmit/retransmit, receive, decode, and render; given its run-time
138 // environment (sender/receiver hardware performance, network conditions,
140 base::TimeDelta target_playout_delay_
;
142 // Hack: This is used in logic that determines whether to skip frames.
143 // TODO(miu): Revisit this. Logic needs to also account for expected decode
145 const base::TimeDelta expected_frame_duration_
;
147 // Set to false initially, then set to true after scheduling the periodic
148 // sending of reports back to the sender. Reports are first scheduled just
149 // after receiving a first packet (since the first packet identifies the
150 // sender for the remainder of the session).
151 bool reports_are_scheduled_
;
153 // Assembles packets into frames, providing this receiver with complete,
154 // decodable EncodedFrames.
157 // Manages sending/receiving of RTCP packets, including sender/receiver
161 // Decrypts encrypted frames.
162 TransportEncryptionHandler decryptor_
;
164 // Outstanding callbacks to run to deliver on client requests for frames.
165 std::list
<ReceiveEncodedFrameCallback
> frame_request_queue_
;
167 // True while there's an outstanding task to re-invoke
168 // EmitAvailableEncodedFrames().
169 bool is_waiting_for_consecutive_frame_
;
171 // This mapping allows us to log FRAME_ACK_SENT as a frame event. In addition
172 // it allows the event to be transmitted via RTCP.
173 RtpTimestamp frame_id_to_rtp_timestamp_
[256];
175 // Lip-sync values used to compute the playout time of each frame from its RTP
176 // timestamp. These are updated each time the first packet of a frame is
178 RtpTimestamp lip_sync_rtp_timestamp_
;
179 base::TimeTicks lip_sync_reference_time_
;
180 ClockDriftSmoother lip_sync_drift_
;
182 // Time interval for sending a RTCP report.
183 const base::TimeDelta rtcp_interval_
;
185 // NOTE: Weak pointers must be invalidated before all other member variables.
186 base::WeakPtrFactory
<FrameReceiver
> weak_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(FrameReceiver
);
194 #endif // MEDIA_CAST_RECEIVER_FRAME_RECEIVER_H_