remoting_sender.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_REMOTING_SENDER_H_
  5. #define COMPONENTS_MIRRORING_SERVICE_REMOTING_SENDER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/component_export.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/sequence_checker.h"
  13. #include "media/cast/sender/frame_sender.h"
  14. #include "media/mojo/mojom/remoting.mojom.h"
  15. #include "mojo/public/cpp/bindings/pending_receiver.h"
  16. #include "mojo/public/cpp/bindings/receiver.h"
  17. namespace base {
  18. class TickClock;
  19. } // namespace base
  20. namespace media {
  21. class MojoDataPipeReader;
  22. } // namespace media
  23. namespace openscreen::cast {
  24. class Sender;
  25. } // namespace openscreen::cast
  26. namespace mirroring {
  27. // RTP sender for a single Cast Remoting RTP stream. The client calls Send() to
  28. // instruct the sender to read from a Mojo data pipe and transmit the data using
  29. // a CastTransport.
  30. class COMPONENT_EXPORT(MIRRORING_SERVICE) RemotingSender final
  31. : public media::mojom::RemotingDataStreamSender,
  32. public media::cast::FrameSender::Client {
  33. public:
  34. // Old way of instantiating using a cast transport. |transport| is expected to
  35. // outlive this class.
  36. // TODO(https://crbug.com/1316434): should be removed once libcast sender is
  37. // successfully launched.
  38. RemotingSender(scoped_refptr<media::cast::CastEnvironment> cast_environment,
  39. media::cast::CastTransport* transport,
  40. const media::cast::FrameSenderConfig& config,
  41. mojo::ScopedDataPipeConsumerHandle pipe,
  42. mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
  43. stream_sender,
  44. base::OnceClosure error_callback);
  45. // New way of instantiating using an openscreen::cast::Sender. Since the
  46. // |Sender| instance is destroyed when renegotiation is complete, |this|
  47. // is also invalid and should be immediately torn down.
  48. RemotingSender(scoped_refptr<media::cast::CastEnvironment> cast_environment,
  49. openscreen::cast::Sender* sender,
  50. const media::cast::FrameSenderConfig& config,
  51. mojo::ScopedDataPipeConsumerHandle pipe,
  52. mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
  53. stream_sender,
  54. base::OnceClosure error_callback);
  55. RemotingSender(const RemotingSender&) = delete;
  56. RemotingSender& operator=(const RemotingSender&) = delete;
  57. ~RemotingSender() override;
  58. private:
  59. RemotingSender(scoped_refptr<media::cast::CastEnvironment> cast_environment,
  60. std::unique_ptr<media::cast::FrameSender> sender,
  61. const media::cast::FrameSenderConfig& config,
  62. mojo::ScopedDataPipeConsumerHandle pipe,
  63. mojo::PendingReceiver<media::mojom::RemotingDataStreamSender>
  64. stream_sender,
  65. base::OnceClosure error_callback);
  66. // Friend class for unit tests.
  67. friend class RemotingSenderTest;
  68. // media::mojom::RemotingDataStreamSender implementation. SendFrame() will
  69. // push callbacks onto the back of the input queue, and these may or may not
  70. // be processed at a later time. It depends on whether the data pipe has data
  71. // available or the CastTransport can accept more frames. CancelInFlightData()
  72. // is processed immediately, and will cause all pending operations to discard
  73. // data when they are processed later.
  74. void SendFrame(uint32_t frame_size) override;
  75. void CancelInFlightData() override;
  76. // FrameSender::Client overrides.
  77. int GetNumberOfFramesInEncoder() const override;
  78. base::TimeDelta GetEncoderBacklogDuration() const override;
  79. void OnFrameCanceled(media::cast::FrameId frame_id) override;
  80. // Attempt to run next pending input task, popping the head of the input queue
  81. // as each task succeeds.
  82. void ProcessNextInputTask();
  83. // These are called via callbacks run from the input queue.
  84. // Consumes a frame of |size| from the associated Mojo data pipe.
  85. void ReadFrame(uint32_t size);
  86. // Sends out the frame to the receiver over network.
  87. void TrySendFrame();
  88. // Called when a frame is completely read/discarded from the data pipe.
  89. void OnFrameRead(bool success);
  90. // Called when an input task completes.
  91. void OnInputTaskComplete();
  92. void OnRemotingDataStreamError();
  93. // Returns true if OnRemotingDataStreamError was called.
  94. bool HadError() const;
  95. SEQUENCE_CHECKER(sequence_checker_);
  96. // The backing frame sender implementation.
  97. std::unique_ptr<media::cast::FrameSender> frame_sender_;
  98. raw_ptr<const base::TickClock> clock_;
  99. // Callback that is run to notify when a fatal error occurs.
  100. base::OnceClosure error_callback_;
  101. std::unique_ptr<media::MojoDataPipeReader> data_pipe_reader_;
  102. // Mojo receiver for this instance. Implementation at the other end of the
  103. // message pipe uses the RemotingDataStreamSender remote to control when
  104. // this RemotingSender consumes from |pipe_|.
  105. mojo::Receiver<media::mojom::RemotingDataStreamSender> stream_sender_;
  106. // The next frame's payload data. Populated by call to OnFrameRead() when
  107. // reading succeeded.
  108. std::string next_frame_data_;
  109. // Queue of pending input operations. |input_queue_discards_remaining_|
  110. // indicates the number of operations where data should be discarded (due to
  111. // CancelInFlightData()).
  112. base::queue<base::RepeatingClosure> input_queue_;
  113. size_t input_queue_discards_remaining_;
  114. // Indicates whether the |data_pipe_reader_| is processing a reading request.
  115. bool is_reading_;
  116. // Set to true if the first frame has not yet been sent, or if a
  117. // CancelInFlightData() operation just completed. This causes TrySendFrame()
  118. // to mark the next frame as the start of a new sequence.
  119. bool flow_restart_pending_;
  120. // The next frame's ID. Before any frames are sent, this will be the ID of
  121. // the first frame.
  122. media::cast::FrameId next_frame_id_ = media::cast::FrameId::first();
  123. // NOTE: Weak pointers must be invalidated before all other member variables.
  124. base::WeakPtrFactory<RemotingSender> weak_factory_{this};
  125. };
  126. } // namespace mirroring
  127. #endif // COMPONENTS_MIRRORING_SERVICE_REMOTING_SENDER_H_