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
19namespace 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.
25class 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
54  virtual void Stop() OVERRIDE;
55
56  void OnReadyStateChanged(blink::WebMediaStreamSource::ReadyState state);
57
58  const blink::WebMediaConstraints& constraints() const {
59    return constraints_;
60  }
61
62 protected:
63  // Used to DCHECK that we are called on the correct thread.
64  base::ThreadChecker thread_checker_;
65
66 private:
67  // MediaStreamVideoSink is a friend to allow it to call AddSink() and
68  // RemoveSink().
69  friend class MediaStreamVideoSink;
70  FRIEND_TEST_ALL_PREFIXES(MediaStreamRemoteVideoSourceTest, StartTrack);
71  FRIEND_TEST_ALL_PREFIXES(MediaStreamRemoteVideoSourceTest, RemoteTrackStop);
72  FRIEND_TEST_ALL_PREFIXES(VideoDestinationHandlerTest, PutFrame);
73
74  // Add |sink| to receive state changes on the main render thread and video
75  // frames in the |callback| method on the IO-thread.
76  // |callback| will be reset on the render thread.
77  // These two methods are private such that no subclass can intercept and
78  // store the callback. This is important to ensure that we can release
79  // the callback on render thread without reference to it on the IO-thread.
80  void AddSink(MediaStreamVideoSink* sink,
81               const VideoCaptureDeliverFrameCB& callback);
82  void RemoveSink(MediaStreamVideoSink* sink);
83
84  std::vector<MediaStreamVideoSink*> sinks_;
85
86  // |FrameDeliverer| is an internal helper object used for delivering video
87  // frames on the IO-thread using callbacks to all registered tracks.
88  class FrameDeliverer;
89  scoped_refptr<FrameDeliverer> frame_deliverer_;
90
91  blink::WebMediaConstraints constraints_;
92
93  // Weak ref to the source this tracks is connected to.  |source_| is owned
94  // by the blink::WebMediaStreamSource and is guaranteed to outlive the
95  // track.
96  MediaStreamVideoSource* source_;
97
98  DISALLOW_COPY_AND_ASSIGN(MediaStreamVideoTrack);
99};
100
101}  // namespace content
102
103#endif  // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_VIDEO_TRACK_H_
104