Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / media_stream_video_source.h
blob3e2cbff21f051d8fd8ead5e849b0bec31dad9030
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.
5 #ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_
8 #include <string>
9 #include <vector>
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_capture_types.h"
20 #include "media/base/video_frame.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"
25 namespace content {
27 class MediaStreamVideoTrack;
28 class VideoTrackAdapter;
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.
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();
51 // Returns the MediaStreamVideoSource object owned by |source|.
52 static MediaStreamVideoSource* GetVideoSource(
53 const blink::WebMediaStreamSource& source);
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);
62 // Return true if |name| is a constraint supported by MediaStreamVideoSource.
63 static bool IsConstraintSupported(const std::string& name);
65 // Returns the MessageLoopProxy where video frames will be delivered on.
66 const scoped_refptr<base::MessageLoopProxy>& io_message_loop() const;
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
79 enum {
80 // Default resolution. If no constraints are specified and the delegate
81 // support it, this is the resolution that will be used.
82 kDefaultWidth = 640,
83 kDefaultHeight = 480,
85 kDefaultFrameRate = 30,
86 kUnknownFrameRate = 0,
89 protected:
90 void DoStopSource() override;
92 // Sets ready state and notifies the ready state to all registered tracks.
93 virtual void SetReadyState(blink::WebMediaStreamSource::ReadyState state);
95 // Sets muted state and notifies it to all registered tracks.
96 virtual void SetMutedState(bool state);
98 // An implementation must fetch the formats that can currently be used by
99 // the source and call OnSupportedFormats when done.
100 // |max_requested_height| and |max_requested_width| is the max height and
101 // width set as a mandatory constraint if set when calling
102 // MediaStreamVideoSource::AddTrack. If max height and max width is not set
103 // |max_requested_height| and |max_requested_width| are 0.
104 virtual void GetCurrentSupportedFormats(
105 int max_requested_width,
106 int max_requested_height,
107 double max_requested_frame_rate,
108 const VideoCaptureDeviceFormatsCB& callback) = 0;
110 // An implementation must start capture frames using the resolution in
111 // |params|. When the source has started or the source failed to start
112 // OnStartDone must be called. An implementation must call
113 // |frame_callback| on the IO thread with the captured frames.
114 virtual void StartSourceImpl(
115 const media::VideoCaptureFormat& format,
116 const VideoCaptureDeliverFrameCB& frame_callback) = 0;
117 void OnStartDone(MediaStreamRequestResult result);
119 // An implementation must immediately stop capture video frames and must not
120 // call OnSupportedFormats after this method has been called. After this
121 // method has been called, MediaStreamVideoSource may be deleted.
122 virtual void StopSourceImpl() = 0;
124 enum State {
125 NEW,
126 RETRIEVING_CAPABILITIES,
127 STARTING,
128 STARTED,
129 ENDED
131 State state() const { return state_; }
133 private:
134 void OnSupportedFormats(const media::VideoCaptureFormats& formats);
136 // Finds the first constraints in |requested_constraints_| that can be
137 // fulfilled. |best_format| is set to the video resolution that can be
138 // fulfilled.
139 bool FindBestFormatWithConstraints(
140 const media::VideoCaptureFormats& formats,
141 media::VideoCaptureFormat* best_format);
143 // Trigger all cached callbacks from AddTrack. AddTrack is successful
144 // if the capture delegate has started and the constraints provided in
145 // AddTrack match the format that was used to start the device.
146 // Note that it must be ok to delete the MediaStreamVideoSource object
147 // in the context of the callback. If gUM fail, the implementation will
148 // simply drop the references to the blink source and track which will lead
149 // to that this object is deleted.
150 void FinalizeAddTrack();
152 State state_;
154 media::VideoCaptureFormat current_format_;
156 struct RequestedConstraints {
157 RequestedConstraints(MediaStreamVideoTrack* track,
158 const VideoCaptureDeliverFrameCB& frame_callback,
159 const blink::WebMediaConstraints& constraints,
160 const ConstraintsCallback& callback);
161 ~RequestedConstraints();
163 MediaStreamVideoTrack* track;
164 VideoCaptureDeliverFrameCB frame_callback;
165 blink::WebMediaConstraints constraints;
166 ConstraintsCallback callback;
168 std::vector<RequestedConstraints> requested_constraints_;
170 media::VideoCaptureFormats supported_formats_;
172 // |track_adapter_| delivers video frames to the tracks on the IO-thread.
173 const scoped_refptr<VideoTrackAdapter> track_adapter_;
175 // Tracks that currently are connected to this source.
176 std::vector<MediaStreamVideoTrack*> tracks_;
178 // NOTE: Weak pointers must be invalidated before all other member variables.
179 base::WeakPtrFactory<MediaStreamVideoSource> weak_factory_;
181 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource);
184 } // namespace content
186 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_