summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/video_capture_impl_manager.h
blob: 9b26b2047016f0ed0d2324442c24b70d94a5651f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// VideoCaptureImplManager manages video capture devices in renderer process.
// The video capture clients use AddDevice() to get a pointer to
// video capture device. VideoCaputreImplManager supports multiple clients
// accessing same device.

#ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
#define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_

#include <list>
#include <map>

#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/threading/thread.h"
#include "base/synchronization/lock.h"
#include "content/common/content_export.h"
#include "media/video/capture/video_capture.h"
#include "media/video/encoded_video_source.h"

namespace content {

class RtcEncodingVideoCapturerFactory;
class VideoCaptureImpl;
class VideoCaptureMessageFilter;

class CONTENT_EXPORT VideoCaptureImplManager
    : public base::RefCountedThreadSafe<VideoCaptureImplManager> {
 public:
  VideoCaptureImplManager();

  // Called by video capture client |handler| to add device referenced
  // by |id| to VideoCaptureImplManager's list of opened device list.
  // A pointer to VideoCapture is returned to client so that client can
  // operate on that pointer, such as StartCaptrue, StopCapture.
  virtual media::VideoCapture* AddDevice(
      media::VideoCaptureSessionId id,
      media::VideoCapture::EventHandler* handler);

  // Called by video capture client |handler| to remove device referenced
  // by |id| from VideoCaptureImplManager's list of opened device list.
  virtual void RemoveDevice(media::VideoCaptureSessionId id,
                            media::VideoCapture::EventHandler* handler);

  // Make all existing VideoCaptureImpl instances stop/resume delivering
  // video frames to their clients, depends on flag |suspend|.
  virtual void SuspendDevices(bool suspend);

  VideoCaptureMessageFilter* video_capture_message_filter() const {
    return filter_.get();
  }

  void set_encoding_capturer_factory(base::WeakPtr<
      RtcEncodingVideoCapturerFactory> encoding_capturer_factory) {
    encoding_capturer_factory_ = encoding_capturer_factory;
  }

 protected:
  virtual ~VideoCaptureImplManager();

 private:
  friend class base::RefCountedThreadSafe<VideoCaptureImplManager>;

  struct Device {
    Device(VideoCaptureImpl* device,
           media::VideoCapture::EventHandler* handler);
    ~Device();

    VideoCaptureImpl* vc;
    std::list<media::VideoCapture::EventHandler*> clients;
  };

  void FreeDevice(VideoCaptureImpl* vc);

  typedef std::map<media::VideoCaptureSessionId, Device*> Devices;
  Devices devices_;
  base::Lock lock_;
  scoped_refptr<VideoCaptureMessageFilter> filter_;
  base::Thread thread_;
  scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;

  // The encoding capturer factory is created by MediaStreamDependencyFactory
  // and owned by its PeerConnectionFactory. It is passed to the manager
  // as a weak pointer at CreatePeerConnectionFactory time because the
  // PeerConnectionFactory may be released earlier than the manager.
  base::WeakPtr<RtcEncodingVideoCapturerFactory> encoding_capturer_factory_;

  DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager);
};

}  // namespace content

#endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_