rtp_stream.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 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 COMPONENTS_MIRRORING_SERVICE_RTP_STREAM_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_RTP_STREAM_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/component_export.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/timer/timer.h"
  13. #include "media/base/audio_bus.h"
  14. #include "media/cast/cast_config.h"
  15. #include "media/cast/constants.h"
  16. #include "media/video/video_encode_accelerator.h"
  17. namespace media {
  18. class VideoFrame;
  19. namespace cast {
  20. class VideoSender;
  21. class AudioSender;
  22. } // namespace cast
  23. } // namespace media
  24. namespace mirroring {
  25. class COMPONENT_EXPORT(MIRRORING_SERVICE) RtpStreamClient {
  26. public:
  27. virtual ~RtpStreamClient() {}
  28. // Called when error happened during streaming.
  29. virtual void OnError(const std::string& message) = 0;
  30. // Request a fresh video frame from the capturer.
  31. virtual void RequestRefreshFrame() = 0;
  32. // The VEA is necessary for hardware encoding.
  33. virtual void CreateVideoEncodeAccelerator(
  34. media::cast::ReceiveVideoEncodeAcceleratorCallback callback) = 0;
  35. };
  36. // Receives video frames and submits the data to media::cast::VideoSender. It
  37. // also includes a timer to request refresh frames when the source halts (e.g.,
  38. // a screen capturer stops delivering frames because the screen is not being
  39. // updated). When a halt is detected, refresh frames will be requested at
  40. // regular intervals for a short period of time. This provides the video
  41. // encoder, downstream, several copies of the last frame so that it may clear up
  42. // lossy encoding artifacts.
  43. class COMPONENT_EXPORT(MIRRORING_SERVICE) VideoRtpStream
  44. : public base::SupportsWeakPtr<VideoRtpStream> {
  45. public:
  46. VideoRtpStream(std::unique_ptr<media::cast::VideoSender> video_sender,
  47. base::WeakPtr<RtpStreamClient> client);
  48. VideoRtpStream(const VideoRtpStream&) = delete;
  49. VideoRtpStream& operator=(const VideoRtpStream&) = delete;
  50. ~VideoRtpStream();
  51. // Called by VideoCaptureClient when a video frame is received.
  52. // |video_frame| is required to provide REFERENCE_TIME in the metadata.
  53. void InsertVideoFrame(scoped_refptr<media::VideoFrame> video_frame);
  54. void SetTargetPlayoutDelay(base::TimeDelta playout_delay);
  55. private:
  56. void OnRefreshTimerFired();
  57. const std::unique_ptr<media::cast::VideoSender> video_sender_;
  58. const base::WeakPtr<RtpStreamClient> client_;
  59. // Requests refresh frames at a constant rate while the source is paused, up
  60. // to a consecutive maximum.
  61. base::RepeatingTimer refresh_timer_;
  62. // Set to true when a request for a refresh frame has been made. This is
  63. // cleared once the next frame is received.
  64. bool expecting_a_refresh_frame_;
  65. };
  66. // Receives audio data and submits the data to media::cast::AudioSender.
  67. class COMPONENT_EXPORT(MIRRORING_SERVICE) AudioRtpStream
  68. : public base::SupportsWeakPtr<AudioRtpStream> {
  69. public:
  70. AudioRtpStream(std::unique_ptr<media::cast::AudioSender> audio_sender,
  71. base::WeakPtr<RtpStreamClient> client);
  72. AudioRtpStream(const AudioRtpStream&) = delete;
  73. AudioRtpStream& operator=(const AudioRtpStream&) = delete;
  74. ~AudioRtpStream();
  75. // Called by AudioCaptureClient when new audio data is available.
  76. void InsertAudio(std::unique_ptr<media::AudioBus> audio_bus,
  77. const base::TimeTicks& estimated_capture_time);
  78. void SetTargetPlayoutDelay(base::TimeDelta playout_delay);
  79. private:
  80. const std::unique_ptr<media::cast::AudioSender> audio_sender_;
  81. const base::WeakPtr<RtpStreamClient> client_;
  82. };
  83. } // namespace mirroring
  84. #endif // COMPONENTS_MIRRORING_SERVICE_RTP_STREAM_H_