OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ | |
6 #define REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/single_thread_task_runner.h" | |
14 #include "base/task/cancelable_task_tracker.h" | |
15 #include "base/threading/thread_checker.h" | |
16 #include "base/timer/timer.h" | |
17 #include "remoting/codec/video_encoder.h" | |
18 #include "remoting/protocol/video_stream.h" | |
19 #include "remoting/protocol/webrtc_transport.h" | |
20 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | |
21 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | |
22 | |
23 namespace remoting { | |
24 namespace protocol { | |
25 | |
26 // Class responsible for scheduling frames for encode and hand-over to | |
27 // Webrtc transport for sending across the network. | |
28 class WebrtcFrameScheduler : public webrtc::DesktopCapturer::Callback { | |
29 public: | |
30 WebrtcFrameScheduler( | |
31 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | |
32 std::unique_ptr<webrtc::DesktopCapturer> capturer, | |
33 WebrtcTransport* webrtc_transport, | |
34 std::unique_ptr<VideoEncoder> encoder); | |
35 ~WebrtcFrameScheduler() override; | |
36 | |
37 // Starts scheduling frames to be encoded and transported. | |
38 void Start(); | |
39 // Stops scheduling frames. | |
40 void Stop(); | |
41 | |
42 void Pause(bool pause); | |
43 | |
44 void SetSizeCallback(const VideoStream::SizeCallback& size_callback); | |
45 | |
46 private: | |
47 // webrtc::DesktopCapturer::Callback interface. | |
48 void OnCaptureResult(webrtc::DesktopCapturer::Result result, | |
49 std::unique_ptr<webrtc::DesktopFrame> frame) override; | |
50 | |
51 // Starts |capture_timer_|. | |
52 void StartCaptureTimer(); | |
53 | |
54 // Called by |capture_timer_|. | |
55 void CaptureNextFrame(); | |
56 | |
57 // Task running on the encoder thread to encode the |frame|. | |
58 static std::unique_ptr<VideoPacket> EncodeFrame( | |
59 VideoEncoder* encoder, | |
60 std::unique_ptr<webrtc::DesktopFrame> frame, | |
61 uint32_t target_bitrate, | |
62 bool key_frame_request, | |
63 int64_t capture_time_ms); | |
64 void OnFrameEncoded(std::unique_ptr<VideoPacket> packet); | |
65 | |
66 void SetKeyFrameRequest(); | |
67 bool ClearAndGetKeyFrameRequest(); | |
68 void SetTargetBitrate(int bitrate); | |
69 | |
70 // Protects |key_frame_request_| and |target_bitrate_kbps_|. | |
71 base::Lock lock_; | |
72 bool key_frame_request_ = false; | |
73 uint32_t target_bitrate_kbps_; | |
74 | |
75 bool received_first_frame_request_ = false; | |
76 | |
77 bool capture_pending_ = false; | |
78 bool encode_pending_ = false; | |
79 | |
80 // Last time capture was completed. | |
81 base::TimeTicks last_capture_completed_ticks_; | |
82 // Last time capture was started. | |
83 base::TimeTicks last_capture_started_ticks_; | |
84 | |
85 webrtc::DesktopSize frame_size_; | |
86 webrtc::DesktopVector frame_dpi_; | |
87 VideoStream::SizeCallback size_callback_; | |
88 | |
89 // Main task runner. | |
90 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
91 | |
92 // Task runner used to run |encoder_|. | |
93 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; | |
94 base::CancelableTaskTracker task_tracker_; | |
95 | |
96 // Capturer used to capture the screen. | |
97 std::unique_ptr<webrtc::DesktopCapturer> capturer_; | |
98 | |
99 std::unique_ptr<base::RepeatingTimer> capture_timer_; | |
100 | |
101 // Used to send across encoded frames. | |
102 WebrtcTransport* webrtc_transport_; | |
103 | |
104 // Used to encode captured frames. Always accessed on the encode thread. | |
105 std::unique_ptr<VideoEncoder> encoder_; | |
106 | |
107 base::ThreadChecker thread_checker_; | |
108 | |
109 base::WeakPtrFactory<WebrtcFrameScheduler> weak_factory_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(WebrtcFrameScheduler); | |
112 }; | |
113 | |
114 } // namespace protocol | |
115 } // namespace remoting | |
116 | |
117 #endif // REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ | |
OLD | NEW |