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_controller.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "media/base/bind_to_current_loop.h" |
| 10 |
| 11 namespace media { |
| 12 |
| 13 RemotingController::RemotingController(mojom::RemoterFactory* remoter_factory) |
| 14 : is_fullscreen_(false), |
| 15 is_sink_available_(false), |
| 16 is_remoting_(false), |
| 17 binding_(this), |
| 18 task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 19 remoter_factory->Create(binding_.CreateInterfacePtrAndBind(), |
| 20 mojo::GetProxy(&remoter_)); |
| 21 } |
| 22 |
| 23 RemotingController::~RemotingController() {} |
| 24 |
| 25 void RemotingController::OnSinkAvailable() { |
| 26 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 27 |
| 28 is_sink_available_ = true; |
| 29 Update(); |
| 30 } |
| 31 |
| 32 void RemotingController::OnSinkGone() { |
| 33 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 34 |
| 35 is_sink_available_ = false; |
| 36 Update(); |
| 37 } |
| 38 |
| 39 void RemotingController::OnStarted() { |
| 40 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 41 |
| 42 VLOG(1) << "Remoting started successively."; |
| 43 if (is_remoting_) |
| 44 switch_renderer_cb_.Run(); |
| 45 else |
| 46 remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK); |
| 47 } |
| 48 |
| 49 void RemotingController::OnStartFailed(mojom::RemotingStartFailReason reason) { |
| 50 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 51 |
| 52 VLOG(1) << "Failed to start remoting:" << reason; |
| 53 is_remoting_ = false; |
| 54 } |
| 55 |
| 56 void RemotingController::OnMessageFromSink( |
| 57 const std::vector<uint8_t>& message) { |
| 58 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 59 |
| 60 // TODO(xjz): Merge with Eric's CL to handle the RPC messages here. |
| 61 NOTIMPLEMENTED(); |
| 62 } |
| 63 |
| 64 void RemotingController::OnStopped(mojom::RemotingStopReason reason) { |
| 65 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 66 |
| 67 VLOG(1) << "Remoting stopped: " << reason; |
| 68 is_remoting_ = false; |
| 69 } |
| 70 |
| 71 void RemotingController::OnEnteredFullscreen() { |
| 72 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 73 |
| 74 is_fullscreen_ = true; |
| 75 Update(); |
| 76 } |
| 77 |
| 78 void RemotingController::OnExitedFullscreen() { |
| 79 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 80 |
| 81 is_fullscreen_ = false; |
| 82 Update(); |
| 83 } |
| 84 |
| 85 void RemotingController::OnSetCdm(CdmContext* cdm_context) { |
| 86 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 87 |
| 88 // TODO(xjz): Not implemented. Will add in up-coming change. |
| 89 NOTIMPLEMENTED(); |
| 90 } |
| 91 |
| 92 void RemotingController::SetSwitchRenderCallback( |
| 93 const SwitchRendererCallback& cb) { |
| 94 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 95 DCHECK(!cb.is_null()); |
| 96 |
| 97 switch_renderer_cb_ = cb; |
| 98 } |
| 99 |
| 100 void RemotingController::OnDecoderConfigChanged( |
| 101 const AudioDecoderConfig& audio_config, |
| 102 const VideoDecoderConfig& video_config) { |
| 103 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 104 |
| 105 audio_decoder_config_ = audio_config; |
| 106 video_decoder_config_ = video_config; |
| 107 Update(); |
| 108 } |
| 109 |
| 110 bool RemotingController::isVideoConfigSupported() { |
| 111 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 112 |
| 113 if (!video_decoder_config_.IsValidConfig()) |
| 114 return false; |
| 115 |
| 116 switch (video_decoder_config_.codec()) { |
| 117 case VideoCodec::kCodecH264: |
| 118 case VideoCodec::kCodecVP8: |
| 119 return true; |
| 120 default: |
| 121 VLOG(2) << "Remoting does not support video codec: " |
| 122 << video_decoder_config_.codec(); |
| 123 return false; |
| 124 } |
| 125 } |
| 126 |
| 127 bool RemotingController::isAudioConfigSupported() { |
| 128 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 129 |
| 130 if (!audio_decoder_config_.IsValidConfig()) |
| 131 return false; |
| 132 |
| 133 switch (audio_decoder_config_.codec()) { |
| 134 case AudioCodec::kCodecAAC: |
| 135 case AudioCodec::kCodecMP3: |
| 136 case AudioCodec::kCodecPCM: |
| 137 case AudioCodec::kCodecVorbis: |
| 138 case AudioCodec::kCodecFLAC: |
| 139 case AudioCodec::kCodecAMR_NB: |
| 140 case AudioCodec::kCodecAMR_WB: |
| 141 case AudioCodec::kCodecPCM_MULAW: |
| 142 case AudioCodec::kCodecGSM_MS: |
| 143 case AudioCodec::kCodecPCM_S16BE: |
| 144 case AudioCodec::kCodecPCM_S24BE: |
| 145 case AudioCodec::kCodecOpus: |
| 146 case AudioCodec::kCodecEAC3: |
| 147 case AudioCodec::kCodecPCM_ALAW: |
| 148 case AudioCodec::kCodecALAC: |
| 149 case AudioCodec::kCodecAC3: |
| 150 return true; |
| 151 default: |
| 152 VLOG(2) << "Remoting does not support audio codec: " |
| 153 << audio_decoder_config_.codec(); |
| 154 return false; |
| 155 } |
| 156 } |
| 157 |
| 158 void RemotingController::Update() { |
| 159 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 160 |
| 161 // TODO(xjz): The switching logic for encrypted content will be added in a |
| 162 // later CL. |
| 163 |
| 164 bool should_be_remoting = |
| 165 is_sink_available_ && is_fullscreen_ && isVideoConfigSupported() && |
| 166 isAudioConfigSupported() && !video_decoder_config_.is_encrypted() && |
| 167 !switch_renderer_cb_.is_null(); |
| 168 if (is_remoting_ == should_be_remoting) |
| 169 return; |
| 170 |
| 171 // Switch between local and remoting. |
| 172 is_remoting_ = should_be_remoting; |
| 173 if (is_remoting_) { |
| 174 remoter_->Start(); |
| 175 } else { |
| 176 switch_renderer_cb_.Run(); |
| 177 remoter_->Stop(mojom::RemotingStopReason::LOCAL_PLAYBACK); |
| 178 } |
| 179 } |
| 180 |
| 181 } // namespace media |
OLD | NEW |