OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ | 5 #ifndef REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ |
6 #define REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ | 6 #define REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/task/cancelable_task_tracker.h" | 14 #include "base/task/cancelable_task_tracker.h" |
15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
16 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
| 17 #include "media/capture/content/feedback_signal_accumulator.h" |
17 #include "remoting/codec/video_encoder.h" | 18 #include "remoting/codec/video_encoder.h" |
18 #include "remoting/protocol/video_stream.h" | 19 #include "remoting/protocol/video_stream.h" |
19 #include "remoting/protocol/webrtc_transport.h" | 20 #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_capturer.h" |
21 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 22 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
22 | 23 |
23 namespace remoting { | 24 namespace remoting { |
24 namespace protocol { | 25 namespace protocol { |
25 | 26 |
| 27 namespace { |
| 28 |
| 29 // Max Quantizer value. |
| 30 const int kMaxQuantizer = 63; |
| 31 |
| 32 // Default target bitrate in kbps |
| 33 const int kDefaultTargetBitrateKbps = 1000; |
| 34 |
| 35 } // namespace |
| 36 |
26 // Class responsible for scheduling frames for encode and hand-over to | 37 // Class responsible for scheduling frames for encode and hand-over to |
27 // WebRtc transport for sending across the network. | 38 // WebRtc transport for sending across the network. |
28 class WebRtcFrameScheduler : public webrtc::DesktopCapturer::Callback { | 39 class WebRtcFrameScheduler : public webrtc::DesktopCapturer::Callback { |
29 public: | 40 public: |
30 WebRtcFrameScheduler( | 41 WebRtcFrameScheduler( |
31 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 42 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
32 std::unique_ptr<webrtc::DesktopCapturer> capturer, | 43 std::unique_ptr<webrtc::DesktopCapturer> capturer, |
33 WebrtcTransport* webrtc_transport, | 44 WebrtcTransport* webrtc_transport, |
34 std::unique_ptr<VideoEncoder> encoder); | 45 std::unique_ptr<VideoEncoder> encoder); |
35 ~WebRtcFrameScheduler() override; | 46 ~WebRtcFrameScheduler() override; |
(...skipping 12 matching lines...) Expand all Loading... |
48 webrtc::SharedMemory* CreateSharedMemory(size_t size) override; | 59 webrtc::SharedMemory* CreateSharedMemory(size_t size) override; |
49 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; | 60 void OnCaptureCompleted(webrtc::DesktopFrame* frame) override; |
50 | 61 |
51 // Callback for CaptureScheduler. | 62 // Callback for CaptureScheduler. |
52 void CaptureNextFrame(); | 63 void CaptureNextFrame(); |
53 | 64 |
54 // Task running on the encoder thread to encode the |frame|. | 65 // Task running on the encoder thread to encode the |frame|. |
55 static std::unique_ptr<VideoPacket> EncodeFrame( | 66 static std::unique_ptr<VideoPacket> EncodeFrame( |
56 VideoEncoder* encoder, | 67 VideoEncoder* encoder, |
57 std::unique_ptr<webrtc::DesktopFrame> frame, | 68 std::unique_ptr<webrtc::DesktopFrame> frame, |
58 bool key_frame_request); | 69 uint32_t target_bitrate, |
| 70 int64_t frame_duration_us, |
| 71 bool key_frame_request, |
| 72 int64_t capture_time_ms); |
59 void OnFrameEncoded(std::unique_ptr<VideoPacket> packet); | 73 void OnFrameEncoded(std::unique_ptr<VideoPacket> packet); |
60 | 74 |
61 void SetKeyFrameRequest(); | 75 void SetKeyFrameRequest(); |
62 bool ClearAndGetKeyFrameRequest(); | 76 bool ClearAndGetKeyFrameRequest(); |
| 77 void SetTargetBitrate(uint32_t bitrate); |
63 | 78 |
64 // Protects |key_frame_request_|. | 79 // Protects |key_frame_request_| and |target_bitrate_kbps_|. |
65 base::Lock lock_; | 80 base::Lock lock_; |
66 bool key_frame_request_ = false; | 81 bool key_frame_request_ = false; |
| 82 uint32_t target_bitrate_kbps_ = kDefaultTargetBitrateKbps; |
| 83 int last_quantizer_ = kMaxQuantizer; |
67 | 84 |
68 bool capture_pending_ = false; | 85 bool capture_pending_ = false; |
69 bool encode_pending_ = false; | 86 bool encode_pending_ = false; |
70 | 87 |
| 88 // Accumulated frame duration. |
| 89 media::FeedbackSignalAccumulator<base::TimeTicks> acc_frame_duration_; |
| 90 // Last time capture was completed. |
| 91 base::TimeTicks last_capture_completed_ticks_; |
| 92 // Last time capture was started. |
| 93 base::TimeTicks last_capture_started_ticks_; |
| 94 |
71 webrtc::DesktopSize frame_size_; | 95 webrtc::DesktopSize frame_size_; |
72 webrtc::DesktopVector frame_dpi_; | 96 webrtc::DesktopVector frame_dpi_; |
73 VideoStream::SizeCallback size_callback_; | 97 VideoStream::SizeCallback size_callback_; |
74 | 98 |
75 // Task runner used to run |encoder_|. | 99 // Task runner used to run |encoder_|. |
76 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; | 100 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; |
77 base::CancelableTaskTracker task_tracker_; | 101 base::CancelableTaskTracker task_tracker_; |
78 | 102 |
79 // Capturer used to capture the screen. | 103 // Capturer used to capture the screen. |
80 std::unique_ptr<webrtc::DesktopCapturer> capturer_; | 104 std::unique_ptr<webrtc::DesktopCapturer> capturer_; |
81 | 105 |
82 std::unique_ptr<base::RepeatingTimer> capture_timer_; | 106 std::unique_ptr<base::RepeatingTimer> capture_timer_; |
83 | 107 |
84 // Used to send across encoded frames. | 108 // Used to send across encoded frames. |
85 WebrtcTransport* webrtc_transport_; | 109 WebrtcTransport* webrtc_transport_; |
86 | 110 |
87 // Used to encode captured frames. Always accessed on the encode thread. | 111 // Used to encode captured frames. Always accessed on the encode thread. |
88 std::unique_ptr<VideoEncoder> encoder_; | 112 std::unique_ptr<VideoEncoder> encoder_; |
89 | 113 |
90 base::ThreadChecker thread_checker_; | 114 base::ThreadChecker thread_checker_; |
91 base::TimeTicks last_capture_ticks_; | |
92 | 115 |
93 base::WeakPtrFactory<WebRtcFrameScheduler> weak_factory_; | 116 base::WeakPtrFactory<WebRtcFrameScheduler> weak_factory_; |
94 | 117 |
95 DISALLOW_COPY_AND_ASSIGN(WebRtcFrameScheduler); | 118 DISALLOW_COPY_AND_ASSIGN(WebRtcFrameScheduler); |
96 }; | 119 }; |
97 | 120 |
98 } // namespace protocol | 121 } // namespace protocol |
99 } // namespace remoting | 122 } // namespace remoting |
100 | 123 |
101 #endif // REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ | 124 #endif // REMOTING_PROTOCOL_WEBRTC_FRAME_SCHEDULER_H_ |
OLD | NEW |