Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / media / cast / video_sender / video_sender.h
blob89ff938ae65d0bf0ee63eae15d3016d5af3414b8
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_SENDER_VIDEO_SENDER_H_
6 #define MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/non_thread_safe.h"
13 #include "base/time/tick_clock.h"
14 #include "base/time/time.h"
15 #include "media/cast/cast_config.h"
16 #include "media/cast/cast_environment.h"
17 #include "media/cast/congestion_control/congestion_control.h"
18 #include "media/cast/logging/logging_defines.h"
19 #include "media/cast/rtcp/rtcp.h"
20 #include "media/cast/rtcp/sender_rtcp_event_subscriber.h"
22 namespace media {
23 class VideoFrame;
25 namespace cast {
26 class LocalRtcpVideoSenderFeedback;
27 class LocalVideoEncoderCallback;
28 class VideoEncoder;
30 namespace transport {
31 class CastTransportSender;
34 // Not thread safe. Only called from the main cast thread.
35 // This class owns all objects related to sending video, objects that create RTP
36 // packets, congestion control, video encoder, parsing and sending of
37 // RTCP packets.
38 // Additionally it posts a bunch of delayed tasks to the main thread for various
39 // timeouts.
40 class VideoSender : public base::NonThreadSafe,
41 public base::SupportsWeakPtr<VideoSender> {
42 public:
43 VideoSender(scoped_refptr<CastEnvironment> cast_environment,
44 const VideoSenderConfig& video_config,
45 const CreateVideoEncodeAcceleratorCallback& create_vea_cb,
46 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb,
47 const CastInitializationCallback& cast_initialization_cb,
48 transport::CastTransportSender* const transport_sender);
50 virtual ~VideoSender();
52 // The video_frame must be valid until the closure callback is called.
53 // The closure callback is called from the video encoder thread as soon as
54 // the encoder is done with the frame; it does not mean that the encoded frame
55 // has been sent out.
56 void InsertRawVideoFrame(const scoped_refptr<media::VideoFrame>& video_frame,
57 const base::TimeTicks& capture_time);
59 // Only called from the main cast thread.
60 void IncomingRtcpPacket(scoped_ptr<Packet> packet);
62 // Store rtp stats computed at the Cast transport sender.
63 void StoreStatistics(const transport::RtcpSenderInfo& sender_info,
64 base::TimeTicks time_sent,
65 uint32 rtp_timestamp);
67 protected:
68 // Protected for testability.
69 void OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback);
71 private:
72 friend class LocalRtcpVideoSenderFeedback;
74 // Schedule when we should send the next RTPC report,
75 // via a PostDelayedTask to the main cast thread.
76 void ScheduleNextRtcpReport();
77 void SendRtcpReport();
79 // Schedule when we should check that we have received an acknowledgment, or a
80 // loss report from our remote peer. If we have not heard back from our remote
81 // peer we speculatively resend our oldest unacknowledged frame (the whole
82 // frame). Note for this to happen we need to lose all pending packets (in
83 // normal operation 3 full frames), hence this is the last resort to prevent
84 // us getting stuck after a long outage.
85 void ScheduleNextResendCheck();
86 void ResendCheck();
88 // Monitor how many frames that are silently dropped by the video sender
89 // per time unit.
90 void ScheduleNextSkippedFramesCheck();
91 void SkippedFramesCheck();
93 void SendEncodedVideoFrame(const transport::EncodedVideoFrame* video_frame,
94 const base::TimeTicks& capture_time);
95 void ResendFrame(uint32 resend_frame_id);
96 void ReceivedAck(uint32 acked_frame_id);
97 void UpdateFramesInFlight();
99 void SendEncodedVideoFrameMainThread(
100 scoped_ptr<transport::EncodedVideoFrame> encoded_frame,
101 const base::TimeTicks& capture_time);
103 void InitializeTimers();
105 base::TimeDelta rtp_max_delay_;
106 const int max_frame_rate_;
108 scoped_refptr<CastEnvironment> cast_environment_;
109 transport::CastTransportSender* const transport_sender_;
111 // Subscribes to raw events.
112 // Processes raw audio events to be sent over to the cast receiver via RTCP.
113 SenderRtcpEventSubscriber event_subscriber_;
114 RtpSenderStatistics rtp_stats_;
115 scoped_ptr<LocalRtcpVideoSenderFeedback> rtcp_feedback_;
116 scoped_ptr<VideoEncoder> video_encoder_;
117 scoped_ptr<Rtcp> rtcp_;
118 uint8 max_unacked_frames_;
119 int last_acked_frame_id_;
120 int last_sent_frame_id_;
121 int frames_in_encoder_;
122 int duplicate_ack_;
123 base::TimeTicks last_send_time_;
124 base::TimeTicks last_checked_skip_count_time_;
125 int last_skip_count_;
126 int current_requested_bitrate_;
127 CongestionControl congestion_control_;
129 // This is a "good enough" mapping for finding the RTP timestamp associated
130 // with a video frame. The key is the lowest 8 bits of frame id (which is
131 // what is sent via RTCP). This map is used for logging purposes. The only
132 // time when this mapping will be incorrect is when it receives an ACK for a
133 // old enough frame such that 8-bit wrap around has already occurred, which
134 // should be pretty rare.
135 RtpTimestamp frame_id_to_rtp_timestamp_[256];
137 bool initialized_;
138 // Indicator for receiver acknowledgments.
139 bool active_session_;
141 // NOTE: Weak pointers must be invalidated before all other member variables.
142 base::WeakPtrFactory<VideoSender> weak_factory_;
144 DISALLOW_COPY_AND_ASSIGN(VideoSender);
147 } // namespace cast
148 } // namespace media
150 #endif // MEDIA_CAST_VIDEO_SENDER_VIDEO_SENDER_H_