Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / mock_media_stream_dependency_factory.h
1 // Copyright (c) 2012 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 CONTENT_RENDERER_MEDIA_MOCK_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
6 #define CONTENT_RENDERER_MEDIA_MOCK_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "content/renderer/media/media_stream_dependency_factory.h"
13 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
14 #include "third_party/libjingle/source/talk/media/base/videorenderer.h"
15
16 namespace content {
17
18 class WebAudioCapturerSource;
19
20 class MockVideoRenderer : public cricket::VideoRenderer {
21  public:
22   MockVideoRenderer();
23   virtual ~MockVideoRenderer();
24   virtual bool SetSize(int width, int height, int reserved) OVERRIDE;
25   virtual bool RenderFrame(const cricket::VideoFrame* frame) OVERRIDE;
26
27   int width() const { return width_; }
28   int height() const { return height_; }
29   int num() const { return num_; }
30
31  private:
32   int width_;
33   int height_;
34   int num_;
35 };
36
37 class MockVideoSource : public webrtc::VideoSourceInterface {
38  public:
39   MockVideoSource();
40
41   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
42   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
43   virtual MediaSourceInterface::SourceState state() const OVERRIDE;
44   virtual cricket::VideoCapturer* GetVideoCapturer() OVERRIDE;
45   virtual void AddSink(cricket::VideoRenderer* output) OVERRIDE;
46   virtual void RemoveSink(cricket::VideoRenderer* output) OVERRIDE;
47   virtual cricket::VideoRenderer* FrameInput() OVERRIDE;
48   virtual const cricket::VideoOptions* options() const OVERRIDE;
49
50   // Changes the state of the source to live and notifies the observer.
51   void SetLive();
52   // Changes the state of the source to ended and notifies the observer.
53   void SetEnded();
54   // Set the video capturer.
55   void SetVideoCapturer(cricket::VideoCapturer* capturer);
56
57   // Test helpers.
58   int GetLastFrameWidth() const;
59   int GetLastFrameHeight() const;
60   int GetFrameNum() const;
61
62  protected:
63   virtual ~MockVideoSource();
64
65  private:
66   void FireOnChanged();
67
68   std::vector<webrtc::ObserverInterface*> observers_;
69   MediaSourceInterface::SourceState state_;
70   scoped_ptr<cricket::VideoCapturer> capturer_;
71   MockVideoRenderer renderer_;
72 };
73
74 class MockAudioSource : public webrtc::AudioSourceInterface {
75  public:
76   explicit MockAudioSource(
77       const webrtc::MediaConstraintsInterface* constraints);
78
79   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
80   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
81   virtual MediaSourceInterface::SourceState state() const OVERRIDE;
82
83   // Changes the state of the source to live and notifies the observer.
84   void SetLive();
85   // Changes the state of the source to ended and notifies the observer.
86   void SetEnded();
87
88   const webrtc::MediaConstraintsInterface::Constraints& optional_constraints() {
89     return optional_constraints_;
90   }
91
92   const webrtc::MediaConstraintsInterface::Constraints&
93   mandatory_constraints() {
94     return mandatory_constraints_;
95   }
96
97  protected:
98   virtual ~MockAudioSource();
99
100  private:
101   webrtc::ObserverInterface* observer_;
102   MediaSourceInterface::SourceState state_;
103   webrtc::MediaConstraintsInterface::Constraints optional_constraints_;
104   webrtc::MediaConstraintsInterface::Constraints mandatory_constraints_;
105 };
106
107 class MockWebRtcVideoTrack : public webrtc::VideoTrackInterface {
108  public:
109   MockWebRtcVideoTrack(const std::string& id,
110                       webrtc::VideoSourceInterface* source);
111   virtual void AddRenderer(webrtc::VideoRendererInterface* renderer) OVERRIDE;
112   virtual void RemoveRenderer(
113       webrtc::VideoRendererInterface* renderer) OVERRIDE;
114   virtual std::string kind() const OVERRIDE;
115   virtual std::string id() const OVERRIDE;
116   virtual bool enabled() const OVERRIDE;
117   virtual TrackState state() const OVERRIDE;
118   virtual bool set_enabled(bool enable) OVERRIDE;
119   virtual bool set_state(TrackState new_state) OVERRIDE;
120   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
121   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
122   virtual webrtc::VideoSourceInterface* GetSource() const OVERRIDE;
123
124  protected:
125   virtual ~MockWebRtcVideoTrack();
126
127  private:
128   bool enabled_;
129   std::string id_;
130   TrackState state_;
131   scoped_refptr<webrtc::VideoSourceInterface> source_;
132   webrtc::ObserverInterface* observer_;
133   webrtc::VideoRendererInterface* renderer_;
134 };
135
136 class MockMediaStream : public webrtc::MediaStreamInterface {
137  public:
138   explicit MockMediaStream(const std::string& label);
139
140   virtual bool AddTrack(webrtc::AudioTrackInterface* track) OVERRIDE;
141   virtual bool AddTrack(webrtc::VideoTrackInterface* track) OVERRIDE;
142   virtual bool RemoveTrack(webrtc::AudioTrackInterface* track) OVERRIDE;
143   virtual bool RemoveTrack(webrtc::VideoTrackInterface* track) OVERRIDE;
144   virtual std::string label() const OVERRIDE;
145   virtual webrtc::AudioTrackVector GetAudioTracks() OVERRIDE;
146   virtual webrtc::VideoTrackVector GetVideoTracks() OVERRIDE;
147   virtual talk_base::scoped_refptr<webrtc::AudioTrackInterface>
148       FindAudioTrack(const std::string& track_id) OVERRIDE;
149   virtual talk_base::scoped_refptr<webrtc::VideoTrackInterface>
150       FindVideoTrack(const std::string& track_id) OVERRIDE;
151   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
152   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
153
154  protected:
155   virtual ~MockMediaStream();
156
157  private:
158   void NotifyObservers();
159
160   std::string label_;
161   webrtc::AudioTrackVector audio_track_vector_;
162   webrtc::VideoTrackVector video_track_vector_;
163
164   typedef std::set<webrtc::ObserverInterface*> ObserverSet;
165   ObserverSet observers_;
166 };
167
168 // A mock factory for creating different objects for
169 // RTC MediaStreams and PeerConnections.
170 class MockMediaStreamDependencyFactory : public MediaStreamDependencyFactory {
171  public:
172   MockMediaStreamDependencyFactory();
173   virtual ~MockMediaStreamDependencyFactory();
174
175   virtual scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
176       const webrtc::PeerConnectionInterface::IceServers& ice_servers,
177       const webrtc::MediaConstraintsInterface* constraints,
178       blink::WebFrame* frame,
179       webrtc::PeerConnectionObserver* observer) OVERRIDE;
180   virtual scoped_refptr<webrtc::AudioSourceInterface>
181       CreateLocalAudioSource(
182           const webrtc::MediaConstraintsInterface* constraints) OVERRIDE;
183   virtual WebRtcVideoCapturerAdapter* CreateVideoCapturer(
184       bool is_screen_capture) OVERRIDE;
185   virtual scoped_refptr<webrtc::VideoSourceInterface>
186       CreateVideoSource(
187           cricket::VideoCapturer* capturer,
188           const blink::WebMediaConstraints& constraints) OVERRIDE;
189   virtual scoped_refptr<WebAudioCapturerSource> CreateWebAudioSource(
190       blink::WebMediaStreamSource* source) OVERRIDE;
191   virtual scoped_refptr<webrtc::MediaStreamInterface>
192       CreateLocalMediaStream(const std::string& label) OVERRIDE;
193   virtual scoped_refptr<webrtc::VideoTrackInterface>
194       CreateLocalVideoTrack(const std::string& id,
195                             webrtc::VideoSourceInterface* source) OVERRIDE;
196   virtual scoped_refptr<webrtc::VideoTrackInterface>
197       CreateLocalVideoTrack(const std::string& id,
198                             cricket::VideoCapturer* capturer) OVERRIDE;
199   virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
200       const std::string& type,
201       const std::string& sdp,
202       webrtc::SdpParseError* error) OVERRIDE;
203   virtual webrtc::IceCandidateInterface* CreateIceCandidate(
204       const std::string& sdp_mid,
205       int sdp_mline_index,
206       const std::string& sdp) OVERRIDE;
207
208   virtual scoped_refptr<WebRtcAudioCapturer> CreateAudioCapturer(
209       int render_view_id, const StreamDeviceInfo& device_info,
210       const blink::WebMediaConstraints& constraints,
211       MediaStreamAudioSource* audio_source) OVERRIDE;
212   void FailToCreateNextAudioCapturer() {
213     fail_to_create_next_audio_capturer_ = true;
214   }
215
216   virtual void StartLocalAudioTrack(
217       WebRtcLocalAudioTrack* audio_track) OVERRIDE;
218
219   MockAudioSource* last_audio_source() { return last_audio_source_.get(); }
220   MockVideoSource* last_video_source() { return last_video_source_.get(); }
221
222  private:
223   bool fail_to_create_next_audio_capturer_;
224   scoped_refptr <MockAudioSource> last_audio_source_;
225   scoped_refptr <MockVideoSource> last_video_source_;
226
227   DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDependencyFactory);
228 };
229
230 }  // namespace content
231
232 #endif  // CONTENT_RENDERER_MEDIA_MOCK_MEDIA_STREAM_DEPENDENCY_FACTORY_H_