• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef AUDIO_STREAM_H
16 #define AUDIO_STREAM_H
17 
18 #include "audio_renderer.h"
19 #include "audio_session.h"
20 #include "timestamp.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 static constexpr int32_t MAX_NUM_BUFFERS = 3;
25 
26 enum AudioMode {
27     AUDIO_MODE_PLAYBACK,
28     AUDIO_MODE_RECORD
29 };
30 
31 class AudioStream : public AudioSession {
32 public:
33     AudioStream(AudioStreamType eStreamType, AudioMode eMode);
34     virtual ~AudioStream();
35 
36     int32_t SetAudioStreamInfo(const AudioStreamParams info);
37     int32_t GetAudioStreamInfo(AudioStreamParams &info);
38 
39     int32_t GetAudioSessionID(uint32_t &sessionID) const;
40     State GetState();
41     bool GetAudioTime(Timestamp &timestamp, Timestamp::Timestampbase base);
42     int32_t GetBufferSize(size_t &bufferSize) const;
43     int32_t GetFrameCount(uint32_t &frameCount) const;
44     int32_t GetLatency(uint64_t &latency) const;
45     static AudioStreamType GetStreamType(ContentType contentType, StreamUsage streamUsage);
46     int32_t SetAudioStreamType(AudioStreamType audioStreamType);
47     int32_t SetVolume(float volume);
48     float GetVolume();
49     int32_t SetRenderRate(AudioRendererRate renderRate);
50     AudioRendererRate GetRenderRate();
51     int32_t SetStreamCallback(const std::shared_ptr<AudioStreamCallback> &callback);
52     int32_t SetRenderMode(AudioRenderMode renderMode);
53     AudioRenderMode GetRenderMode();
54     int32_t SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback);
55     int32_t GetBufferDesc(BufferDesc &bufDesc);
56     int32_t Enqueue(const BufferDesc &bufDesc);
57     int32_t Clear();
58 
59     std::vector<AudioSampleFormat> GetSupportedFormats() const;
60     std::vector<AudioChannel> GetSupportedChannels() const;
61     std::vector<AudioEncodingType> GetSupportedEncodingTypes() const;
62     std::vector<AudioSamplingRate> GetSupportedSamplingRates() const;
63 
64     // Common APIs
65     bool StartAudioStream();
66     bool PauseAudioStream();
67     bool StopAudioStream();
68     bool ReleaseAudioStream();
69     bool FlushAudioStream();
70 
71     // Playback related APIs
72     bool DrainAudioStream();
73     size_t Write(uint8_t *buffer, size_t buffer_size);
74 
75     // Recording related APIs
76     int32_t Read(uint8_t &buffer, size_t userSize, bool isBlockingRead);
77 private:
78     AudioStreamType eStreamType_;
79     AudioMode eMode_;
80     State state_;
81     std::atomic<bool> isReadInProgress_;
82     std::atomic<bool> isWriteInProgress_;
83     bool resetTime_;
84     uint64_t resetTimestamp_;
85     struct timespec baseTimestamp_;
86     AudioRenderMode renderMode_;
87     std::queue<BufferDesc> freeBufferQ_;
88     std::queue<BufferDesc> filledBufferQ_;
89     std::array<std::unique_ptr<uint8_t[]>, MAX_NUM_BUFFERS> bufferPool_ = {};
90     std::unique_ptr<std::thread> writeThread_ = nullptr;
91     bool isReadyToWrite_;
92     void WriteBuffers();
93 
94     static const std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> streamTypeMap_;
95     static std::map<std::pair<ContentType, StreamUsage>, AudioStreamType> CreateStreamMap();
96     bool isFirstRead_;
97 };
98 } // namespace AudioStandard
99 } // namespace OHOS
100 #endif // AUDIO_STREAM_H
101