Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / media_stream_video_track.h
1 // Copyright 2013 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_TRACK_H_
6 #define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_TRACK_H_
7
8 #include <vector>
9
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/threading/thread_checker.h"
14 #include "content/common/content_export.h"
15 #include "content/public/renderer/media_stream_video_sink.h"
16 #include "content/renderer/media/media_stream_track.h"
17 #include "content/renderer/media/media_stream_video_source.h"
18
19 namespace content {
20
21 // MediaStreamVideoTrack is a video specific representation of a
22 // blink::WebMediaStreamTrack in content. It is owned by the blink object
23 // and can be retrieved from a blink object using
24 // WebMediaStreamTrack::extraData() or MediaStreamVideoTrack::GetVideoTrack.
25 class CONTENT_EXPORT MediaStreamVideoTrack : public MediaStreamTrack {
26  public:
27   // Help method to create a blink::WebMediaStreamTrack and a
28   // MediaStreamVideoTrack instance. The MediaStreamVideoTrack object is owned
29   // by the blink object in its WebMediaStreamTrack::ExtraData member.
30   // |callback| is triggered if the track is added to the source
31   // successfully and will receive video frames that match |constraints|
32   // or if the source fail to provide video frames.
33   // If |enabled| is true, sinks added to the track will
34   // receive video frames when the source deliver frames to the track.
35   static blink::WebMediaStreamTrack CreateVideoTrack(
36       MediaStreamVideoSource* source,
37       const blink::WebMediaConstraints& constraints,
38       const MediaStreamVideoSource::ConstraintsCallback& callback,
39       bool enabled);
40
41   static MediaStreamVideoTrack* GetVideoTrack(
42       const blink::WebMediaStreamTrack& track);
43
44   // Constructor for video tracks.
45   MediaStreamVideoTrack(
46       MediaStreamVideoSource* source,
47       const blink::WebMediaConstraints& constraints,
48       const MediaStreamVideoSource::ConstraintsCallback& callback,
49       bool enabled);
50   virtual ~MediaStreamVideoTrack();
51
52   virtual void SetEnabled(bool enabled) OVERRIDE;
53   virtual void Stop() OVERRIDE;
54
55   void OnReadyStateChanged(blink::WebMediaStreamSource::ReadyState state);
56
57   const blink::WebMediaConstraints& constraints() const {
58     return constraints_;
59   }
60
61  protected:
62   // Used to DCHECK that we are called on the correct thread.
63   base::ThreadChecker thread_checker_;
64
65  private:
66   // MediaStreamVideoSink is a friend to allow it to call AddSink() and
67   // RemoveSink().
68   friend class MediaStreamVideoSink;
69   FRIEND_TEST_ALL_PREFIXES(MediaStreamRemoteVideoSourceTest, StartTrack);
70   FRIEND_TEST_ALL_PREFIXES(MediaStreamRemoteVideoSourceTest, RemoteTrackStop);
71   FRIEND_TEST_ALL_PREFIXES(VideoDestinationHandlerTest, PutFrame);
72
73   // Add |sink| to receive state changes on the main render thread and video
74   // frames in the |callback| method on the IO-thread.
75   // |callback| will be reset on the render thread.
76   // These two methods are private such that no subclass can intercept and
77   // store the callback. This is important to ensure that we can release
78   // the callback on render thread without reference to it on the IO-thread.
79   void AddSink(MediaStreamVideoSink* sink,
80                const VideoCaptureDeliverFrameCB& callback);
81   void RemoveSink(MediaStreamVideoSink* sink);
82
83   std::vector<MediaStreamVideoSink*> sinks_;
84
85   // |FrameDeliverer| is an internal helper object used for delivering video
86   // frames on the IO-thread using callbacks to all registered tracks.
87   class FrameDeliverer;
88   scoped_refptr<FrameDeliverer> frame_deliverer_;
89
90   blink::WebMediaConstraints constraints_;
91
92   // Weak ref to the source this tracks is connected to.  |source_| is owned
93   // by the blink::WebMediaStreamSource and is guaranteed to outlive the
94   // track.
95   MediaStreamVideoSource* source_;
96
97   DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoTrack);
98 };
99
100 }  // namespace content
101
102 #endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_TRACK_H_