blob: 6b02825a7aa9e549fc72da4c79075f962f988fc5 (
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
// 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 owns VideoCaptureImpl objects. Clients who
// want access to a video capture device call UseDevice() to get a handle
// to VideoCaptureImpl.
//
// THREADING
//
// VideoCaptureImplManager lives only on the render thread. All methods
// must be called on this thread.
//
// The handle returned by UseDevice() is thread-safe. It ensures
// destruction is handled on the render thread.
#ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
#define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
#include <map>
#include "base/callback.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/synchronization/lock.h"
#include "base/threading/thread_checker.h"
#include "content/common/content_export.h"
#include "media/video/capture/video_capture.h"
namespace content {
class VideoCaptureImpl;
class VideoCaptureImplManager;
class VideoCaptureMessageFilter;
// Thread-safe wrapper for a media::VideoCapture object. During
// destruction |destruction_cb| is called. This mechanism is used
// by VideoCaptureImplManager to ensure de-initialization and
// destruction of the media::VideoCapture object happens on the render
// thread.
class CONTENT_EXPORT VideoCaptureHandle : media::VideoCapture {
public:
virtual ~VideoCaptureHandle();
// media::VideoCapture implementations.
virtual void StartCapture(
EventHandler* handler,
const media::VideoCaptureParams& params) OVERRIDE;
virtual void StopCapture(EventHandler* handler) OVERRIDE;
virtual bool CaptureStarted() OVERRIDE;
virtual int CaptureFrameRate() OVERRIDE;
virtual void GetDeviceSupportedFormats(
const DeviceFormatsCallback& callback) OVERRIDE;
virtual void GetDeviceFormatsInUse(
const DeviceFormatsInUseCallback& callback) OVERRIDE;
private:
friend class VideoCaptureImplManager;
VideoCaptureHandle(media::VideoCapture* impl,
base::Closure destruction_cb);
media::VideoCapture* impl_;
base::Closure destruction_cb_;
DISALLOW_COPY_AND_ASSIGN(VideoCaptureHandle);
};
class CONTENT_EXPORT VideoCaptureImplManager {
public:
VideoCaptureImplManager();
virtual ~VideoCaptureImplManager();
// Returns a video capture device referenced by |id|.
scoped_ptr<VideoCaptureHandle> UseDevice(media::VideoCaptureSessionId id);
// Make all existing VideoCaptureImpl instances stop/resume delivering
// video frames to their clients, depends on flag |suspend|.
void SuspendDevices(bool suspend);
VideoCaptureMessageFilter* video_capture_message_filter() const {
return filter_.get();
}
protected:
// Used in tests to inject a mock VideoCaptureImpl.
virtual VideoCaptureImpl* CreateVideoCaptureImpl(
media::VideoCaptureSessionId id,
VideoCaptureMessageFilter* filter) const;
private:
void UnrefDevice(media::VideoCaptureSessionId id);
// The int is used to count clients of the corresponding VideoCaptureImpl.
typedef std::map<media::VideoCaptureSessionId,
std::pair<int, linked_ptr<VideoCaptureImpl> > >
VideoCaptureDeviceMap;
VideoCaptureDeviceMap devices_;
scoped_refptr<VideoCaptureMessageFilter> filter_;
// Bound to the render thread.
base::ThreadChecker thread_checker_;
// Bound to the render thread.
// NOTE: Weak pointers must be invalidated before all other member variables.
base::WeakPtrFactory<VideoCaptureImplManager> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManager);
};
} // namespace content
#endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_MANAGER_H_
|