1 // Copyright 2014 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 THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MOCK_MEDIA_STREAM_VIDEO_SOURCE_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MOCK_MEDIA_STREAM_VIDEO_SOURCE_H_
7 
8 #include "third_party/blink/public/web/modules/mediastream/media_stream_video_source.h"
9 
10 #include "base/macros.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 
13 namespace blink {
14 
15 class MockMediaStreamVideoSource : public blink::MediaStreamVideoSource {
16  public:
17   MockMediaStreamVideoSource();
18   explicit MockMediaStreamVideoSource(bool respond_to_request_refresh_frame);
19   MockMediaStreamVideoSource(const media::VideoCaptureFormat& format,
20                              bool respond_to_request_refresh_frame);
21   ~MockMediaStreamVideoSource() override;
22 
23   MOCK_METHOD1(DoSetMutedState, void(bool muted_state));
24 
25   MOCK_METHOD0(OnEncodedSinkEnabled, void());
26   MOCK_METHOD0(OnEncodedSinkDisabled, void());
27   MOCK_METHOD0(OnRequestRefreshFrame, void());
28   MOCK_METHOD1(OnCapturingLinkSecured, void(bool));
29   MOCK_CONST_METHOD0(SupportsEncodedOutput, bool());
30 
31   // Simulate that the underlying source start successfully.
32   void StartMockedSource();
33 
34   // Simulate that the underlying source fail to start.
35   void FailToStartMockedSource();
36 
37   // Returns true if StartSource  has been called and StartMockedSource
38   // or FailToStartMockedSource has not been called.
SourceHasAttemptedToStart()39   bool SourceHasAttemptedToStart() { return attempted_to_start_; }
40 
41   // Delivers |frame| to all registered tracks on the IO thread. Its up to the
42   // caller to make sure MockMediaStreamVideoSource is not destroyed before the
43   // frame has been delivered.
44   void DeliverVideoFrame(scoped_refptr<media::VideoFrame> frame);
45 
46   // Delivers |frame| to all registered encoded sinks on the IO thread. Its up
47   // to the caller to make sure MockMediaStreamVideoSource is not destroyed
48   // before the frame has been delivered.
49   void DeliverEncodedVideoFrame(scoped_refptr<EncodedVideoFrame> frame);
50 
start_format()51   const media::VideoCaptureFormat& start_format() const { return format_; }
max_requested_height()52   int max_requested_height() const { return max_requested_height_; }
max_requested_width()53   int max_requested_width() const { return max_requested_width_; }
max_requested_frame_rate()54   double max_requested_frame_rate() const { return max_requested_frame_rate_; }
55 
SetMutedState(bool muted_state)56   void SetMutedState(bool muted_state) override {
57     blink::MediaStreamVideoSource::SetMutedState(muted_state);
58     DoSetMutedState(muted_state);
59   }
60 
EnableStopForRestart()61   void EnableStopForRestart() { can_stop_for_restart_ = true; }
DisableStopForRestart()62   void DisableStopForRestart() { can_stop_for_restart_ = false; }
63 
EnableRestart()64   void EnableRestart() { can_restart_ = true; }
DisableRestart()65   void DisableRestart() { can_restart_ = false; }
66 
is_suspended()67   bool is_suspended() { return is_suspended_; }
68 
69   // Implements blink::MediaStreamVideoSource.
70   void RequestRefreshFrame() override;
71   base::Optional<media::VideoCaptureParams> GetCurrentCaptureParams()
72       const override;
73   void OnHasConsumers(bool has_consumers) override;
74 
75  protected:
76   // Implements MediaStreamSource.
77   void DoChangeSource(const blink::MediaStreamDevice& new_device) override;
78 
79   // Implements blink::MediaStreamVideoSource.
80   void StartSourceImpl(VideoCaptureDeliverFrameCB frame_callback,
81                        EncodedVideoFrameCB encoded_frame_callback) override;
82   void StopSourceImpl() override;
83   base::Optional<media::VideoCaptureFormat> GetCurrentFormat() const override;
84   void StopSourceForRestartImpl() override;
85   void RestartSourceImpl(const media::VideoCaptureFormat& new_format) override;
86 
87  private:
88   media::VideoCaptureFormat format_;
89   bool respond_to_request_refresh_frame_;
90   int max_requested_height_;
91   int max_requested_width_;
92   double max_requested_frame_rate_;
93   bool attempted_to_start_;
94   bool is_stopped_for_restart_ = false;
95   bool can_stop_for_restart_ = true;
96   bool can_restart_ = true;
97   bool is_suspended_ = false;
98   blink::VideoCaptureDeliverFrameCB frame_callback_;
99   EncodedVideoFrameCB encoded_frame_callback_;
100 
101   DISALLOW_COPY_AND_ASSIGN(MockMediaStreamVideoSource);
102 };
103 
104 }  // namespace blink
105 
106 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_MEDIASTREAM_MOCK_MEDIA_STREAM_VIDEO_SOURCE_H_
107