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(const AudioTrack & audioTrack)30 bool AudioPlayer::Init(const AudioTrack &audioTrack)
31 {
32 SHARING_LOGD("trace.");
33 playerId_ = GetId();
34 audioCodecId_ = audioTrack.codecId;
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 audioTrack_ = audioTrack;
45 audioDecoder_->Init(audioTrack_);
46 return true;
47 }
48
Start()49 bool AudioPlayer::Start()
50 {
51 SHARING_LOGD("trace.");
52 if (isRunning_) {
53 SHARING_LOGW("audio player is running.");
54 return true;
55 }
56
57 if (CODEC_NONE == audioCodecId_ || nullptr == audioDecoder_ || nullptr == audioDecoderReceiver_ ||
58 nullptr == audioSink_) {
59 SHARING_LOGE("start must be after init, CodecId:%{public}d!", (int32_t)audioCodecId_);
60 return false;
61 }
62
63 if (audioSink_->Prepare(audioTrack_.channels, audioTrack_.sampleRate) != 0 || audioSink_->Start() != 0) {
64 SHARING_LOGE("audio player start failed.");
65 return false;
66 }
67
68 audioDecoder_->AddAudioDestination(audioDecoderReceiver_);
69 audioDecoder_->Start();
70 isRunning_ = true;
71 return true;
72 }
73
SetVolume(float volume)74 void AudioPlayer::SetVolume(float volume)
75 {
76 SHARING_LOGD("trace.");
77 if (audioSink_) {
78 audioSink_->SetVolume(volume);
79 }
80 }
81
Stop()82 void AudioPlayer::Stop()
83 {
84 SHARING_LOGD("trace.");
85 if (isRunning_) {
86 if (audioDecoder_) {
87 audioDecoder_->RemoveAudioDestination(audioDecoderReceiver_);
88 audioDecoder_->Stop();
89 }
90 if (audioSink_) {
91 audioSink_->Stop();
92 }
93 isRunning_ = false;
94 }
95 }
96
Release()97 void AudioPlayer::Release()
98 {
99 SHARING_LOGD("trace.");
100 if (audioSink_ != nullptr) {
101 audioSink_->Release();
102 audioSink_ = nullptr;
103 }
104 if (audioDecoder_ != nullptr) {
105 audioDecoder_->Release();
106 }
107 }
108
ProcessAudioData(DataBuffer::Ptr data)109 void AudioPlayer::ProcessAudioData(DataBuffer::Ptr data)
110 {
111 MEDIA_LOGD("trace.");
112 if (!isRunning_) {
113 return;
114 }
115
116 if (data == nullptr || audioDecoder_ == nullptr || data->Peek() == nullptr || data->Size() <= 0) {
117 return;
118 }
119
120 auto frame = FrameImpl::Create();
121 frame->codecId_ = audioCodecId_;
122 frame->Assign(data->Peek(), data->Size());
123 audioDecoder_->OnFrame(frame);
124 }
125
126 } // namespace Sharing
127 } // namespace OHOS