• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
56 
57 struct HapticSource {
58     std::string hapticUri = "";
59     std::string effectId = "";
60 };
61 
62 struct AudioHapticPlayerParam {
63     AudioHapticPlayerOptions options;
64     std::string audioUri;
65     HapticSource hapticSource;
66     AudioLatencyMode latencyMode;
67     AudioStandard::StreamUsage streamUsage;
68 
AudioHapticPlayerParamAudioHapticPlayerParam69     AudioHapticPlayerParam() {};
AudioHapticPlayerParamAudioHapticPlayerParam70     AudioHapticPlayerParam(const AudioHapticPlayerOptions &options,
71         const std::string &audioUri, const HapticSource &hapticSource,
72         const AudioLatencyMode &latencyMode, const AudioStandard::StreamUsage &streamUsage)
73         : options(options),
74           audioUri(audioUri),
75           hapticSource(hapticSource),
76           latencyMode(latencyMode),
77           streamUsage(streamUsage) {};
78 };
79 
80 class AudioHapticPlayerCallback;
81 
82 class AudioHapticPlayer {
83 public:
84     virtual ~AudioHapticPlayer() = default;
85 
86     virtual bool IsMuted(const AudioHapticType &audioHapticType) const = 0;
87 
88     virtual int32_t Prepare() = 0;
89 
90     virtual int32_t Start() = 0;
91 
92     virtual int32_t Stop() = 0;
93 
94     virtual int32_t Release() = 0;
95 
96     virtual int32_t SetVolume(float volume) = 0;
97 
98     virtual int32_t SetHapticIntensity(float intensity) = 0;
99 
100     virtual int32_t SetLoop(bool loop) = 0;
101 
102     virtual int32_t SetAudioHapticPlayerCallback(
103         const std::shared_ptr<AudioHapticPlayerCallback> &playerCallback) = 0;
104 
105     virtual int32_t GetAudioCurrentTime() = 0;
106 };
107 
108 class AudioHapticPlayerCallback {
109 public:
110     virtual ~AudioHapticPlayerCallback() = default;
111 
112     /**
113      * Called when an interrupt is received.
114      *
115      * @param interruptEvent Indicates the InterruptEvent information needed by client.
116      * For details, refer InterruptEventInternal struct in audio_info.h
117      */
118     virtual void OnInterrupt(const AudioStandard::InterruptEvent &interruptEvent) = 0;
119 
120     /**
121      * Called when reaching the end of stream.
122      */
123     virtual void OnEndOfStream(void) = 0;
124 
125     /**
126      * Called when reaching errs from player.
127      *
128      * @param errorCode error code.
129      */
130     virtual void OnError(int32_t errorCode) = 0;
131 };
132 
133 class __attribute__((visibility("default"))) AudioHapticPlayerFactory {
134 public:
135     static std::shared_ptr<AudioHapticPlayer> CreateAudioHapticPlayer(const AudioHapticPlayerParam &param);
136 
137 private:
138     static std::mutex createPlayerMutex_;
139     AudioHapticPlayerFactory() = default;
140     ~AudioHapticPlayerFactory() = default;
141 };
142 } // Media
143 } // OHOS
144 #endif // AUDIO_HAPTIC_PLAYER_H
145