blob: 49810b3409229ab70e9431676a8a6f1a441db455 [file] [log] [blame]
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/media_state_observer.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
namespace {
// Values defined in Tab.java and must be kept in sync.
enum MediaState {
NONE = 0,
MUTED = 1,
AUDIBLE = 2,
RECORDING = 3,
SHARING = 4,
};
} // namespace
// TODO(crbug.com/454045510): Add unit tests.
MediaStateObserver::MediaStateObserver(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
content::WebContentsUserData<MediaStateObserver>(*web_contents) {
media_stream_capture_indicator_observation_.Observe(
MediaCaptureDevicesDispatcher::GetInstance()
->GetMediaStreamCaptureIndicator()
.get());
}
MediaStateObserver::~MediaStateObserver() = default;
void MediaStateObserver::DidUpdateAudioMutingState(bool muted) {
if (is_audio_muted_ == muted) {
return;
}
is_audio_muted_ = muted;
UpdateMediaState();
}
void MediaStateObserver::OnAudioStateChanged(bool audible) {
if (is_audible_ == audible) {
return;
}
is_audible_ = audible;
UpdateMediaState();
}
void MediaStateObserver::OnIsCapturingAudioChanged(
content::WebContents* web_contents,
bool is_capturing_audio) {
if (web_contents != WebContentsObserver::web_contents() ||
is_capturing_audio_ == is_capturing_audio) {
return;
}
is_capturing_audio_ = is_capturing_audio;
UpdateMediaState();
}
void MediaStateObserver::OnIsCapturingVideoChanged(
content::WebContents* web_contents,
bool is_capturing_video) {
if (web_contents != WebContentsObserver::web_contents() ||
is_capturing_video_ == is_capturing_video) {
return;
}
is_capturing_video_ = is_capturing_video;
UpdateMediaState();
}
void MediaStateObserver::UpdateMediaState() {
TabAndroid* tab = TabAndroid::FromWebContents(web_contents());
if (!tab) {
return;
}
if (is_capturing_video_ || is_capturing_audio_) {
tab->SetMediaState(MediaState::RECORDING);
} else if (is_audible_) {
tab->SetMediaState(is_audio_muted_ ? MediaState::MUTED
: MediaState::AUDIBLE);
} else {
tab->SetMediaState(MediaState::NONE);
}
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(MediaStateObserver);