#include "chrome/browser/media/cast_mirroring_service_host.h"
#include <algorithm>
#include <optional>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "cast_mirroring_service_host.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/media/cast_remoting_connector.h"
#include "chrome/browser/media/router/discovery/access_code/access_code_cast_feature.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/tab_sharing/tab_sharing_ui.h"
#include "components/access_code_cast/common/access_code_cast_metrics.h"
#include "components/mirroring/browser/single_client_video_capture_host.h"
#include "components/mirroring/mojom/cast_message_channel.mojom.h"
#include "components/mirroring/mojom/mirroring_service.mojom.h"
#include "components/mirroring/mojom/session_observer.mojom.h"
#include "components/mirroring/mojom/session_parameters.mojom.h"
#include "content/public/browser/audio_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/desktop_streams_registry.h"
#include "content/public/browser/gpu_client.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/service_process_host.h"
#include "content/public/browser/video_capture_device_launcher.h"
#include "content/public/browser/web_contents.h"
#include "media/audio/audio_device_description.h"
#include "media/base/media_switches.h"
#include "media/capture/mojom/video_capture.mojom.h"
#include "media/capture/video_capture_types.h"
#include "media/mojo/mojom/audio_data_pipe.mojom.h"
#include "media/mojo/mojom/audio_input_stream.mojom.h"
#include "media/mojo/mojom/audio_processing.mojom.h"
#include "media/mojo/services/mojo_video_encoder_metrics_provider_service.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/viz/public/mojom/gpu.mojom.h"
#include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "url/origin.h"
BrowserThread;
namespace mirroring {
namespace {
AudioInputStream;
AudioInputStreamClient;
AudioInputStreamObserver;
constexpr gfx::Size kMaxResolution(1920, 1080);
static const char* kPassthroughSwitches[]{ … };
mojo::SelfOwnedReceiverRef<media::mojom::VideoCaptureHost>
CreateVideoCaptureHostOnIO(
const std::string& device_id,
blink::mojom::MediaStreamType type,
mojo::PendingReceiver<media::mojom::VideoCaptureHost> receiver) { … }
void PauseVideoCaptureHostOnIO(media::mojom::VideoCaptureHost* host,
base::UnguessableToken device_id,
base::OnceClosure on_paused_callback) { … }
void ResumeVideoCaptureHostOnIO(media::mojom::VideoCaptureHost* host,
base::UnguessableToken device_id,
base::UnguessableToken session_id,
media::VideoCaptureParams params,
base::OnceClosure on_resumed_callback) { … }
blink::mojom::MediaStreamType ConvertVideoStreamType(
content::DesktopMediaID::Type type) { … }
content::WebContents* GetContents(
const content::WebContentsMediaCaptureId& id) { … }
Profile* GetProfileOrLastUsedProfile(content::WebContents* web_contents) { … }
bool IsAccessCodeCastTabSwitchingUIEnabled(
const content::WebContentsMediaCaptureId& id) { … }
std::optional<gfx::Size> GetScreenResolution() { … }
}
CastMirroringServiceHost::CastMirroringServiceHost(
content::DesktopMediaID source_media_id)
: … { … }
CastMirroringServiceHost::~CastMirroringServiceHost() { … }
void CastMirroringServiceHost::Start(
mojom::SessionParametersPtr session_params,
mojo::PendingRemote<mojom::SessionObserver> observer,
mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel,
mojo::PendingReceiver<mojom::CastMessageChannel> inbound_channel,
const std::string& sink_name) { … }
std::optional<int> CastMirroringServiceHost::GetTabSourceId() const { … }
content::DesktopMediaID CastMirroringServiceHost::BuildMediaIdForWebContents(
content::WebContents* contents) { … }
gfx::Size CastMirroringServiceHost::GetCaptureResolutionConstraint() { … }
gfx::Size CastMirroringServiceHost::GetClampedResolution(
gfx::Size screen_resolution) { … }
void CastMirroringServiceHost::BindGpu(
mojo::PendingReceiver<viz::mojom::Gpu> receiver) { … }
void CastMirroringServiceHost::GetVideoCaptureHost(
mojo::PendingReceiver<media::mojom::VideoCaptureHost> receiver) { … }
void CastMirroringServiceHost::GetVideoEncoderMetricsProvider(
mojo::PendingReceiver<media::mojom::VideoEncoderMetricsProvider> receiver) { … }
void CastMirroringServiceHost::SetVideoCaptureHost(
mojo::SelfOwnedReceiverRef<media::mojom::VideoCaptureHost>
video_capture_host) { … }
void CastMirroringServiceHost::GetNetworkContext(
mojo::PendingReceiver<network::mojom::NetworkContext> receiver) { … }
void CastMirroringServiceHost::CreateAudioStream(
mojo::PendingRemote<mojom::AudioStreamCreatorClient> requestor,
const media::AudioParameters& params,
uint32_t total_segments) { … }
void CastMirroringServiceHost::CreateAudioStreamForTab(
mojo::PendingRemote<mojom::AudioStreamCreatorClient> requestor,
const media::AudioParameters& params,
uint32_t total_segments,
const base::UnguessableToken& group_id) { … }
void CastMirroringServiceHost::CreateAudioStreamForDesktop(
mojo::PendingRemote<mojom::AudioStreamCreatorClient> requestor,
const media::AudioParameters& params,
uint32_t total_segments) { … }
void CastMirroringServiceHost::ConnectToRemotingSource(
mojo::PendingRemote<media::mojom::Remoter> remoter,
mojo::PendingReceiver<media::mojom::RemotingSource> receiver) { … }
void CastMirroringServiceHost::WebContentsDestroyed() { … }
void CastMirroringServiceHost::ShowCaptureIndicator() { … }
void CastMirroringServiceHost::ShowTabSharingUI(
const blink::mojom::StreamDevices& devices) { … }
void CastMirroringServiceHost::SwitchMirroringSourceTab(
const content::DesktopMediaID& media_id,
bool captured_surface_control_active) { … }
void CastMirroringServiceHost::RecordTabUIUsageMetricsIfNeededAndReset() { … }
void CastMirroringServiceHost::DestroyTab(OffscreenTab* tab) { … }
void CastMirroringServiceHost::OpenOffscreenTab(
content::BrowserContext* context,
const GURL& presentation_url,
const std::string& presentation_id) { … }
void CastMirroringServiceHost::Pause(base::OnceClosure on_paused_callback) { … }
void CastMirroringServiceHost::Resume(base::OnceClosure on_resumed_callback) { … }
void CastMirroringServiceHost::GetMirroringStats(
base::OnceCallback<void(const base::Value)> json_stats_cb) { … }
}