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 AUDIO_HAPTIC_PLAYER_H
17 #define AUDIO_HAPTIC_PLAYER_H
18 
19 #include <mutex>
20 
21 #include "audio_info.h"
22 
23 namespace OHOS {
24 namespace Media {
25 enum AudioHapticType {
26     AUDIO_HAPTIC_TYPE_AUDIO = 0,
27     AUDIO_HAPTIC_TYPE_HAPTIC = 1,
28 };
29 
30 enum class AudioHapticPlayerState {
31     /** INVALID state */
32     STATE_INVALID = -1,
33     /** Create New instance */
34     STATE_NEW,
35     /** Prepared state */
36     STATE_PREPARED,
37     /** Running state */
38     STATE_RUNNING,
39     /** Stopped state */
40     STATE_STOPPED,
41     /** Released state */
42     STATE_RELEASED,
43     /** Paused state */
44     STATE_PAUSED
45 };
46 
47 enum AudioLatencyMode {
48     AUDIO_LATENCY_MODE_NORMAL = 0,
49     AUDIO_LATENCY_MODE_FAST = 1
50 };
51 
52 struct AudioHapticPlayerOptions {
53     bool muteAudio;
54     bool muteHaptics;
55     bool parallelPlayFlag = false;
56 };
57 
58 enum HapticsMode {
59     HAPTICS_MODE_INVALID = -1,
60     HAPTICS_MODE_NONE = 0,
61     HAPTICS_MODE_SYNC = 1,
62     HAPTICS_MODE_NON_SYNC = 2,
63 };
64 
65 struct HapticSource {
66     std::string hapticUri = "";
67     std::string effectId = "";
68 };
69 
70 struct AudioHapticPlayerParam {
71     AudioHapticPlayerOptions options;
72     std::string audioUri;
73     HapticSource hapticSource;
74     AudioLatencyMode latencyMode;
75     AudioStandard::StreamUsage streamUsage;
76 
AudioHapticPlayerParamAudioHapticPlayerParam77     AudioHapticPlayerParam() {};
AudioHapticPlayerParamAudioHapticPlayerParam78     AudioHapticPlayerParam(const AudioHapticPlayerOptions &options,
79         const std::string &audioUri, const HapticSource &hapticSource,
80         const AudioLatencyMode &latencyMode, const AudioStandard::StreamUsage &streamUsage)
81         : options(options),
82           audioUri(audioUri),
83           hapticSource(hapticSource),
84           latencyMode(latencyMode),
85           streamUsage(streamUsage) {};
86 };
87 
88 class AudioHapticPlayerCallback;
89 
90 class AudioHapticPlayer {
91 public:
92     virtual ~AudioHapticPlayer() = default;
93 
94     virtual bool IsMuted(const AudioHapticType &audioHapticType) const = 0;
95 
96     virtual int32_t Prepare() = 0;
97 
98     virtual int32_t Start() = 0;
99 
100     virtual int32_t Stop() = 0;
101 
102     virtual int32_t Release() = 0;
103 
104     virtual int32_t SetVolume(float volume) = 0;
105 
106     virtual int32_t SetHapticIntensity(float intensity) = 0;
107 
108     virtual int32_t SetLoop(bool loop) = 0;
109 
110     virtual int32_t SetAudioHapticPlayerCallback(
111         const std::shared_ptr<AudioHapticPlayerCallback> &playerCallback) = 0;
112 
113     virtual int32_t GetAudioCurrentTime() = 0;
114 
115     virtual HapticsMode GetHapticsMode() const = 0;
116 
117     virtual void SetHapticsMode(HapticsMode hapticsMode) = 0;
118 };
119 
120 class AudioHapticPlayerCallback {
121 public:
122     virtual ~AudioHapticPlayerCallback() = default;
123 
124     /**
125      * Called when an interrupt is received.
126      *
127      * @param interruptEvent Indicates the InterruptEvent information needed by client.
128      * For details, refer InterruptEventInternal struct in audio_info.h
129      */
130     virtual void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) = 0;
131 
132     /**
133      * Called when reaching the end of stream.
134      */
135     virtual void OnEndOfStream(void) = 0;
136 
137     /**
138      * Called when reaching errs from player.
139      *
140      * @param errorCode error code.
141      */
142     virtual void OnError(int32_t errorCode) = 0;
143 };
144 
145 class __attribute__((visibility("default"))) AudioHapticPlayerFactory {
146 public:
147     static std::shared_ptr<AudioHapticPlayer> CreateAudioHapticPlayer(const AudioHapticPlayerParam &param);
148 
149 private:
150     static std::mutex createPlayerMutex_;
151     AudioHapticPlayerFactory() = default;
152     ~AudioHapticPlayerFactory() = default;
153 };
154 } // Media
155 } // OHOS
156 #endif // AUDIO_HAPTIC_PLAYER_H
157