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 "base/compiler_specific.h"
9 #include "base/memory/ref_counted.h"
10 #include "content/common/content_export.h"
11 #include "content/renderer/media/media_stream_dependency_factory.h"
12 #include "content/renderer/media/media_stream_source.h"
13 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
14 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
23 class MediaStreamDependencyFactory;
25 // MediaStreamVideoSource is an interface used for sending video frames to a
26 // MediaStreamVideoTrack.
27 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html
28 // All methods calls will be done from the main render thread.
29 class CONTENT_EXPORT MediaStreamVideoSource
30 : public MediaStreamSource,
31 NON_EXPORTED_BASE(public webrtc::ObserverInterface),
32 NON_EXPORTED_BASE(public base::NonThreadSafe) {
34 explicit MediaStreamVideoSource(
35 MediaStreamDependencyFactory* factory);
38 // Puts |track| in the registered tracks list.
39 virtual void AddTrack(const blink::WebMediaStreamTrack& track,
40 const blink::WebMediaConstraints& constraints,
41 const ConstraintsCallback& callback) OVERRIDE;
42 virtual void RemoveTrack(const blink::WebMediaStreamTrack& track) OVERRIDE;
44 // TODO(ronghuawu): Remove webrtc::VideoSourceInterface from the public
45 // interface of this class.
46 webrtc::VideoSourceInterface* GetAdapter() {
51 virtual void DoStopSource() OVERRIDE {}
53 // Called when the first track is added to this source.
54 // It currently creates a webrtc::VideoSourceInterface.
55 // If a derived class overrides this method, it must call SetAdapter.
56 virtual void InitAdapter(const blink::WebMediaConstraints& constraints);
58 // Set the webrtc::VideoSourceInterface adapter used by this class.
59 // It must be called by a derived class that overrides the InitAdapter method.
60 void SetAdapter(webrtc::VideoSourceInterface* adapter) {
65 MediaStreamDependencyFactory* factory() { return factory_; }
67 // Sets ready state and notifies the ready state to all registered tracks.
68 virtual void SetReadyState(blink::WebMediaStreamSource::ReadyState state);
70 // Delivers |frame| to registered tracks according to their constraints.
71 // Note: current implementation assumes |frame| be contiguous layout of image
73 virtual void DeliverVideoFrame(const scoped_refptr<media::VideoFrame>& frame);
75 // Implements webrtc::Observer.
76 virtual void OnChanged() OVERRIDE;
78 virtual ~MediaStreamVideoSource();
81 // Checks if the underlying source state has changed from an initializing
82 // state to a final state and in that case trigger all callbacks in
83 // |constraints_callbacks_|.
84 void TriggerConstraintsCallbackOnStateChange();
87 MediaStreamDependencyFactory* factory_;
88 scoped_refptr<webrtc::VideoSourceInterface> adapter_;
91 base::TimeDelta first_frame_timestamp_;
93 blink::WebMediaConstraints current_constraints_;
94 std::vector<ConstraintsCallback> constraints_callbacks_;
96 DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoSource);
99 } // namespace content
101 #endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_SOURCE_H_