Chromium Code Reviews| 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..59868c1f479df9b6f3730e2fa63b71051797473c |
| --- /dev/null |
| +++ b/media/remoting/remoting_controller.cc |
| @@ -0,0 +1,275 @@ |
| +// 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 "base/logging.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "media/remoting/remoting_cdm.h" |
| + |
| +namespace media { |
| + |
| +RemotingController::RemotingController( |
| + mojom::RemotingSourceRequest source_request, |
| + mojom::RemoterPtr remoter) |
| + : binding_(this, std::move(source_request)), |
| + remoter_(std::move(remoter)), |
| + task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| + weak_factory_(this) {} |
| + |
| +RemotingController::~RemotingController() {} |
| + |
| +void RemotingController::OnSinkAvailable() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + is_sink_available_ = true; |
| + UpdateAndMaybeSwitch(); |
| +} |
| + |
| +void RemotingController::OnSinkGone() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + is_sink_available_ = false; |
| + UpdateAndMaybeSwitch(); |
| +} |
| + |
| +void RemotingController::OnStarted() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + VLOG(1) << "Remoting started successively."; |
| + |
| + // |switch_renderer_cb_| not being set indicates that this remoting session |
| + // is started for creating CDM. |
| + if (switch_renderer_cb_.is_null()) { |
| + is_remoting_cdm_ = true; |
| + DCHECK(!cdm_check_cb_.is_null()); |
| + cdm_check_cb_.Run(true); |
| + } else if (is_remoting_) { |
| + 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; |
| + |
| + is_remoting_ = false; |
| + if (switch_renderer_cb_.is_null()) { |
| + DCHECK(!cdm_check_cb_.is_null()); |
| + is_remoting_cdm_ = false; |
| + cdm_check_cb_.Run(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. |
| + NOTIMPLEMENTED(); |
| +} |
| + |
| +void RemotingController::OnStopped(mojom::RemotingStopReason reason) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + VLOG(1) << "Remoting stopped: " << reason; |
| + |
| + is_remoting_ = false; |
| + if (is_remoting_cdm_) |
| + is_remoting_failed_ = true; |
| +} |
| + |
| +void RemotingController::OnEnteredFullscreen() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + is_fullscreen_ = true; |
| + UpdateAndMaybeSwitch(); |
| +} |
| + |
| +void RemotingController::OnExitedFullscreen() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + is_fullscreen_ = false; |
| + UpdateAndMaybeSwitch(); |
| +} |
| + |
| +void RemotingController::OnSetCdm(CdmContext* cdm_context) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + if (auto* cdm = RemotingCdm::From(cdm_context)) { |
|
miu
2016/10/08 01:06:16
Possible edge-case issue: What if a remoting sessi
xjz
2016/10/20 21:25:28
This seems not a problem now. If a remoting sessio
|
| + cdm_remoting_controller_ = cdm->remoting_controller(); |
| + if (!cdm_remoting_controller_) |
| + return; |
| + cdm_remoting_controller_->SetSwitchRendererCallback(switch_renderer_cb_); |
| + // This may trigger the switching renderer call in |
| + // |cdm_remoting_controller_|. |
| + cdm_remoting_controller_->OnMetadataChanged(metadata_); |
| + } |
| +} |
| + |
| +void RemotingController::SetSwitchRendererCallback( |
| + const SwitchRendererCallback& cb) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + DCHECK(!cb.is_null()); |
| + |
| + switch_renderer_cb_ = cb; |
| +} |
| + |
| +void RemotingController::OnMetadataChanged(const PipelineMetadata& metadata) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + if (cdm_remoting_controller_) { |
| + cdm_remoting_controller_->OnMetadataChanged(metadata); |
| + return; |
| + } |
| + |
| + metadata_ = metadata; |
| + has_video_ = metadata.has_video; |
| + has_audio_ = metadata.has_audio; |
| + if (!has_video_ && !has_audio_) |
| + return; |
| + |
| + if (has_video_) { |
| + DCHECK(metadata.video_decoder_config.IsValidConfig()); |
| + video_decoder_config_ = metadata.video_decoder_config; |
| + is_encrypted_ |= video_decoder_config_.is_encrypted(); |
| + } |
| + if (has_audio_) { |
| + DCHECK(metadata.audio_decoder_config.IsValidConfig()); |
| + audio_decoder_config_ = metadata.audio_decoder_config; |
| + is_encrypted_ |= audio_decoder_config_.is_encrypted(); |
| + } |
| + UpdateAndMaybeSwitch(); |
| +} |
| + |
| +bool RemotingController::IsVideoCodecSupported() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + DCHECK(has_video_); |
| + |
| + 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::IsAudioCodecSupported() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + DCHECK(has_audio_); |
| + |
| + 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; |
| + } |
| +} |
| + |
| +bool RemotingController::ShouldBeRemoting() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + if (has_video_ && !IsVideoCodecSupported()) |
| + return false; |
| + if (has_audio_ && !IsAudioCodecSupported()) |
| + return false; |
| + |
| + if (is_encrypted_) { |
| + DCHECK(!cdm_remoting_controller_); |
| + return is_remoting_cdm_ && is_sink_available_; |
| + } |
| + |
| + if (!is_sink_available_) |
| + return false; |
| + if (!is_fullscreen_) |
| + return false; |
| + return true; |
| +} |
| + |
| +void RemotingController::UpdateAndMaybeSwitch() { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + |
| + // Demuxer is not initialized yet. |
| + if (!has_audio_ && !has_video_) |
| + return; |
| + |
| + // This RemotingController is created when creating CDM, and is not attached |
| + // to media element yet. |
| + if (switch_renderer_cb_.is_null()) |
| + return; |
| + |
| + // When cdm is attached, renderer switching will only happen in |
| + // |cdm_remoting_controller_| instead of here. |
| + if (cdm_remoting_controller_) |
| + return; |
| + |
| + // Remoting was failed and is not able to be restarted until user explicitly |
| + // reloads the page. |
| + if (is_remoting_failed_) |
| + return; |
| + |
| + bool should_be_remoting = ShouldBeRemoting(); |
| + if (is_remoting_ == should_be_remoting) |
| + return; |
| + |
| + // Switch between local renderer and remoting renderer. |
| + is_remoting_ = should_be_remoting; |
| + if (is_remoting_) { |
| + if (!is_remoting_cdm_) |
| + // |swithc_renderer_cb_.Run()| will be called after remoting is started |
| + // successfully for non-encrypted contents. |
| + remoter_->Start(); |
| + else |
| + // Remoting was already started for encrypted contents. Calling |
| + // |switch_renderer_cb_| to switch to remoting renderer. |
| + switch_renderer_cb_.Run(); |
| + } else { |
| + switch_renderer_cb_.Run(); |
| + remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK); |
| + if (is_remoting_cdm_) |
| + // This is also set in OnStopped(). |
| + is_remoting_failed_ = true; |
| + } |
| +} |
| + |
| +void RemotingController::ShouldCreateRemotingCdm(const CdmCheckCallback& cb) { |
| + DCHECK(task_runner_->BelongsToCurrentThread()); |
| + DCHECK(!cb.is_null()); |
| + DCHECK(switch_renderer_cb_.is_null()); |
| + |
| + cdm_check_cb_ = cb; |
| + if (!is_sink_available_) { |
| + cdm_check_cb_.Run(false); |
|
miu
2016/10/08 01:06:16
What if the sink becomes available later? Say, in
xjz
2016/10/20 21:25:28
Can we just consider simple case? If the sink is n
|
| + is_remoting_cdm_ = false; |
| + } else { |
| + // Will run |cdm_check_cb_| in OnStarted() or OnStartFailed. |
| + remoter_->Start(); |
| + } |
| +} |
| + |
| +} // namespace media |