1 /*
2  * Copyright (c) 2021-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 HISTREAMER_SDL_AU_SINK_PLUGIN_H
17 #define HISTREAMER_SDL_AU_SINK_PLUGIN_H
18 
19 #include <atomic>
20 #include "SDL.h"
21 #include "foundation/utils/ring_buffer.h"
22 #include "plugin/common/plugin_audio_tags.h"
23 #include "plugin/convert/ffmpeg_convert.h"
24 #include "plugin/interface/audio_sink_plugin.h"
25 #include "plugin/plugins/ffmpeg_adapter/utils/ffmpeg_utils.h"
26 
27 namespace OHOS {
28 namespace Media {
29 namespace Plugin {
30 namespace Sdl {
31 class SdlAudioSinkPlugin : public std::enable_shared_from_this<SdlAudioSinkPlugin>, public AudioSinkPlugin {
32 public:
33     explicit SdlAudioSinkPlugin(std::string name);
34     ~SdlAudioSinkPlugin() override = default;
35 
36     Status Init() override;
37 
38     Status Deinit() override;
39 
40     Status Prepare() override;
41 
42     Status Reset() override;
43 
44     Status Start() override;
45 
46     Status Stop() override;
47 
48     Status GetParameter(Tag tag, ValueType &value) override;
49 
50     Status SetParameter(Tag tag, const ValueType &value) override;
51 
52     std::shared_ptr<Allocator> GetAllocator() override;
53 
54     Status SetCallback(Callback* cb) override;
55 
56     // audio sink
57 
58     Status GetMute(bool &mute) override;
59 
60     Status SetMute(bool mute) override;
61 
62     Status GetVolume(float &volume) override;
63 
64     Status SetVolume(float volume) override;
65 
66     Status GetSpeed(float &speed) override;
67 
68     Status SetSpeed(float speed) override;
69 
70     Status Pause() override;
71 
72     Status Resume() override;
73 
74     Status GetLatency(uint64_t &nanoSec) override;
75 
76     Status GetFrameSize(size_t &size) override;
77 
78     Status GetFrameCount(uint32_t &count) override;
79 
80     Status Write(const std::shared_ptr<Buffer>& buffer) override;
81 
82     Status Flush() override;
83 
84     Status Drain() override;
85 private:
86     void AudioCallback(void* userdata, uint8_t* stream, int len); // NOLINT: void*
87 
88     bool needResample_ {false};
89     std::vector<uint8_t> mixCache_ {};
90     std::unique_ptr<RingBuffer> rb {};
91     size_t srcFrameSize_ {};
92     SDL_AudioSpec wantedSpec_ {};
93     uint32_t channels_ {2}; // 2: STEREO
94     uint32_t sampleRate_ {0};
95     uint32_t samplesPerFrame_ {0};
96     uint32_t bitsPerSample_ {0};
97     uint64_t channelLayout_ {0};
98     AudioSampleFormat audioFormat_ {AudioSampleFormat::NONE};
99     int volume_;
100     const AVSampleFormat reFfDestFmt_ {AV_SAMPLE_FMT_S16};
101     AVSampleFormat reSrcFfFmt_ {AV_SAMPLE_FMT_NONE};
102     std::shared_ptr<Ffmpeg::Resample> resample_ {nullptr};
103 };
104 } // namespace Sdl
105 } // namespace Plugin
106 } // namespace Media
107 } // namespace OHOS
108 #endif // HISTREAMER_SDL_AU_SINK_PLUGIN_H
109