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