• 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 FAST_AUDIO_RENDERER_SINK_H
17 #define FAST_AUDIO_RENDERER_SINK_H
18 
19 #include "audio_info.h"
20 #include "audio_manager.h"
21 #include "audio_sink_callback.h"
22 
23 #include <cstdio>
24 #include <list>
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 typedef struct {
29     const char *adapterName;
30     uint32_t openMicSpeaker;
31     AudioFormat format;
32     uint32_t sampleFmt;
33     uint32_t sampleRate;
34     uint32_t channel;
35     float volume;
36     const char *filePath;
37 } AudioSinkAttr;
38 
39 class FastAudioRendererSink {
40 public:
41     static FastAudioRendererSink *GetInstance(void);
42 
43     int32_t Init(AudioSinkAttr &atrr);
44     void DeInit(void);
45     int32_t Start(void);
46     int32_t Stop(void);
47     int32_t Flush(void);
48     int32_t Reset(void);
49     int32_t Pause(void);
50     int32_t Resume(void);
51     int32_t RenderFrame(char &frame, uint64_t len, uint64_t &writeLen);
52     int32_t SetVolume(float left, float right);
53     int32_t GetVolume(float &left, float &right);
54     int32_t SetVoiceVolume(float volume);
55     int32_t GetLatency(uint32_t *latency);
56     bool rendererInited_;
57 
58     int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec);
59 
60     uint32_t frameSizeInByte_ = 1;
61     uint32_t eachReadFrameSize_ = 0;
62 private:
63     FastAudioRendererSink();
64     ~FastAudioRendererSink();
65     AudioSinkAttr attr_;
66     bool started_;
67     bool paused_;
68     float leftVolume_;
69     float rightVolume_;
70     int32_t routeHandle_ = -1;
71     std::string adapterNameCase_;
72     struct AudioManager *audioManager_;
73     struct AudioAdapter *audioAdapter_;
74     struct AudioRender *audioRender_;
75     struct AudioPort audioPort_ = {};
76 
77     char *bufferAddresss_ = nullptr;
78     size_t bufferSize_ = 0;
79     uint32_t bufferTotalFrameSize_ = 0;
80 
81     bool isFirstWrite_ = true;
82 
83     uint64_t alreadyReadFrames_ = 0;
84     uint32_t curReadPos_ = 0;
85     uint32_t curWritePos_ = 0;
86     uint32_t writeAheadPeriod_ = 1;
87 
88     int bufferFd_ = -1;
89 
90     int32_t PrepareMmapBuffer();
91     void ReleaseMmapBuffer();
92 
93     void PreparePosition();
94 
95     int32_t CreateRender(const struct AudioPort &renderPort);
96     int32_t InitAudioManager();
97 #ifdef DUMPFILE
98     FILE *pfd;
99 #endif // DUMPFILE
100 };
101 }  // namespace AudioStandard
102 }  // namespace OHOS
103 #endif // FAST_AUDIO_RENDERER_SINK_H
104