AppMediaPlaybackManager.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "AppMediaPlaybackManager.h"
  18. #include <app-common/zap-generated/attributes/Accessors.h>
  19. #include <app-common/zap-generated/ids/Clusters.h>
  20. #include <app/util/config.h>
  21. #include <cstdint>
  22. #include <jni.h>
  23. #include <json/json.h>
  24. #include <lib/support/CHIPJNIError.h>
  25. #include <lib/support/JniReferences.h>
  26. #include <lib/support/JniTypeWrappers.h>
  27. using namespace std;
  28. using namespace chip;
  29. using namespace chip::app;
  30. using namespace chip::app::DataModel;
  31. using namespace chip::app::Clusters::MediaPlayback;
  32. using namespace chip::Uint8;
  33. using chip::CharSpan;
  34. AppMediaPlaybackManager::AppMediaPlaybackManager(ContentAppAttributeDelegate * attributeDelegate) :
  35. mAttributeDelegate(attributeDelegate){};
  36. PlaybackStateEnum AppMediaPlaybackManager::HandleGetCurrentState()
  37. {
  38. uint64_t ret = HandleMediaRequestGetAttribute(MEDIA_PLAYBACK_ATTRIBUTE_PLAYBACK_STATE);
  39. return static_cast<PlaybackStateEnum>(ret);
  40. }
  41. uint64_t AppMediaPlaybackManager::HandleGetStartTime()
  42. {
  43. return HandleMediaRequestGetAttribute(MEDIA_PLAYBACK_ATTRIBUTE_START_TIME);
  44. }
  45. uint64_t AppMediaPlaybackManager::HandleGetDuration()
  46. {
  47. return HandleMediaRequestGetAttribute(MEDIA_PLAYBACK_ATTRIBUTE_DURATION);
  48. }
  49. float AppMediaPlaybackManager::HandleGetPlaybackSpeed()
  50. {
  51. uint64_t ret = HandleMediaRequestGetAttribute(MEDIA_PLAYBACK_ATTRIBUTE_SPEED);
  52. return static_cast<float>(ret) / 10000.0f;
  53. }
  54. uint64_t AppMediaPlaybackManager::HandleGetSeekRangeStart()
  55. {
  56. return HandleMediaRequestGetAttribute(MEDIA_PLAYBACK_ATTRIBUTE_SEEK_RANGE_START);
  57. }
  58. uint64_t AppMediaPlaybackManager::HandleGetSeekRangeEnd()
  59. {
  60. return HandleMediaRequestGetAttribute(MEDIA_PLAYBACK_ATTRIBUTE_SEEK_RANGE_END);
  61. }
  62. void AppMediaPlaybackManager::HandlePlay(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  63. {
  64. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_PLAY, 0));
  65. }
  66. void AppMediaPlaybackManager::HandlePause(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  67. {
  68. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_PAUSE, 0));
  69. }
  70. void AppMediaPlaybackManager::HandleStop(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  71. {
  72. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_STOP, 0));
  73. }
  74. void AppMediaPlaybackManager::HandleFastForward(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  75. {
  76. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_FAST_FORWARD, 0));
  77. }
  78. void AppMediaPlaybackManager::HandlePrevious(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  79. {
  80. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_PREVIOUS, 0));
  81. }
  82. void AppMediaPlaybackManager::HandleRewind(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  83. {
  84. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_REWIND, 0));
  85. }
  86. void AppMediaPlaybackManager::HandleSkipBackward(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper,
  87. const uint64_t & deltaPositionMilliseconds)
  88. {
  89. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_SKIP_BACKWARD, deltaPositionMilliseconds));
  90. }
  91. void AppMediaPlaybackManager::HandleSkipForward(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper,
  92. const uint64_t & deltaPositionMilliseconds)
  93. {
  94. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_SKIP_FORWARD, deltaPositionMilliseconds));
  95. }
  96. void AppMediaPlaybackManager::HandleSeek(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper,
  97. const uint64_t & positionMilliseconds)
  98. {
  99. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_SEEK, positionMilliseconds));
  100. }
  101. void AppMediaPlaybackManager::HandleNext(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  102. {
  103. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_NEXT, 0));
  104. }
  105. void AppMediaPlaybackManager::HandleStartOver(CommandResponseHelper<Commands::PlaybackResponse::Type> & helper)
  106. {
  107. helper.Success(HandleMediaRequest(MEDIA_PLAYBACK_REQUEST_START_OVER, 0));
  108. }
  109. uint64_t AppMediaPlaybackManager::HandleMediaRequestGetAttribute(chip::AttributeId attributeId)
  110. {
  111. ChipLogProgress(Zcl, "Received AppMediaPlaybackManager::HandleMediaRequestGetAttribute:%d", attributeId);
  112. chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::MediaPlayback::Id, attributeId);
  113. std::string resStr = mAttributeDelegate->Read(aPath);
  114. ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response %s", resStr.c_str());
  115. uint64_t ret = std::numeric_limits<uint64_t>::max();
  116. if (resStr.length() != 0)
  117. {
  118. Json::Reader reader;
  119. Json::Value value;
  120. if (reader.parse(resStr, value))
  121. {
  122. std::string attrId = to_string(attributeId);
  123. ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute response parsing done. reading attr %s",
  124. attrId.c_str());
  125. if (!value[attrId].empty() && value[attrId].isUInt())
  126. {
  127. ret = static_cast<uint64_t>(value[attrId].asUInt());
  128. return ret;
  129. }
  130. ChipLogError(Zcl,
  131. "AppMediaPlaybackManager::HandleMediaRequestGetAttribute error. Invalid response from the content app.");
  132. return ret;
  133. }
  134. }
  135. ChipLogError(
  136. Zcl, "AppMediaPlaybackManager::HandleMediaRequestGetAttribute error. Did not get a response back from the content app.");
  137. return ret;
  138. }
  139. Commands::PlaybackResponse::Type AppMediaPlaybackManager::HandleMediaRequest(MediaPlaybackRequest mediaPlaybackRequest,
  140. uint64_t deltaPositionMilliseconds)
  141. {
  142. // Ideally should not come here
  143. ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleMediaRequest");
  144. Commands::PlaybackResponse::Type response;
  145. response.status = MediaPlaybackStatusEnum::kInvalidStateForCommand;
  146. return response;
  147. }
  148. CHIP_ERROR AppMediaPlaybackManager::HandleGetSampledPosition(AttributeValueEncoder & aEncoder)
  149. {
  150. Structs::PlaybackPositionStruct::Type response;
  151. response.updatedAt = 0;
  152. response.position = Nullable<uint64_t>(0);
  153. ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleGetSampledPosition");
  154. chip::app::ConcreteReadAttributePath aPath(mEndpointId, chip::app::Clusters::MediaPlayback::Id,
  155. chip::app::Clusters::MediaPlayback::Attributes::SampledPosition::Id);
  156. std::string resStr = mAttributeDelegate->Read(aPath);
  157. ChipLogProgress(Zcl, "AppMediaPlaybackManager::HandleGetSampledPosition response %s", resStr.c_str());
  158. if (resStr.length() != 0)
  159. {
  160. Json::Reader reader;
  161. Json::Value value;
  162. if (reader.parse(resStr, value))
  163. {
  164. std::string attrId = to_string(chip::app::Clusters::MediaPlayback::Attributes::SampledPosition::Id);
  165. ChipLogProgress(Zcl, "AppContentLauncherManager::HandleGetSampledPosition response parsing done. reading attr %s",
  166. attrId.c_str());
  167. if (!value[attrId].empty() && value[attrId].isObject())
  168. {
  169. std::string updatedAt = to_string(
  170. static_cast<uint32_t>(chip::app::Clusters::MediaPlayback::Structs::PlaybackPositionStruct::Fields::kUpdatedAt));
  171. std::string position = to_string(
  172. static_cast<uint32_t>(chip::app::Clusters::MediaPlayback::Structs::PlaybackPositionStruct::Fields::kPosition));
  173. if (!value[attrId][updatedAt].empty() && !value[attrId][position].empty() && value[attrId][updatedAt].isUInt() &&
  174. value[attrId][position].isUInt())
  175. {
  176. // valid response
  177. response.updatedAt = value[attrId][updatedAt].asUInt();
  178. response.position = Nullable<uint64_t>(value[attrId][position].asUInt());
  179. return aEncoder.Encode(response);
  180. }
  181. }
  182. }
  183. }
  184. ChipLogError(Zcl, "AppMediaPlaybackManager::GetAttribute error. Invalid response from the content app.");
  185. return aEncoder.Encode(response);
  186. }
  187. uint32_t AppMediaPlaybackManager::GetFeatureMap(chip::EndpointId endpoint)
  188. {
  189. if (endpoint >= EMBER_AF_CONTENT_LAUNCHER_CLUSTER_SERVER_ENDPOINT_COUNT)
  190. {
  191. return mDynamicEndpointFeatureMap;
  192. }
  193. uint32_t featureMap = 0;
  194. Attributes::FeatureMap::Get(endpoint, &featureMap);
  195. return featureMap;
  196. }