• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef AUDIO_INFO_H
16 #define AUDIO_INFO_H
17 
18 #ifdef __MUSL__
19 #include <stdint.h>
20 #endif // __MUSL__
21 
22 #include <cmath>
23 #include <limits>
24 #include <string>
25 #include <vector>
26 #include <array>
27 #include <unistd.h>
28 #include <unordered_map>
29 #include <parcel.h>
30 #include <audio_source_type.h>
31 #include <audio_device_info.h>
32 #include <audio_interrupt_info.h>
33 #include <audio_session_info.h>
34 #include <audio_stream_info.h>
35 #include <audio_asr.h>
36 
37 namespace OHOS {
38 namespace AudioStandard {
39 constexpr int32_t MAX_NUM_STREAMS = 3;
40 constexpr int32_t RENDERER_STREAM_USAGE_SHIFT = 16;
41 constexpr int32_t MINIMUM_BUFFER_SIZE_MSEC = 5;
42 constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 20;
43 constexpr int32_t MIN_SERVICE_COUNT = 2;
44 constexpr int32_t ROOT_UID = 0;
45 constexpr int32_t INVALID_UID = -1;
46 constexpr int32_t INTELL_VOICE_SERVICR_UID = 1042;
47 constexpr int32_t RSS_UID = 1096;
48 constexpr int32_t NETWORK_ID_SIZE = 80;
49 constexpr int32_t DEFAULT_VOLUME_GROUP_ID = 1;
50 constexpr int32_t DEFAULT_VOLUME_INTERRUPT_ID = 1;
51 constexpr int32_t AUDIO_FLAG_INVALID = -1;
52 constexpr int32_t AUDIO_FLAG_NORMAL = 0;
53 constexpr int32_t AUDIO_FLAG_MMAP = 1;
54 constexpr int32_t AUDIO_FLAG_VOIP_FAST = 2;
55 constexpr int32_t AUDIO_FLAG_DIRECT = 3;
56 constexpr int32_t AUDIO_FLAG_VOIP_DIRECT = 4;
57 constexpr int32_t AUDIO_FLAG_FORCED_NORMAL = 10;
58 constexpr int32_t AUDIO_USAGE_NORMAL = 0;
59 constexpr int32_t AUDIO_USAGE_VOIP = 1;
60 constexpr uint32_t STREAM_FLAG_NORMAL = 0;
61 constexpr uint32_t STREAM_FLAG_FAST = 1;
62 constexpr uint32_t STREAM_FLAG_DIRECT = 2;
63 constexpr float MAX_STREAM_SPEED_LEVEL = 4.0f;
64 constexpr float MIN_STREAM_SPEED_LEVEL = 0.125f;
65 constexpr int32_t EMPTY_UID = 0;
66 constexpr int32_t AUDIO_NORMAL_MANAGER_TYPE = 0;
67 constexpr int32_t AUDIO_DIRECT_MANAGER_TYPE = 2;
68 
69 constexpr uint32_t MIN_SESSIONID = 100000;
70 constexpr uint32_t MAX_SESSIONID = UINT32_MAX - MIN_SESSIONID;
71 
72 const float MIN_FLOAT_VOLUME = 0.0f;
73 const float MAX_FLOAT_VOLUME = 1.0f;
74 
75 const std::string MICROPHONE_PERMISSION = "ohos.permission.MICROPHONE";
76 const std::string MANAGE_INTELLIGENT_VOICE_PERMISSION = "ohos.permission.MANAGE_INTELLIGENT_VOICE";
77 const std::string MANAGE_AUDIO_CONFIG = "ohos.permission.MANAGE_AUDIO_CONFIG";
78 const std::string MICROPHONE_CONTROL_PERMISSION = "ohos.permission.MICROPHONE_CONTROL";
79 const std::string MODIFY_AUDIO_SETTINGS_PERMISSION = "ohos.permission.MODIFY_AUDIO_SETTINGS";
80 const std::string ACCESS_NOTIFICATION_POLICY_PERMISSION = "ohos.permission.ACCESS_NOTIFICATION_POLICY";
81 const std::string USE_BLUETOOTH_PERMISSION = "ohos.permission.USE_BLUETOOTH";
82 const std::string CAPTURER_VOICE_DOWNLINK_PERMISSION = "ohos.permission.CAPTURE_VOICE_DOWNLINK_AUDIO";
83 const std::string RECORD_VOICE_CALL_PERMISSION = "ohos.permission.RECORD_VOICE_CALL";
84 const std::string MANAGE_SYSTEM_AUDIO_EFFECTS = "ohos.permission.MANAGE_SYSTEM_AUDIO_EFFECTS";
85 const std::string CAST_AUDIO_OUTPUT_PERMISSION = "ohos.permission.CAST_AUDIO_OUTPUT";
86 const std::string DUMP_AUDIO_PERMISSION = "ohos.permission.DUMP_AUDIO";
87 const std::string CAPTURE_PLAYBACK_PERMISSION = "ohos.permission.CAPTURE_PLAYBACK";
88 
89 constexpr std::string_view PRIMARY_WAKEUP = "Built_in_wakeup";
90 constexpr std::string_view VOICE_CALL_REC_NAME = "Voice_call_rec";
91 
92 const std::string INNER_CAPTURER_SOURCE = "Speaker.monitor";
93 const std::string INNER_CAPTURER_SINK = "InnerCapturerSink";
94 const std::string NEW_INNER_CAPTURER_SOURCE = "InnerCapturerSink.monitor";
95 const std::string REMOTE_CAST_INNER_CAPTURER_SINK_NAME = "RemoteCastInnerCapturer";
96 const std::string MONITOR_SOURCE_SUFFIX = ".monitor";
97 const std::string DUP_STREAM = "DupStream";
98 const std::string DUAL_TONE_STREAM = "DualToneStream";
99 const std::string NORMAL_STREAM = "NormalStream";
100 
101 #ifdef FEATURE_DTMF_TONE
102 // Maximun number of sine waves in a tone segment
103 constexpr uint32_t TONEINFO_MAX_WAVES = 3;
104 
105 // Maximun number of segments in a tone descriptor
106 constexpr uint32_t TONEINFO_MAX_SEGMENTS = 12;
107 constexpr uint32_t TONEINFO_INF = 0xFFFFFFFF;
108 class ToneSegment : public Parcelable {
109 public:
110     uint32_t duration;
111     uint16_t waveFreq[TONEINFO_MAX_WAVES+1];
112     uint16_t loopCnt;
113     uint16_t loopIndx;
Marshalling(Parcel & parcel)114     bool Marshalling(Parcel &parcel) const override
115     {
116         parcel.WriteUint32(duration);
117         parcel.WriteUint16(loopCnt);
118         parcel.WriteUint16(loopIndx);
119         for (uint32_t i = 0; i < TONEINFO_MAX_WAVES + 1; i++) {
120             parcel.WriteUint16(waveFreq[i]);
121         }
122         return true;
123     }
Unmarshalling(Parcel & parcel)124     void Unmarshalling(Parcel &parcel)
125     {
126         duration = parcel.ReadUint32();
127         loopCnt = parcel.ReadUint16();
128         loopIndx = parcel.ReadUint16();
129         for (uint32_t i = 0; i < TONEINFO_MAX_WAVES + 1; i++) {
130             waveFreq[i] = parcel.ReadUint16();
131         }
132     }
133 };
134 
135 class ToneInfo : public Parcelable {
136 public:
137     ToneSegment segments[TONEINFO_MAX_SEGMENTS+1];
138     uint32_t segmentCnt;
139     uint32_t repeatCnt;
140     uint32_t repeatSegment;
Marshalling(Parcel & parcel)141     bool Marshalling(Parcel &parcel) const override
142     {
143         parcel.WriteUint32(segmentCnt);
144         parcel.WriteUint32(repeatCnt);
145         parcel.WriteUint32(repeatSegment);
146         for (uint32_t i = 0; i < segmentCnt; i++) {
147             segments[i].Marshalling(parcel);
148         }
149         return true;
150     }
Unmarshalling(Parcel & parcel)151     void Unmarshalling(Parcel &parcel)
152     {
153         segmentCnt = parcel.ReadUint32();
154         repeatCnt = parcel.ReadUint32();
155         repeatSegment = parcel.ReadUint32();
156         for (uint32_t i = 0; i < segmentCnt; i++) {
157             segments[i].Unmarshalling(parcel);
158         }
159     }
160 };
161 #endif
162 
163 enum VolumeAdjustType {
164     /**
165      * Adjust volume up
166      */
167     VOLUME_UP = 0,
168     /**
169      * Adjust volume down
170      */
171     VOLUME_DOWN = 1,
172 };
173 
174 enum ChannelBlendMode {
175     /**
176      * No channel process.
177      */
178     MODE_DEFAULT = 0,
179     /**
180      * Blend left and right channel.
181      */
182     MODE_BLEND_LR = 1,
183     /**
184      * Replicate left to right channel.
185      */
186     MODE_ALL_LEFT = 2,
187     /**
188      * Replicate right to left channel.
189      */
190     MODE_ALL_RIGHT = 3,
191 };
192 
193 enum ConnectType {
194     /**
195      * Group connect type of local device
196      */
197     CONNECT_TYPE_LOCAL = 0,
198     /**
199      * Group connect type of distributed device
200      */
201     CONNECT_TYPE_DISTRIBUTED
202 };
203 
204 typedef AudioStreamType AudioVolumeType;
205 
206 enum VolumeFlag {
207     /**
208      * Show system volume bar
209      */
210     FLAG_SHOW_SYSTEM_UI = 1,
211 };
212 
213 enum AudioOffloadType {
214     /**
215      * Indicates audio offload state default.
216      */
217     OFFLOAD_DEFAULT = -1,
218     /**
219      * Indicates audio offload state : screen is active & app is foreground.
220      */
221     OFFLOAD_ACTIVE_FOREGROUND = 0,
222     /**
223      * Indicates audio offload state : screen is active & app is background.
224      */
225     OFFLOAD_ACTIVE_BACKGROUND = 1,
226     /**
227      * Indicates audio offload state : screen is inactive & app is background.
228      */
229     OFFLOAD_INACTIVE_BACKGROUND = 3,
230 };
231 
232 enum FocusType {
233     /**
234      * Recording type.
235      */
236     FOCUS_TYPE_RECORDING = 0,
237 };
238 
239 enum AudioErrors {
240     /**
241      * Common errors.
242      */
243     ERROR_INVALID_PARAM = 6800101,
244     ERROR_NO_MEMORY     = 6800102,
245     ERROR_ILLEGAL_STATE = 6800103,
246     ERROR_UNSUPPORTED   = 6800104,
247     ERROR_TIMEOUT       = 6800105,
248     /**
249      * Audio specific errors.
250      */
251     ERROR_STREAM_LIMIT  = 6800201,
252     /**
253      * Default error.
254      */
255     ERROR_SYSTEM        = 6800301
256 };
257 
258 // Ringer Mode
259 enum AudioRingerMode {
260     RINGER_MODE_SILENT = 0,
261     RINGER_MODE_VIBRATE = 1,
262     RINGER_MODE_NORMAL = 2
263 };
264 
265 /**
266  * Enumerates audio stream privacy type for playback capture.
267  */
268 enum AudioPrivacyType {
269     PRIVACY_TYPE_PUBLIC = 0,
270     PRIVACY_TYPE_PRIVATE = 1
271 };
272 
273 /**
274 * Enumerates the renderer playback speed.
275 */
276 enum AudioRendererRate {
277     RENDER_RATE_NORMAL = 0,
278     RENDER_RATE_DOUBLE = 1,
279     RENDER_RATE_HALF = 2,
280 };
281 
282 /**
283 * media safe volume status
284 */
285 enum SafeStatus : int32_t {
286     SAFE_UNKNOWN = -1,
287     SAFE_INACTIVE = 0,
288     SAFE_ACTIVE = 1,
289 };
290 
291 enum CallbackChange : int32_t {
292     CALLBACK_UNKNOWN = 0,
293     CALLBACK_FOCUS_INFO_CHANGE,
294     CALLBACK_RENDERER_STATE_CHANGE,
295     CALLBACK_CAPTURER_STATE_CHANGE,
296     CALLBACK_MICMUTE_STATE_CHANGE,
297     CALLBACK_AUDIO_SESSION,
298     CALLBACK_PREFERRED_OUTPUT_DEVICE_CHANGE,
299     CALLBACK_PREFERRED_INPUT_DEVICE_CHANGE,
300     CALLBACK_SET_VOLUME_KEY_EVENT,
301     CALLBACK_SET_DEVICE_CHANGE,
302     CALLBACK_SET_RINGER_MODE,
303     CALLBACK_SET_MIC_STATE_CHANGE,
304     CALLBACK_SPATIALIZATION_ENABLED_CHANGE,
305     CALLBACK_HEAD_TRACKING_ENABLED_CHANGE,
306     CALLBACK_SET_MICROPHONE_BLOCKED,
307     CALLBACK_DEVICE_CHANGE_WITH_INFO,
308     CALLBACK_HEAD_TRACKING_DATA_REQUESTED_CHANGE,
309     CALLBACK_MAX,
310 };
311 
312 constexpr CallbackChange CALLBACK_ENUMS[] = {
313     CALLBACK_UNKNOWN,
314     CALLBACK_FOCUS_INFO_CHANGE,
315     CALLBACK_RENDERER_STATE_CHANGE,
316     CALLBACK_CAPTURER_STATE_CHANGE,
317     CALLBACK_MICMUTE_STATE_CHANGE,
318     CALLBACK_AUDIO_SESSION,
319     CALLBACK_PREFERRED_OUTPUT_DEVICE_CHANGE,
320     CALLBACK_PREFERRED_INPUT_DEVICE_CHANGE,
321     CALLBACK_SET_VOLUME_KEY_EVENT,
322     CALLBACK_SET_DEVICE_CHANGE,
323     CALLBACK_SET_RINGER_MODE,
324     CALLBACK_SET_MIC_STATE_CHANGE,
325     CALLBACK_SPATIALIZATION_ENABLED_CHANGE,
326     CALLBACK_HEAD_TRACKING_ENABLED_CHANGE,
327     CALLBACK_SET_MICROPHONE_BLOCKED,
328     CALLBACK_DEVICE_CHANGE_WITH_INFO,
329     CALLBACK_HEAD_TRACKING_DATA_REQUESTED_CHANGE,
330 };
331 
332 static_assert((sizeof(CALLBACK_ENUMS) / sizeof(CallbackChange)) == static_cast<size_t>(CALLBACK_MAX),
333     "check CALLBACK_ENUMS");
334 
335 struct VolumeEvent {
336     AudioVolumeType volumeType;
337     int32_t volume;
338     bool updateUi;
339     int32_t volumeGroupId;
340     std::string networkId;
341 };
342 
343 struct AudioParameters {
344     AudioSampleFormat format;
345     AudioChannel channels;
346     AudioSamplingRate samplingRate;
347     AudioEncodingType encoding;
348     ContentType contentType;
349     StreamUsage usage;
350     DeviceRole deviceRole;
351     DeviceType deviceType;
352 };
353 
354 struct A2dpDeviceConfigInfo {
355     DeviceStreamInfo streamInfo;
356     bool absVolumeSupport = false;
357     int32_t volumeLevel;
358     bool mute = false;
359 };
360 
361 struct AudioRendererInfo {
362     ContentType contentType = CONTENT_TYPE_UNKNOWN;
363     StreamUsage streamUsage = STREAM_USAGE_UNKNOWN;
364     int32_t rendererFlags = AUDIO_FLAG_NORMAL;
365     std::string sceneType = "";
366     bool spatializationEnabled = false;
367     bool headTrackingEnabled = false;
368     int32_t originalFlag = AUDIO_FLAG_NORMAL;
369     AudioPipeType pipeType = PIPE_TYPE_UNKNOWN;
370     AudioSamplingRate samplingRate = SAMPLE_RATE_8000;
371     AudioSampleFormat format = SAMPLE_S16LE;
372     uint8_t encodingType = 0;
373     uint64_t channelLayout = 0ULL;
374     bool isOffloadAllowed = true;
375 
MarshallingAudioRendererInfo376     bool Marshalling(Parcel &parcel) const
377     {
378         return parcel.WriteInt32(static_cast<int32_t>(contentType))
379             && parcel.WriteInt32(static_cast<int32_t>(streamUsage))
380             && parcel.WriteInt32(rendererFlags)
381             && parcel.WriteInt32(originalFlag)
382             && parcel.WriteString(sceneType)
383             && parcel.WriteBool(spatializationEnabled)
384             && parcel.WriteBool(headTrackingEnabled)
385             && parcel.WriteInt32(static_cast<int32_t>(pipeType))
386             && parcel.WriteInt32(static_cast<int32_t>(samplingRate))
387             && parcel.WriteInt32(format)
388             && parcel.WriteUint8(encodingType)
389             && parcel.WriteUint64(channelLayout)
390             && parcel.WriteBool(isOffloadAllowed);
391     }
UnmarshallingAudioRendererInfo392     void Unmarshalling(Parcel &parcel)
393     {
394         contentType = static_cast<ContentType>(parcel.ReadInt32());
395         streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
396         rendererFlags = parcel.ReadInt32();
397         originalFlag = parcel.ReadInt32();
398         sceneType = parcel.ReadString();
399         spatializationEnabled = parcel.ReadBool();
400         headTrackingEnabled = parcel.ReadBool();
401         pipeType = static_cast<AudioPipeType>(parcel.ReadInt32());
402         samplingRate = static_cast<AudioSamplingRate>(parcel.ReadInt32());
403         format = static_cast<AudioSampleFormat>(parcel.ReadInt32());
404         encodingType = parcel.ReadUint8();
405         channelLayout = parcel.ReadUint64();
406         isOffloadAllowed = parcel.ReadBool();
407     }
408 };
409 
410 class AudioCapturerInfo {
411 public:
412     SourceType sourceType = SOURCE_TYPE_INVALID;
413     int32_t capturerFlags = 0;
414     AudioPipeType pipeType = PIPE_TYPE_UNKNOWN;
415     AudioSamplingRate samplingRate = SAMPLE_RATE_8000;
416     uint8_t encodingType = 0;
417     uint64_t channelLayout = 0ULL;
418     std::string sceneType = "";
419     int32_t originalFlag = AUDIO_FLAG_NORMAL;
AudioCapturerInfo(SourceType sourceType_,int32_t capturerFlags_)420     AudioCapturerInfo(SourceType sourceType_, int32_t capturerFlags_) : sourceType(sourceType_),
421         capturerFlags(capturerFlags_) {}
AudioCapturerInfo(const AudioCapturerInfo & audioCapturerInfo)422     AudioCapturerInfo(const AudioCapturerInfo &audioCapturerInfo)
423     {
424         *this = audioCapturerInfo;
425     }
426     AudioCapturerInfo() = default;
427     ~AudioCapturerInfo()= default;
Marshalling(Parcel & parcel)428     bool Marshalling(Parcel &parcel) const
429     {
430         return parcel.WriteInt32(static_cast<int32_t>(sourceType))
431             && parcel.WriteInt32(capturerFlags)
432             && parcel.WriteInt32(static_cast<int32_t>(pipeType))
433             && parcel.WriteInt32(static_cast<int32_t>(samplingRate))
434             && parcel.WriteUint8(encodingType)
435             && parcel.WriteUint64(channelLayout)
436             && parcel.WriteString(sceneType)
437             && parcel.WriteInt32(originalFlag);
438     }
Unmarshalling(Parcel & parcel)439     void Unmarshalling(Parcel &parcel)
440     {
441         sourceType = static_cast<SourceType>(parcel.ReadInt32());
442         capturerFlags = parcel.ReadInt32();
443         pipeType = static_cast<AudioPipeType>(parcel.ReadInt32());
444         samplingRate = static_cast<AudioSamplingRate>(parcel.ReadInt32());
445         encodingType = parcel.ReadUint8();
446         channelLayout = parcel.ReadUint64();
447         sceneType = parcel.ReadString();
448         originalFlag = parcel.ReadInt32();
449     }
450 };
451 
452 struct AudioRendererDesc {
453     ContentType contentType = CONTENT_TYPE_UNKNOWN;
454     StreamUsage streamUsage = STREAM_USAGE_UNKNOWN;
455 };
456 
457 struct AudioRendererOptions {
458     AudioStreamInfo streamInfo;
459     AudioRendererInfo rendererInfo;
460     AudioPrivacyType privacyType = PRIVACY_TYPE_PUBLIC;
461     AudioSessionStrategy strategy = { AudioConcurrencyMode::INVALID };
462 };
463 
464 struct MicStateChangeEvent {
465     bool mute;
466 };
467 
468 enum AudioScene : int32_t {
469     /**
470      * Invalid
471      */
472     AUDIO_SCENE_INVALID = -1,
473     /**
474      * Default audio scene
475      */
476     AUDIO_SCENE_DEFAULT,
477     /**
478      * Ringing audio scene
479      * Only available for system api.
480      */
481     AUDIO_SCENE_RINGING,
482     /**
483      * Phone call audio scene
484      * Only available for system api.
485      */
486     AUDIO_SCENE_PHONE_CALL,
487     /**
488      * Voice chat audio scene
489      */
490     AUDIO_SCENE_PHONE_CHAT,
491     /**
492      * AvSession set call start flag
493      */
494     AUDIO_SCENE_CALL_START,
495     /**
496      * AvSession set call end flag
497      */
498     AUDIO_SCENE_CALL_END,
499     /**
500      * Voice ringing audio scene
501      * Only available for system api.
502      */
503     AUDIO_SCENE_VOICE_RINGING,
504     /**
505      * Max
506      */
507     AUDIO_SCENE_MAX,
508 };
509 
510 enum AudioDeviceUsage : uint32_t {
511     /**
512      * Media output devices.
513      * @syscap SystemCapability.Multimedia.Audio.Device
514      * @systemapi
515      * @since 11
516      */
517     MEDIA_OUTPUT_DEVICES = 1,
518     /**
519      * Media input devices.
520      * @syscap SystemCapability.Multimedia.Audio.Device
521      * @systemapi
522      * @since 11
523      */
524     MEDIA_INPUT_DEVICES = 2,
525     /**
526      * All media devices.
527      * @syscap SystemCapability.Multimedia.Audio.Device
528      * @systemapi
529      * @since 11
530      */
531     ALL_MEDIA_DEVICES = 3,
532     /**
533      * Call output devices.
534      * @syscap SystemCapability.Multimedia.Audio.Device
535      * @systemapi
536      * @since 11
537      */
538     CALL_OUTPUT_DEVICES = 4,
539     /**
540      * Call input devices.
541      * @syscap SystemCapability.Multimedia.Audio.Device
542      * @systemapi
543      * @since 11
544      */
545     CALL_INPUT_DEVICES = 8,
546     /**
547      * All call devices.
548      * @syscap SystemCapability.Multimedia.Audio.Device
549      * @systemapi
550      * @since 11
551      */
552     ALL_CALL_DEVICES = 12,
553     /**
554      * All devices.
555      * @syscap SystemCapability.Multimedia.Audio.Device
556      * @systemapi
557      * @since 11
558      */
559     D_ALL_DEVICES = 15,
560 };
561 
562 enum FilterMode : uint32_t {
563     INCLUDE = 0,
564     EXCLUDE,
565     MAX_FILTER_MODE
566 };
567 
568 // 1.If the size of usages or pids is 0, FilterMode will not work.
569 // 2.Filters will only works with FileterMode INCLUDE or EXCLUDE while the vector size is not zero.
570 // 3.If usages and pids are both not empty, the result is the intersection of the two Filter.
571 // 4.If usages.size() == 0, defalut usages will be filtered with FilterMode::INCLUDE.
572 // 5.Default usages are MEDIA MUSIC MOVIE GAME and BOOK.
573 struct CaptureFilterOptions {
574     std::vector<StreamUsage> usages;
575     FilterMode usageFilterMode {FilterMode::INCLUDE};
576     std::vector<int32_t> pids;
577     FilterMode pidFilterMode {FilterMode::INCLUDE};
578 };
579 
580 struct AudioPlaybackCaptureConfig {
581     CaptureFilterOptions filterOptions;
582     bool silentCapture {false}; // To be deprecated since 12
583 };
584 
585 struct AudioCapturerOptions {
586     AudioStreamInfo streamInfo;
587     AudioCapturerInfo capturerInfo;
588     AudioPlaybackCaptureConfig playbackCaptureConfig;
589     AudioSessionStrategy strategy = { AudioConcurrencyMode::INVALID };
590 };
591 
592 struct AppInfo {
593     int32_t appUid { INVALID_UID };
594     uint32_t appTokenId { 0 };
595     int32_t appPid { 0 };
596     uint64_t appFullTokenId { 0 };
597 };
598 
599 struct BufferQueueState {
600     uint32_t numBuffers;
601     uint32_t currentIndex;
602 };
603 
604 enum AudioRenderMode {
605     RENDER_MODE_NORMAL,
606     RENDER_MODE_CALLBACK
607 };
608 
609 enum AudioCaptureMode {
610     CAPTURE_MODE_NORMAL,
611     CAPTURE_MODE_CALLBACK
612 };
613 
614 struct SinkInfo {
615     uint32_t sinkId; // sink id
616     std::string sinkName;
617     std::string adapterName;
618 };
619 
620 struct SinkInput {
621     int32_t streamId;
622     AudioStreamType streamType;
623 
624     // add for routing stream.
625     int32_t uid; // client uid
626     int32_t pid; // client pid
627     uint32_t paStreamId; // streamId
628     uint32_t deviceSinkId; // sink id
629     std::string sinkName; // sink name
630     int32_t statusMark; // mark the router status
631     uint64_t startTime; // when this router is created
632 };
633 
634 struct SourceOutput {
635     int32_t streamId;
636     AudioStreamType streamType;
637 
638     // add for routing stream.
639     int32_t uid; // client uid
640     int32_t pid; // client pid
641     uint32_t paStreamId; // streamId
642     uint32_t deviceSourceId; // sink id
643     int32_t statusMark; // mark the router status
644     uint64_t startTime; // when this router is created
645 };
646 
647 typedef uint32_t AudioIOHandle;
648 
FLOAT_COMPARE_EQ(const float & x,const float & y)649 static inline bool FLOAT_COMPARE_EQ(const float& x, const float& y)
650 {
651     return (std::abs((x) - (y)) <= (std::numeric_limits<float>::epsilon()));
652 }
653 
654 enum AudioServiceIndex {
655     HDI_SERVICE_INDEX = 0,
656     AUDIO_SERVICE_INDEX
657 };
658 
659 /**
660  * @brief Enumerates the rendering states of the current device.
661  */
662 enum RendererState {
663     /** INVALID state */
664     RENDERER_INVALID = -1,
665     /** Create New Renderer instance */
666     RENDERER_NEW,
667     /** Reneder Prepared state */
668     RENDERER_PREPARED,
669     /** Rendere Running state */
670     RENDERER_RUNNING,
671     /** Renderer Stopped state */
672     RENDERER_STOPPED,
673     /** Renderer Released state */
674     RENDERER_RELEASED,
675     /** Renderer Paused state */
676     RENDERER_PAUSED
677 };
678 
679 /**
680  * @brief Enumerates the capturing states of the current device.
681  */
682 enum CapturerState {
683     /** Capturer INVALID state */
684     CAPTURER_INVALID = -1,
685     /** Create new capturer instance */
686     CAPTURER_NEW,
687     /** Capturer Prepared state */
688     CAPTURER_PREPARED,
689     /** Capturer Running state */
690     CAPTURER_RUNNING,
691     /** Capturer Stopped state */
692     CAPTURER_STOPPED,
693     /** Capturer Released state */
694     CAPTURER_RELEASED,
695     /** Capturer Paused state */
696     CAPTURER_PAUSED
697 };
698 
699 enum State {
700     /** INVALID */
701     INVALID = -1,
702     /** New */
703     NEW,
704     /** Prepared */
705     PREPARED,
706     /** Running */
707     RUNNING,
708     /** Stopped */
709     STOPPED,
710     /** Released */
711     RELEASED,
712     /** Paused */
713     PAUSED,
714     /** Stopping */
715     STOPPING
716 };
717 
718 struct AudioRegisterTrackerInfo {
719     uint32_t sessionId;
720     int32_t clientPid;
721     State state;
722     AudioRendererInfo rendererInfo;
723     AudioCapturerInfo capturerInfo;
724     int32_t channelCount;
725     uint32_t appTokenId;
726 };
727 
728 enum StateChangeCmdType {
729     CMD_FROM_CLIENT = 0,
730     CMD_FROM_SYSTEM = 1
731 };
732 
733 enum AudioMode {
734     AUDIO_MODE_PLAYBACK,
735     AUDIO_MODE_RECORD
736 };
737 
738 // LEGACY_INNER_CAP: Called from hap build with api < 12, work normally.
739 // LEGACY_MUTE_CAP: Called from hap build with api >= 12, will cap mute data.
740 // MODERN_INNER_CAP: Called from SA with inner-cap right, work with filter.
741 enum InnerCapMode : uint32_t {
742     LEGACY_INNER_CAP = 0,
743     LEGACY_MUTE_CAP,
744     MODERN_INNER_CAP,
745     INVALID_CAP_MODE
746 };
747 
748 struct AudioProcessConfig {
749     int32_t callerUid = INVALID_UID;
750 
751     AppInfo appInfo;
752 
753     AudioStreamInfo streamInfo;
754 
755     AudioMode audioMode = AUDIO_MODE_PLAYBACK;
756 
757     AudioRendererInfo rendererInfo;
758 
759     AudioCapturerInfo capturerInfo;
760 
761     AudioStreamType streamType = STREAM_DEFAULT;
762 
763     DeviceType deviceType = DEVICE_TYPE_INVALID;
764 
765     bool isInnerCapturer = false;
766 
767     bool isWakeupCapturer = false;
768 
769     uint32_t originalSessionId = 0;
770 
771     AudioPrivacyType privacyType = PRIVACY_TYPE_PUBLIC;
772 
773     InnerCapMode innerCapMode {InnerCapMode::INVALID_CAP_MODE};
774 };
775 
776 struct Volume {
777     bool isMute = false;
778     float volumeFloat = 1.0f;
779     uint32_t volumeInt = 0;
780 };
781 
782 enum StreamSetState {
783     STREAM_PAUSE,
784     STREAM_RESUME
785 };
786 
787 struct StreamSetStateEventInternal {
788     StreamSetState streamSetState;
789     StreamUsage streamUsage;
790 };
791 
792 class AudioRendererChangeInfo {
793 public:
794     int32_t createrUID;
795     int32_t clientUID;
796     int32_t sessionId;
797     int32_t callerPid;
798     int32_t clientPid;
799     int32_t tokenId;
800     int32_t channelCount;
801     AudioRendererInfo rendererInfo;
802     RendererState rendererState;
803     DeviceInfo outputDeviceInfo;
804     bool prerunningState = false;
805 
AudioRendererChangeInfo(const AudioRendererChangeInfo & audioRendererChangeInfo)806     AudioRendererChangeInfo(const AudioRendererChangeInfo &audioRendererChangeInfo)
807     {
808         *this = audioRendererChangeInfo;
809     }
810     AudioRendererChangeInfo() = default;
811     ~AudioRendererChangeInfo() = default;
Marshalling(Parcel & parcel)812     bool Marshalling(Parcel &parcel) const
813     {
814         return parcel.WriteInt32(createrUID)
815             && parcel.WriteInt32(clientUID)
816             && parcel.WriteInt32(sessionId)
817             && parcel.WriteInt32(callerPid)
818             && parcel.WriteInt32(clientPid)
819             && parcel.WriteInt32(tokenId)
820             && parcel.WriteInt32(channelCount)
821             && rendererInfo.Marshalling(parcel)
822             && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.contentType))
823             && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.streamUsage))
824             && parcel.WriteInt32(rendererInfo.rendererFlags)
825             && parcel.WriteInt32(rendererInfo.originalFlag)
826             && parcel.WriteInt32(rendererInfo.samplingRate)
827             && parcel.WriteInt32(rendererInfo.format)
828             && parcel.WriteInt32(static_cast<int32_t>(rendererState))
829             && outputDeviceInfo.Marshalling(parcel);
830     }
Marshalling(Parcel & parcel,bool hasBTPermission,bool hasSystemPermission,int32_t apiVersion)831     bool Marshalling(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission, int32_t apiVersion) const
832     {
833         return parcel.WriteInt32(createrUID)
834             && parcel.WriteInt32(hasSystemPermission ? clientUID : EMPTY_UID)
835             && parcel.WriteInt32(sessionId)
836             && parcel.WriteInt32(callerPid)
837             && parcel.WriteInt32(clientPid)
838             && parcel.WriteInt32(tokenId)
839             && parcel.WriteInt32(channelCount)
840             && rendererInfo.Marshalling(parcel)
841             && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.contentType))
842             && parcel.WriteInt32(static_cast<int32_t>(rendererInfo.streamUsage))
843             && parcel.WriteInt32(rendererInfo.rendererFlags)
844             && parcel.WriteInt32(rendererInfo.originalFlag)
845             && parcel.WriteInt32(rendererInfo.samplingRate)
846             && parcel.WriteInt32(rendererInfo.format)
847             && parcel.WriteInt32(hasSystemPermission ? static_cast<int32_t>(rendererState) :
848                 RENDERER_INVALID)
849             && outputDeviceInfo.Marshalling(parcel, hasBTPermission, hasSystemPermission, apiVersion);
850     }
Unmarshalling(Parcel & parcel)851     void Unmarshalling(Parcel &parcel)
852     {
853         createrUID = parcel.ReadInt32();
854         clientUID = parcel.ReadInt32();
855         sessionId = parcel.ReadInt32();
856         callerPid = parcel.ReadInt32();
857         clientPid = parcel.ReadInt32();
858         tokenId = parcel.ReadInt32();
859         channelCount = parcel.ReadInt32();
860 
861         rendererInfo.Unmarshalling(parcel);
862 
863         rendererInfo.contentType = static_cast<ContentType>(parcel.ReadInt32());
864         rendererInfo.streamUsage = static_cast<StreamUsage>(parcel.ReadInt32());
865         rendererInfo.rendererFlags = parcel.ReadInt32();
866         rendererInfo.originalFlag = parcel.ReadInt32();
867         rendererInfo.samplingRate = static_cast<AudioSamplingRate>(parcel.ReadInt32());
868         rendererInfo.format = static_cast<AudioSampleFormat>(parcel.ReadInt32());
869 
870         rendererState = static_cast<RendererState>(parcel.ReadInt32());
871         outputDeviceInfo.Unmarshalling(parcel);
872     }
873 };
874 
875 class AudioCapturerChangeInfo {
876 public:
877     int32_t createrUID;
878     int32_t clientUID;
879     int32_t sessionId;
880     int32_t callerPid;
881     int32_t clientPid;
882     AudioCapturerInfo capturerInfo;
883     CapturerState capturerState;
884     DeviceInfo inputDeviceInfo;
885     bool muted;
886     uint32_t appTokenId;
887 
AudioCapturerChangeInfo(const AudioCapturerChangeInfo & audioCapturerChangeInfo)888     AudioCapturerChangeInfo(const AudioCapturerChangeInfo &audioCapturerChangeInfo)
889     {
890         *this = audioCapturerChangeInfo;
891     }
892     AudioCapturerChangeInfo() = default;
893     ~AudioCapturerChangeInfo() = default;
Marshalling(Parcel & parcel)894     bool Marshalling(Parcel &parcel) const
895     {
896         return parcel.WriteInt32(createrUID)
897             && parcel.WriteInt32(clientUID)
898             && parcel.WriteInt32(sessionId)
899             && parcel.WriteInt32(callerPid)
900             && parcel.WriteInt32(clientPid)
901             && capturerInfo.Marshalling(parcel)
902             && parcel.WriteInt32(static_cast<int32_t>(capturerState))
903             && inputDeviceInfo.Marshalling(parcel)
904             && parcel.WriteBool(muted)
905             && parcel.WriteUint32(appTokenId);
906     }
907 
Marshalling(Parcel & parcel,bool hasBTPermission,bool hasSystemPermission,int32_t apiVersion)908     bool Marshalling(Parcel &parcel, bool hasBTPermission, bool hasSystemPermission, int32_t apiVersion) const
909     {
910         return parcel.WriteInt32(createrUID)
911             && parcel.WriteInt32(hasSystemPermission ? clientUID : EMPTY_UID)
912             && parcel.WriteInt32(sessionId)
913             && parcel.WriteInt32(callerPid)
914             && parcel.WriteInt32(clientPid)
915             && capturerInfo.Marshalling(parcel)
916             && parcel.WriteInt32(hasSystemPermission ? static_cast<int32_t>(capturerState) : CAPTURER_INVALID)
917             && inputDeviceInfo.Marshalling(parcel, hasBTPermission, hasSystemPermission, apiVersion)
918             && parcel.WriteBool(muted)
919             && parcel.WriteUint32(appTokenId);
920     }
921 
Unmarshalling(Parcel & parcel)922     void Unmarshalling(Parcel &parcel)
923     {
924         createrUID = parcel.ReadInt32();
925         clientUID = parcel.ReadInt32();
926         sessionId = parcel.ReadInt32();
927         callerPid = parcel.ReadInt32();
928         clientPid = parcel.ReadInt32();
929         capturerInfo.Unmarshalling(parcel);
930         capturerState = static_cast<CapturerState>(parcel.ReadInt32());
931         inputDeviceInfo.Unmarshalling(parcel);
932         muted = parcel.ReadBool();
933         appTokenId = parcel.ReadUint32();
934     }
935 };
936 
937 struct AudioStreamChangeInfo {
938     AudioRendererChangeInfo audioRendererChangeInfo;
939     AudioCapturerChangeInfo audioCapturerChangeInfo;
940 };
941 
942 enum AudioPin {
943     AUDIO_PIN_NONE = 0, // Invalid pin
944     AUDIO_PIN_OUT_SPEAKER = 1 << 0, // Speaker output pin
945     AUDIO_PIN_OUT_HEADSET = 1 << 1, // Wired headset pin for output
946     AUDIO_PIN_OUT_LINEOUT = 1 << 2, // Line-out pin
947     AUDIO_PIN_OUT_HDMI = 1 << 3, // HDMI output pin
948     AUDIO_PIN_OUT_USB = 1 << 4, // USB output pin
949     AUDIO_PIN_OUT_USB_EXT = 1 << 5, // Extended USB output pin
950     AUDIO_PIN_OUT_BLUETOOTH_SCO = 1 << 6, // Bluetooth SCO output pin
951     AUDIO_PIN_OUT_DAUDIO_DEFAULT = 1 << 7, // Daudio default output pin
952     AUDIO_PIN_OUT_HEADPHONE = 1 << 8, // Wired headphone output pin
953     AUDIO_PIN_OUT_USB_HEADSET = 1 << 9,  // Arm usb output pin
954     AUDIO_PIN_OUT_DP = 1 << 11,
955     AUDIO_PIN_IN_MIC = 1 << 27 | 1 << 0, // Microphone input pin
956     AUDIO_PIN_IN_HS_MIC = 1 << 27 | 1 << 1, // Wired headset microphone pin for input
957     AUDIO_PIN_IN_LINEIN = 1 << 27 | 1 << 2, // Line-in pin
958     AUDIO_PIN_IN_USB_EXT = 1 << 27 | 1 << 3, // Extended USB input pin
959     AUDIO_PIN_IN_BLUETOOTH_SCO_HEADSET = 1 << 27 | 1 << 4, // Bluetooth SCO headset input pin
960     AUDIO_PIN_IN_DAUDIO_DEFAULT = 1 << 27 | 1 << 5, // Daudio default input pin
961     AUDIO_PIN_IN_USB_HEADSET = 1 << 27 | 1 << 6,  // Arm usb input pin
962 };
963 
964 enum AudioParamKey {
965     NONE = 0,
966     VOLUME = 1,
967     INTERRUPT = 2,
968     PARAM_KEY_STATE = 5,
969     A2DP_SUSPEND_STATE = 6,  // for bluetooth sink
970     BT_HEADSET_NREC = 7,
971     BT_WBS = 8,
972     A2DP_OFFLOAD_STATE = 9, // for a2dp offload
973     GET_DP_DEVICE_INFO = 10, // for dp sink
974     USB_DEVICE = 101, // Check USB device type ARM or HIFI
975     PERF_INFO = 201,
976     MMI = 301,
977     PARAM_KEY_LOWPOWER = 1000,
978 };
979 
980 struct DStatusInfo {
981     char networkId[NETWORK_ID_SIZE];
982     AudioPin hdiPin = AUDIO_PIN_NONE;
983     int32_t mappingVolumeId = 0;
984     int32_t mappingInterruptId = 0;
985     int32_t deviceId;
986     int32_t channelMasks;
987     std::string deviceName = "";
988     bool isConnected = false;
989     std::string macAddress;
990     DeviceStreamInfo streamInfo = {};
991     ConnectType connectType = CONNECT_TYPE_LOCAL;
992 };
993 
994 struct AudioRendererDataInfo {
995     uint8_t *buffer;
996     size_t flag;
997 };
998 
999 enum AudioPermissionState {
1000     AUDIO_PERMISSION_START = 0,
1001     AUDIO_PERMISSION_STOP = 1,
1002 };
1003 
1004 class AudioRendererPolicyServiceDiedCallback {
1005 public:
1006     virtual ~AudioRendererPolicyServiceDiedCallback() = default;
1007 
1008     /**
1009      * Called when audio policy service died.
1010      * @since 10
1011      */
1012     virtual void OnAudioPolicyServiceDied() = 0;
1013 };
1014 
1015 class AudioStreamPolicyServiceDiedCallback {
1016 public:
1017     virtual ~AudioStreamPolicyServiceDiedCallback() = default;
1018 
1019     /**
1020      * Called when audio policy service died.
1021      * @since 11
1022      */
1023     virtual void OnAudioPolicyServiceDied() = 0;
1024 };
1025 
1026 /**
1027  * Describes three-dimensional value.
1028  * @since 11
1029  */
1030 struct Vector3D {
1031     /**
1032      * X-axis value.
1033      * @since 11
1034      */
1035     float x;
1036     /**
1037      * Y-axis value.
1038      * @since 11
1039      */
1040     float y;
1041     /**
1042      * Z-axis value.
1043      * @since 11
1044      */
1045     float z;
1046 };
1047 
1048 struct SessionInfo {
1049     SourceType sourceType;
1050     uint32_t rate;
1051     uint32_t channels;
1052 };
1053 
1054 enum CastType {
1055     CAST_TYPE_NULL = 0,
1056     CAST_TYPE_ALL,
1057     CAST_TYPE_PROJECTION,
1058     CAST_TYPE_COOPERATION,
1059 };
1060 
1061 class AudioPnpDeviceChangeCallback {
1062 public:
1063     virtual ~AudioPnpDeviceChangeCallback() = default;
1064     virtual void OnPnpDeviceStatusChanged(const std::string &info) = 0;
1065     virtual void OnMicrophoneBlocked(const std::string &info) = 0;
1066 };
1067 
1068 struct SourceInfo {
1069     SourceType sourceType_;
1070     uint32_t rate_;
1071     uint32_t channels_;
1072 };
1073 
1074 /**
1075  * @brief Device group used by set/get volume.
1076  */
1077 enum DeviceGroup {
1078     /** Invalid device group */
1079     DEVICE_GROUP_INVALID = -1,
1080     /** Built in device */
1081     DEVICE_GROUP_BUILT_IN,
1082     /** Wired device */
1083     DEVICE_GROUP_WIRED,
1084     /** Wireless device */
1085     DEVICE_GROUP_WIRELESS,
1086     /** Remote cast device */
1087     DEVICE_GROUP_REMOTE_CAST,
1088     /* earpiece device*/
1089     DEVICE_GROUP_EARPIECE,
1090 };
1091 
1092 static const std::map<DeviceType, DeviceGroup> DEVICE_GROUP_FOR_VOLUME = {
1093     {DEVICE_TYPE_EARPIECE, DEVICE_GROUP_EARPIECE},
1094     {DEVICE_TYPE_SPEAKER, DEVICE_GROUP_BUILT_IN},
1095     {DEVICE_TYPE_WIRED_HEADSET, DEVICE_GROUP_WIRED},
1096     {DEVICE_TYPE_USB_HEADSET, DEVICE_GROUP_WIRED},
1097     {DEVICE_TYPE_USB_ARM_HEADSET, DEVICE_GROUP_WIRED},
1098     {DEVICE_TYPE_DP, DEVICE_GROUP_WIRED},
1099     {DEVICE_TYPE_BLUETOOTH_A2DP, DEVICE_GROUP_WIRELESS},
1100     {DEVICE_TYPE_BLUETOOTH_SCO, DEVICE_GROUP_WIRELESS},
1101     {DEVICE_TYPE_REMOTE_CAST, DEVICE_GROUP_REMOTE_CAST},
1102 };
1103 
GetVolumeGroupForDevice(DeviceType deviceType)1104 static inline DeviceGroup GetVolumeGroupForDevice(DeviceType deviceType)
1105 {
1106     auto it = DEVICE_GROUP_FOR_VOLUME.find(deviceType);
1107     if (it == DEVICE_GROUP_FOR_VOLUME.end()) {
1108         return DEVICE_GROUP_INVALID;
1109     }
1110     return it->second;
1111 }
1112 
1113 enum RouterType {
1114     /**
1115      * None router.
1116      * @since 12
1117      */
1118     ROUTER_TYPE_NONE = 0,
1119     /**
1120      * Default router.
1121      * @since 12
1122      */
1123     ROUTER_TYPE_DEFAULT,
1124     /**
1125      * Stream filter router.
1126      * @since 12
1127      */
1128     ROUTER_TYPE_STREAM_FILTER,
1129     /**
1130      * Package filter router.
1131      * @since 12
1132      */
1133     ROUTER_TYPE_PACKAGE_FILTER,
1134     /**
1135      * Cockpit phone router.
1136      * @since 12
1137      */
1138     ROUTER_TYPE_COCKPIT_PHONE,
1139     /**
1140      * Privacy priority router.
1141      * @since 12
1142      */
1143     ROUTER_TYPE_PRIVACY_PRIORITY,
1144     /**
1145      * Public priority router.
1146      * @since 12
1147      */
1148     ROUTER_TYPE_PUBLIC_PRIORITY,
1149     /**
1150      * Pair device router.
1151      * @since 12
1152      */
1153     ROUTER_TYPE_PAIR_DEVICE,
1154     /**
1155      * User select router.
1156      * @since 12
1157      */
1158     ROUTER_TYPE_USER_SELECT,
1159 };
1160 
1161 enum RenderMode {
1162     /**
1163      * Primary render mode.
1164      * @since 12
1165      */
1166     PRIMARY,
1167     /**
1168      * VOIP render mode.
1169      * @since 12
1170      */
1171     VOIP,
1172     /**
1173      * Offload render mode.
1174      * @since 12
1175      */
1176     OFFLOAD,
1177     /**
1178      * Low latency render mode.
1179      * @since 12
1180      */
1181     LOW_LATENCY,
1182 };
1183 
1184 enum WriteDataCallbackType {
1185     /**
1186      * Use OH_AudioRenderer_Callbacks.OH_AudioRenderer_OnWriteData
1187      * @since 12
1188      */
1189     WRITE_DATA_CALLBACK_WITHOUT_RESULT = 0,
1190     /**
1191      * Use OH_AudioRenderer_OnWriteDataCallback.
1192      * @since 12
1193      */
1194     WRITE_DATA_CALLBACK_WITH_RESULT = 1
1195 };
1196 
1197 enum PolicyType {
1198     EDM_POLICY_TYPE = 0,
1199     PRIVACY_POLCIY_TYPE = 1,
1200     TEMPORARY_POLCIY_TYPE = 2,
1201 };
1202 
1203 static inline const std::unordered_set<SourceType> specialSourceTypeSet_ = {
1204     SOURCE_TYPE_PLAYBACK_CAPTURE,
1205     SOURCE_TYPE_WAKEUP,
1206     SOURCE_TYPE_VIRTUAL_CAPTURE,
1207     SOURCE_TYPE_REMOTE_CAST
1208 };
1209 } // namespace AudioStandard
1210 } // namespace OHOS
1211 #endif // AUDIO_INFO_H
1212