• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 AV_PLAYER_CALLBACK
17 #define AV_PLAYER_CALLBACK
18 
19 #include <map>
20 #include <mutex>
21 #include "player.h"
22 #include "media_errors.h"
23 #include "common_napi.h"
24 
25 namespace OHOS {
26 namespace Media {
27 class AVPlayerNotify {
28 public:
29     AVPlayerNotify() = default;
30     virtual ~AVPlayerNotify() = default;
31     virtual void NotifyDuration(int32_t duration) = 0;
32     virtual void NotifyPosition(int32_t position) = 0;
33     virtual void NotifyState(PlayerStates state) = 0;
34     virtual void NotifyVideoSize(int32_t width, int32_t height) = 0;
35 };
36 
37 class AVPlayerCallback : public PlayerCallback {
38 public:
39     AVPlayerCallback(napi_env env, AVPlayerNotify *listener);
40     virtual ~AVPlayerCallback();
41     void OnError(int32_t errorCode, const std::string &errorMsg) override;
42     void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) override;
43     void OnErrorCb(MediaServiceExtErrCodeAPI9 errorCode, const std::string &errorMsg);
44     PlayerStates GetCurrentState() const;
45     int32_t GetVideoWidth() const;
46     int32_t GetVideoHeight() const;
47     void SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref);
48     void ClearCallbackReference();
49     void ClearCallbackReference(const std::string &name);
50     void Start();
51     void Pause();
52     void Release();
53 
54 private:
55     void OnStateChangeCb(PlayerStates state, const Format &infoBody);
56     void OnVolumeChangeCb(const Format &infoBody);
57     void OnSeekDoneCb(int32_t currentPositon) const;
58     void OnSpeedDoneCb(int32_t speedMode) const;
59     void OnBitRateDoneCb(int32_t bitRate) const;
60     void OnPositionUpdateCb(int32_t position) const;
61     void OnDurationUpdateCb(int32_t duration) const;
62     void OnBufferingUpdateCb(const Format &infoBody) const;
63     void OnMessageCb(int32_t extra, const Format &infoBody) const;
64     void OnStartRenderFrameCb() const;
65     void OnVideoSizeChangedCb(const Format &infoBody);
66     void OnAudioInterruptCb(const Format &infoBody) const;
67     void OnBitRateCollectedCb(const Format &infoBody) const;
68     void OnEosCb(int32_t isLooping) const;
69 
70     std::mutex mutex_;
71     napi_env env_ = nullptr;
72     std::map<std::string, std::weak_ptr<AutoRef>> refMap_;
73     AVPlayerNotify *listener_ = nullptr;
74     std::atomic<bool> isloaded_ = false;
75     PlayerStates state_ = PLAYER_IDLE;
76 };
77 } // namespace Media
78 } // namespace OHOS
79 #endif // PLAYER_CALLBACK_NAPI_H_