• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TYPE_MAX
55     };
56 
NapiStreamPlayerListener()57     NapiStreamPlayerListener() {};
58     ~NapiStreamPlayerListener() override;
59 
60     void OnStateChanged(const PlayerStates playbackState, bool isPlayWhenReady) override;
61     void OnPositionChanged(int position, int bufferPosition, int duration) override;
62     void OnMediaItemChanged(const MediaInfo &mediaInfo) override;
63     void OnVolumeChanged(int volume, int maxVolume) override;
64     void OnVideoSizeChanged(int width, int height) override;
65     void OnLoopModeChanged(const LoopMode loopMode) override;
66     void OnPlaySpeedChanged(const PlaybackSpeed speed) override;
67     void OnPlayerError(int errorCode, const std::string &errorMsg) override;
68     void OnNextRequest() override;
69     void OnPreviousRequest() override;
70     void OnSeekDone(int position) override;
71     void OnEndOfStream(int isLooping) override;
72     void OnPlayRequest(const MediaInfo &mediaInfo) override;
73     void OnImageChanged(std::shared_ptr<Media::PixelMap> pixelMap) override;
74     void OnAlbumCoverChanged(std::shared_ptr<Media::PixelMap> pixelMap) override;
75 
76     napi_status AddCallback(napi_env env, int32_t event, napi_value callback);
77     napi_status RemoveCallback(napi_env env, int32_t event, napi_value callback);
78 
79 private:
80     void HandleEvent(int32_t event, NapiArgsGetter getter);
81     napi_status ClearCallback(napi_env env);
82 
83     std::mutex lock_;
84     std::shared_ptr<NapiCallback> callback_;
85     std::list<napi_ref> callbacks_[EVENT_TYPE_MAX] {};
86 };
87 } // namespace CastEngineClient
88 } // namespace CastEngine
89 } // namespace OHOS
90 #endif