forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_video_stream_subscription_impl.cc
165 lines (147 loc) · 5.44 KB
/
push_video_stream_subscription_impl.cc
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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.
#include "services/video_capture/push_video_stream_subscription_impl.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "services/video_capture/broadcasting_receiver.h"
namespace video_capture {
PushVideoStreamSubscriptionImpl::PushVideoStreamSubscriptionImpl(
mojo::PendingReceiver<mojom::PushVideoStreamSubscription>
subscription_receiver,
mojo::PendingRemote<mojom::VideoFrameHandler> subscriber,
const media::VideoCaptureParams& requested_settings,
mojom::VideoSource::CreatePushSubscriptionCallback creation_callback,
BroadcastingReceiver* broadcaster,
mojo::Remote<mojom::Device>* device)
: receiver_(this, std::move(subscription_receiver)),
subscriber_(std::move(subscriber)),
requested_settings_(requested_settings),
creation_callback_(std::move(creation_callback)),
broadcaster_(broadcaster),
device_(device),
status_(Status::kCreationCallbackNotYetRun),
broadcaster_client_id_(0) {
DCHECK(broadcaster_);
DCHECK(device_);
}
PushVideoStreamSubscriptionImpl::~PushVideoStreamSubscriptionImpl() = default;
void PushVideoStreamSubscriptionImpl::SetOnClosedHandler(
base::OnceCallback<void(base::OnceClosure done_cb)> handler) {
on_closed_handler_ = std::move(handler);
receiver_.set_disconnect_handler(
base::BindOnce(&PushVideoStreamSubscriptionImpl::OnConnectionLost,
weak_factory_.GetWeakPtr()));
}
void PushVideoStreamSubscriptionImpl::OnDeviceStartSucceededWithSettings(
const media::VideoCaptureParams& settings) {
if (status_ != Status::kCreationCallbackNotYetRun) {
// Creation callback has already been run from a previous device start.
return;
}
mojom::CreatePushSubscriptionResultCode result_code =
settings == requested_settings_
? mojom::CreatePushSubscriptionResultCode::
kCreatedWithRequestedSettings
: mojom::CreatePushSubscriptionResultCode::
kCreatedWithDifferentSettings;
std::move(creation_callback_).Run(result_code, settings);
status_ = Status::kNotYetActivated;
}
void PushVideoStreamSubscriptionImpl::OnDeviceStartFailed() {
if (status_ != Status::kCreationCallbackNotYetRun) {
// Creation callback has already been run from a previous device start.
return;
}
std::move(creation_callback_)
.Run(mojom::CreatePushSubscriptionResultCode::kFailed,
requested_settings_);
status_ = Status::kClosed;
}
void PushVideoStreamSubscriptionImpl::Activate() {
if (status_ != Status::kNotYetActivated)
return;
broadcaster_client_id_ = broadcaster_->AddClient(
std::move(subscriber_), requested_settings_.buffer_type);
status_ = Status::kActive;
}
void PushVideoStreamSubscriptionImpl::Suspend(SuspendCallback callback) {
if (status_ != Status::kActive)
return;
broadcaster_->SuspendClient(broadcaster_client_id_);
status_ = Status::kSuspended;
std::move(callback).Run();
}
void PushVideoStreamSubscriptionImpl::Resume() {
if (status_ != Status::kSuspended)
return;
broadcaster_->ResumeClient(broadcaster_client_id_);
status_ = Status::kActive;
}
void PushVideoStreamSubscriptionImpl::GetPhotoState(
GetPhotoStateCallback callback) {
switch (status_) {
case Status::kCreationCallbackNotYetRun: // Fall through.
case Status::kClosed:
// Ignore the call.
return;
case Status::kNotYetActivated: // Fall through.
case Status::kActive: // Fall through.
case Status::kSuspended:
(*device_)->GetPhotoState(std::move(callback));
return;
}
}
void PushVideoStreamSubscriptionImpl::SetPhotoOptions(
media::mojom::PhotoSettingsPtr settings,
SetPhotoOptionsCallback callback) {
switch (status_) {
case Status::kCreationCallbackNotYetRun: // Fall through.
case Status::kClosed:
// Ignore the call.
return;
case Status::kNotYetActivated: // Fall through.
case Status::kActive: // Fall through.
case Status::kSuspended:
(*device_)->SetPhotoOptions(std::move(settings), std::move(callback));
return;
}
}
void PushVideoStreamSubscriptionImpl::TakePhoto(TakePhotoCallback callback) {
switch (status_) {
case Status::kCreationCallbackNotYetRun: // Fall through.
case Status::kClosed:
// Ignore the call.
return;
case Status::kNotYetActivated: // Fall through.
case Status::kActive: // Fall through.
case Status::kSuspended:
(*device_)->TakePhoto(std::move(callback));
return;
}
}
void PushVideoStreamSubscriptionImpl::Close(CloseCallback callback) {
switch (status_) {
case Status::kCreationCallbackNotYetRun:
case Status::kClosed:
std::move(callback).Run();
return;
case Status::kActive: // Fall through.
case Status::kSuspended:
broadcaster_->RemoveClient(broadcaster_client_id_);
status_ = Status::kClosed;
if (on_closed_handler_)
std::move(on_closed_handler_).Run(std::move(callback));
return;
case Status::kNotYetActivated:
status_ = Status::kClosed;
if (on_closed_handler_)
std::move(on_closed_handler_).Run(std::move(callback));
return;
}
}
void PushVideoStreamSubscriptionImpl::OnConnectionLost() {
if (on_closed_handler_)
std::move(on_closed_handler_).Run(base::DoNothing());
}
} // namespace video_capture