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