#include "components/mirroring/service/openscreen_session_host.h"
#include <algorithm>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "base/cpu.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/task/bind_post_task.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/time/default_tick_clock.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/mirroring/service/captured_audio_input.h"
#include "components/mirroring/service/mirroring_features.h"
#include "components/mirroring/service/rpc_dispatcher_impl.h"
#include "components/mirroring/service/video_capture_client.h"
#include "components/openscreen_platform/network_context.h"
#include "components/openscreen_platform/network_util.h"
#include "gpu/config/gpu_feature_info.h"
#include "gpu/ipc/client/gpu_channel_host.h"
#include "media/audio/audio_input_device.h"
#include "media/base/audio_capturer_source.h"
#include "media/base/audio_codecs.h"
#include "media/base/audio_parameters.h"
#include "media/base/video_codecs.h"
#include "media/capture/video_capture_types.h"
#include "media/cast/common/openscreen_conversion_helpers.h"
#include "media/cast/common/packet.h"
#include "media/cast/encoding/encoding_support.h"
#include "media/cast/openscreen/config_conversions.h"
#include "media/cast/sender/audio_sender.h"
#include "media/cast/sender/video_sender.h"
#include "media/gpu/gpu_video_accelerator_util.h"
#include "media/mojo/clients/mojo_video_encode_accelerator.h"
#include "media/mojo/clients/mojo_video_encoder_metrics_provider.h"
#include "media/video/video_encode_accelerator.h"
#include "mojo/public/cpp/system/platform_handle.h"
#include "net/base/ip_endpoint.h"
#include "services/viz/public/cpp/gpu/gpu.h"
#include "third_party/openscreen/src/cast/streaming/answer_messages.h"
#include "third_party/openscreen/src/cast/streaming/capture_recommendations.h"
#include "third_party/openscreen/src/cast/streaming/environment.h"
#include "third_party/openscreen/src/cast/streaming/offer_messages.h"
FrameEvent;
FrameSenderConfig;
OperationalStatus;
Packet;
PacketEvent;
RtpPayloadType;
RemotingSinkAudioCapability;
RemotingSinkVideoCapability;
SessionError;
SessionType;
namespace mirroring {
namespace {
constexpr base::TimeDelta kBandwidthUpdateInterval = …;
constexpr base::TimeDelta kStartRemotePlaybackTimeOut = …;
constexpr char kLogPrefix[] = …;
int NumberOfEncodeThreads() { … }
media::mojom::RemotingSinkMetadata ToRemotingSinkMetadata(
const openscreen::cast::RemotingCapabilities& capabilities,
const std::string& friendly_name) { … }
void UpdateConfigUsingSessionParameters(
const mojom::SessionParameters& session_params,
FrameSenderConfig& config) { … }
void UpdateAudioConfigMaxBitrate(FrameSenderConfig& audio_config) { … }
const std::string ToString(const media::VideoCaptureParams& params) { … }
void RecordRemotePlaybackSessionLoadTime(std::optional<base::Time> start_time) { … }
void RecordRemotePlaybackSessionStartsBeforeTimeout(bool started) { … }
}
class OpenscreenSessionHost::AudioCapturingCallback final
: public media::AudioCapturerSource::CaptureCallback { … };
OpenscreenSessionHost::OpenscreenSessionHost(
mojom::SessionParametersPtr session_params,
const gfx::Size& max_resolution,
mojo::PendingRemote<mojom::SessionObserver> observer,
mojo::PendingRemote<mojom::ResourceProvider> resource_provider,
mojo::PendingRemote<mojom::CastMessageChannel> outbound_channel,
mojo::PendingReceiver<mojom::CastMessageChannel> inbound_channel,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
: … { … }
OpenscreenSessionHost::~OpenscreenSessionHost() { … }
void OpenscreenSessionHost::AsyncInitialize(
AsyncInitializedCallback initialized_cb) { … }
void OpenscreenSessionHost::OnNegotiated(
const openscreen::cast::SenderSession* session,
openscreen::cast::SenderSession::ConfiguredSenders senders,
Recommendations capture_recommendations) { … }
void OpenscreenSessionHost::OnCapabilitiesDetermined(
const openscreen::cast::SenderSession* session,
openscreen::cast::RemotingCapabilities capabilities) { … }
void OpenscreenSessionHost::OnError(
const openscreen::cast::SenderSession* session,
const openscreen::Error& error) { … }
void OpenscreenSessionHost::OnError(const std::string& message) { … }
void OpenscreenSessionHost::RequestRefreshFrame() { … }
void OpenscreenSessionHost::CreateVideoEncodeAccelerator(
media::cast::ReceiveVideoEncodeAcceleratorCallback callback) { … }
void OpenscreenSessionHost::ConnectToRemotingSource(
mojo::PendingRemote<media::mojom::Remoter> remoter,
mojo::PendingReceiver<media::mojom::RemotingSource> receiver) { … }
void OpenscreenSessionHost::RequestRemotingStreaming() { … }
void OpenscreenSessionHost::RestartMirroringStreaming() { … }
void OpenscreenSessionHost::SwitchSourceTab() { … }
void OpenscreenSessionHost::OnAsyncInitialized(
const SupportedProfiles& profiles) { … }
void OpenscreenSessionHost::ReportAndLogError(SessionError error,
std::string_view message) { … }
void OpenscreenSessionHost::StopStreaming() { … }
void OpenscreenSessionHost::StopSession() { … }
void OpenscreenSessionHost::SetConstraints(
const Recommendations& recommendations,
std::optional<FrameSenderConfig>& audio_config,
std::optional<FrameSenderConfig>& video_config) { … }
void OpenscreenSessionHost::CreateAudioStream(
mojo::PendingRemote<mojom::AudioStreamCreatorClient> client,
const media::AudioParameters& params,
uint32_t shared_memory_count) { … }
void OpenscreenSessionHost::OnEncoderStatusChange(OperationalStatus status) { … }
void OpenscreenSessionHost::SetTargetPlayoutDelay(
base::TimeDelta playout_delay) { … }
void OpenscreenSessionHost::ProcessFeedback(
const media::VideoCaptureFeedback& feedback) { … }
int OpenscreenSessionHost::GetSuggestedVideoBitrate(int min_bitrate,
int max_bitrate) const { … }
void OpenscreenSessionHost::UpdateBandwidthEstimate() { … }
void OpenscreenSessionHost::Negotiate() { … }
void OpenscreenSessionHost::NegotiateMirroring() { … }
void OpenscreenSessionHost::NegotiateRemoting() { … }
void OpenscreenSessionHost::InitMediaRemoter(
const openscreen::cast::RemotingCapabilities& capabilities) { … }
void OpenscreenSessionHost::OnRemotingStartTimeout() { … }
network::mojom::NetworkContext* OpenscreenSessionHost::GetNetworkContext() { … }
base::Value::Dict OpenscreenSessionHost::GetMirroringStats() const { … }
void OpenscreenSessionHost::SetSenderStatsForTest(
const openscreen::cast::SenderStats& test_stats) { … }
}