OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 #include "media/remoting/remoting_source_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/threading/thread_checker.h" | |
10 | |
11 namespace media { | |
12 | |
13 RemotingSourceImpl::RemotingSourceImpl( | |
14 mojom::RemotingSourceRequest source_request, | |
15 mojom::RemoterPtr remoter) | |
16 : binding_(this, std::move(source_request)), remoter_(std::move(remoter)) {} | |
17 | |
18 RemotingSourceImpl::~RemotingSourceImpl() { | |
19 if (!clients_.empty()) { | |
20 UpdateAndNotifyState(RemotingSessionState::SESSION_PERMANENTLY_STOPPED); | |
miu
2016/10/25 04:21:27
Instead of this, consider calling Shutdown() so th
xjz
2016/10/26 22:00:27
Done.
| |
21 clients_.clear(); | |
22 } | |
23 } | |
24 | |
25 void RemotingSourceImpl::OnSinkAvailable() { | |
26 DCHECK(thread_checker_.CalledOnValidThread()); | |
27 is_sink_available_ = true; | |
28 if (state_ == RemotingSessionState::SESSION_UNAVAILABLE) | |
29 UpdateAndNotifyState(RemotingSessionState::SESSION_CAN_START); | |
30 } | |
31 | |
32 void RemotingSourceImpl::OnSinkGone() { | |
33 DCHECK(thread_checker_.CalledOnValidThread()); | |
34 is_sink_available_ = false; | |
35 if (state_ == RemotingSessionState::SESSION_PERMANENTLY_STOPPED) | |
36 return; | |
37 if (state_ == RemotingSessionState::SESSION_CAN_START) { | |
38 UpdateAndNotifyState(RemotingSessionState::SESSION_UNAVAILABLE); | |
39 return; | |
40 } | |
41 if (state_ == RemotingSessionState::SESSION_STARTED) { | |
miu
2016/10/25 04:21:27
Or if in the SESSION_STARTING state too, right?
xjz
2016/10/26 22:00:27
Done.
| |
42 remoter_->Stop(mojom::RemotingStopReason::ROUTE_TERMINATED); | |
miu
2016/10/25 04:21:27
Actually, you shouldn't need to call Stop() here.
xjz
2016/10/26 22:00:27
Done.
| |
43 UpdateAndNotifyState(RemotingSessionState::SESSION_STOPPING); | |
44 } | |
45 } | |
46 | |
47 void RemotingSourceImpl::OnStarted() { | |
48 VLOG(1) << "Remoting started successively."; | |
49 if (!is_sink_available_ || clients_.empty() || | |
miu
2016/10/25 04:21:26
The "!is_sink_available_" shouldn't be needed. (Th
xjz
2016/10/26 22:00:27
Done.
| |
50 state_ == RemotingSessionState::SESSION_PERMANENTLY_STOPPED || | |
51 state_ == RemotingSessionState::SESSION_STOPPING) { | |
52 for (Client* client : clients_) | |
53 client->OnStarted(false); | |
54 remoter_->Stop(mojom::RemotingStopReason::ROUTE_TERMINATED); | |
miu
2016/10/25 04:21:27
The reason should be LOCAL_PLAYBACK since either t
xjz
2016/10/26 22:00:27
Done.
| |
55 if (state_ != RemotingSessionState::SESSION_PERMANENTLY_STOPPED) | |
56 state_ = RemotingSessionState::SESSION_STOPPING; | |
57 return; | |
58 } | |
59 for (Client* client : clients_) | |
60 client->OnStarted(true); | |
61 state_ = RemotingSessionState::SESSION_STARTED; | |
62 } | |
63 | |
64 void RemotingSourceImpl::OnStartFailed(mojom::RemotingStartFailReason reason) { | |
65 VLOG(1) << "Failed to start remoting:" << reason; | |
66 for (Client* client : clients_) | |
67 client->OnStarted(false); | |
68 if (state_ == RemotingSessionState::SESSION_PERMANENTLY_STOPPED) | |
69 return; | |
70 state_ = is_sink_available_ ? RemotingSessionState::SESSION_CAN_START | |
miu
2016/10/25 04:21:27
This should just be "state_ = SESSION_UNAVAILABLE"
xjz
2016/10/26 22:00:27
Done.
| |
71 : RemotingSessionState::SESSION_UNAVAILABLE; | |
72 } | |
73 | |
74 void RemotingSourceImpl::OnStopped(mojom::RemotingStopReason reason) { | |
75 VLOG(1) << "Remoting stopped: " << reason; | |
76 if (state_ == RemotingSessionState::SESSION_PERMANENTLY_STOPPED) | |
77 return; | |
78 RemotingSessionState state = is_sink_available_ | |
miu
2016/10/25 04:21:27
This should just be "state_ = SESSION_UNAVAILABLE"
xjz
2016/10/26 22:00:27
Done.
| |
79 ? RemotingSessionState::SESSION_CAN_START | |
80 : RemotingSessionState::SESSION_UNAVAILABLE; | |
81 UpdateAndNotifyState(state); | |
82 } | |
83 | |
84 void RemotingSourceImpl::OnMessageFromSink( | |
85 const std::vector<uint8_t>& message) { | |
86 DCHECK(thread_checker_.CalledOnValidThread()); | |
87 | |
88 // TODO(xjz): Merge with Eric's CL to handle the RPC messages here. | |
89 NOTIMPLEMENTED(); | |
90 } | |
91 | |
92 void RemotingSourceImpl::UpdateAndNotifyState(RemotingSessionState state) { | |
93 if (state_ == state) | |
94 return; | |
95 state_ = state; | |
96 for (Client* client : clients_) | |
97 client->OnSessionStateChanged(); | |
98 } | |
99 | |
100 void RemotingSourceImpl::StartRemoting(Client* client) { | |
101 DCHECK(clients_.find(client) != clients_.end()); | |
102 | |
103 switch (state_) { | |
104 case SESSION_CAN_START: | |
105 remoter_->Start(); | |
106 UpdateAndNotifyState(RemotingSessionState::SESSION_STARTING); | |
107 break; | |
108 case SESSION_STARTING: | |
109 break; | |
110 case SESSION_STARTED: | |
111 client->OnStarted(true); | |
112 break; | |
113 case SESSION_STOPPING: | |
114 case SESSION_UNAVAILABLE: | |
115 case SESSION_PERMANENTLY_STOPPED: | |
116 client->OnStarted(false); | |
117 break; | |
118 } | |
119 } | |
120 | |
121 void RemotingSourceImpl::StopRemoting(Client* client) { | |
122 DCHECK(clients_.find(client) != clients_.end()); | |
123 | |
124 VLOG(1) << "RemotingSourceImpl::StopRemoting: " << state_; | |
125 | |
126 if (state_ != RemotingSessionState::SESSION_STARTING && | |
127 state_ != RemotingSessionState::SESSION_STARTED) | |
128 return; | |
129 | |
130 if (state_ == RemotingSessionState::SESSION_STARTED) | |
131 remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK); | |
132 UpdateAndNotifyState(RemotingSessionState::SESSION_STOPPING); | |
133 } | |
134 | |
135 void RemotingSourceImpl::AddClient(Client* client) { | |
136 if (clients_.find(client) != clients_.end()) | |
137 return; | |
138 clients_.insert(client); | |
139 client->OnSessionStateChanged(); | |
140 } | |
141 | |
142 void RemotingSourceImpl::RemoveClient(Client* client) { | |
143 if (clients_.find(client) == clients_.end()) | |
144 return; | |
145 clients_.erase(client); | |
146 if (clients_.empty() && state_ == RemotingSessionState::SESSION_STARTED) { | |
miu
2016/10/25 04:21:27
Or if in the SESSION_STARTING state too, right?
xjz
2016/10/26 22:00:27
My original thought was to call Stop() in OnStarte
| |
147 remoter_->Stop(mojom::RemotingStopReason::SOURCE_GONE); | |
148 state_ = RemotingSessionState::SESSION_STOPPING; | |
149 } | |
150 } | |
151 | |
152 void RemotingSourceImpl::ShutDown() { | |
153 if (state_ == RemotingSessionState::SESSION_STARTED) | |
miu
2016/10/25 04:21:27
ditto: We want to call Stop() if SESSION_STARTING.
xjz
2016/10/26 22:00:27
Done.
| |
154 remoter_->Stop(mojom::RemotingStopReason::UNEXPECTED_FAILURE); | |
155 UpdateAndNotifyState(RemotingSessionState::SESSION_PERMANENTLY_STOPPED); | |
156 } | |
157 | |
158 } // namespace media | |
OLD | NEW |