OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_REMOTING_REMOTING_SOURCE_IMPL_H_ | 5 #ifndef MEDIA_REMOTING_SHARED_SESSION_H_ |
6 #define MEDIA_REMOTING_REMOTING_SOURCE_IMPL_H_ | 6 #define MEDIA_REMOTING_SHARED_SESSION_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
| 9 |
9 #include "base/callback.h" | 10 #include "base/callback.h" |
10 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
11 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
12 #include "media/mojo/interfaces/remoting.mojom.h" | 13 #include "media/mojo/interfaces/remoting.mojom.h" |
13 #include "media/remoting/rpc/rpc_broker.h" | 14 #include "media/remoting/rpc_broker.h" |
14 #include "mojo/public/cpp/bindings/binding.h" | 15 #include "mojo/public/cpp/bindings/binding.h" |
15 | 16 |
16 namespace media { | 17 namespace media { |
| 18 namespace remoting { |
17 | 19 |
18 // State transition diagram: | 20 // A single shared remoting session for multiple clients. The session will |
19 // | |
20 // .--> SESSION_UNAVAILABLE | |
21 // | | ^ | |
22 // | V | | |
23 // | SESSION_CAN_START | |
24 // | | | |
25 // | V | |
26 // | .---SESSION_STARTING --. | |
27 // | | | | | |
28 // | | V | | |
29 // | | SESSION_STARTED----| | |
30 // | | | | | |
31 // | | V | | |
32 // | '-> SESSION_STOPPING | | |
33 // '-----' | | | |
34 // V V | |
35 // SESSION_PERMANENTLY_STOPPED | |
36 | |
37 enum RemotingSessionState { | |
38 // Remoting sink is not available. Can't start remoting. | |
39 SESSION_UNAVAILABLE, | |
40 // Remoting sink is available, Can start remoting. | |
41 SESSION_CAN_START, | |
42 // Starting a remoting session. | |
43 SESSION_STARTING, | |
44 // Remoting session is successively started. | |
45 SESSION_STARTED, | |
46 // Stopping the session. | |
47 SESSION_STOPPING, | |
48 // Remoting session is permanently stopped. This state indicates that the | |
49 // video stack cannot continue operation. For example, if a remoting session | |
50 // involving CDM content was stopped, there is no way to continue playback | |
51 // because the CDM is required but is no longer available. | |
52 SESSION_PERMANENTLY_STOPPED, | |
53 }; | |
54 | |
55 // Maintains a single remoting session for multiple clients. The session will | |
56 // start remoting when receiving the first request. Once remoting is started, | 21 // start remoting when receiving the first request. Once remoting is started, |
57 // it will be stopped when any of the following happens: | 22 // it will be stopped when any of the following happens: |
58 // 1) Receives the request from any client to stop remoting. | 23 // 1) Receives the request from any client to stop remoting. |
59 // 2) Remote sink is gone. | 24 // 2) Remote sink is gone. |
60 // 3) Any client requests to permanently terminate the session. | 25 // 3) Any client requests to permanently terminate the session. |
61 // 4) All clients are destroyed. | 26 // 4) All clients are destroyed. |
62 // | 27 // |
63 // This class is ref-counted because, in some cases, an instance will have | 28 // This class is ref-counted because, in some cases, an instance will have |
64 // shared ownership between RemotingRendererController and | 29 // shared ownership between RendererController and RemotingCdmController. |
65 // RemotingCdmController. | 30 class SharedSession final : public mojom::RemotingSource, |
66 class RemotingSourceImpl final | 31 public base::RefCountedThreadSafe<SharedSession> { |
67 : public mojom::RemotingSource, | |
68 public base::RefCountedThreadSafe<RemotingSourceImpl> { | |
69 public: | 32 public: |
| 33 // State transition diagram: |
| 34 // |
| 35 // .--> SESSION_UNAVAILABLE |
| 36 // | | ^ |
| 37 // | V | |
| 38 // | SESSION_CAN_START |
| 39 // | | |
| 40 // | V |
| 41 // | .---SESSION_STARTING --. |
| 42 // | | | | |
| 43 // | | V | |
| 44 // | | SESSION_STARTED----| |
| 45 // | | | | |
| 46 // | | V | |
| 47 // | '-> SESSION_STOPPING | |
| 48 // '-----' | | |
| 49 // V V |
| 50 // SESSION_PERMANENTLY_STOPPED |
| 51 enum SessionState { |
| 52 // Remoting sink is not available. Can't start remoting. |
| 53 SESSION_UNAVAILABLE, |
| 54 // Remoting sink is available, Can start remoting. |
| 55 SESSION_CAN_START, |
| 56 // Starting a remoting session. |
| 57 SESSION_STARTING, |
| 58 // Remoting session is successively started. |
| 59 SESSION_STARTED, |
| 60 // Stopping the session. |
| 61 SESSION_STOPPING, |
| 62 // Remoting session is permanently stopped. This state indicates that the |
| 63 // video stack cannot continue operation. For example, if a remoting session |
| 64 // involving CDM content was stopped, there is no way to continue playback |
| 65 // because the CDM is required but is no longer available. |
| 66 SESSION_PERMANENTLY_STOPPED, |
| 67 }; |
| 68 |
70 class Client { | 69 class Client { |
71 public: | 70 public: |
72 // Get notified whether the remoting session is successively started. | 71 // Get notified whether the remoting session is successively started. |
73 virtual void OnStarted(bool success) = 0; | 72 virtual void OnStarted(bool success) = 0; |
74 // Get notified when session state changes. | 73 // Get notified when session state changes. |
75 virtual void OnSessionStateChanged() = 0; | 74 virtual void OnSessionStateChanged() = 0; |
76 }; | 75 }; |
77 | 76 |
78 RemotingSourceImpl(mojom::RemotingSourceRequest source_request, | 77 SharedSession(mojom::RemotingSourceRequest source_request, |
79 mojom::RemoterPtr remoter); | 78 mojom::RemoterPtr remoter); |
80 | 79 |
81 // Get the current session state. | 80 // Get the current session state. |
82 RemotingSessionState state() const { | 81 SessionState state() const { |
83 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
84 return state_; | 83 return state_; |
85 } | 84 } |
86 | 85 |
87 mojom::RemotingSinkCapabilities sink_capabilities() const { | 86 mojom::RemotingSinkCapabilities sink_capabilities() const { |
88 return sink_capabilities_; | 87 return sink_capabilities_; |
89 } | 88 } |
90 | 89 |
91 bool is_remote_decryption_available() const { | 90 bool is_remote_decryption_available() const { |
92 return sink_capabilities_ == | 91 return sink_capabilities_ == |
(...skipping 28 matching lines...) Expand all Loading... |
121 | 120 |
122 // Permanently terminates the current remoting session. | 121 // Permanently terminates the current remoting session. |
123 void Shutdown(); | 122 void Shutdown(); |
124 | 123 |
125 // Add/remove a client to/from |clients_|. | 124 // Add/remove a client to/from |clients_|. |
126 // Note: Clients can only added/removed through these methods. | 125 // Note: Clients can only added/removed through these methods. |
127 // Remoting session will be stopped if all clients are gone. | 126 // Remoting session will be stopped if all clients are gone. |
128 void AddClient(Client* client); | 127 void AddClient(Client* client); |
129 void RemoveClient(Client* client); | 128 void RemoveClient(Client* client); |
130 | 129 |
131 remoting::RpcBroker* GetRpcBroker() const; | 130 RpcBroker* rpc_broker() { return &rpc_broker_; } |
132 | 131 |
133 private: | 132 private: |
134 friend class base::RefCountedThreadSafe<RemotingSourceImpl>; | 133 friend class base::RefCountedThreadSafe<SharedSession>; |
135 ~RemotingSourceImpl() override; | 134 ~SharedSession() override; |
136 | 135 |
137 // Updates the current session state and notifies all the clients if state | 136 // Updates the current session state and notifies all the clients if state |
138 // changes. | 137 // changes. |
139 void UpdateAndNotifyState(RemotingSessionState state); | 138 void UpdateAndNotifyState(SessionState state); |
140 | 139 |
141 // Callback from RpcBroker when sending message to remote sink. | 140 // Callback from RpcBroker when sending message to remote sink. |
142 void SendMessageToSink(std::unique_ptr<std::vector<uint8_t>> message); | 141 void SendMessageToSink(std::unique_ptr<std::vector<uint8_t>> message); |
143 | 142 |
144 // TODO(xjz): Might merge RpcBroker into RemotingSourceImpl. | 143 // Handles dispatching of incoming and outgoing RPC messages. |
145 // Handle incomging and outgoing RPC message. | 144 RpcBroker rpc_broker_; |
146 remoting::RpcBroker rpc_broker_; | |
147 | 145 |
148 const mojo::Binding<mojom::RemotingSource> binding_; | 146 const mojo::Binding<mojom::RemotingSource> binding_; |
149 const mojom::RemoterPtr remoter_; | 147 const mojom::RemoterPtr remoter_; |
150 | 148 |
151 // When the sink is available, this describes its capabilities. When not | 149 // When the sink is available, this describes its capabilities. When not |
152 // available, this is always NONE. Updated by OnSinkAvailable/Gone(). | 150 // available, this is always NONE. Updated by OnSinkAvailable/Gone(). |
153 mojom::RemotingSinkCapabilities sink_capabilities_ = | 151 mojom::RemotingSinkCapabilities sink_capabilities_ = |
154 mojom::RemotingSinkCapabilities::NONE; | 152 mojom::RemotingSinkCapabilities::NONE; |
155 | 153 |
156 // The current state. | 154 // The current state. |
157 RemotingSessionState state_ = RemotingSessionState::SESSION_UNAVAILABLE; | 155 SessionState state_ = SESSION_UNAVAILABLE; |
158 | 156 |
159 // Clients are added/removed to/from this list by calling Add/RemoveClient(). | 157 // Clients are added/removed to/from this list by calling Add/RemoveClient(). |
160 // All the clients are not belong to this class. They are supposed to call | 158 // All the clients are not belong to this class. They are supposed to call |
161 // RemoveClient() before they are gone. | 159 // RemoveClient() before they are gone. |
162 std::vector<Client*> clients_; | 160 std::vector<Client*> clients_; |
163 | 161 |
164 // This is used to check all the methods are called on the current thread in | 162 // This is used to check all the methods are called on the current thread in |
165 // debug builds. | 163 // debug builds. |
166 base::ThreadChecker thread_checker_; | 164 base::ThreadChecker thread_checker_; |
167 }; | 165 }; |
168 | 166 |
| 167 } // namespace remoting |
169 } // namespace media | 168 } // namespace media |
170 | 169 |
171 #endif // MEDIA_REMOTING_REMOTING_SOURCE_IMPL_H_ | 170 #endif // MEDIA_REMOTING_SHARED_SESSION_H_ |
OLD | NEW |