OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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_VIDEO_STREAM_H_ | 5 #ifndef REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_ |
6 #define REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_ | 6 #define REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
18 #include "remoting/codec/video_encoder.h" | 18 #include "remoting/codec/video_encoder.h" |
| 19 #include "remoting/protocol/host_video_stats_dispatcher.h" |
19 #include "remoting/protocol/video_stream.h" | 20 #include "remoting/protocol/video_stream.h" |
20 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" | 21 #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h" |
21 | 22 |
22 namespace webrtc { | 23 namespace webrtc { |
23 class MediaStreamInterface; | 24 class MediaStreamInterface; |
24 class PeerConnectionInterface; | 25 class PeerConnectionInterface; |
25 class PeerConnectionFactoryInterface; | 26 class PeerConnectionFactoryInterface; |
26 class VideoTrackInterface; | 27 class VideoTrackInterface; |
27 } // namespace webrtc | 28 } // namespace webrtc |
28 | 29 |
29 namespace remoting { | 30 namespace remoting { |
30 namespace protocol { | 31 namespace protocol { |
31 | 32 |
| 33 class HostVideoStatsDispatcher; |
32 class WebrtcVideoCapturerAdapter; | 34 class WebrtcVideoCapturerAdapter; |
33 class WebrtcTransport; | 35 class WebrtcTransport; |
34 | 36 |
35 class WebrtcVideoStream : public VideoStream, | 37 class WebrtcVideoStream : public VideoStream, |
36 public webrtc::DesktopCapturer::Callback { | 38 public webrtc::DesktopCapturer::Callback, |
| 39 public HostVideoStatsDispatcher::EventHandler { |
37 public: | 40 public: |
38 WebrtcVideoStream(); | 41 WebrtcVideoStream(); |
39 ~WebrtcVideoStream() override; | 42 ~WebrtcVideoStream() override; |
40 | 43 |
41 bool Start( | 44 bool Start( |
42 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer, | 45 std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer, |
43 WebrtcTransport* webrtc_transport, | 46 WebrtcTransport* webrtc_transport, |
44 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 47 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
45 std::unique_ptr<VideoEncoder> video_encoder); | 48 std::unique_ptr<VideoEncoder> video_encoder); |
46 | 49 |
47 // VideoStream interface. | 50 // VideoStream interface. |
48 void Pause(bool pause) override; | 51 void Pause(bool pause) override; |
49 void OnInputEventReceived(int64_t event_timestamp) override; | 52 void OnInputEventReceived(int64_t event_timestamp) override; |
50 void SetLosslessEncode(bool want_lossless) override; | 53 void SetLosslessEncode(bool want_lossless) override; |
51 void SetLosslessColor(bool want_lossless) override; | 54 void SetLosslessColor(bool want_lossless) override; |
52 void SetObserver(Observer* observer) override; | 55 void SetObserver(Observer* observer) override; |
53 | 56 |
54 private: | 57 private: |
| 58 struct FrameTimestamps; |
| 59 struct PacketWithTimestamps; |
| 60 |
55 // webrtc::DesktopCapturer::Callback interface. | 61 // webrtc::DesktopCapturer::Callback interface. |
56 void OnCaptureResult(webrtc::DesktopCapturer::Result result, | 62 void OnCaptureResult(webrtc::DesktopCapturer::Result result, |
57 std::unique_ptr<webrtc::DesktopFrame> frame) override; | 63 std::unique_ptr<webrtc::DesktopFrame> frame) override; |
58 | 64 |
| 65 // HostVideoStatsDispatcher::EventHandler interface. |
| 66 void OnChannelInitialized(ChannelDispatcherBase* channel_dispatcher) override; |
| 67 void OnChannelClosed(ChannelDispatcherBase* channel_dispatcher) override; |
| 68 |
59 // Starts |capture_timer_|. | 69 // Starts |capture_timer_|. |
60 void StartCaptureTimer(); | 70 void StartCaptureTimer(); |
61 | 71 |
62 // Called by |capture_timer_|. | 72 // Called by |capture_timer_|. |
63 void CaptureNextFrame(); | 73 void CaptureNextFrame(); |
64 | 74 |
65 void OnFrameEncoded(std::unique_ptr<VideoPacket> packet); | 75 // Task running on the encoder thread to encode the |frame|. |
| 76 static PacketWithTimestamps EncodeFrame( |
| 77 VideoEncoder* encoder, |
| 78 std::unique_ptr<webrtc::DesktopFrame> frame, |
| 79 std::unique_ptr<WebrtcVideoStream::FrameTimestamps> timestamps, |
| 80 uint32_t target_bitrate_kbps, |
| 81 bool key_frame_request); |
| 82 void OnFrameEncoded(PacketWithTimestamps packet); |
66 | 83 |
67 void SetKeyFrameRequest(); | 84 void SetKeyFrameRequest(); |
68 bool ClearAndGetKeyFrameRequest(); | 85 bool ClearAndGetKeyFrameRequest(); |
69 void SetTargetBitrate(int bitrate); | 86 void SetTargetBitrate(int bitrate); |
70 | 87 |
| 88 // Capturer used to capture the screen. |
| 89 std::unique_ptr<webrtc::DesktopCapturer> capturer_; |
| 90 // Used to send across encoded frames. |
| 91 WebrtcTransport* webrtc_transport_ = nullptr; |
| 92 // Task runner used to run |encoder_|. |
| 93 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner_; |
| 94 // Used to encode captured frames. Always accessed on the encode thread. |
| 95 std::unique_ptr<VideoEncoder> encoder_; |
| 96 |
71 scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; | 97 scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; |
72 scoped_refptr<webrtc::MediaStreamInterface> stream_; | 98 scoped_refptr<webrtc::MediaStreamInterface> stream_; |
73 | 99 |
| 100 HostVideoStatsDispatcher video_stats_dispatcher_; |
| 101 |
| 102 // Timestamps for the frame to be captured next. |
| 103 std::unique_ptr<FrameTimestamps> next_frame_timestamps_; |
| 104 |
| 105 // Timestamps for the frame that's being captured. |
| 106 std::unique_ptr<FrameTimestamps> captured_frame_timestamps_; |
| 107 |
74 bool key_frame_request_ = false; | 108 bool key_frame_request_ = false; |
75 uint32_t target_bitrate_kbps_ = 1000; // Initial bitrate. | 109 uint32_t target_bitrate_kbps_ = 1000; // Initial bitrate. |
76 | 110 |
77 bool received_first_frame_request_ = false; | 111 bool received_first_frame_request_ = false; |
78 | 112 |
79 bool capture_pending_ = false; | 113 bool capture_pending_ = false; |
80 bool encode_pending_ = false; | 114 bool encode_pending_ = false; |
81 | 115 |
82 // Last time capture was started. | 116 // Last time capture was started. |
83 base::TimeTicks last_capture_started_ticks_; | 117 base::TimeTicks last_capture_started_ticks_; |
84 | 118 |
85 webrtc::DesktopSize frame_size_; | 119 webrtc::DesktopSize frame_size_; |
86 webrtc::DesktopVector frame_dpi_; | 120 webrtc::DesktopVector frame_dpi_; |
87 Observer* observer_ = nullptr; | 121 Observer* observer_ = nullptr; |
88 | 122 |
89 // Main task runner. | 123 base::RepeatingTimer capture_timer_; |
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 | |
95 // Capturer used to capture the screen. | |
96 std::unique_ptr<webrtc::DesktopCapturer> capturer_; | |
97 | |
98 std::unique_ptr<base::RepeatingTimer> capture_timer_; | |
99 | |
100 // Used to send across encoded frames. | |
101 WebrtcTransport* webrtc_transport_ = nullptr; | |
102 | |
103 // Used to encode captured frames. Always accessed on the encode thread. | |
104 std::unique_ptr<VideoEncoder> encoder_; | |
105 | 124 |
106 base::ThreadChecker thread_checker_; | 125 base::ThreadChecker thread_checker_; |
107 | 126 |
108 base::WeakPtrFactory<WebrtcVideoStream> weak_factory_; | 127 base::WeakPtrFactory<WebrtcVideoStream> weak_factory_; |
109 | 128 |
110 DISALLOW_COPY_AND_ASSIGN(WebrtcVideoStream); | 129 DISALLOW_COPY_AND_ASSIGN(WebrtcVideoStream); |
111 }; | 130 }; |
112 | 131 |
113 } // namespace protocol | 132 } // namespace protocol |
114 } // namespace remoting | 133 } // namespace remoting |
115 | 134 |
116 #endif // REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_ | 135 #endif // REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_ |
OLD | NEW |