Skip to content

Commit

Permalink
Adding support for Media commands (beyond LaunchURL) to tv-casting-co…
Browse files Browse the repository at this point in the history
…mmon (#20779)

* Adding support for Media commands (beyond LaunchURL) to tv-casting-common

* Prefixed command names with cluster names for better organization
  • Loading branch information
sharadb-amazon authored Jul 19, 2022
1 parent bc706b7 commit 16cae34
Show file tree
Hide file tree
Showing 16 changed files with 840 additions and 37 deletions.
13 changes: 13 additions & 0 deletions examples/tv-casting-app/tv-casting-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,24 @@ chip_data_model("tv-casting-common") {
"${chip_root}/zzz_generated/chip-tool/zap-generated/cluster/logging/DataModelLogger.cpp",
"commands/clusters/ModelCommand.cpp",
"commands/common/CHIPCommand.cpp",
"include/ApplicationLauncher.h",
"include/CastingServer.h",
"include/ContentLauncher.h",
"include/KeypadInput.h",
"include/LevelControl.h",
"include/MediaCommandBase.h",
"include/MediaPlayback.h",
"include/TargetEndpointInfo.h",
"include/TargetNavigator.h",
"include/TargetVideoPlayerInfo.h",
"src/ApplicationLauncher.cpp",
"src/CastingServer.cpp",
"src/ContentLauncher.cpp",
"src/KeypadInput.cpp",
"src/LevelControl.cpp",
"src/MediaPlayback.cpp",
"src/TargetEndpointInfo.cpp",
"src/TargetNavigator.cpp",
"src/TargetVideoPlayerInfo.cpp",
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MediaCommandBase.h"

#include <functional>
#include <zap-generated/CHIPClusters.h>

class LaunchAppCommand
: public MediaCommandBase<chip::app::Clusters::ApplicationLauncher::Commands::LaunchApp::Type,
chip::app::Clusters::ApplicationLauncher::Commands::LauncherResponse::DecodableType>
{
public:
LaunchAppCommand() : MediaCommandBase(chip::app::Clusters::ApplicationLauncher::Id) {}

CHIP_ERROR Invoke(chip::app::Clusters::ApplicationLauncher::Structs::Application::Type application,
chip::Optional<chip::ByteSpan> data, std::function<void(CHIP_ERROR)> responseCallback);
};

class StopAppCommand : public MediaCommandBase<chip::app::Clusters::ApplicationLauncher::Commands::StopApp::Type,
chip::app::Clusters::ApplicationLauncher::Commands::LauncherResponse::DecodableType>
{
public:
StopAppCommand() : MediaCommandBase(chip::app::Clusters::ApplicationLauncher::Id) {}

CHIP_ERROR Invoke(chip::app::Clusters::ApplicationLauncher::Structs::Application::Type application,
std::function<void(CHIP_ERROR)> responseCallback);
};

class HideAppCommand : public MediaCommandBase<chip::app::Clusters::ApplicationLauncher::Commands::HideApp::Type,
chip::app::Clusters::ApplicationLauncher::Commands::LauncherResponse::DecodableType>
{
public:
HideAppCommand() : MediaCommandBase(chip::app::Clusters::ApplicationLauncher::Id) {}

CHIP_ERROR Invoke(chip::app::Clusters::ApplicationLauncher::Structs::Application::Type application,
std::function<void(CHIP_ERROR)> responseCallback);
};
89 changes: 81 additions & 8 deletions examples/tv-casting-app/tv-casting-common/include/CastingServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@

#pragma once

#include "ApplicationLauncher.h"
#include "ContentLauncher.h"
#include "KeypadInput.h"
#include "LevelControl.h"
#include "MediaPlayback.h"
#include "TargetEndpointInfo.h"
#include "TargetNavigator.h"
#include "TargetVideoPlayerInfo.h"

#include <app/server/Server.h>
#include <controller/CHIPCommissionableNodeController.h>
#include <functional>
#include <zap-generated/CHIPClusters.h>

#include "TargetEndpointInfo.h"
#include "TargetVideoPlayerInfo.h"

constexpr chip::System::Clock::Seconds16 kCommissioningWindowTimeout = chip::System::Clock::Seconds16(3 * 60);
constexpr chip::EndpointId kTvEndpoint = 1;

Expand Down Expand Up @@ -58,17 +64,44 @@ class CastingServer
const chip::app::DataModel::DecodableList<chip::ClusterId> & responseList);
static void OnDescriptorReadFailureResponse(void * context, CHIP_ERROR error);

CHIP_ERROR ContentLauncherLaunchURL(const char * contentUrl, const char * contentDisplayStr,
std::function<void(CHIP_ERROR)> launchURLResponseCallback);
static void OnContentLauncherSuccessResponse(
void * context, const chip::app::Clusters::ContentLauncher::Commands::LaunchResponse::DecodableType & response);
static void OnContentLauncherFailureResponse(void * context, CHIP_ERROR error);
[[deprecated("Use ContentLauncher_LaunchURL(..) instead")]] CHIP_ERROR
ContentLauncherLaunchURL(const char * contentUrl, const char * contentDisplayStr,
std::function<void(CHIP_ERROR)> launchURLResponseCallback);

chip::NodeId GetVideoPlayerNodeForFabricIndex(chip::FabricIndex fabricIndex);
chip::FabricIndex GetVideoPlayerFabricIndexForNode(chip::NodeId nodeId);
chip::FabricIndex CurrentFabricIndex() { return mTargetVideoPlayerInfo.GetFabricIndex(); }
void SetDefaultFabricIndex();

CHIP_ERROR ContentLauncher_LaunchURL(
const char * contentUrl, const char * contentDisplayStr,
chip::Optional<chip::app::Clusters::ContentLauncher::Structs::BrandingInformation::Type> brandingInformation,
std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR ContentLauncher_LaunchContent(chip::app::Clusters::ContentLauncher::Structs::ContentSearch::Type search,
bool autoPlay, chip::Optional<chip::CharSpan> data,
std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR LevelControl_Step(chip::app::Clusters::LevelControl::StepMode stepMode, uint8_t stepSize, uint16_t transitionTime,
uint8_t optionMask, uint8_t optionOverride, std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR LevelControl_MoveToLevel(uint8_t level, uint16_t transitionTime, uint8_t optionMask, uint8_t optionOverride,
std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_Play(std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_Pause(std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_StopPlayback(std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_Next(std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_Seek(uint64_t position, std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_SkipForward(uint64_t deltaPositionMilliseconds, std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR MediaPlayback_SkipBackward(uint64_t deltaPositionMilliseconds, std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR ApplicationLauncher_LaunchApp(chip::app::Clusters::ApplicationLauncher::Structs::Application::Type application,
chip::Optional<chip::ByteSpan> data, std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR ApplicationLauncher_StopApp(chip::app::Clusters::ApplicationLauncher::Structs::Application::Type application,
std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR ApplicationLauncher_HideApp(chip::app::Clusters::ApplicationLauncher::Structs::Application::Type application,
std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR TargetNavigator_NavigateTarget(const uint8_t target, const chip::Optional<chip::CharSpan> data,
std::function<void(CHIP_ERROR)> responseCallback);
CHIP_ERROR KeypadInput_SendKey(const chip::app::Clusters::KeypadInput::CecKeyCode keyCode,
std::function<void(CHIP_ERROR)> responseCallback);

private:
CHIP_ERROR InitBindingHandlers();
static void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
Expand All @@ -81,4 +114,44 @@ class CastingServer
chip::Controller::CommissionableNodeController mCommissionableNodeController;
std::function<void(CHIP_ERROR)> mLaunchURLResponseCallback;
std::function<void(CHIP_ERROR)> mCommissioningCompleteCallback;

/**
* @brief Content Launcher cluster
*/
LaunchURLCommand mLaunchURLCommand;
LaunchContentCommand mLaunchContentCommand;

/**
* @brief Level Control cluster
*/
StepCommand mStepCommand;
MoveToLevelCommand mMoveToLevelCommand;

/**
* @brief Media Playback cluster
*/
PlayCommand mPlayCommand;
PauseCommand mPauseCommand;
StopPlaybackCommand mStopPlaybackCommand;
NextCommand mNextCommand;
SeekCommand mSeekCommand;
SkipForwardCommand mSkipForwardCommand;
SkipBackwardCommand mSkipBackwardCommand;

/**
* @brief Application Launcher cluster
*/
LaunchAppCommand mLaunchAppCommand;
StopAppCommand mStopAppCommand;
HideAppCommand mHideAppCommand;

/**
* @brief Target Navigator cluster
*/
NavigateTargetCommand mNavigateTargetCommand;

/**
* @brief Keypad Input cluster
*/
SendKeyCommand mSendKeyCommand;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MediaCommandBase.h"

#include <functional>
#include <zap-generated/CHIPClusters.h>

class LaunchURLCommand : public MediaCommandBase<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type,
chip::app::Clusters::ContentLauncher::Commands::LaunchResponse::DecodableType>
{
public:
LaunchURLCommand() : MediaCommandBase(chip::app::Clusters::ContentLauncher::Id) {}

CHIP_ERROR Invoke(const char * contentUrl, const char * contentDisplayStr,
chip::Optional<chip::app::Clusters::ContentLauncher::Structs::BrandingInformation::Type> brandingInformation,
std::function<void(CHIP_ERROR)> responseCallback);
};

class LaunchContentCommand : public MediaCommandBase<chip::app::Clusters::ContentLauncher::Commands::LaunchContent::Type,
chip::app::Clusters::ContentLauncher::Commands::LaunchResponse::DecodableType>
{
public:
LaunchContentCommand() : MediaCommandBase(chip::app::Clusters::ContentLauncher::Id) {}

CHIP_ERROR Invoke(chip::app::Clusters::ContentLauncher::Structs::ContentSearch::Type search, bool autoPlay,
chip::Optional<chip::CharSpan> data, std::function<void(CHIP_ERROR)> responseCallback);
};
31 changes: 31 additions & 0 deletions examples/tv-casting-app/tv-casting-common/include/KeypadInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MediaCommandBase.h"

#include <functional>
#include <zap-generated/CHIPClusters.h>

class SendKeyCommand : public MediaCommandBase<chip::app::Clusters::KeypadInput::Commands::SendKey::Type,
chip::app::Clusters::KeypadInput::Commands::SendKeyResponse::DecodableType>
{
public:
SendKeyCommand() : MediaCommandBase(chip::app::Clusters::KeypadInput::Id) {}

CHIP_ERROR Invoke(const chip::app::Clusters::KeypadInput::CecKeyCode keyCode, std::function<void(CHIP_ERROR)> responseCallback);
};
42 changes: 42 additions & 0 deletions examples/tv-casting-app/tv-casting-common/include/LevelControl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MediaCommandBase.h"

#include <functional>
#include <zap-generated/CHIPClusters.h>

class StepCommand
: public MediaCommandBase<chip::app::Clusters::LevelControl::Commands::Step::Type, chip::app::DataModel::NullObjectType>
{
public:
StepCommand() : MediaCommandBase(chip::app::Clusters::LevelControl::Id) {}

CHIP_ERROR Invoke(chip::app::Clusters::LevelControl::StepMode stepMode, uint8_t stepSize, uint16_t transitionTime,
uint8_t optionMask, uint8_t optionOverride, std::function<void(CHIP_ERROR)> responseCallback);
};

class MoveToLevelCommand
: public MediaCommandBase<chip::app::Clusters::LevelControl::Commands::MoveToLevel::Type, chip::app::DataModel::NullObjectType>
{
public:
MoveToLevelCommand() : MediaCommandBase(chip::app::Clusters::LevelControl::Id) {}

CHIP_ERROR Invoke(uint8_t level, uint16_t transitionTime, uint8_t optionMask, uint8_t optionOverride,
std::function<void(CHIP_ERROR)> responseCallback);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once

#include "TargetVideoPlayerInfo.h"

#include <controller/CHIPCluster.h>
#include <functional>

template <typename RequestType, typename ResponseType>
class MediaCommandBase
{
public:
MediaCommandBase(chip::ClusterId clusterId) { mClusterId = clusterId; }

CHIP_ERROR SetTarget(TargetVideoPlayerInfo & mTargetVideoPlayerInfo, chip::EndpointId tvEndpoint)
{
mOperationalDeviceProxy = mTargetVideoPlayerInfo.GetOperationalDeviceProxy();
if (mOperationalDeviceProxy == nullptr)
{
ChipLogError(AppServer, "Failed in getting an instance of OperationalDeviceProxy");
return CHIP_ERROR_PEER_NODE_NOT_FOUND;
}

mTvEndpoint = tvEndpoint;
return CHIP_NO_ERROR;
}

CHIP_ERROR Invoke(RequestType request, std::function<void(CHIP_ERROR)> responseCallback)
{
VerifyOrDieWithMsg(mOperationalDeviceProxy != nullptr, AppServer, "Target unknown");

sResponseCallback = responseCallback;

class MediaClusterBase : public chip::Controller::ClusterBase
{
public:
MediaClusterBase(chip::Messaging::ExchangeManager & exchangeManager, const chip::SessionHandle & session,
chip::ClusterId cluster, chip::EndpointId endpoint) :
ClusterBase(exchangeManager, session, cluster, endpoint)
{}
};

MediaClusterBase cluster(*mOperationalDeviceProxy->GetExchangeManager(),
mOperationalDeviceProxy->GetSecureSession().Value(), mClusterId, mTvEndpoint);
return cluster.InvokeCommand(request, nullptr, OnSuccess, OnFailure);
}

static void OnSuccess(void * context, const ResponseType & response) { sResponseCallback(CHIP_NO_ERROR); }

static void OnFailure(void * context, CHIP_ERROR error) { sResponseCallback(error); }

protected:
chip::ClusterId mClusterId;
chip::OperationalDeviceProxy * mOperationalDeviceProxy = nullptr;
chip::EndpointId mTvEndpoint;
static std::function<void(CHIP_ERROR)> sResponseCallback;
};

template <typename RequestType, typename ResponseType>
std::function<void(CHIP_ERROR)> MediaCommandBase<RequestType, ResponseType>::sResponseCallback = {};
Loading

0 comments on commit 16cae34

Please sign in to comment.