Index: media/remoting/remoting_controller.cc |
diff --git a/media/remoting/remoting_controller.cc b/media/remoting/remoting_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a208f84f6de3d398e5fa53d9b0085936da244f72 |
--- /dev/null |
+++ b/media/remoting/remoting_controller.cc |
@@ -0,0 +1,182 @@ |
+// Copyright 2016 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 "media/remoting/remoting_controller.h" |
+ |
+#include "base/bind.h" |
+#include "media/base/bind_to_current_loop.h" |
+ |
+namespace media { |
+ |
+RemotingController::RemotingController(mojom::RemoterFactory* remoter_factory) |
+ : is_fullscreen_(false), |
+ is_sink_available_(false), |
+ use_remoting_renderer_(false), |
+ binding_(this), |
+ task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
+ remoter_factory->Create(binding_.CreateInterfacePtrAndBind(), |
miu
2016/09/28 07:23:09
This issue came up on Eric's change: We should be
xjz
2016/09/29 22:54:22
As discussed offline, keep this on main thread for
|
+ mojo::GetProxy(&remoter_)); |
+} |
+ |
+RemotingController::~RemotingController() {} |
+ |
+void RemotingController::OnSinkAvailable() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ is_sink_available_ = true; |
+ Update(); |
+} |
+ |
+void RemotingController::OnSinkGone() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ is_sink_available_ = false; |
+ Update(); |
+} |
+ |
+void RemotingController::OnStarted() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ VLOG(1) << "Remoting started successively."; |
+ if (use_remoting_renderer_) { |
+ if (!switch_renderer_cb_.is_null()) |
+ switch_renderer_cb_.Run(); |
+ } else { |
+ remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK); |
+ } |
+} |
+ |
+void RemotingController::OnStartFailed(mojom::RemotingStartFailReason reason) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ VLOG(1) << "Failed to start remoting: " << reason; |
+ use_remoting_renderer_ = false; |
+} |
+ |
+void RemotingController::OnMessageFromSink( |
+ const std::vector<uint8_t>& message) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ // TODO(xjz): Merge with Eric's CL to handle the RPC messages here. |
+} |
+ |
+void RemotingController::OnStopped(mojom::RemotingStopReason reason) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ VLOG(1) << "Remoting stopped: " << reason; |
+ if (use_remoting_renderer_) |
+ remoter_->Start(); |
miu
2016/09/28 01:33:32
Thinking aloud: Maybe this method should just set
xjz
2016/09/29 22:54:22
Done.
|
+} |
+ |
+void RemotingController::OnEnteredFullscreen() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ is_fullscreen_ = true; |
+ Update(); |
+} |
+ |
+void RemotingController::OnExitedFullscreen() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ is_fullscreen_ = false; |
+ Update(); |
+} |
+ |
+void RemotingController::OnSetCdm(CdmContext* cdm_context) { |
+ // TODO(xjz): Not implemented. Will add in up-coming change. |
miu
2016/09/28 01:33:32
Note: Please use the NOTIMPLEMENTED() logging macr
xjz
2016/09/29 22:54:22
Done.
|
+} |
+ |
+void RemotingController::SetSwitchRenderCallback( |
+ const RendererController::SwitchRendererCallback& cb) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ DCHECK(!cb.is_null()); |
+ |
+ switch_renderer_cb_ = BindToCurrentLoop(cb); |
+} |
+ |
+void RemotingController::OnSetDecoderConfig( |
+ const AudioDecoderConfig& audio_config, |
+ const VideoDecoderConfig& video_config) { |
+ audio_decoder_config_ = audio_config; |
miu
2016/09/28 01:33:32
Please add DCHECK(task_runner->Belongs()) here.
B
xjz
2016/09/29 22:54:21
Done.
|
+ video_decoder_config_ = video_config; |
+ Update(); |
+} |
+ |
+bool RemotingController::isVideoConfigSupported() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ if (!video_decoder_config_.IsValidConfig()) |
+ return false; |
+ |
+ switch (video_decoder_config_.codec()) { |
+ case VideoCodec::kCodecH264: |
+ case VideoCodec::kCodecVP8: |
+ return true; |
+ default: |
+ VLOG(2) << "Remoting does not support video codec: " |
+ << video_decoder_config_.codec(); |
+ return false; |
+ } |
+} |
+ |
+bool RemotingController::isAudioConfigSupported() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ if (!audio_decoder_config_.IsValidConfig()) |
+ return false; |
+ |
+ switch (audio_decoder_config_.codec()) { |
+ case AudioCodec::kCodecAAC: |
+ case AudioCodec::kCodecMP3: |
+ case AudioCodec::kCodecPCM: |
+ case AudioCodec::kCodecVorbis: |
+ case AudioCodec::kCodecFLAC: |
+ case AudioCodec::kCodecAMR_NB: |
+ case AudioCodec::kCodecAMR_WB: |
+ case AudioCodec::kCodecPCM_MULAW: |
+ case AudioCodec::kCodecGSM_MS: |
+ case AudioCodec::kCodecPCM_S16BE: |
+ case AudioCodec::kCodecPCM_S24BE: |
+ case AudioCodec::kCodecOpus: |
+ case AudioCodec::kCodecEAC3: |
+ case AudioCodec::kCodecPCM_ALAW: |
+ case AudioCodec::kCodecALAC: |
+ case AudioCodec::kCodecAC3: |
+ return true; |
+ default: |
+ VLOG(2) << "Remoting does not support audio codec: " |
+ << audio_decoder_config_.codec(); |
+ return false; |
+ } |
+} |
+ |
+void RemotingController::Update() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ // TODO(xjz): The swithcing logic for encrypted content will be added in a |
miu
2016/09/28 01:33:32
typo: s/swithcing/switching/
xjz
2016/09/29 22:54:21
Done.
|
+ // later CL. |
+ if (video_decoder_config_.is_encrypted()) |
miu
2016/09/28 01:33:32
Instead of returning early, I'd suggest you add it
xjz
2016/09/29 22:54:22
Done.
|
+ return; |
+ |
+ bool should_remoting = is_sink_available_ && is_fullscreen_ && |
miu
2016/09/28 01:33:32
naming: should_be_remoting
xjz
2016/09/29 22:54:21
Done.
|
+ isVideoConfigSupported() && isAudioConfigSupported(); |
+ if (use_remoting_renderer_ == should_remoting) |
+ return; |
+ |
+ // Switch between local and remoting. |
+ use_remoting_renderer_ = should_remoting; |
+ if (use_remoting_renderer_) { |
+ remoter_->Start(); |
+ } else { |
+ if (!switch_renderer_cb_.is_null()) |
miu
2016/09/28 01:33:32
In the "should" boolean above, I'd suggest adding:
xjz
2016/09/29 22:54:22
Done.
|
+ switch_renderer_cb_.Run(); |
+ // TODO(xjz): Send RPC message to notify ChromeCast. |
+ remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK); |
+ } |
+} |
+ |
+bool RemotingController::ShouldUseRemotingRenderer() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ |
+ return use_remoting_renderer_; |
+} |
+ |
+} // namespace media |