1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_AVCAST_CONTROLLER_ITEM_H 17 #define OHOS_AVCAST_CONTROLLER_ITEM_H 18 19 #include "i_avcast_controller_proxy.h" 20 #include "av_cast_controller_callback_proxy.h" 21 #include "avcast_controller_stub.h" 22 #include "avcast_control_command.h" 23 #include "av_shared_memory_base.h" 24 25 namespace OHOS::AVSession { 26 class AVCastControllerItem : public AVCastControllerStub, public IAVCastControllerProxyListener, 27 public std::enable_shared_from_this<IAVCastControllerProxyListener> { 28 public: 29 AVCastControllerItem(); 30 31 ~AVCastControllerItem(); 32 33 void Init(std::shared_ptr<IAVCastControllerProxy> castControllerProxy, 34 const std::function<void(int32_t, std::vector<int32_t>&)>& validCommandsChangecallback, 35 const std::function<void()>& preparecallback); 36 37 void OnCastPlaybackStateChange(const AVPlaybackState& state) override; 38 39 void OnMediaItemChange(const AVQueueItem& avQueueItem) override; 40 41 void OnPlayNext() override; 42 43 void OnPlayPrevious() override; 44 45 void OnSeekDone(const int32_t seekNumber) override; 46 47 void OnVideoSizeChange(const int32_t width, const int32_t height) override; 48 49 void OnPlayerError(const int32_t errorCode, const std::string& errorMsg) override; 50 51 void OnEndOfStream(const int32_t isLooping) override; 52 53 void OnPlayRequest(const AVQueueItem& avQueueItem) override; 54 55 void OnKeyRequest(const std::string& assetId, const std::vector<uint8_t>& keyRequestData) override; 56 57 void OnValidCommandChange(const std::vector<int32_t> &cmds) override; 58 59 void OnCustomData(const AAFwk::WantParams& data) override; 60 61 int32_t onDataSrcRead(const std::shared_ptr<AVSharedMemoryBase>& mem, 62 uint32_t length, int64_t pos, int32_t& result) override; 63 64 int32_t SendControlCommand(const AVCastControlCommand& cmd) override; 65 66 int32_t SendCustomData(const AAFwk::WantParams& data) override; 67 68 int32_t Start(const AVQueueItem& avQueueItem) override; 69 70 int32_t Prepare(const AVQueueItem& avQueueItem) override; 71 72 int32_t GetDuration(int32_t& duration) override; 73 74 int32_t GetCastAVPlaybackState(AVPlaybackState& avPlaybackState) override; 75 76 int32_t GetSupportedDecoders(std::vector<std::string>& decoderTypes) override; 77 78 int32_t GetRecommendedResolutionLevel(std::string& decoderType, ResolutionLevel& resolutionLevel) override; 79 80 int32_t GetSupportedHdrCapabilities(std::vector<HDRFormat>& hdrFormats) override; 81 82 int32_t GetSupportedPlaySpeeds(std::vector<float>& playSpeeds) override; 83 84 int32_t GetCurrentItem(AVQueueItem& currentItem) override; 85 86 int32_t GetValidCommands(std::vector<int32_t>& cmds) override; 87 88 int32_t SetDisplaySurface(std::string& surfaceId) override; 89 90 int32_t SetCastPlaybackFilter(const AVPlaybackState::PlaybackStateMaskType& filter) override; 91 92 int32_t ProcessMediaKeyResponse(const std::string& assetId, const std::vector<uint8_t>& response) override; 93 94 bool RegisterControllerListener(std::shared_ptr<IAVCastControllerProxy> castControllerProxy); 95 96 int32_t AddAvailableCommand(const int32_t cmd) override; 97 98 int32_t RemoveAvailableCommand(const int32_t cmd) override; 99 100 int32_t HandleCastValidCommandChange(const std::vector<int32_t> &cmds); 101 102 void SetSessionTag(const std::string& tag); 103 104 void SetSessionId(const std::string id); 105 106 void SetUserId(const int32_t userId); 107 108 int32_t Destroy() override; 109 110 void SetQueueItemDataSrc(const AVQueueItem& avQueueItem); 111 112 void buildExtraCastInfo(const AVQueueItem& avQueueItem); 113 114 std::string GetSourceType(const AVQueueItem& avQueueItem); 115 116 protected: 117 int32_t RegisterCallbackInner(const sptr<IRemoteObject>& callback) override; 118 119 private: 120 bool IsStopState(int32_t playbackState); 121 void CheckIfCancelCastCapsule(); 122 void ReportPrepare(int32_t preRet, const AVQueueItem& avQueueItem); 123 124 std::shared_ptr<IAVCastControllerProxy> castControllerProxy_; 125 sptr<IAVCastControllerCallback> callback_; 126 AVPlaybackState::PlaybackStateMaskType castPlaybackMask_; 127 std::function<void(int32_t, std::vector<int32_t>&)> validCommandsChangecallback_; 128 std::function<void()> preparecallback_; 129 int32_t removeCmdStep_ = 1000; 130 int32_t currentState_ = AVPlaybackState::PLAYBACK_STATE_INITIAL; 131 std::string sessionTag_; 132 std::string sessionId_; 133 int32_t userId_ = 0; 134 bool isSessionCallbackAvailable_ = true; 135 std::recursive_mutex castControllerLock_; 136 std::recursive_mutex castControllerCallbackLock_; 137 std::mutex callbackToSessionLock_; 138 bool isPlayingState_ = false; 139 }; 140 } // namespace OHOS::AVSession 141 #endif // OHOS_AVCAST_CONTROLLER_ITEM_H 142