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 39 // the 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 // An implementation must fetch the formats that can currently be used by 92 // the source and call OnSupportedFormats when done. 93 // |max_requested_height| and |max_requested_width| is the max height and 94 // width set as a mandatory constraint if set when calling 95 // MediaStreamVideoSource::AddTrack. If max height and max width is not set 96 // |max_requested_height| and |max_requested_width| are 0. 97 virtual void GetCurrentSupportedFormats( 98 int max_requested_width, 99 int max_requested_height, 100 const VideoCaptureDeviceFormatsCB& callback) = 0; 101 102 // An implementation must start capture frames using the resolution in 103 // |params|. When the source has started or the source failed to start 104 // OnStartDone must be called. An implementation must call 105 // invoke |frame_callback| on the IO thread with the captured frames. 106 // TODO(perkj): pass a VideoCaptureFormats instead of VideoCaptureParams for 107 // subclasses to customize. 108 virtual void StartSourceImpl( 109 const media::VideoCaptureParams& params, 110 const VideoCaptureDeliverFrameCB& frame_callback) = 0; 111 void OnStartDone(bool success); 112 113 // An implementation must immediately stop capture video frames and must not 114 // call OnSupportedFormats after this method has been called. After this 115 // method has been called, MediaStreamVideoSource may be deleted. 116 virtual void StopSourceImpl() = 0; 117 118 enum State { 119 NEW, 120 RETRIEVING_CAPABILITIES, 121 STARTING, 122 STARTED, 123 ENDED 124 }; 125 State state() const { return state_; } 126 127 private: 128 void OnSupportedFormats(const media::VideoCaptureFormats& formats); 129 130 // Finds the first constraints in |requested_constraints_| that can be 131 // fulfilled. |best_format| is set to the video resolution that can be 132 // fulfilled. 133 bool FindBestFormatWithConstraints( 134 const media::VideoCaptureFormats& formats, 135 media::VideoCaptureFormat* best_format); 136 137 // Trigger all cached callbacks from AddTrack. AddTrack is successful 138 // if the capture delegate has started and the constraints provided in 139 // AddTrack match the format that was used to start the device. 140 // Note that it must be ok to delete the MediaStreamVideoSource object 141 // in the context of the callback. If gUM fail, the implementation will 142 // simply drop the references to the blink source and track which will lead 143 // to that this object is deleted. 144 void FinalizeAddTrack(); 145 146 State state_; 147 148 media::VideoCaptureFormat current_format_; 149 150 struct RequestedConstraints { 151 RequestedConstraints(MediaStreamVideoTrack* track, 152 const VideoCaptureDeliverFrameCB& frame_callback, 153 const blink::WebMediaConstraints& constraints, 154 const ConstraintsCallback& callback); 155 ~RequestedConstraints(); 156 157 MediaStreamVideoTrack* track; 158 VideoCaptureDeliverFrameCB frame_callback; 159 blink::WebMediaConstraints constraints; 160 ConstraintsCallback callback; 161 }; 162 std::vector<RequestedConstraints> requested_constraints_; 163 164 media::VideoCaptureFormats supported_formats_; 165 166 // |track_adapter_| delivers video frames to the tracks on the IO-thread. 167 scoped_refptr<VideoTrackAdapter> track_adapter_; 168 169 // Tracks that currently are connected to this source. 170 std::vector<MediaStreamVideoTrack*> tracks_; 171 172 // NOTE: Weak pointers must be invalidated before all other member variables. 173 base::WeakPtrFactory<MediaStreamVideoSource> weak_factory_; 174 175 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource); 176 }; 177 178 } // namespace content 179 180 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_ 181