forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_client_video_capture_host.h
135 lines (115 loc) · 5.53 KB
/
single_client_video_capture_host.h
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2018 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.
#ifndef CHROME_BROWSER_MEDIA_SINGLE_CLIENT_VIDEO_CAPTURE_HOST_H_
#define CHROME_BROWSER_MEDIA_SINGLE_CLIENT_VIDEO_CAPTURE_HOST_H_
#include <memory>
#include <string>
#include <utility>
#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "content/public/browser/video_capture_device_launcher.h"
#include "content/public/common/media_stream_request.h"
#include "media/capture/mojo/video_capture.mojom.h"
#include "media/capture/video/video_frame_receiver.h"
namespace media {
// Implements a subset of mojom::VideoCaptureHost to proxy between a
// content::LaunchedVideoCaptureDevice and a single client. On Start(), uses the
// provided DeviceLauncherCreateCallback to launch a video capture device that
// outputs frames to |this|. The frames are received through the
// VideoFrameReceiver interface and are forwarded to |observer| through the
// mojom::VideoCaptureObserver interface.
// Instances of this class must be operated from the same thread that is reqired
// by the DeviceLauncherCreateCallback.
class SingleClientVideoCaptureHost final : public mojom::VideoCaptureHost,
public VideoFrameReceiver {
public:
using DeviceLauncherCreateCallback = base::RepeatingCallback<
std::unique_ptr<content::VideoCaptureDeviceLauncher>()>;
SingleClientVideoCaptureHost(const std::string& device_id,
content::MediaStreamType type,
const VideoCaptureParams& params,
DeviceLauncherCreateCallback callback);
~SingleClientVideoCaptureHost() override;
// mojom::VideoCaptureHost implementations
// |device_id| and |session_id| are ignored since there will be only one
// device and one client. |params| is also ignored since it is already set
// through the constructor.
void Start(int32_t device_id,
int32_t session_id,
const media::VideoCaptureParams& params,
mojom::VideoCaptureObserverPtr observer) override;
void Stop(int32_t device_id) override;
void Pause(int32_t device_id) override;
void Resume(int32_t device_id,
int32_t session_id,
const media::VideoCaptureParams& params) override;
void RequestRefreshFrame(int32_t device_id) override;
void ReleaseBuffer(int32_t device_id,
int32_t buffer_id,
double consumer_resource_utilization) override;
void GetDeviceSupportedFormats(
int32_t device_id,
int32_t session_id,
GetDeviceSupportedFormatsCallback callback) override;
void GetDeviceFormatsInUse(int32_t device_id,
int32_t session_id,
GetDeviceFormatsInUseCallback callback) override;
// media::VideoFrameReceiver implementations
using Buffer = VideoCaptureDevice::Client::Buffer;
void OnNewBufferHandle(
int buffer_id,
std::unique_ptr<media::VideoCaptureDevice::Client::Buffer::HandleProvider>
handle_provider) override;
void OnFrameReadyInBuffer(
int buffer_id,
int frame_feedback_id,
std::unique_ptr<
media::VideoCaptureDevice::Client::Buffer::ScopedAccessPermission>
buffer_read_permission,
media::mojom::VideoFrameInfoPtr frame_info) override;
void OnBufferRetired(int buffer_id) override;
void OnError() override;
void OnLog(const std::string& message) override;
void OnStarted() override;
void OnStartedUsingGpuDecode() override;
void OnDeviceLaunched(
std::unique_ptr<content::LaunchedVideoCaptureDevice> device);
void OnDeviceLaunchFailed();
void OnDeviceLaunchAborted();
private:
// Reports the |consumer_resource_utilization| and removes the buffer context.
void OnFinishedConsumingBuffer(int buffer_context_id,
double consumer_resource_utilization);
const std::string device_id_;
const content::MediaStreamType type_;
const VideoCaptureParams params_;
const DeviceLauncherCreateCallback device_launcher_callback_;
mojom::VideoCaptureObserverPtr observer_;
std::unique_ptr<content::LaunchedVideoCaptureDevice> launched_device_;
// Unique ID assigned for the next buffer provided by OnNewBufferHandle().
int next_buffer_context_id_ = 0;
// Records the assigned buffer_context_id for buffers that are not retired.
// The |buffer_id| provided by OnNewBufferHandle() is used as the key.
base::flat_map<int, int> id_map_;
// Tracks the the retired buffers that are still held by |observer_|.
base::flat_set<int> retired_buffers_;
// Records the |frame_feedback_id| and |buffer_read_permission| provided by
// OnFrameReadyInBuffer(). The key is the assigned buffer context id. Each
// entry is removed after the |observer_| finishes consuming the buffer by
// calling ReleaseBuffer(). When Stop() is called, all the buffers are cleared
// immediately.
using BufferContext = std::pair<
int,
std::unique_ptr<
VideoCaptureDevice::Client::Buffer::ScopedAccessPermission>>;
base::flat_map<int, BufferContext> buffer_context_map_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<SingleClientVideoCaptureHost> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(SingleClientVideoCaptureHost);
};
} // namespace media
#endif // CHROME_BROWSER_MEDIA_SINGLE_CLIENT_VIDEO_CAPTURE_HOST_H_