cbf8c826460d4db933f61945d3b310e338ab2d10
[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 <string>
9 #include <vector>
10
11 #include "base/compiler_specific.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "content/common/content_export.h"
16 #include "content/common/media/video_capture.h"
17 #include "content/public/renderer/media_stream_video_sink.h"
18 #include "content/renderer/media/media_stream_source.h"
19 #include "media/base/video_frame.h"
20 #include "media/video/capture/video_capture_types.h"
21 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
22 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
23 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
24
25 namespace content {
26
27 class MediaStreamVideoTrack;
28 class VideoTrackAdapter;
29
30 // MediaStreamVideoSource is an interface used for sending video frames to a
31 // MediaStreamVideoTrack.
32 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html
33 // The purpose of this base class is to be able to implement different
34 // MediaStreaVideoSources such as local video capture, video sources received
35 // on a PeerConnection or a source created in NaCl.
36 // All methods calls will be done from the main render thread.
37 //
38 // When the first track is added to the source by calling AddTrack, the
39 // MediaStreamVideoSource implementation calls GetCurrentSupportedFormats.
40 // The source implementation must call OnSupportedFormats.
41 // MediaStreamVideoSource then match the constraints provided in AddTrack with
42 // the formats and call StartSourceImpl. The source implementation must call
43 // OnStartDone when the underlying source has been started or failed to start.
44 class CONTENT_EXPORT MediaStreamVideoSource
45     : public MediaStreamSource,
46       NON_EXPORTED_BASE(public base::NonThreadSafe) {
47  public:
48   MediaStreamVideoSource();
49   virtual ~MediaStreamVideoSource();
50
51   // Returns the MediaStreamVideoSource object owned by |source|.
52   static MediaStreamVideoSource* GetVideoSource(
53       const blink::WebMediaStreamSource& source);
54
55   // Puts |track| in the registered tracks list.
56   void AddTrack(MediaStreamVideoTrack* track,
57                 const VideoCaptureDeliverFrameCB& frame_callback,
58                 const blink::WebMediaConstraints& constraints,
59                 const ConstraintsCallback& callback);
60   void RemoveTrack(MediaStreamVideoTrack* track);
61
62   // Return true if |name| is a constraint supported by MediaStreamVideoSource.
63   static bool IsConstraintSupported(const std::string& name);
64
65   // Returns the MessageLoopProxy where video frames will be delivered on.
66   const scoped_refptr<base::MessageLoopProxy>& io_message_loop() const;
67
68   // Constraint keys used by a video source.
69   // Specified by draft-alvestrand-constraints-resolution-00b
70   static const char kMinAspectRatio[];  // minAspectRatio
71   static const char kMaxAspectRatio[];  // maxAspectRatio
72   static const char kMaxWidth[];  // maxWidth
73   static const char kMinWidth[];  // minWidthOnCaptureFormats
74   static const char kMaxHeight[];  // maxHeight
75   static const char kMinHeight[];  // minHeight
76   static const char kMaxFrameRate[];  // maxFrameRate
77   static const char kMinFrameRate[];  // minFrameRate
78
79   // Default resolution. If no constraints are specified and the delegate
80   // support it, this is the resolution that will be used.
81   static const int kDefaultWidth;
82   static const int kDefaultHeight;
83   static const int kDefaultFrameRate;
84
85  protected:
86   virtual void DoStopSource() OVERRIDE;
87
88   // Sets ready state and notifies the ready state to all registered tracks.
89   virtual void SetReadyState(blink::WebMediaStreamSource::ReadyState state);
90
91   // Sets muted state and notifies it to all registered tracks.
92   virtual void SetMutedState(bool state);
93
94   // An implementation must fetch the formats that can currently be used by
95   // the source and call OnSupportedFormats when done.
96   // |max_requested_height| and |max_requested_width| is the max height and
97   // width set as a mandatory constraint if set when calling
98   // MediaStreamVideoSource::AddTrack. If max height and max width is not set
99   // |max_requested_height| and |max_requested_width| are 0.
100   virtual void GetCurrentSupportedFormats(
101       int max_requested_width,
102       int max_requested_height,
103       double max_requested_frame_rate,
104       const VideoCaptureDeviceFormatsCB& callback) = 0;
105
106   // An implementation must start capture frames using the resolution in
107   // |params|. When the source has started or the source failed to start
108   // OnStartDone must be called. An implementation must call
109   // invoke |frame_callback| on the IO thread with the captured frames.
110   // TODO(perkj): pass a VideoCaptureFormats instead of VideoCaptureParams for
111   // subclasses to customize.
112   virtual void StartSourceImpl(
113       const media::VideoCaptureParams& params,
114       const VideoCaptureDeliverFrameCB& frame_callback) = 0;
115   void OnStartDone(MediaStreamRequestResult result);
116
117   // An implementation must immediately stop capture video frames and must not
118   // call OnSupportedFormats after this method has been called. After this
119   // method has been called, MediaStreamVideoSource may be deleted.
120   virtual void StopSourceImpl() = 0;
121
122   enum State {
123     NEW,
124     RETRIEVING_CAPABILITIES,
125     STARTING,
126     STARTED,
127     ENDED
128   };
129   State state() const { return state_; }
130
131  private:
132   void OnSupportedFormats(const media::VideoCaptureFormats& formats);
133
134   // Finds the first constraints in |requested_constraints_| that can be
135   // fulfilled. |best_format| is set to the video resolution that can be
136   // fulfilled.
137   bool FindBestFormatWithConstraints(
138       const media::VideoCaptureFormats& formats,
139       media::VideoCaptureFormat* best_format);
140
141   // Trigger all cached callbacks from AddTrack. AddTrack is successful
142   // if the capture delegate has started and the constraints provided in
143   // AddTrack match the format that was used to start the device.
144   // Note that it must be ok to delete the MediaStreamVideoSource object
145   // in the context of the callback. If gUM fail, the implementation will
146   // simply drop the references to the blink source and track which will lead
147   // to that this object is deleted.
148   void FinalizeAddTrack();
149
150   State state_;
151   bool muted_state_;
152
153   media::VideoCaptureFormat current_format_;
154
155   struct RequestedConstraints {
156     RequestedConstraints(MediaStreamVideoTrack* track,
157                          const VideoCaptureDeliverFrameCB& frame_callback,
158                          const blink::WebMediaConstraints& constraints,
159                          const ConstraintsCallback& callback);
160     ~RequestedConstraints();
161
162     MediaStreamVideoTrack* track;
163     VideoCaptureDeliverFrameCB frame_callback;
164     blink::WebMediaConstraints constraints;
165     ConstraintsCallback callback;
166   };
167   std::vector<RequestedConstraints> requested_constraints_;
168
169   media::VideoCaptureFormats supported_formats_;
170
171   // |track_adapter_| delivers video frames to the tracks on the IO-thread.
172   scoped_refptr<VideoTrackAdapter> track_adapter_;
173
174   // Tracks that currently are connected to this source.
175   std::vector<MediaStreamVideoTrack*> tracks_;
176
177   // NOTE: Weak pointers must be invalidated before all other member variables.
178   base::WeakPtrFactory<MediaStreamVideoSource> weak_factory_;
179
180   DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource);
181 };
182
183 }  // namespace content
184
185 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_