diff options
author | garykac@google.com <garykac@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-02 22:23:17 +0000 |
---|---|---|
committer | garykac@google.com <garykac@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-02 22:23:17 +0000 |
commit | 3b3d3af27fd5e50466cc9151dfd92062b8c962a0 (patch) | |
tree | 2904b0d106d21cd38c3b63cf56ad6a3cfa5b7490 /remoting | |
parent | ff8fddef2e9db0874a8afb16d9950ad8b83191d7 (diff) | |
download | chromium_src-3b3d3af27fd5e50466cc9151dfd92062b8c962a0.zip chromium_src-3b3d3af27fd5e50466cc9151dfd92062b8c962a0.tar.gz chromium_src-3b3d3af27fd5e50466cc9151dfd92062b8c962a0.tar.bz2 |
Re-order host SessionManager code to group by thread. This makes the flow of control
slightly easier to follow.
BUG=none
TEST=unittests
Review URL: http://codereview.chromium.org/2886013
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@51570 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'remoting')
-rw-r--r-- | remoting/host/session_manager.cc | 308 | ||||
-rw-r--r-- | remoting/host/session_manager.h | 44 |
2 files changed, 187 insertions, 165 deletions
diff --git a/remoting/host/session_manager.cc b/remoting/host/session_manager.cc index 29dfb8b..e9a73d3 100644 --- a/remoting/host/session_manager.cc +++ b/remoting/host/session_manager.cc @@ -57,11 +57,56 @@ SessionManager::~SessionManager() { clients_.clear(); } +// Public methods -------------------------------------------------------------- + void SessionManager::Start() { capture_loop_->PostTask( FROM_HERE, NewRunnableMethod(this, &SessionManager::DoStart)); } +void SessionManager::Pause() { + capture_loop_->PostTask( + FROM_HERE, NewRunnableMethod(this, &SessionManager::DoPause)); +} + +void SessionManager::SetMaxRate(double rate) { + capture_loop_->PostTask( + FROM_HERE, NewRunnableMethod(this, &SessionManager::DoSetMaxRate, rate)); +} + +void SessionManager::AddClient(scoped_refptr<ClientConnection> client) { + // Gets the init information for the client. + capture_loop_->PostTask( + FROM_HERE, + NewRunnableMethod(this, &SessionManager::DoGetInitInfo, client)); +} + +void SessionManager::RemoveClient(scoped_refptr<ClientConnection> client) { + network_loop_->PostTask( + FROM_HERE, + NewRunnableMethod(this, &SessionManager::DoRemoveClient, client)); +} + +void SessionManager::RemoveAllClients() { + network_loop_->PostTask( + FROM_HERE, + NewRunnableMethod(this, &SessionManager::DoRemoveAllClients)); +} + +// Private accessors ----------------------------------------------------------- + +Capturer* SessionManager::capturer() { + DCHECK_EQ(capture_loop_, MessageLoop::current()); + return capturer_.get(); +} + +Encoder* SessionManager::encoder() { + DCHECK_EQ(encode_loop_, MessageLoop::current()); + return encoder_.get(); +} + +// Capturer thread ------------------------------------------------------------- + void SessionManager::DoStart() { DCHECK_EQ(capture_loop_, MessageLoop::current()); @@ -79,22 +124,6 @@ void SessionManager::DoStart() { NewRunnableMethod(this, &SessionManager::DoStartRateControl)); } -void SessionManager::DoStartRateControl() { - DCHECK_EQ(network_loop_, MessageLoop::current()); - - if (rate_control_started_) { - NOTREACHED() << "Rate regulation already started"; - return; - } - rate_control_started_ = true; - ScheduleNextRateControl(); -} - -void SessionManager::Pause() { - capture_loop_->PostTask( - FROM_HERE, NewRunnableMethod(this, &SessionManager::DoPause)); -} - void SessionManager::DoPause() { DCHECK_EQ(capture_loop_, MessageLoop::current()); @@ -111,38 +140,44 @@ void SessionManager::DoPause() { NewRunnableMethod(this, &SessionManager::DoPauseRateControl)); } -void SessionManager::DoPauseRateControl() { - DCHECK_EQ(network_loop_, MessageLoop::current()); - - if (!rate_control_started_) { - NOTREACHED() << "Rate regulation not started"; +void SessionManager::DoSetRate(double rate) { + DCHECK_EQ(capture_loop_, MessageLoop::current()); + if (rate == rate_) return; - } - rate_control_started_ = false; -} -void SessionManager::SetMaxRate(double rate) { - capture_loop_->PostTask( - FROM_HERE, NewRunnableMethod(this, &SessionManager::DoSetMaxRate, rate)); -} + // Change the current capture rate. + rate_ = rate; -void SessionManager::AddClient(scoped_refptr<ClientConnection> client) { - // Gets the init information for the client. - capture_loop_->PostTask( - FROM_HERE, - NewRunnableMethod(this, &SessionManager::DoGetInitInfo, client)); + // If we have already started then schedule the next capture with the new + // rate. + if (started_) + ScheduleNextCapture(); } -void SessionManager::RemoveClient(scoped_refptr<ClientConnection> client) { - network_loop_->PostTask( - FROM_HERE, - NewRunnableMethod(this, &SessionManager::DoRemoveClient, client)); +void SessionManager::DoSetMaxRate(double max_rate) { + DCHECK_EQ(capture_loop_, MessageLoop::current()); + + // TODO(hclam): Should also check for small epsilon. + if (max_rate != 0) { + max_rate_ = max_rate; + DoSetRate(max_rate); + } else { + NOTREACHED() << "Rate is too small."; + } } -void SessionManager::RemoveAllClients() { - network_loop_->PostTask( +void SessionManager::ScheduleNextCapture() { + DCHECK_EQ(capture_loop_, MessageLoop::current()); + + if (rate_ == 0) + return; + + base::TimeDelta interval = base::TimeDelta::FromMilliseconds( + static_cast<int>(base::Time::kMillisecondsPerSecond / rate_)); + capture_loop_->PostDelayedTask( FROM_HERE, - NewRunnableMethod(this, &SessionManager::DoRemoveAllClients)); + NewRunnableMethod(this, &SessionManager::DoCapture), + interval.InMilliseconds()); } void SessionManager::DoCapture() { @@ -183,6 +218,14 @@ void SessionManager::DoCapture() { NewCallback(this, &SessionManager::CaptureDoneCallback)); } +void SessionManager::CaptureDoneCallback( + scoped_refptr<Capturer::CaptureData> capture_data) { + DCHECK_EQ(capture_loop_, MessageLoop::current()); + encode_loop_->PostTask( + FROM_HERE, + NewRunnableMethod(this, &SessionManager::DoEncode, capture_data)); +} + void SessionManager::DoFinishEncode() { DCHECK_EQ(capture_loop_, MessageLoop::current()); @@ -196,46 +239,6 @@ void SessionManager::DoFinishEncode() { DoCapture(); } -void SessionManager::DoEncode( - scoped_refptr<Capturer::CaptureData> capture_data) { - DCHECK_EQ(encode_loop_, MessageLoop::current()); - - // TODO(hclam): Enable |force_refresh| if a new client was - // added. - encoder_->Encode(capture_data, false, - NewCallback(this, &SessionManager::EncodeDataAvailableTask)); -} - -void SessionManager::DoSendUpdate(const UpdateStreamPacketHeader* header, - const scoped_refptr<media::DataBuffer> data, - Encoder::EncodingState state) { - // Take ownership of header. - scoped_ptr<const UpdateStreamPacketHeader> header_owner(header); - DCHECK_EQ(network_loop_, MessageLoop::current()); - - for (ClientConnectionList::const_iterator i = clients_.begin(); - i < clients_.end(); - ++i) { - if (state & Encoder::EncodingStarting) { - (*i)->SendBeginUpdateStreamMessage(); - } - - (*i)->SendUpdateStreamPacketMessage(header, data); - - if (state & Encoder::EncodingEnded) { - (*i)->SendEndUpdateStreamMessage(); - } - } -} - -void SessionManager::DoSendInit(scoped_refptr<ClientConnection> client, - int width, int height) { - DCHECK_EQ(network_loop_, MessageLoop::current()); - - // Sends the client init information. - client->SendInitClientMessage(width, height); -} - void SessionManager::DoGetInitInfo(scoped_refptr<ClientConnection> client) { DCHECK_EQ(capture_loop_, MessageLoop::current()); @@ -253,55 +256,34 @@ void SessionManager::DoGetInitInfo(scoped_refptr<ClientConnection> client) { NewRunnableMethod(this, &SessionManager::DoAddClient, client)); } -void SessionManager::DoSetRate(double rate) { - DCHECK_EQ(capture_loop_, MessageLoop::current()); - if (rate == rate_) - return; - - // Change the current capture rate. - rate_ = rate; - - // If we have already started then schedule the next capture with the new - // rate. - if (started_) - ScheduleNextCapture(); -} - -void SessionManager::DoSetMaxRate(double max_rate) { - DCHECK_EQ(capture_loop_, MessageLoop::current()); +// Network thread -------------------------------------------------------------- - // TODO(hclam): Should also check for small epsilon. - if (max_rate != 0) { - max_rate_ = max_rate; - DoSetRate(max_rate); - } else { - NOTREACHED() << "Rate is too small."; - } -} - -void SessionManager::DoAddClient(scoped_refptr<ClientConnection> client) { +void SessionManager::DoStartRateControl() { DCHECK_EQ(network_loop_, MessageLoop::current()); - // TODO(hclam): Force a full frame for next encode. - clients_.push_back(client); + if (rate_control_started_) { + NOTREACHED() << "Rate regulation already started"; + return; + } + rate_control_started_ = true; + ScheduleNextRateControl(); } -void SessionManager::DoRemoveClient(scoped_refptr<ClientConnection> client) { +void SessionManager::DoPauseRateControl() { DCHECK_EQ(network_loop_, MessageLoop::current()); - // TODO(hclam): Is it correct to do to a scoped_refptr? - ClientConnectionList::iterator it - = std::find(clients_.begin(), clients_.end(), client); - if (it != clients_.end()) { - clients_.erase(it); + if (!rate_control_started_) { + NOTREACHED() << "Rate regulation not started"; + return; } + rate_control_started_ = false; } -void SessionManager::DoRemoveAllClients() { - DCHECK_EQ(network_loop_, MessageLoop::current()); - - // Clear the list of clients. - clients_.clear(); +void SessionManager::ScheduleNextRateControl() { + network_loop_->PostDelayedTask( + FROM_HERE, + NewRunnableMethod(this, &SessionManager::DoRateControl), + kRateControlInterval.InMilliseconds()); } void SessionManager::DoRateControl() { @@ -339,33 +321,71 @@ void SessionManager::DoRateControl() { ScheduleNextRateControl(); } -void SessionManager::ScheduleNextCapture() { - DCHECK_EQ(capture_loop_, MessageLoop::current()); +void SessionManager::DoSendUpdate(const UpdateStreamPacketHeader* header, + const scoped_refptr<media::DataBuffer> data, + Encoder::EncodingState state) { + // Take ownership of header. + scoped_ptr<const UpdateStreamPacketHeader> header_owner(header); + DCHECK_EQ(network_loop_, MessageLoop::current()); - if (rate_ == 0) - return; + for (ClientConnectionList::const_iterator i = clients_.begin(); + i < clients_.end(); + ++i) { + if (state & Encoder::EncodingStarting) { + (*i)->SendBeginUpdateStreamMessage(); + } - base::TimeDelta interval = base::TimeDelta::FromMilliseconds( - static_cast<int>(base::Time::kMillisecondsPerSecond / rate_)); - capture_loop_->PostDelayedTask( - FROM_HERE, - NewRunnableMethod(this, &SessionManager::DoCapture), - interval.InMilliseconds()); + (*i)->SendUpdateStreamPacketMessage(header, data); + + if (state & Encoder::EncodingEnded) { + (*i)->SendEndUpdateStreamMessage(); + } + } } -void SessionManager::ScheduleNextRateControl() { - network_loop_->PostDelayedTask( - FROM_HERE, - NewRunnableMethod(this, &SessionManager::DoRateControl), - kRateControlInterval.InMilliseconds()); +void SessionManager::DoSendInit(scoped_refptr<ClientConnection> client, + int width, int height) { + DCHECK_EQ(network_loop_, MessageLoop::current()); + + // Sends the client init information. + client->SendInitClientMessage(width, height); } -void SessionManager::CaptureDoneCallback( +void SessionManager::DoAddClient(scoped_refptr<ClientConnection> client) { + DCHECK_EQ(network_loop_, MessageLoop::current()); + + // TODO(hclam): Force a full frame for next encode. + clients_.push_back(client); +} + +void SessionManager::DoRemoveClient(scoped_refptr<ClientConnection> client) { + DCHECK_EQ(network_loop_, MessageLoop::current()); + + // TODO(hclam): Is it correct to do to a scoped_refptr? + ClientConnectionList::iterator it + = std::find(clients_.begin(), clients_.end(), client); + if (it != clients_.end()) { + clients_.erase(it); + } +} + +void SessionManager::DoRemoveAllClients() { + DCHECK_EQ(network_loop_, MessageLoop::current()); + + // Clear the list of clients. + clients_.clear(); +} + +// Encoder thread -------------------------------------------------------------- + +void SessionManager::DoEncode( scoped_refptr<Capturer::CaptureData> capture_data) { - DCHECK_EQ(capture_loop_, MessageLoop::current()); - encode_loop_->PostTask( - FROM_HERE, - NewRunnableMethod(this, &SessionManager::DoEncode, capture_data)); + DCHECK_EQ(encode_loop_, MessageLoop::current()); + + // TODO(hclam): Enable |force_refresh| if a new client was + // added. + encoder_->Encode(capture_data, false, + NewCallback(this, &SessionManager::EncodeDataAvailableTask)); } void SessionManager::EncodeDataAvailableTask( @@ -392,14 +412,4 @@ void SessionManager::EncodeDataAvailableTask( } } -Capturer* SessionManager::capturer() { - DCHECK_EQ(capture_loop_, MessageLoop::current()); - return capturer_.get(); -} - -Encoder* SessionManager::encoder() { - DCHECK_EQ(encode_loop_, MessageLoop::current()); - return encoder_.get(); -} - } // namespace remoting diff --git a/remoting/host/session_manager.h b/remoting/host/session_manager.h index 8f2cbc1..b56564f 100644 --- a/remoting/host/session_manager.h +++ b/remoting/host/session_manager.h @@ -99,46 +99,58 @@ class SessionManager : public base::RefCountedThreadSafe<SessionManager> { void RemoveAllClients(); private: + // Getters for capturer and encoder. + Capturer* capturer(); + Encoder* encoder(); + + // Capturer thread ---------------------------------------------------------- + void DoStart(); void DoPause(); - void DoStartRateControl(); - void DoPauseRateControl(); + + void DoSetRate(double rate); + void DoSetMaxRate(double max_rate); + + // Hepler method to schedule next capture using the current rate. + void ScheduleNextCapture(); void DoCapture(); + void CaptureDoneCallback(scoped_refptr<Capturer::CaptureData> capture_data); void DoFinishEncode(); - void DoEncode(scoped_refptr<Capturer::CaptureData> capture_data); + void DoGetInitInfo(scoped_refptr<ClientConnection> client); + + // Network thread ----------------------------------------------------------- + + void DoStartRateControl(); + void DoPauseRateControl(); + + // Helper method to schedule next rate regulation task. + void ScheduleNextRateControl(); + + void DoRateControl(); + // DoSendUpdate takes ownership of header and is responsible for deleting it. void DoSendUpdate(const UpdateStreamPacketHeader* header, const scoped_refptr<media::DataBuffer> data, Encoder::EncodingState state); void DoSendInit(scoped_refptr<ClientConnection> client, int width, int height); - void DoGetInitInfo(scoped_refptr<ClientConnection> client); - void DoSetRate(double rate); - void DoSetMaxRate(double max_rate); + void DoAddClient(scoped_refptr<ClientConnection> client); void DoRemoveClient(scoped_refptr<ClientConnection> client); void DoRemoveAllClients(); - void DoRateControl(); - // Hepler method to schedule next capture using the current rate. - void ScheduleNextCapture(); + // Encoder thread ----------------------------------------------------------- - // Helper method to schedule next rate regulation task. - void ScheduleNextRateControl(); + void DoEncode(scoped_refptr<Capturer::CaptureData> capture_data); - void CaptureDoneCallback(scoped_refptr<Capturer::CaptureData> capture_data); // EncodeDataAvailableTask takes ownership of header and is responsible for // deleting it. void EncodeDataAvailableTask(const UpdateStreamPacketHeader *header, const scoped_refptr<media::DataBuffer>& data, Encoder::EncodingState state); - // Getters for capturer and encoder. - Capturer* capturer(); - Encoder* encoder(); - // Message loops used by this class. MessageLoop* capture_loop_; MessageLoop* encode_loop_; |