Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / media_stream_video_source.h
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 CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_
7
8 #include "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "content/common/content_export.h"
11 #include "content/renderer/media/media_stream_dependency_factory.h"
12 #include "content/renderer/media/media_stream_source.h"
13 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
16
17 namespace media {
18 class VideoFrame;
19 }
20
21 namespace content {
22
23 class MediaStreamDependencyFactory;
24
25 // MediaStreamVideoSource is an interface used for sending video frames to a
26 // MediaStreamVideoTrack.
27 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html
28 // All methods calls will be done from the main render thread.
29 class CONTENT_EXPORT MediaStreamVideoSource
30     : public MediaStreamSource,
31       NON_EXPORTED_BASE(public webrtc::ObserverInterface),
32       NON_EXPORTED_BASE(public base::NonThreadSafe) {
33  public:
34   explicit MediaStreamVideoSource(
35       MediaStreamDependencyFactory* factory);
36
37
38   // Puts |track| in the registered tracks list.
39   virtual void AddTrack(const blink::WebMediaStreamTrack& track,
40                         const blink::WebMediaConstraints& constraints,
41                         const ConstraintsCallback& callback) OVERRIDE;
42   virtual void RemoveTrack(const blink::WebMediaStreamTrack& track) OVERRIDE;
43
44   // TODO(ronghuawu): Remove webrtc::VideoSourceInterface from the public
45   // interface of this class.
46   webrtc::VideoSourceInterface* GetAdapter() {
47     return adapter_;
48   }
49
50  protected:
51   virtual void DoStopSource() OVERRIDE {}
52
53   // Called when the first track is added to this source.
54   // It currently creates a webrtc::VideoSourceInterface.
55   // If a derived class overrides this method, it must call SetAdapter.
56   virtual void InitAdapter(const blink::WebMediaConstraints& constraints);
57
58   // Set the webrtc::VideoSourceInterface adapter used by this class.
59   // It must be called by a derived class that overrides the InitAdapter method.
60   void SetAdapter(webrtc::VideoSourceInterface* adapter) {
61     DCHECK(!adapter_);
62     adapter_ = adapter;
63   }
64
65   MediaStreamDependencyFactory* factory() { return factory_; }
66
67   // Sets ready state and notifies the ready state to all registered tracks.
68   virtual void SetReadyState(blink::WebMediaStreamSource::ReadyState state);
69
70   // Delivers |frame| to registered tracks according to their constraints.
71   // Note: current implementation assumes |frame| be contiguous layout of image
72   // planes and I420.
73   virtual void DeliverVideoFrame(const scoped_refptr<media::VideoFrame>& frame);
74
75   // Implements webrtc::Observer.
76   virtual void OnChanged() OVERRIDE;
77
78   virtual ~MediaStreamVideoSource();
79
80  private:
81   // Checks if the underlying source state has changed from an initializing
82   // state to a final state and in that case trigger all callbacks in
83   // |constraints_callbacks_|.
84   void TriggerConstraintsCallbackOnStateChange();
85
86   bool initializing_;
87   MediaStreamDependencyFactory* factory_;
88   scoped_refptr<webrtc::VideoSourceInterface> adapter_;
89   int width_;
90   int height_;
91   base::TimeDelta first_frame_timestamp_;
92
93   blink::WebMediaConstraints current_constraints_;
94   std::vector<ConstraintsCallback> constraints_callbacks_;
95
96   DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource);
97 };
98
99 }  // namespace content
100
101 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_