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/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"
22
23namespace crypto {
24  class Encryptor;
25}
26
27namespace media {
28namespace cast {
29
30class Framer;
31class LocalRtpVideoData;
32class LocalRtpVideoFeedback;
33class PacedPacketSender;
34class PeerVideoReceiver;
35class Rtcp;
36class RtpReceiverStatistics;
37class VideoDecoder;
38
39// Should only be called from the Main cast thread.
40class VideoReceiver : public base::NonThreadSafe,
41                      public base::SupportsWeakPtr<VideoReceiver> {
42 public:
43  VideoReceiver(scoped_refptr<CastEnvironment> cast_environment,
44                const VideoReceiverConfig& video_config,
45                PacedPacketSender* const packet_sender);
46
47  virtual ~VideoReceiver();
48
49  // Request a raw frame. Will return frame via callback when available.
50  void GetRawVideoFrame(const VideoFrameDecodedCallback& callback);
51
52  // Request an encoded frame. Will return frame via callback when available.
53  void GetEncodedVideoFrame(const VideoFrameEncodedCallback& callback);
54
55  // Insert a RTP packet to the video receiver.
56  void IncomingPacket(const uint8* packet, size_t length,
57                      const base::Closure callback);
58
59 protected:
60  void IncomingParsedRtpPacket(const uint8* payload_data,
61                               size_t payload_size,
62                               const RtpCastHeader& rtp_header);
63
64  void DecodeVideoFrameThread(
65      scoped_ptr<EncodedVideoFrame> encoded_frame,
66      const base::TimeTicks render_time,
67      const VideoFrameDecodedCallback& frame_decoded_callback);
68
69 private:
70  friend class LocalRtpVideoData;
71  friend class LocalRtpVideoFeedback;
72
73  void CastFeedback(const RtcpCastMessage& cast_message);
74
75  void DecodeVideoFrame(const VideoFrameDecodedCallback& callback,
76                        scoped_ptr<EncodedVideoFrame> encoded_frame,
77                        const base::TimeTicks& render_time);
78
79  bool DecryptVideoFrame(scoped_ptr<EncodedVideoFrame>* video_frame);
80
81  bool PullEncodedVideoFrame(uint32 rtp_timestamp,
82                             bool next_frame,
83                             scoped_ptr<EncodedVideoFrame>* encoded_frame,
84                             base::TimeTicks* render_time);
85
86  void PlayoutTimeout();
87
88  // Returns Render time based on current time and the rtp timestamp.
89  base::TimeTicks GetRenderTime(base::TimeTicks now, uint32 rtp_timestamp);
90
91  void InitializeTimers();
92
93  // Schedule timing for the next cast message.
94  void ScheduleNextCastMessage();
95
96  // Schedule timing for the next RTCP report.
97  void ScheduleNextRtcpReport();
98
99  // Actually send the next cast message.
100  void SendNextCastMessage();
101
102  // Actually send the next RTCP report.
103  void SendNextRtcpReport();
104
105  scoped_ptr<VideoDecoder> video_decoder_;
106  scoped_refptr<CastEnvironment> cast_environment_;
107  scoped_ptr<Framer> framer_;
108  const 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  std::string iv_mask_;
120  std::list<VideoFrameEncodedCallback> queued_encoded_callbacks_;
121  bool time_incoming_packet_updated_;
122  base::TimeTicks time_incoming_packet_;
123  uint32 incoming_rtp_timestamp_;
124
125  base::WeakPtrFactory<VideoReceiver> weak_factory_;
126
127  DISALLOW_COPY_AND_ASSIGN(VideoReceiver);
128};
129
130}  // namespace cast
131}  // namespace media
132
133#endif  // MEDIA_CAST_VIDEO_RECEIVER_VIDEO_RECEIVER_H_
134