webrtc_video_stream.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.
  4. #ifndef REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_
  5. #define REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "remoting/base/session_options.h"
  14. #include "remoting/protocol/video_channel_state_observer.h"
  15. #include "remoting/protocol/video_stream.h"
  16. #include "remoting/protocol/webrtc_video_track_source.h"
  17. #include "third_party/webrtc/api/rtp_transceiver_interface.h"
  18. #include "third_party/webrtc/api/scoped_refptr.h"
  19. #include "third_party/webrtc/api/video_codecs/sdp_video_format.h"
  20. #include "third_party/webrtc/modules/desktop_capture/desktop_capturer.h"
  21. namespace webrtc {
  22. class PeerConnectionInterface;
  23. } // namespace webrtc
  24. namespace remoting {
  25. namespace protocol {
  26. class HostVideoStatsDispatcher;
  27. class WebrtcVideoEncoderFactory;
  28. class WebrtcFrameScheduler;
  29. class WebrtcTransport;
  30. class WebrtcVideoStream : public VideoStream,
  31. public webrtc::DesktopCapturer::Callback,
  32. public VideoChannelStateObserver {
  33. public:
  34. WebrtcVideoStream(const std::string& stream_name,
  35. const SessionOptions& options);
  36. WebrtcVideoStream(const WebrtcVideoStream&) = delete;
  37. WebrtcVideoStream& operator=(const WebrtcVideoStream&) = delete;
  38. ~WebrtcVideoStream() override;
  39. void set_video_stats_dispatcher(
  40. base::WeakPtr<HostVideoStatsDispatcher> video_stats_dispatcher) {
  41. video_stats_dispatcher_ = video_stats_dispatcher;
  42. }
  43. void Start(std::unique_ptr<webrtc::DesktopCapturer> desktop_capturer,
  44. WebrtcTransport* webrtc_transport,
  45. WebrtcVideoEncoderFactory* video_encoder_factory);
  46. // VideoStream interface.
  47. void SetEventTimestampsSource(scoped_refptr<InputEventTimestampsSource>
  48. event_timestamps_source) override;
  49. void Pause(bool pause) override;
  50. void SetLosslessEncode(bool want_lossless) override;
  51. void SetLosslessColor(bool want_lossless) override;
  52. void SetObserver(Observer* observer) override;
  53. void SelectSource(webrtc::ScreenId id) override;
  54. // VideoChannelStateObserver interface.
  55. void OnKeyFrameRequested() override;
  56. void OnTargetBitrateChanged(int bitrate_kbps) override;
  57. void OnFrameEncoded(WebrtcVideoEncoder::EncodeResult encode_result,
  58. const WebrtcVideoEncoder::EncodedFrame* frame) override;
  59. void OnEncodedFrameSent(
  60. webrtc::EncodedImageCallback::Result result,
  61. const WebrtcVideoEncoder::EncodedFrame& frame) override;
  62. private:
  63. struct FrameStats;
  64. // webrtc::DesktopCapturer::Callback interface.
  65. void OnCaptureResult(webrtc::DesktopCapturer::Result result,
  66. std::unique_ptr<webrtc::DesktopFrame> frame) override;
  67. // Called by the |scheduler_|.
  68. void CaptureNextFrame();
  69. // Called by |video_track_source_|.
  70. void OnSinkAddedOrUpdated(const rtc::VideoSinkWants& wants);
  71. // Screen ID of the monitor being captured, from SelectSource().
  72. webrtc::ScreenId screen_id_ = webrtc::kInvalidScreenId;
  73. // Label of the associated WebRTC video-stream.
  74. std::string stream_name_;
  75. // Capturer used to capture the screen.
  76. std::unique_ptr<webrtc::DesktopCapturer> capturer_;
  77. // Used to send captured frames to the encoder.
  78. rtc::scoped_refptr<WebrtcVideoTrackSource> video_track_source_;
  79. // The transceiver created for this video-stream.
  80. rtc::scoped_refptr<webrtc::RtpTransceiverInterface> transceiver_;
  81. scoped_refptr<InputEventTimestampsSource> event_timestamps_source_;
  82. scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
  83. base::WeakPtr<HostVideoStatsDispatcher> video_stats_dispatcher_;
  84. // Stats of the frame that's being captured.
  85. std::unique_ptr<FrameStats> current_frame_stats_;
  86. std::unique_ptr<WebrtcFrameScheduler> scheduler_;
  87. webrtc::DesktopSize frame_size_;
  88. webrtc::DesktopVector frame_dpi_;
  89. raw_ptr<Observer> observer_ = nullptr;
  90. base::ThreadChecker thread_checker_;
  91. const SessionOptions session_options_;
  92. base::WeakPtrFactory<WebrtcVideoStream> weak_factory_{this};
  93. };
  94. } // namespace protocol
  95. } // namespace remoting
  96. #endif // REMOTING_PROTOCOL_WEBRTC_VIDEO_STREAM_H_