1 /* 2 * Copyright (c) 2022-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_NAPI_AVSESSION_CALLBACK_H 17 #define OHOS_NAPI_AVSESSION_CALLBACK_H 18 19 #include <list> 20 #include "avsession_info.h" 21 #include "key_event.h" 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 #include "napi_async_callback.h" 25 26 namespace OHOS::AVSession { 27 class NapiAVSessionCallback : public AVSessionCallback { 28 public: 29 enum { 30 EVENT_PLAY, 31 EVENT_PAUSE, 32 EVENT_STOP, 33 EVENT_PLAY_NEXT, 34 EVENT_PLAY_PREVIOUS, 35 EVENT_FAST_FORWARD, 36 EVENT_REWIND, 37 EVENT_SEEK, 38 EVENT_SET_SPEED, 39 EVENT_SET_LOOP_MODE, 40 EVENT_TOGGLE_FAVORITE, 41 EVENT_MEDIA_KEY_EVENT, 42 EVENT_OUTPUT_DEVICE_CHANGE, 43 EVENT_SEND_COMMON_COMMAND, 44 EVENT_SKIP_TO_QUEUE_ITEM, 45 EVENT_TYPE_MAX 46 }; 47 48 NapiAVSessionCallback(); 49 ~NapiAVSessionCallback() override; 50 51 void OnPlay() override; 52 void OnPause() override; 53 void OnStop() override; 54 void OnPlayNext() override; 55 void OnPlayPrevious() override; 56 void OnFastForward() override; 57 void OnRewind() override; 58 void OnSeek(int64_t time) override; 59 void OnSetSpeed(double speed) override; 60 void OnSetLoopMode(int32_t loopMode) override; 61 void OnToggleFavorite(const std::string& assertId) override; 62 void OnMediaKeyEvent(const MMI::KeyEvent& keyEvent) override; 63 void OnOutputDeviceChange(const int32_t connectionState, const OutputDeviceInfo& outputDeviceInfo) override; 64 void OnCommonCommand(const std::string& commonCommand, const AAFwk::WantParams& commandArgs) override; 65 void OnSkipToQueueItem(int32_t itemId) override; 66 67 napi_status AddCallback(napi_env env, int32_t event, napi_value callback); 68 napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback); 69 70 bool IsCallbacksEmpty(int32_t event); 71 72 private: 73 void HandleEvent(int32_t event); 74 75 template<typename T> 76 void HandleEvent(int32_t event, const T& param); 77 78 template<typename T> 79 void HandleEvent(int32_t event, const std::string& firstParam, const T& secondParam); 80 81 template<typename T> 82 void HandleEvent(int32_t event, const int32_t firstParam, const T& secondParam); 83 84 std::mutex lock_; 85 std::shared_ptr<NapiAsyncCallback> asyncCallback_; 86 std::list<napi_ref> callbacks_[EVENT_TYPE_MAX] {}; 87 }; 88 } // namespace OHOS::AVSession 89 #endif // OHOS_NAPI_AVSESSION_CALLBACK_H 90