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.
5 #ifndef MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_
6 #define MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_
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/cast_config.h"
17 #include "media/cast/cast_environment.h"
18 #include "media/cast/cast_receiver.h"
19 #include "media/cast/rtcp/rtcp.h"
20 #include "media/cast/rtp_receiver/rtp_receiver.h"
21 #include "media/cast/rtp_receiver/rtp_receiver_defines.h"
31 class LocalRtpVideoData;
32 class LocalRtpVideoFeedback;
33 class PeerVideoReceiver;
35 class RtpReceiverStatistics;
38 // Should only be called from the Main cast thread.
39 class VideoReceiver : public base::NonThreadSafe,
40 public base::SupportsWeakPtr<VideoReceiver> {
42 VideoReceiver(scoped_refptr<CastEnvironment> cast_environment,
43 const VideoReceiverConfig& video_config,
44 transport::PacedPacketSender* const packet_sender);
46 virtual ~VideoReceiver();
48 // Request a raw frame. Will return frame via callback when available.
49 void GetRawVideoFrame(const VideoFrameDecodedCallback& callback);
51 // Request an encoded frame. Will return frame via callback when available.
52 void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback);
54 // Insert a RTP packet to the video receiver.
55 void IncomingPacket(const uint8* packet, size_t length,
56 const base::Closure callback);
59 void IncomingParsedRtpPacket(const uint8* payload_data,
61 const RtpCastHeader& rtp_header);
63 void DecodeVideoFrameThread(
64 scoped_ptr<transport::EncodedVideoFrame> encoded_frame,
65 const base::TimeTicks render_time,
66 const VideoFrameDecodedCallback& frame_decoded_callback);
69 friend class LocalRtpVideoData;
70 friend class LocalRtpVideoFeedback;
72 void CastFeedback(const RtcpCastMessage& cast_message);
74 void DecodeVideoFrame(const VideoFrameDecodedCallback& callback,
75 scoped_ptr<transport::EncodedVideoFrame> encoded_frame,
76 const base::TimeTicks& render_time);
78 bool DecryptVideoFrame(scoped_ptr<transport::EncodedVideoFrame>* video_frame);
80 bool PullEncodedVideoFrame(
83 scoped_ptr<transport::EncodedVideoFrame>* encoded_frame,
84 base::TimeTicks* render_time);
86 void PlayoutTimeout();
88 // Returns Render time based on current time and the rtp timestamp.
89 base::TimeTicks GetRenderTime(base::TimeTicks now, uint32 rtp_timestamp);
91 void InitializeTimers();
93 // Schedule timing for the next cast message.
94 void ScheduleNextCastMessage();
96 // Schedule timing for the next RTCP report.
97 void ScheduleNextRtcpReport();
99 // Actually send the next cast message.
100 void SendNextCastMessage();
102 // Actually send the next RTCP report.
103 void SendNextRtcpReport();
105 scoped_ptr<VideoDecoder> video_decoder_;
106 scoped_refptr<CastEnvironment> cast_environment_;
107 scoped_ptr<Framer> framer_;
108 const transport::VideoCodec codec_;
109 base::TimeDelta target_delay_delta_;
110 base::TimeDelta frame_delay_;
111 scoped_ptr<LocalRtpVideoData> incoming_payload_callback_;
112 scoped_ptr<LocalRtpVideoFeedback> incoming_payload_feedback_;
113 RtpReceiver rtp_receiver_;
114 scoped_ptr<Rtcp> rtcp_;
115 scoped_ptr<RtpReceiverStatistics> rtp_video_receiver_statistics_;
116 base::TimeTicks time_last_sent_cast_message_;
117 base::TimeDelta time_offset_; // Sender-receiver offset estimation.
118 scoped_ptr<crypto::Encryptor> decryptor_;
119 scoped_ptr<crypto::SymmetricKey> decryption_key_;
120 std::string iv_mask_;
121 std::list<VideoFrameEncodedCallback> queued_encoded_callbacks_;
122 bool time_incoming_packet_updated_;
123 base::TimeTicks time_incoming_packet_;
124 uint32 incoming_rtp_timestamp_;
125 base::TimeTicks last_render_time_;
127 base::WeakPtrFactory<VideoReceiver> weak_factory_;
129 DISALLOW_COPY_AND_ASSIGN(VideoReceiver);
135 #endif // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_