Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / remoting / host / video_frame_pump.h
blob5bafa3e17fefe6e5af27dac1c39743538d948648
1 // Copyright 2015 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 REMOTING_HOST_VIDEO_FRAME_PUMP_H_
6 #define REMOTING_HOST_VIDEO_FRAME_PUMP_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/time/time.h"
13 #include "base/timer/timer.h"
14 #include "remoting/codec/video_encoder.h"
15 #include "remoting/host/capture_scheduler.h"
16 #include "remoting/proto/video.pb.h"
17 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
19 namespace base {
20 class SingleThreadTaskRunner;
21 } // namespace base
23 namespace remoting {
25 namespace protocol {
26 class VideoFeedbackStub;
27 class VideoStub;
28 } // namespace protocol
30 // Class responsible for scheduling frame captures from a screen capturer.,
31 // delivering them to a VideoEncoder to encode, and
32 // finally passing the encoded video packets to the specified VideoStub to send
33 // on the network.
35 // THREADING
37 // This class is supplied TaskRunners to use for capture, encode and network
38 // operations. Capture, encode and network transmission tasks are interleaved
39 // as illustrated below:
41 // | CAPTURE ENCODE NETWORK
42 // | .............
43 // | . Capture .
44 // | .............
45 // | ............
46 // | . .
47 // | ............. . .
48 // | . Capture . . Encode .
49 // | ............. . .
50 // | . .
51 // | ............
52 // | ............. ............ ..........
53 // | . Capture . . . . Send .
54 // | ............. . . ..........
55 // | . Encode .
56 // | . .
57 // | . .
58 // | ............
59 // | Time
60 // v
62 // VideoFramePump would ideally schedule captures so as to saturate the slowest
63 // of the capture, encode and network processes. However, it also needs to
64 // rate-limit captures to avoid overloading the host system, either by consuming
65 // too much CPU, or hogging the host's graphics subsystem.
66 class VideoFramePump : public webrtc::DesktopCapturer::Callback {
67 public:
68 // Enables timestamps for generated frames. Used for testing.
69 static void EnableTimestampsForTests();
71 // Creates a VideoFramePump running capture, encode and network tasks on the
72 // supplied TaskRunners. Video will be pumped to |video_stub|, which must
73 // outlive the pump..
74 VideoFramePump(
75 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner,
76 scoped_ptr<webrtc::DesktopCapturer> capturer,
77 scoped_ptr<VideoEncoder> encoder,
78 protocol::VideoStub* video_stub);
79 ~VideoFramePump() override;
81 // Pauses or resumes scheduling of frame captures. Pausing/resuming captures
82 // only affects capture scheduling and does not stop/start the capturer.
83 void Pause(bool pause);
85 // Updates event timestamp from the last event received from the client. This
86 // value is sent back to the client for roundtrip latency estimates.
87 void SetLatestEventTimestamp(int64 latest_event_timestamp);
89 // Sets whether the video encoder should be requested to encode losslessly,
90 // or to use a lossless color space (typically requiring higher bandwidth).
91 void SetLosslessEncode(bool want_lossless);
92 void SetLosslessColor(bool want_lossless);
94 protocol::VideoFeedbackStub* video_feedback_stub() {
95 return &capture_scheduler_;
98 private:
99 // webrtc::DesktopCapturer::Callback interface.
100 webrtc::SharedMemory* CreateSharedMemory(size_t size) override;
101 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override;
103 // Callback for CaptureScheduler.
104 void CaptureNextFrame();
106 // Sends encoded frame
107 void SendEncodedFrame(int64 latest_event_timestamp,
108 base::TimeTicks timestamp,
109 scoped_ptr<VideoPacket> packet);
111 // Callback passed to |video_stub_|.
112 void OnVideoPacketSent();
114 // Called by |keep_alive_timer_|.
115 void SendKeepAlivePacket();
117 // Callback for |video_stub_| called after a keep-alive packet is sent.
118 void OnKeepAlivePacketSent();
120 // Task runner used to run |encoder_|.
121 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_;
123 // Capturer used to capture the screen.
124 scoped_ptr<webrtc::DesktopCapturer> capturer_;
126 // Used to encode captured frames. Always accessed on the encode thread.
127 scoped_ptr<VideoEncoder> encoder_;
129 // Interface through which video frames are passed to the client.
130 protocol::VideoStub* video_stub_;
132 // Timer used to ensure that we send empty keep-alive frames to the client
133 // even when the video stream is paused or encoder is busy.
134 base::Timer keep_alive_timer_;
136 // CaptureScheduler calls CaptureNextFrame() whenever a new frame needs to be
137 // captured.
138 CaptureScheduler capture_scheduler_;
140 // Number updated by the caller to trace performance.
141 int64 latest_event_timestamp_;
143 base::ThreadChecker thread_checker_;
145 base::WeakPtrFactory<VideoFramePump> weak_factory_;
147 DISALLOW_COPY_AND_ASSIGN(VideoFramePump);
150 } // namespace remoting
152 #endif // REMOTING_HOST_VIDEO_FRAME_PUMP_H_