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
3 // found in the LICENSE file.
4 
5 #ifndef MEDIA_REMOTING_FAKE_REMOTER_H_
6 #define MEDIA_REMOTING_FAKE_REMOTER_H_
7 
8 #include "media/base/decoder_buffer.h"
9 #include "media/mojo/common/mojo_data_pipe_read_write.h"
10 #include "media/mojo/mojom/remoting.mojom.h"
11 #include "mojo/public/cpp/bindings/pending_receiver.h"
12 #include "mojo/public/cpp/bindings/pending_remote.h"
13 #include "mojo/public/cpp/bindings/receiver.h"
14 #include "mojo/public/cpp/bindings/remote.h"
15 
16 namespace media {
17 namespace remoting {
18 
19 class RendererController;
20 
21 class FakeRemotingDataStreamSender : public mojom::RemotingDataStreamSender {
22  public:
23   FakeRemotingDataStreamSender(
24       mojo::PendingReceiver<mojom::RemotingDataStreamSender> receiver,
25       mojo::ScopedDataPipeConsumerHandle consumer_handle);
26   ~FakeRemotingDataStreamSender() override;
27 
send_frame_count()28   uint32_t send_frame_count() const { return send_frame_count_; }
cancel_in_flight_count()29   uint32_t cancel_in_flight_count() const { return cancel_in_flight_count_; }
30   void ResetHistory();
31   bool ValidateFrameBuffer(size_t index,
32                            size_t size,
33                            bool key_frame,
34                            int pts_ms);
35 
36  private:
37   // mojom::RemotingDataStreamSender implementation.
38   void SendFrame(uint32_t frame_size) final;
39   void CancelInFlightData() final;
40 
41   void OnFrameRead(bool success);
42 
43   mojo::Receiver<RemotingDataStreamSender> receiver_;
44   MojoDataPipeReader data_pipe_reader_;
45 
46   std::vector<uint8_t> next_frame_data_;
47   std::vector<std::vector<uint8_t>> received_frame_list;
48   uint32_t send_frame_count_;
49   uint32_t cancel_in_flight_count_;
50 
51   DISALLOW_COPY_AND_ASSIGN(FakeRemotingDataStreamSender);
52 };
53 
54 class FakeRemoter final : public mojom::Remoter {
55  public:
56   // |start_will_fail| indicates whether starting remoting will fail.
57   FakeRemoter(mojo::PendingRemote<mojom::RemotingSource> source,
58               bool start_will_fail);
59   ~FakeRemoter() override;
60 
61   // mojom::Remoter implementations.
62   void Start() override;
63   void StartDataStreams(mojo::ScopedDataPipeConsumerHandle audio_pipe,
64                         mojo::ScopedDataPipeConsumerHandle video_pipe,
65                         mojo::PendingReceiver<mojom::RemotingDataStreamSender>
66                             audio_sender_receiver,
67                         mojo::PendingReceiver<mojom::RemotingDataStreamSender>
68                             video_sender_receiver) override;
69   void Stop(mojom::RemotingStopReason reason) override;
70   void SendMessageToSink(const std::vector<uint8_t>& message) override;
71   void EstimateTransmissionCapacity(
72       mojom::Remoter::EstimateTransmissionCapacityCallback callback) override;
73 
74  private:
75   void Started();
76   void StartFailed();
77   void Stopped(mojom::RemotingStopReason reason);
78 
79   mojo::Remote<mojom::RemotingSource> source_;
80   bool start_will_fail_;
81 
82   std::unique_ptr<FakeRemotingDataStreamSender> audio_stream_sender_;
83   std::unique_ptr<FakeRemotingDataStreamSender> video_stream_sender_;
84 
85   base::WeakPtrFactory<FakeRemoter> weak_factory_{this};
86 
87   DISALLOW_COPY_AND_ASSIGN(FakeRemoter);
88 };
89 
90 class FakeRemoterFactory final : public mojom::RemoterFactory {
91  public:
92   // |start_will_fail| indicates whether starting remoting will fail.
93   explicit FakeRemoterFactory(bool start_will_fail);
94   ~FakeRemoterFactory() override;
95 
96   // mojom::RemoterFactory implementation.
97   void Create(mojo::PendingRemote<mojom::RemotingSource> source,
98               mojo::PendingReceiver<mojom::Remoter> receiver) override;
99 
100   static std::unique_ptr<RendererController> CreateController(
101       bool start_will_fail);
102 
103  private:
104   bool start_will_fail_;
105 
106   DISALLOW_COPY_AND_ASSIGN(FakeRemoterFactory);
107 };
108 
109 }  // namespace remoting
110 }  // namespace media
111 
112 #endif  // MEDIA_REMOTING_FAKE_REMOTER_H_
113