1 /*
2  * Copyright (c) 2020-2021 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 FRAMEWORKS_AUDIO_SOURCE_INCLUDE_AUDIO_SOURCE_H_
17 #define FRAMEWORKS_AUDIO_SOURCE_INCLUDE_AUDIO_SOURCE_H_
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include <time.h>
23 #include <vector>
24 
25 #include "audio_manager.h"
26 #include "format.h"
27 #include "media_errors.h"
28 #include "media_info.h"
29 
30 
31 namespace OHOS {
32 namespace Audio {
33 struct AudioSourceConfig {
34     /**
35      * Enumerates currently supported devices by audio source type,
36      * and binds current audio source a specified device.
37      */
38     uint32_t deviceId;
39     AudioCodecFormat audioFormat;
40     int32_t sampleRate = 0;
41     int32_t channelCount = 0;
42     bool interleaved;
43     AudioBitWidth bitWidth = BIT_WIDTH_16;
44     AudioStreamType streamUsage;
45 };
46 
47 struct AudioFrame {
48     uint8_t *buffer;    /* the virtual address of stream */
49     uint32_t bufferLen;   /* stream length, by bytes */
50     struct AudioTimeStamp time;
51     uint64_t frames;
52 };
53 
54 class AudioSource {
55 public:
56     AudioSource();
57     ~AudioSource();
58 
59     /**
60      * Enumerates currently supported devices by audio source type.
61      *
62      * @param inputSource the type of source audio.
63      * @param devices holds an array of satisfied audio device description, including name and identity.
64      * @return Returns SUCCESS if success, other values otherwise.
65      */
66     int32_t EnumDeviceBySourceType(AudioSourceType inputSource, std::vector<AudioDeviceDesc> &devices);
67 
68      /**
69      * Obtains the minimum frame count (in BytesPerSample) required in the specified conditions.
70      *
71      * @param sampleRate Indicates the sampling rate (Hz).
72      * @param channelCount Indicates the audio channel count.
73      * @param audioFormat Indicates the audio data format.
74      * @param frameCount the minimum frame count (in BytesPerSample).
75      * @return Returns {@code true} if success; returns {@code false} otherwise.
76      */
77     static bool GetMinFrameCount(int32_t sampleRate, int32_t channelCount,
78                                  AudioCodecFormat audioFormat, size_t &frameCount);
79 
80     /**
81      * Obtains the frame count (in BytesPerSample) required in the current conditions.
82      *
83      * @return Returns the frame count (in BytesPerSample); returns {@code -1} if an exception occurs.
84      */
85     uint64_t GetFrameCount();
86 
87     /**
88      * Initializes the audio source according to a specific configuration.
89      *
90      * @param config a configuration of audio source.
91      * @return Returns SUCCESS if success, other values otherwise.
92      */
93     int32_t Initialize(const AudioSourceConfig &config);
94 
95     /**
96      * Sets input device's identity when switching device.
97      *
98      * @param deviceId identity to set.
99      * @return Returns SUCCESS if set successfully, other values otherwise.
100     */
101     int32_t SetInputDevice(uint32_t deviceId);
102 
103     /**
104      * Gets current device's identity.
105      *
106      * @param deviceId holds the identity of current device, if success.
107      * @return Returns SUCCESS if success, other values otherwise.
108      */
109     int32_t GetCurrentDeviceId(uint32_t &deviceId);
110 
111     /**
112      * Starts audio source.
113      *
114      * @return Returns SUCCESS if success, other values otherwise.
115     */
116     int32_t Start();
117 
118     /**
119      *
120      * Reads frame from source.
121      *
122      * @param frame, the buffer to storage the info of frame that read from source.
123      * @param isBlockingRead reading mode.
124      * @return Returns size of data actually read.
125     */
126     int32_t ReadFrame(AudioFrame &frame, bool isBlockingRead);
127 
128     /**
129      * Stops audio source.
130      *
131      * @return Returns SUCCESS if success, other values otherwise.
132     */
133     int32_t Stop();
134 
135     /**
136     * release.
137     */
138     int32_t Release();
139 
140 private:
141     int32_t InitCheck();
142 
143 private:
144     bool initialized_;
145     bool started_;
146     AudioAdapter *audioAdapter_;
147     AudioCapture *audioCapture_;
148     AudioPort capturePort_ = {};
149 };
150 }  // namespace Audio
151 }  // namespace OHOS
152 #endif  // FRAMEWORKS_AUDIO_SOURCE_INCLUDE_AUDIO_SOURCE_H_
153