1 // Copyright 2020 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 "cast/standalone_receiver/mirroring_application.h"
6
7 #include "cast/common/public/message_port.h"
8 #include "cast/streaming/environment.h"
9 #include "cast/streaming/message_fields.h"
10 #include "cast/streaming/receiver_session.h"
11 #include "platform/api/task_runner.h"
12 #include "util/osp_logging.h"
13
14 namespace openscreen {
15 namespace cast {
16
17 const char kMirroringAppId[] = "0F5096E8";
18 const char kMirroringAudioOnlyAppId[] = "85CDB22F";
19
20 const char kMirroringDisplayName[] = "Chrome Mirroring";
21 const char kRemotingRpcNamespace[] = "urn:x-cast:com.google.cast.remoting";
22
MirroringApplication(TaskRunner * task_runner,const IPAddress & interface_address,ApplicationAgent * agent)23 MirroringApplication::MirroringApplication(TaskRunner* task_runner,
24 const IPAddress& interface_address,
25 ApplicationAgent* agent)
26 : task_runner_(task_runner),
27 interface_address_(interface_address),
28 app_ids_({kMirroringAppId, kMirroringAudioOnlyAppId}),
29 agent_(agent) {
30 OSP_DCHECK(task_runner_);
31 OSP_DCHECK(agent_);
32 agent_->RegisterApplication(this);
33 }
34
~MirroringApplication()35 MirroringApplication::~MirroringApplication() {
36 agent_->UnregisterApplication(this); // ApplicationAgent may call Stop().
37 OSP_DCHECK(!current_session_);
38 }
39
GetAppIds() const40 const std::vector<std::string>& MirroringApplication::GetAppIds() const {
41 return app_ids_;
42 }
43
Launch(const std::string & app_id,const Json::Value & app_params,MessagePort * message_port)44 bool MirroringApplication::Launch(const std::string& app_id,
45 const Json::Value& app_params,
46 MessagePort* message_port) {
47 if ((app_id != kMirroringAppId && app_id != kMirroringAudioOnlyAppId) ||
48 !message_port || current_session_) {
49 return false;
50 }
51
52 wake_lock_ = ScopedWakeLock::Create(task_runner_);
53 environment_ = std::make_unique<Environment>(
54 &Clock::now, task_runner_,
55 IPEndpoint{interface_address_, kDefaultCastStreamingPort});
56 controller_ =
57 std::make_unique<StreamingPlaybackController>(task_runner_, this);
58 current_session_ = std::make_unique<ReceiverSession>(
59 controller_.get(), environment_.get(), message_port,
60 ReceiverSession::Preferences{});
61 return true;
62 }
63
GetSessionId()64 std::string MirroringApplication::GetSessionId() {
65 return current_session_ ? current_session_->session_id() : std::string();
66 }
67
GetDisplayName()68 std::string MirroringApplication::GetDisplayName() {
69 return current_session_ ? kMirroringDisplayName : std::string();
70 }
71
GetSupportedNamespaces()72 std::vector<std::string> MirroringApplication::GetSupportedNamespaces() {
73 return {kCastWebrtcNamespace, kRemotingRpcNamespace};
74 }
75
Stop()76 void MirroringApplication::Stop() {
77 current_session_.reset();
78 controller_.reset();
79 environment_.reset();
80 wake_lock_.reset();
81 }
82
OnPlaybackError(StreamingPlaybackController *,Error error)83 void MirroringApplication::OnPlaybackError(StreamingPlaybackController*,
84 Error error) {
85 OSP_LOG_ERROR << "[MirroringApplication] " << error;
86 agent_->StopApplicationIfRunning(this); // ApplicationAgent calls Stop().
87 }
88
89 } // namespace cast
90 } // namespace openscreen
91