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_
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"
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
) {
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 task runner where video frames will be delivered on.
66 base::SingleThreadTaskRunner
* io_task_runner() 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
[]; // minWidth
74 static const char kMaxHeight
[]; // maxHeight
75 static const char kMinHeight
[]; // minHeight
76 static const char kMaxFrameRate
[]; // maxFrameRate
77 static const char kMinFrameRate
[]; // minFrameRate
80 // Default resolution. If no constraints are specified and the delegate
81 // support it, this is the resolution that will be used.
85 kDefaultFrameRate
= 30,
86 kUnknownFrameRate
= 0,
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 capturing frames using the requested
111 // |format|. The fulfilled |constraints| are provided as additional context,
112 // and may be used to modify the behavior of the source. When the source has
113 // started or the source failed to start OnStartDone must be called. An
114 // implementation must call |frame_callback| on the IO thread with the
116 virtual void StartSourceImpl(
117 const media::VideoCaptureFormat
& format
,
118 const blink::WebMediaConstraints
& constraints
,
119 const VideoCaptureDeliverFrameCB
& frame_callback
) = 0;
120 void OnStartDone(MediaStreamRequestResult result
);
122 // An implementation must immediately stop capture video frames and must not
123 // call OnSupportedFormats after this method has been called. After this
124 // method has been called, MediaStreamVideoSource may be deleted.
125 virtual void StopSourceImpl() = 0;
129 RETRIEVING_CAPABILITIES
,
134 State
state() const { return state_
; }
137 void OnSupportedFormats(const media::VideoCaptureFormats
& formats
);
139 // Finds the first WebMediaConstraints in |requested_constraints_| that allows
140 // the use of one of the |formats|. |best_format| and |fulfilled_constraints|
141 // are set to the results of this search-and-match operation. Returns false
142 // if no WebMediaConstraints allow the use any of the |formats|.
143 bool FindBestFormatWithConstraints(
144 const media::VideoCaptureFormats
& formats
,
145 media::VideoCaptureFormat
* best_format
,
146 blink::WebMediaConstraints
* fulfilled_constraints
);
148 // Trigger all cached callbacks from AddTrack. AddTrack is successful
149 // if the capture delegate has started and the constraints provided in
150 // AddTrack match the format that was used to start the device.
151 // Note that it must be ok to delete the MediaStreamVideoSource object
152 // in the context of the callback. If gUM fail, the implementation will
153 // simply drop the references to the blink source and track which will lead
154 // to that this object is deleted.
155 void FinalizeAddTrack();
159 media::VideoCaptureFormat current_format_
;
161 struct RequestedConstraints
{
162 RequestedConstraints(MediaStreamVideoTrack
* track
,
163 const VideoCaptureDeliverFrameCB
& frame_callback
,
164 const blink::WebMediaConstraints
& constraints
,
165 const ConstraintsCallback
& callback
);
166 ~RequestedConstraints();
168 MediaStreamVideoTrack
* track
;
169 VideoCaptureDeliverFrameCB frame_callback
;
170 blink::WebMediaConstraints constraints
;
171 ConstraintsCallback callback
;
173 std::vector
<RequestedConstraints
> requested_constraints_
;
175 media::VideoCaptureFormats supported_formats_
;
177 // |track_adapter_| delivers video frames to the tracks on the IO-thread.
178 const scoped_refptr
<VideoTrackAdapter
> track_adapter_
;
180 // Tracks that currently are connected to this source.
181 std::vector
<MediaStreamVideoTrack
*> tracks_
;
183 // NOTE: Weak pointers must be invalidated before all other member variables.
184 base::WeakPtrFactory
<MediaStreamVideoSource
> weak_factory_
;
186 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource
);
189 } // namespace content
191 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_