1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 #include "audio_player.h"
17 #include "common/media_log.h"
18 #include "protocol/frame/aac_frame.h"
19 #include "protocol/frame/frame.h"
20
21 namespace OHOS {
22 namespace Sharing {
23
~AudioPlayer()24 AudioPlayer::~AudioPlayer()
25 {
26 SHARING_LOGD("trace.");
27 Release();
28 }
29
Init(CodecId audioCodecId)30 bool AudioPlayer::Init(CodecId audioCodecId)
31 {
32 SHARING_LOGD("trace.");
33 playerId_ = GetId();
34 audioCodecId_ = audioCodecId;
35 audioSink_ = std::make_shared<AudioSink>(playerId_);
36 audioDecoderReceiver_ = std::make_shared<AudioDecoderReceiver>(audioSink_);
37
38 audioDecoder_ = CodecFactory::CreateAudioDecoder(audioCodecId_);
39 if (audioDecoder_ == nullptr) {
40 SHARING_LOGE("CreateAudioDecoder failed for CodecId:%{public}d!", (int32_t)audioCodecId_);
41 return false;
42 }
43
44 audioDecoder_->Init();
45 return true;
46 }
47
SetAudioFormat(int32_t channel,int32_t sampleRate)48 bool AudioPlayer::SetAudioFormat(int32_t channel, int32_t sampleRate)
49 {
50 SHARING_LOGD("trace.");
51 channel_ = channel;
52 sampleRate_ = sampleRate;
53 return true;
54 }
55
Start()56 bool AudioPlayer::Start()
57 {
58 SHARING_LOGD("trace.");
59 if (isRunning_) {
60 SHARING_LOGW("audio player is running.");
61 return true;
62 }
63
64 if (CODEC_NONE == audioCodecId_ || nullptr == audioDecoder_ || nullptr == audioDecoderReceiver_ ||
65 nullptr == audioSink_) {
66 SHARING_LOGE("start must be after init, CodecId:%{public}d!", (int32_t)audioCodecId_);
67 return false;
68 }
69
70 if (audioSink_->Prepare(channel_, sampleRate_) != 0 || audioSink_->Start() != 0) {
71 SHARING_LOGE("audio player start failed.");
72 return false;
73 }
74
75 audioDecoder_->AddAudioDestination(audioDecoderReceiver_);
76 isRunning_ = true;
77 return true;
78 }
79
SetVolume(float volume)80 void AudioPlayer::SetVolume(float volume)
81 {
82 SHARING_LOGD("trace.");
83 if (audioSink_) {
84 audioSink_->SetVolume(volume);
85 }
86 }
87
Stop()88 void AudioPlayer::Stop()
89 {
90 SHARING_LOGD("trace.");
91 if (isRunning_) {
92 if (audioDecoder_) {
93 audioDecoder_->RemoveAudioDestination(audioDecoderReceiver_);
94 }
95 if (audioSink_) {
96 audioSink_->Stop();
97 }
98 isRunning_ = false;
99 }
100 }
101
Release()102 void AudioPlayer::Release()
103 {
104 SHARING_LOGD("trace.");
105 if (audioSink_ != nullptr) {
106 audioSink_->Release();
107 audioSink_ = nullptr;
108 }
109 }
110
ProcessAudioData(DataBuffer::Ptr data)111 void AudioPlayer::ProcessAudioData(DataBuffer::Ptr data)
112 {
113 MEDIA_LOGD("trace.");
114 if (!isRunning_) {
115 return;
116 }
117
118 if (data == nullptr || audioDecoder_ == nullptr || data->Peek() == nullptr || data->Size() <= 0) {
119 return;
120 }
121
122 auto frame = FrameImpl::Create();
123 frame->codecId_ = audioCodecId_;
124 frame->Assign(data->Peek(), data->Size());
125 audioDecoder_->OnFrame(frame);
126 }
127
128 } // namespace Sharing
129 } // namespace OHOS