• 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 AUDIO_CONTAINER_BASE_H
17 #define AUDIO_CONTAINER_BASE_H
18 
19 #include "audio_container_stream_callback.h"
20 #include "audio_stream_death_recipient.h"
21 
22 #include "audio_log.h"
23 #include "unistd.h"
24 #include "securec.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 class AudioContainerBase : public AudioTimer {
29 public:
30     /**
31     * Initializes audio service client for the required client type
32     *
33     * @param eClientType indicates the client type like playback, record or controller.
34     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
35     */
36     int32_t Initialize(ASClientType eClientType, int serviceId);
37 
38     // Stream handling APIs
39 
40     /**
41     * Creates & initializes resources based on the audioParams and audioType
42     *
43     * @param audioParams indicate format, sampling rate and number of channels
44     * @param audioType indicate the stream type like music, system, ringtone etc
45     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
46     */
47     int32_t CreateStream(AudioStreamParams audioParams, AudioStreamType audioType);
48 
49     /**
50      * @brief Obtains session ID .
51      *
52      * @return Returns unique session ID for the created session
53     */
54     int32_t GetSessionID(uint32_t &sessionID, const int32_t &trackId) const;
55 
56     /**
57     * Starts the stream created using CreateStreamGa
58     *
59     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
60     */
61     int32_t StartStream(const int32_t &trackId);
62 
63     /**
64     * Stops the stream created using CreateStreamGa
65     *
66     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
67     */
68     int32_t StopStream(const int32_t &trackId);
69 
70     /**
71     * Flushes the stream created using CreateStreamGa. This is applicable for
72     * playback only
73     *
74     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
75     */
76     int32_t FlushStream(const int32_t &trackId);
77 
78     /**
79     * Drains the stream created using CreateStreamGa. This is applicable for
80     * playback only
81     *
82     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
83     */
84     int32_t DrainStream(const int32_t &trackId);
85 
86     /**
87     * Pauses the stream
88     *
89     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
90     */
91     int32_t PauseStream(const int32_t &trackId);
92 
93     /**
94     * Update the stream type
95     *
96     * @param audioStreamType Audio stream type
97     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
98     */
99     int32_t SetStreamType(AudioStreamType audioStreamType, const int32_t &trackId);
100 
101     /**
102     * Writes audio data of the stream created using CreateStreamGa to active sink device
103     *
104     * @param buffer contains audio data to write
105     * @param bufferSize indicates the size of audio data in bytes to write from the buffer
106     * @param pError indicates pointer to error which will be filled in case of internal errors
107     * @return returns size of audio data written in bytes.
108     */
109     size_t WriteStream(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId);
110 
111     /**
112     * Writes audio data of the stream created using CreateStreamGa to active sink device
113     Used when render mode is RENDER_MODE_CALLBACK
114     *
115     * @param buffer contains audio data to write
116     * @param bufferSize indicates the size of audio data in bytes to write from the buffer
117     * @param pError indicates pointer to error which will be filled in case of internal errors
118     * @return returns size of audio data written in bytes.
119     */
120     size_t WriteStreamInCb(const StreamBuffer &stream, int32_t &pError, const int32_t &trackId);
121 
122     /**
123     * Reads audio data of the stream created using CreateStreamGa from active source device
124     *
125     * @param StreamBuffer including buffer to be filled with audio data
126     * and bufferSize indicating the size of audio data to read into buffer
127     * @param isBlocking indicates if the read is blocking or not
128     * @return Returns size read if success; returns {@code -1} failure.
129     */
130     virtual int32_t ReadStream(StreamBuffer &stream, bool isBlocking, const int32_t &trackId);
131 
132     /**
133     * Release the resources allocated using CreateStreamGa
134     *
135     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
136     */
137     int32_t ReleaseStream(const int32_t &trackId);
138 
139     /**
140     * Provides the current timestamp for playback/record stream created using CreateStreamGa
141     *
142     * @param timeStamp will be filled up with current timestamp
143     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
144     */
145     int32_t GetCurrentTimeStamp(uint64_t &timeStamp, const int32_t &trackId);
146 
147     /**
148     * Provides the current latency for playback/record stream created using CreateStreamGa
149     *
150     * @param latency will be filled up with the current latency in microseconds
151     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
152     */
153     int32_t GetAudioLatency(uint64_t &latency, const int32_t &trackId) const;
154 
155     /**
156     * Provides the playback/record stream parameters created using CreateStreamGa
157     *
158     * @param audioParams will be filled up with stream audio parameters
159     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
160     */
161     int32_t GetAudioStreamParams(AudioStreamParams &audioParams, const int32_t &trackId);
162 
163     /**
164     * Provides the minimum buffer size required for this audio stream
165     * created using CreateStreamGa
166     * @param minBufferSize will be set to minimum buffer size in bytes
167     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
168     */
169     int32_t GetMinimumBufferSize(size_t &minBufferSize, const int32_t &trackId) const;
170 
171     /**
172     * Provides the minimum frame count required for this audio stream
173     * created using CreateStreamGa
174     * @param frameCount will be set to minimum number of frames
175     * @return Returns {@code 0} if success; returns {@code -1} otherwise.
176     */
177     int32_t GetMinimumFrameCount(uint32_t &frameCount, const int32_t &trackId) const;
178 
179     /**
180      * @brief Set the track volume
181      *
182      * @param volume The volume to be set for the current track.
183      * @return Returns {@link SUCCESS} if volume is successfully set; returns an error code
184      * defined in {@link audio_errors.h} otherwise.
185      */
186     int32_t SetStreamVolume(float volume, const int32_t &trackId);
187 
188     /**
189      * @brief Obtains the current track volume
190      *
191      * @return Returns current track volume
192      */
193     float GetStreamVolume();
194 
195     /**
196      * @brief Set the render rate
197      *
198      * @param renderRate The rate at which the stream needs to be rendered.
199      * @return Returns {@link SUCCESS} if render rate is successfully set; returns an error code
200      * defined in {@link audio_errors.h} otherwise.
201      */
202     int32_t SetStreamRenderRate(AudioRendererRate renderRate, const int32_t &trackId);
203 
204     /**
205      * @brief Obtains the current render rate
206      *
207      * @return Returns current render rate
208      */
209     AudioRendererRate GetStreamRenderRate();
210 
211     /**
212      * @brief Saves StreamCallback
213      *
214      * @param callback callback reference to be saved.
215      * @return none.
216      */
217 
218     void SaveStreamCallback(const std::weak_ptr<AudioStreamCallback> &callback);
219 
220     /**
221      * @brief Sets the render mode. By default the mode is RENDER_MODE_NORMAL.
222      * This API is needs to be used only if RENDER_MODE_CALLBACK is required.
223      *
224      * * @param renderMode The mode of render.
225      * @return  Returns {@link SUCCESS} if render mode is successfully set; returns an error code
226      * defined in {@link audio_errors.h} otherwise.
227      */
228     int32_t SetAudioRenderMode(AudioRenderMode renderMode, const int32_t &trackId);
229 
230     /**
231      * @brief Obtains the render mode.
232      *
233      * @return  Returns current render mode.
234      */
235     AudioRenderMode GetAudioRenderMode();
236 
237     /**
238      * @brief Set the buffer duration in msec
239      *
240      * @param bufferSizeInMsec buffer size in duration.
241      * @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise.
242      */
243     int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec);
244 
245     /**
246     * Set the renderer frame position callback
247     *
248     * @param callback indicates pointer for registered callbacks
249     * @return none
250     */
251     void SetRendererPositionCallback(int64_t markPosition, const std::shared_ptr<RendererPositionCallback> &callback);
252 
253     /**
254     * Unset the renderer frame position callback
255     *
256     * @return none
257     */
258     void UnsetRendererPositionCallback();
259 
260     /**
261     * Set the renderer frame period position callback
262     *
263     * @param callback indicates pointer for registered callbacks
264     * @return none
265     */
266     void SetRendererPeriodPositionCallback(int64_t markPosition,
267         const std::shared_ptr<RendererPeriodPositionCallback> &callback);
268 
269     /**
270     * Unset the renderer frame period position callback
271     *
272     * @return none
273     */
274     void UnsetRendererPeriodPositionCallback();
275 
276     /**
277     * Set the capturer frame position callback
278     *
279     * @param callback indicates pointer for registered callbacks
280     * @return none
281     */
282     void SetCapturerPositionCallback(int64_t markPosition, const std::shared_ptr<CapturerPositionCallback> &callback);
283 
284     /**
285     * Unset the capturer frame position callback
286     *
287     * @return none
288     */
289     void UnsetCapturerPositionCallback();
290 
291     /**
292     * Set the capturer frame period position callback
293     *
294     * @param callback indicates pointer for registered callbacks
295     * @return none
296     */
297     void SetCapturerPeriodPositionCallback(int64_t markPosition,
298         const std::shared_ptr<CapturerPeriodPositionCallback> &callback);
299 
300     /**
301     * Unset the capturer frame period position callback
302     *
303     * @return none
304     */
305     void UnsetCapturerPeriodPositionCallback();
306 
307     // Audio timer callback
308     virtual void OnTimeOut();
309 
310     int32_t SetAudioCaptureMode(AudioCaptureMode captureMode);
311 
312     int32_t SaveReadCallback(const std::weak_ptr<AudioCapturerReadCallback> &callback);
313     /**
314      * @brief Set the applicationcache path to access the application resources
315      *
316      * @return none
317      */
318     void SetAppCachePath(const std::string cachePath);
319 
320     AudioCaptureMode GetAudioCaptureMode();
321 
322     void InitAudioStreamManagerGa(int serviceId);
323 
324     /**
325      * @brief Registers the renderer write callback listener.
326      * This API should only be used if RENDER_MODE_CALLBACK is needed.
327      *
328      * @return Returns {@link SUCCESS} if callback registration is successful; returns an error code
329      * defined in {@link audio_errors.h} otherwise.
330      */
331     virtual int32_t SaveWriteCallback(const std::weak_ptr<AudioRendererWriteCallback> &callback,
332         const int32_t &trackId = 0);
333 
334     static void RegisterAudioStreamDeathRecipient(sptr<IRemoteObject> &object);
335 
336 private:
337     static void AudioStreamDied(pid_t pid);
338 };
339 } // namespace AudioStandard
340 } // namespace OHOS
341 #endif // AUDIO_CONTAINER_BASE_H