1 /* 2 * Copyright (C) 2023-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 * Description: supply stream player listener for napi interface. 15 * Author: huangchanggui 16 * Create: 2023-1-11 17 */ 18 19 #ifndef NAPI_STREAM_PLAYER_LISTENER_H 20 #define NAPI_STREAM_PLAYER_LISTENER_H 21 22 #include <memory> 23 #include <list> 24 #include "napi/native_api.h" 25 #include "napi/native_node_api.h" 26 #include "cast_engine_common.h" 27 #include "napi_callback.h" 28 #include "napi_castengine_utils.h" 29 #include "i_stream_player.h" 30 #include "pixel_map.h" 31 32 namespace OHOS { 33 namespace CastEngine { 34 namespace CastEngineClient { 35 class NapiStreamPlayerListener : public IStreamPlayerListener { 36 public: 37 using NapiArgsGetter = std::function<void(napi_env env, int &argc, napi_value *argv)>; 38 enum { 39 EVENT_PLAYER_STATUS_CHANGED, 40 EVENT_POSITION_CHANGED, 41 EVENT_MEDIA_ITEM_CHANGED, 42 EVENT_VOLUME_CHANGED, 43 EVENT_VIDEO_SIZE_CHANGED, 44 EVENT_LOOP_MODE_CHANGED, 45 EVENT_PLAY_SPEED_CHANGED, 46 EVENT_PLAYER_ERROR, 47 EVENT_NEXT_REQUEST, 48 EVENT_PREVIOUS_REQUEST, 49 EVENT_SEEK_DONE, 50 EVENT_END_OF_STREAM, 51 EVENT_PLAY_REQUEST, 52 EVENT_IMAGE_CHANGED, 53 EVENT_ALBUM_COVER_CHANGED, 54 EVENT_KEY_REQUEST, 55 EVENT_AVAILABLE_CAPABILITY_CHANGED, 56 EVENT_TYPE_MAX 57 }; 58 NapiStreamPlayerListener()59 NapiStreamPlayerListener() {}; 60 ~NapiStreamPlayerListener() override; 61 62 void OnStateChanged(const PlayerStates playbackState, bool isPlayWhenReady) override; 63 void OnPositionChanged(int position, int bufferPosition, int duration) override; 64 void OnMediaItemChanged(const MediaInfo &mediaInfo) override; 65 void OnVolumeChanged(int volume, int maxVolume) override; 66 void OnVideoSizeChanged(int width, int height) override; 67 void OnLoopModeChanged(const LoopMode loopMode) override; 68 void OnPlaySpeedChanged(const PlaybackSpeed speed) override; 69 void OnPlayerError(int errorCode, const std::string &errorMsg) override; 70 void OnNextRequest() override; 71 void OnPreviousRequest() override; 72 void OnSeekDone(int position) override; 73 void OnEndOfStream(int isLooping) override; 74 void OnPlayRequest(const MediaInfo &mediaInfo) override; 75 void OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap) override; 76 void OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap) override; 77 void OnKeyRequest(const std::string &mediaId, const std::vector<uint8_t> &keyRequestData) override; 78 void OnAvailableCapabilityChanged(const StreamCapability &streamCapability) override; 79 void OnData(const DataType dataType, const std::string &dataStr) override; 80 81 napi_status AddCallback(napi_env env, int32_t event, napi_value callback); 82 napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback); 83 84 private: 85 void HandleEvent(int32_t event, NapiArgsGetter &getter); 86 87 std::mutex lock_; 88 std::shared_ptr<NapiCallback> callback_; 89 }; 90 } // namespace CastEngineClient 91 } // namespace CastEngine 92 } // namespace OHOS 93 #endif