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 #define HST_LOG_TAG "PluginCoreAuSink"
17
18 #include "plugin/core/audio_sink.h"
19 #include <atomic>
20 #include "foundation/log.h"
21 #include "foundation/osal/thread/scoped_lock.h"
22 #include "plugin/core/plugin_core_utils.h"
23 #include "plugin/interface/audio_sink_plugin.h"
24
25 using namespace OHOS::Media::Plugin;
26
AudioSink(uint32_t pkgVer,uint32_t apiVer,std::shared_ptr<AudioSinkPlugin> plugin)27 AudioSink::AudioSink(uint32_t pkgVer, uint32_t apiVer, std::shared_ptr<AudioSinkPlugin> plugin)
28 : Base(pkgVer, apiVer, plugin), audioSink(std::move(plugin)) {}
29
Pause()30 Status AudioSink::Pause()
31 {
32 MEDIA_LOG_I(PUBLIC_LOG_S " Enter.", __FUNCTION__);
33 OSAL::ScopedLock lock(stateChangeMutex_);
34 if (pluginState_ != State::RUNNING) {
35 MEDIA_LOG_I("plugin " PUBLIC_LOG_S " pause in status " PUBLIC_LOG_S ", ignore pause",
36 plugin_->GetName().c_str(), GetStateString(pluginState_.load()));
37 return Status::OK;
38 }
39 auto ret = audioSink->Pause();
40 LOG_WARN_IF_NOT_OK(plugin_, ret);
41 if (ret == Status::OK) {
42 pluginState_ = State::PAUSED;
43 }
44 MEDIA_LOG_I(PUBLIC_LOG_S " Exit.", __FUNCTION__);
45 return ret;
46 }
47
Resume()48 Status AudioSink::Resume()
49 {
50 MEDIA_LOG_I(PUBLIC_LOG_S " Enter.", __FUNCTION__);
51 OSAL::ScopedLock lock(stateChangeMutex_);
52 if (pluginState_ != State::PAUSED) {
53 MEDIA_LOG_I("plugin " PUBLIC_LOG_S " resume in status " PUBLIC_LOG_S ", ignore pause",
54 plugin_->GetName().c_str(), GetStateString(pluginState_.load()));
55 return Status::OK;
56 }
57 auto ret = audioSink->Resume();
58 LOG_WARN_IF_NOT_OK(plugin_, ret);
59 if (ret == Status::OK) {
60 pluginState_ = State::RUNNING;
61 }
62 MEDIA_LOG_I(PUBLIC_LOG_S " Exit.", __FUNCTION__);
63 return ret;
64 }
65
Flush()66 Status AudioSink::Flush()
67 {
68 return audioSink->Flush();
69 }
70
Drain()71 Status AudioSink::Drain()
72 {
73 return audioSink->Drain();
74 }
75
Write(const std::shared_ptr<Buffer> & input)76 Status AudioSink::Write(const std::shared_ptr<Buffer>& input)
77 {
78 return audioSink->Write(input);
79 }
80
SetVolume(float volume)81 Status AudioSink::SetVolume(float volume)
82 {
83 return audioSink->SetVolume(volume);
84 }
85
GetLatency(uint64_t & hstTime)86 Status AudioSink::GetLatency(uint64_t& hstTime)
87 {
88 return audioSink->GetLatency(hstTime);
89 }