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 #include <sys/time.h>
17 #include <utility>
18
19 #include <climits>
20 #include <cmath>
21 #include <cfloat>
22 #include "securec.h"
23 #include "audio_log.h"
24 #include "audio_policy_manager.h"
25 #include "tone_player_private.h"
26 #include "audio_utils.h"
27
28 namespace OHOS {
29 namespace AudioStandard {
30 namespace {
31 constexpr int32_t C20MS = 20;
32 constexpr int32_t C1000MS = 1000;
33 constexpr int32_t CMAXWAIT = 3;
34 constexpr int32_t CDOUBLE = 2;
35 constexpr int32_t AMPLITUDE = 16000;
36 constexpr int32_t BIT8 = 8;
37 }
38
39 #ifdef DUMPFILE
40 const char *g_tonePlayerTestFilePath = "/data/local/tmp/toneplayer_test.pcm";
41 #endif // DUMPFILE
42
Create(const AudioRendererInfo & rendererInfo)43 std::shared_ptr<TonePlayer> TonePlayer::Create(const AudioRendererInfo &rendererInfo)
44 {
45 if (!PermissionUtil::VerifySelfPermission()) {
46 AUDIO_ERR_LOG("Create: No system permission");
47 return nullptr;
48 }
49 return std::make_shared <TonePlayerPrivate>("", rendererInfo);
50 }
51
Create(const std::string cachePath,const AudioRendererInfo & rendererInfo)52 std::shared_ptr<TonePlayer> TonePlayer::Create(const std::string cachePath, const AudioRendererInfo &rendererInfo)
53 {
54 if (!PermissionUtil::VerifySelfPermission()) {
55 AUDIO_ERR_LOG("Create: No system permission");
56 return nullptr;
57 }
58 return std::make_shared <TonePlayerPrivate>(cachePath, rendererInfo);
59 }
60
TonePlayerPrivate(const std::string cachePath,const AudioRendererInfo & rendereInfo)61 TonePlayerPrivate::TonePlayerPrivate(const std::string cachePath, const AudioRendererInfo &rendereInfo)
62 {
63 tonePlayerState_ = TONE_PLAYER_IDLE;
64 rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
65 rendererOptions.streamInfo.samplingRate = SAMPLE_RATE_44100;
66 rendererOptions.streamInfo.format = SAMPLE_S16LE;
67 rendererOptions.streamInfo.channels = MONO;
68
69 // contentType::CONTENT_TYPE_MUSIC;
70 rendererOptions.rendererInfo.contentType = rendereInfo.contentType;
71
72 // streamUsage::STREAM_USAGE_MEDIA;
73 rendererOptions.rendererInfo.streamUsage = rendereInfo.streamUsage;
74 rendererOptions.rendererInfo.rendererFlags = rendereInfo.rendererFlags;
75 supportedTones_ = AudioPolicyManager::GetInstance().GetSupportedTones();
76 volume_ = TRACK_VOLUME;
77 toneInfo_ = NULL;
78 initialToneInfo_ = NULL;
79 toneDataGenLoop_ = nullptr;
80 samplingRate_ = rendererOptions.streamInfo.samplingRate;
81 if (!cachePath.empty()) {
82 AUDIO_INFO_LOG("copy application cache path");
83 cachePath_.assign(cachePath);
84 }
85 AUDIO_DEBUG_LOG("TonePlayerPrivate constructor: volume=%{public}f, size=%{public}zu",
86 volume_, supportedTones_.size());
87 #ifdef DUMPFILE
88 pfd_ = nullptr;
89 #endif // DUMPFILE
90 }
91
92 TonePlayer::~TonePlayer() = default;
93
~TonePlayerPrivate()94 TonePlayerPrivate::~TonePlayerPrivate()
95 {
96 AUDIO_INFO_LOG("TonePlayerPrivate destructor");
97 if (audioRenderer_ != nullptr) {
98 StopTone();
99 mutexLock_.lock();
100 if (audioRenderer_ != nullptr) {
101 audioRenderer_->Clear();
102 } else {
103 AUDIO_ERR_LOG("~TonePlayerPrivate audioRenderer_ is null");
104 }
105 mutexLock_.unlock();
106 }
107 if (toneDataGenLoop_ && toneDataGenLoop_->joinable()) {
108 toneDataGenLoop_->join();
109 }
110 audioRenderer_ = nullptr;
111 }
112
LoadEventStateHandler()113 bool TonePlayerPrivate::LoadEventStateHandler()
114 {
115 AUDIO_INFO_LOG("TonePlayerPrivate::LoadEventStateHandler start");
116 bool result = true;
117 if (audioRenderer_ != nullptr) {
118 result = StopTone();
119 mutexLock_.lock();
120 if (audioRenderer_ != nullptr) {
121 if (audioRenderer_->Clear() != 0) {
122 result = false;
123 }
124 } else {
125 AUDIO_ERR_LOG("LoadEventStateHandler audioRenderer_ is null");
126 }
127 mutexLock_.unlock();
128 tonePlayerState_ = TONE_PLAYER_IDLE;
129 audioRenderer_ = nullptr;
130 }
131 if (tonePlayerState_ == TONE_PLAYER_IDLE) {
132 AUDIO_DEBUG_LOG("Load Init AudioRenderer");
133 if ((audioRenderer_ == nullptr) && (!InitAudioRenderer())) {
134 AUDIO_ERR_LOG("LoadEventStateHandler InitAudioRenderer fail");
135 return false;
136 }
137 tonePlayerState_ = TONE_PLAYER_INIT;
138 }
139 if (InitToneWaveInfo() == false) {
140 AUDIO_ERR_LOG("Wave Initialization Failed");
141 tonePlayerState_ = TONE_PLAYER_IDLE;
142 return false;
143 }
144 tonePlayerState_ = TONE_PLAYER_STARTING;
145 toneDataState_ = TONE_DATA_LOADING;
146 if (toneDataGenLoop_ == nullptr) {
147 toneDataGenLoop_ = std::make_unique<std::thread>(&TonePlayerPrivate::AudioToneDataThreadFunc, this);
148 }
149 return result;
150 }
151
PlayEventStateHandler()152 bool TonePlayerPrivate::PlayEventStateHandler()
153 {
154 bool result = true;
155 status_t retStatus;
156 mutexLock_.unlock();
157 mutexLock_.lock();
158 if (audioRenderer_ != nullptr) {
159 result = audioRenderer_->Start();
160 } else {
161 AUDIO_ERR_LOG("PlayEventStateHandler audioRenderer_ is null");
162 }
163 mutexLock_.unlock();
164 mutexLock_.lock();
165 if (tonePlayerState_ == TONE_PLAYER_STARTING) {
166 mutexLock_.unlock();
167 std::unique_lock<std::mutex> lock(cbkCondLock_);
168 retStatus = waitAudioCbkCond_.wait_for(lock, std::chrono::seconds(CMAXWAIT));
169 AUDIO_DEBUG_LOG("Immediate start got notified, status %{public}d", retStatus);
170 mutexLock_.lock();
171 if (retStatus == std::cv_status::timeout) {
172 AUDIO_ERR_LOG("Immediate start timed out, status %{public}d", retStatus);
173 return false;
174 }
175 }
176 return result;
177 }
178
TonePlayerStateHandler(int16_t event)179 bool TonePlayerPrivate::TonePlayerStateHandler(int16_t event)
180 {
181 AUDIO_INFO_LOG("TonePlayerPrivate HandlePlayerState");
182 switch (event) {
183 case PLAYER_EVENT_LOAD:
184 if (LoadEventStateHandler() == false) {
185 return false;
186 }
187 break;
188 case PLAYER_EVENT_PLAY:
189 if (PlayEventStateHandler() == false) {
190 tonePlayerState_ = TONE_PLAYER_IDLE;
191 return false;
192 }
193 AUDIO_DEBUG_LOG("PLAYER_EVENT tonePlayerState_ %{public}d", tonePlayerState_);
194 break;
195 case PLAYER_EVENT_STOP:
196 StopEventStateHandler();
197 break;
198 }
199 return true;
200 }
201
LoadTone(ToneType toneType)202 bool TonePlayerPrivate::LoadTone(ToneType toneType)
203 {
204 AUDIO_INFO_LOG("LoadTone type: %{public}d, tonePlayerState_ %{public}d", toneType, tonePlayerState_);
205 bool result = false;
206 if (!PermissionUtil::VerifySelfPermission()) {
207 AUDIO_ERR_LOG("LoadTone: No system permission");
208 return false;
209 }
210 if (toneType >= NUM_TONES) {
211 return result;
212 }
213 mutexLock_.lock();
214 if (tonePlayerState_ != TONE_PLAYER_IDLE && tonePlayerState_ != TONE_PLAYER_INIT) {
215 mutexLock_.unlock();
216 return result;
217 }
218
219 if (std::find(supportedTones_.begin(), supportedTones_.end(), (int32_t)toneType) == supportedTones_.end()) {
220 mutexLock_.unlock();
221 return result;
222 }
223
224 // Get descriptor for requested tone
225 initialToneInfo_ = AudioPolicyManager::GetInstance().GetToneConfig(toneType);
226 if (initialToneInfo_->segmentCnt == 0) {
227 AUDIO_ERR_LOG("LoadTone failed, calling GetToneConfig returned invalid");
228 mutexLock_.unlock();
229 return result;
230 }
231 result = TonePlayerStateHandler(PLAYER_EVENT_LOAD);
232 mutexLock_.unlock();
233 #ifdef DUMPFILE
234 pfd_ = fopen(g_tonePlayerTestFilePath, "wb+");
235 if (pfd_ == nullptr) {
236 AUDIO_ERR_LOG("Error opening pcm test file!");
237 }
238 #endif // DUMPFILE
239 return result;
240 }
241
StartTone()242 bool TonePlayerPrivate::StartTone()
243 {
244 AUDIO_INFO_LOG("STARTTONE tonePlayerState_ %{public}d", tonePlayerState_);
245 bool retVal = false;
246 mutexLock_.lock();
247 if (tonePlayerState_ == TONE_PLAYER_IDLE || tonePlayerState_ == TONE_PLAYER_INIT) {
248 if (LoadEventStateHandler() == false) {
249 mutexLock_.unlock();
250 return false;
251 }
252 }
253 retVal = TonePlayerStateHandler(PLAYER_EVENT_PLAY);
254 mutexLock_.unlock();
255 return retVal;
256 }
257
Release()258 bool TonePlayerPrivate::Release()
259 {
260 bool retVal = true;
261 if (audioRenderer_ != nullptr) {
262 retVal = StopTone();
263 mutexLock_.lock();
264 if (audioRenderer_ != nullptr) {
265 if (audioRenderer_->Clear() != 0) {
266 retVal = false;
267 }
268 } else {
269 AUDIO_ERR_LOG("Release audioRenderer_ is null");
270 }
271 mutexLock_.unlock();
272 tonePlayerState_ = TONE_PLAYER_IDLE;
273 }
274 audioRenderer_ = nullptr;
275 return retVal;
276 }
277
StopTone()278 bool TonePlayerPrivate::StopTone()
279 {
280 AUDIO_INFO_LOG("StopTone tonePlayerState_ %{public}d", tonePlayerState_);
281 bool retVal = true;
282 mutexLock_.lock();
283 if (tonePlayerState_ == TONE_PLAYER_IDLE || tonePlayerState_ == TONE_PLAYER_INIT) {
284 AUDIO_INFO_LOG("-stop tone End");
285 mutexLock_.unlock();
286 return retVal;
287 }
288
289 retVal = TonePlayerStateHandler(PLAYER_EVENT_STOP);
290 mutexLock_.unlock();
291 mutexLock_.lock();
292 if (audioRenderer_ != nullptr) {
293 retVal = audioRenderer_->Stop();
294 } else {
295 AUDIO_ERR_LOG("StopTone audioRenderer_ is null");
296 }
297 mutexLock_.unlock();
298 #ifdef DUMPFILE
299 if (pfd_) {
300 fclose(pfd_);
301 pfd_ = nullptr;
302 }
303 #endif // DUMPFILE
304 return retVal;
305 }
306
StopEventStateHandler()307 void TonePlayerPrivate::StopEventStateHandler()
308 {
309 if (tonePlayerState_ == TONE_PLAYER_PLAYING || tonePlayerState_ == TONE_PLAYER_STARTING) {
310 tonePlayerState_ = TONE_PLAYER_STOPPING;
311 }
312 AUDIO_DEBUG_LOG("WAITING wait_for cond");
313 mutexLock_.unlock();
314 std::unique_lock<std::mutex> lock(cbkCondLock_);
315 status_t retStatus = waitAudioCbkCond_.wait_for(lock, std::chrono::seconds(CMAXWAIT));
316 mutexLock_.lock();
317 if (retStatus != std::cv_status::timeout) {
318 AUDIO_ERR_LOG("StopTone waiting wait_for cond got notified");
319 tonePlayerState_ = TONE_PLAYER_INIT;
320 } else {
321 AUDIO_ERR_LOG("Stop timed out");
322 tonePlayerState_ = TONE_PLAYER_IDLE;
323 }
324 }
325
InitAudioRenderer()326 bool TonePlayerPrivate::InitAudioRenderer()
327 {
328 processSize_ = (rendererOptions.streamInfo.samplingRate * C20MS) / C1000MS;
329 if (!cachePath_.empty()) {
330 audioRenderer_ = AudioRenderer::Create(cachePath_, rendererOptions);
331 } else {
332 audioRenderer_ = AudioRenderer::Create(rendererOptions);
333 }
334 if (audioRenderer_ == nullptr) {
335 AUDIO_ERR_LOG("initAudioRenderer: Renderer create failed");
336 return false;
337 }
338
339 AUDIO_DEBUG_LOG("initAudioRenderer: Playback renderer created");
340 if (audioRenderer_->SetRenderMode(RENDER_MODE_CALLBACK)) {
341 AUDIO_ERR_LOG("initAudioRenderer: SetRenderMode failed");
342 return false;
343 }
344 AUDIO_DEBUG_LOG("initAudioRenderer: SetRenderMode Sucessful");
345 if (audioRenderer_->SetRendererWriteCallback(shared_from_this())) {
346 AUDIO_ERR_LOG("initAudioRenderer: SetRendererWriteCallback failed");
347 return false;
348 }
349 AUDIO_DEBUG_LOG("initAudioRenderer: SetRendererWriteCallback Sucessful");
350 if (audioRenderer_->SetRendererCallback(shared_from_this())) {
351 AUDIO_ERR_LOG("initAudioRenderer: SetRendererCallbackfailed");
352 return false;
353 }
354
355 AUDIO_DEBUG_LOG("initAudioRenderer: SetRendererCallback Sucessful");
356 audioRenderer_->SetVolume(volume_);
357 AUDIO_INFO_LOG("initAudioRenderer: SetVolume Sucessful");
358 return true;
359 }
360
OnInterrupt(const InterruptEvent & interruptEvent)361 void TonePlayerPrivate::OnInterrupt(const InterruptEvent &interruptEvent)
362 {
363 AUDIO_INFO_LOG("OnInterrupt eventType: %{public}d", interruptEvent.eventType);
364 }
365
OnWriteData(size_t length)366 void TonePlayerPrivate::OnWriteData(size_t length)
367 {
368 AUDIO_DEBUG_LOG("OnWriteData Cbk: %{public}zu", length);
369 AudioToneRendererCallback();
370 }
371
OnStateChange(const RendererState state,const StateChangeCmdType cmdType)372 void TonePlayerPrivate::OnStateChange(const RendererState state,
373 const StateChangeCmdType __attribute__((unused)) cmdType)
374 {
375 AUDIO_INFO_LOG("OnStateChange Cbk: %{public}d not calling", state);
376 if (state == RENDERER_RUNNING || state == RENDERER_STOPPED) {
377 AUDIO_INFO_LOG("OnStateChange state: %{public}d", state);
378 }
379 }
380
CheckToneStopped()381 bool TonePlayerPrivate::CheckToneStopped()
382 {
383 AUDIO_DEBUG_LOG("CheckToneStopped state: %{public}d tot: %{public}d max: %{public}d not calling",
384 tonePlayerState_, totalSample_, maxSample_);
385 std::shared_ptr<ToneInfo> toneDesc = toneInfo_;
386 if (tonePlayerState_ == TONE_PLAYER_STOPPED) {
387 return true;
388 }
389 if (toneDesc->segments[currSegment_].duration == 0 ||
390 totalSample_ > maxSample_ || tonePlayerState_ == TONE_PLAYER_STOPPING) {
391 if (tonePlayerState_ == TONE_PLAYER_PLAYING) {
392 tonePlayerState_ = TONE_PLAYER_STOPPING;
393 AUDIO_DEBUG_LOG("Audicallback move playing to stoping");
394 }
395 return true;
396 }
397 return false;
398 }
399
CheckToneStarted(uint32_t reqSample,int8_t * audioBuffer)400 bool TonePlayerPrivate::CheckToneStarted(uint32_t reqSample, int8_t *audioBuffer)
401 {
402 std::shared_ptr<ToneInfo> toneDesc = toneInfo_;
403 if (tonePlayerState_ == TONE_PLAYER_STARTING) {
404 tonePlayerState_ = TONE_PLAYER_PLAYING;
405 AUDIO_DEBUG_LOG("Audicallback move to playing");
406 if (toneDesc->segments[currSegment_].duration != 0) {
407 sampleCount_ = 0;
408 GetSamples(toneDesc->segments[currSegment_].waveFreq, audioBuffer, reqSample);
409 }
410 AUDIO_DEBUG_LOG("CheckToneStarted GenerateStartWave to currseg: %{public}d ", currSegment_);
411 return true;
412 }
413 return false;
414 }
415
GetCurrentSegmentUpdated(std::shared_ptr<ToneInfo> toneDesc)416 void TonePlayerPrivate::GetCurrentSegmentUpdated(std::shared_ptr<ToneInfo> toneDesc)
417 {
418 if (toneDesc->segments[currSegment_].loopCnt) {
419 if (loopCounter_ < toneDesc->segments[currSegment_].loopCnt) {
420 currSegment_ = toneDesc->segments[currSegment_].loopIndx;
421 ++loopCounter_;
422 } else {
423 // completed loop. go to next segment
424 loopCounter_ = 0;
425 currSegment_++;
426 }
427 } else {
428 // no looping required , go to next segment
429 currSegment_++;
430 }
431 AUDIO_INFO_LOG("GetCurrentSegmentUpdated loopCounter_: %{public}d, currSegment_: %{public}d",
432 loopCounter_, currSegment_);
433 }
434
CheckToneContinuity()435 bool TonePlayerPrivate::CheckToneContinuity()
436 {
437 AUDIO_INFO_LOG("CheckToneContinuity Entry loopCounter_: %{public}d, currSegment_: %{public}d",
438 loopCounter_, currSegment_);
439 bool retVal = false;
440 std::shared_ptr<ToneInfo> toneDesc = toneInfo_;
441 GetCurrentSegmentUpdated(toneDesc);
442
443 // Handle loop if last segment reached
444 if (toneDesc->segments[currSegment_].duration == 0) {
445 AUDIO_DEBUG_LOG("Last Seg: %{public}d", currSegment_);
446 if (currCount_ < toneDesc->repeatCnt) {
447 currSegment_ = toneDesc->repeatSegment;
448 ++currCount_;
449 retVal = true;
450 } else {
451 retVal = false;
452 }
453 } else {
454 retVal = true;
455 }
456 AUDIO_DEBUG_LOG("CheckToneContinuity End loopCounter_: %{public}d, currSegment_: %{public}d currCount_: %{public}d",
457 loopCounter_, currSegment_, currCount_);
458 return retVal;
459 }
460
ContinueToneplay(uint32_t reqSample,int8_t * audioBuffer)461 bool TonePlayerPrivate::ContinueToneplay(uint32_t reqSample, int8_t *audioBuffer)
462 {
463 std::shared_ptr<ToneInfo> toneDesc = toneInfo_;
464 if (tonePlayerState_ != TONE_PLAYER_PLAYING) {
465 return false;
466 }
467 if (totalSample_ <= nextSegSample_) {
468 if (toneDesc->segments[currSegment_].duration != 0) {
469 GetSamples(toneDesc->segments[currSegment_].waveFreq, audioBuffer, reqSample);
470 }
471 return true;
472 }
473 AUDIO_DEBUG_LOG(" Current Seg Last minute Playing Tone");
474
475 if (CheckToneContinuity()) {
476 if (toneDesc->segments[currSegment_].duration != 0) {
477 sampleCount_ = 0;
478 GetSamples(toneDesc->segments[currSegment_].waveFreq, audioBuffer, reqSample);
479 }
480 }
481 nextSegSample_
482 += (toneDesc->segments[currSegment_].duration * samplingRate_) / C1000MS;
483 AUDIO_INFO_LOG("ContinueToneplay nextSegSample_: %{public}d", nextSegSample_);
484 return true;
485 }
486
AudioToneRendererCallback()487 void TonePlayerPrivate::AudioToneRendererCallback()
488 {
489 std::unique_lock<std::mutex> lock(dataCondLock_);
490 if (toneDataState_ == TONE_DATA_LOADED) {
491 AUDIO_DEBUG_LOG("Notifing Data AudioToneRendererCallback");
492 waitToneDataCond_.notify_all();
493 }
494 if (toneDataState_ == TONE_DATA_LOADING) {
495 toneDataState_ = TONE_DATA_REQUESTED;
496 waitToneDataCond_.wait_for(lock, std::chrono::seconds(CMAXWAIT));
497 }
498 AUDIO_INFO_LOG("AudioToneRendererCallback Exited");
499 }
500
AudioToneSequenceGen(BufferDesc & bufDesc)501 bool TonePlayerPrivate::AudioToneSequenceGen(BufferDesc &bufDesc)
502 {
503 int8_t *audioBuffer = (int8_t *)bufDesc.buffer;
504 uint32_t totalBufAvailable = bufDesc.bufLength / sizeof(int16_t);
505 bool retVal = true;
506 while (totalBufAvailable) {
507 uint32_t reqSamples = totalBufAvailable < processSize_ * CDOUBLE ? totalBufAvailable : processSize_;
508 bool lSignal = false;
509 AUDIO_DEBUG_LOG("AudioToneDataThreadFunc, lReqSmp: %{public}d totalBufAvailable: %{public}d",
510 reqSamples, totalBufAvailable);
511 mutexLock_.lock();
512
513 // Update pcm frame count and end time (current time at the end of this process)
514 totalSample_ += reqSamples;
515 if (CheckToneStopped()) {
516 if (tonePlayerState_ == TONE_PLAYER_STOPPING) {
517 tonePlayerState_ = TONE_PLAYER_STOPPED;
518 mutexLock_.unlock();
519 break;
520 }
521 if (tonePlayerState_ == TONE_PLAYER_STOPPED) {
522 tonePlayerState_ = TONE_PLAYER_INIT;
523 totalBufAvailable = 0;
524 bufDesc.dataLength = 0;
525 AUDIO_DEBUG_LOG("Notifing all the STOP");
526 mutexLock_.unlock();
527 waitAudioCbkCond_.notify_all();
528 mutexLock_.lock();
529 if (audioRenderer_ != nullptr) {
530 retVal = audioRenderer_->Stop();
531 } else {
532 AUDIO_ERR_LOG("AudioToneSequenceGen audioRenderer_ is null");
533 }
534 mutexLock_.unlock();
535 return false;
536 }
537 } else if (CheckToneStarted(reqSamples, audioBuffer)) {
538 bufDesc.dataLength += reqSamples * sizeof(int16_t);
539 lSignal = true;
540 } else {
541 if (ContinueToneplay(reqSamples, audioBuffer)) {
542 bufDesc.dataLength += reqSamples * sizeof(int16_t);
543 }
544 }
545 totalBufAvailable -= reqSamples;
546 audioBuffer += reqSamples * sizeof(int16_t);
547 mutexLock_.unlock();
548 if (lSignal) {
549 AUDIO_INFO_LOG("Notifing all the start");
550 waitAudioCbkCond_.notify_all();
551 }
552 }
553 return retVal;
554 }
555
AudioToneDataThreadFunc()556 void TonePlayerPrivate::AudioToneDataThreadFunc()
557 {
558 while (true) {
559 std::unique_lock<std::mutex> lock(dataCondLock_);
560 if (toneDataState_ == TONE_DATA_LOADING) {
561 toneDataState_ = TONE_DATA_LOADED;
562 waitToneDataCond_.wait_for(lock, std::chrono::seconds(CMAXWAIT));
563 }
564
565 BufferDesc bufDesc = {};
566 mutexLock_.lock();
567 if (audioRenderer_ != nullptr) {
568 audioRenderer_->GetBufferDesc(bufDesc);
569 } else {
570 AUDIO_ERR_LOG("AudioToneDataThreadFunc audioRenderer_ is null");
571 }
572 mutexLock_.unlock();
573 std::shared_ptr<ToneInfo> lpToneDesc = toneInfo_;
574 bufDesc.dataLength = 0;
575 AUDIO_DEBUG_LOG("AudioToneDataThreadFunc, tonePlayerState_: %{public}d", tonePlayerState_);
576 if (bufDesc.bufLength == 0) {
577 AUDIO_DEBUG_LOG("notify bufDesc bufLength is 0");
578 waitAudioCbkCond_.notify_all();
579 return;
580 }
581
582 // Clear output buffer: WaveGenerator accumulates into audioBuffer buffer
583 memset_s(bufDesc.buffer, bufDesc.bufLength, 0, bufDesc.bufLength);
584 if (AudioToneSequenceGen(bufDesc) == false) {
585 return;
586 }
587 AUDIO_INFO_LOG("Exiting the AudioToneDataThreadFunc: %{public}zu,tonePlayerState_: %{public}d",
588 bufDesc.dataLength, tonePlayerState_);
589 if (bufDesc.dataLength) {
590 #ifdef DUMPFILE
591 size_t writeResult = fwrite((void*)bufDesc.buffer, 1, bufDesc.dataLength, pfd_);
592 if (writeResult != bufDesc.dataLength) {
593 AUDIO_ERR_LOG("Failed to write the file.");
594 }
595 #endif // DUMPFILE
596 mutexLock_.lock();
597 if (audioRenderer_ != nullptr) {
598 audioRenderer_->Enqueue(bufDesc);
599 } else {
600 AUDIO_ERR_LOG("AudioToneDataThreadFunc Enqueue audioRenderer_ is null");
601 }
602 mutexLock_.unlock();
603 if (toneDataState_ == TONE_DATA_REQUESTED) {
604 AUDIO_INFO_LOG("Notifing Data AudioToneDataThreadFunc");
605 waitToneDataCond_.notify_all();
606 }
607 toneDataState_ = TONE_DATA_LOADING;
608 mutexLock_.lock();
609 if (tonePlayerState_ == TONE_PLAYER_STOPPED) {
610 AUDIO_INFO_LOG("Notifing tone player STOP");
611 tonePlayerState_ = TONE_PLAYER_INIT;
612 mutexLock_.unlock();
613 waitAudioCbkCond_.notify_all();
614 return;
615 }
616 mutexLock_.unlock();
617 }
618 }
619 }
620
InitToneWaveInfo()621 bool TonePlayerPrivate::InitToneWaveInfo()
622 {
623 AUDIO_INFO_LOG("InitToneWaveInfo, Entered");
624 if (initialToneInfo_ == NULL) {
625 return false;
626 }
627 toneInfo_ = initialToneInfo_;
628 maxSample_ = TONEINFO_INF;
629
630 // Initialize tone sequencer
631 totalSample_ = 0;
632 currSegment_ = 0;
633 currCount_ = 0;
634 loopCounter_ = 0;
635 if (toneInfo_->segments[0].duration == TONEINFO_INF) {
636 nextSegSample_ = TONEINFO_INF;
637 } else {
638 nextSegSample_ = (toneInfo_->segments[0].duration * samplingRate_) / C1000MS;
639 }
640 AUDIO_DEBUG_LOG("Prepare wave, nextSegSample_: %{public}d", nextSegSample_);
641 return true;
642 }
643
GetSamples(uint16_t * freqs,int8_t * buffer,uint32_t reqSamples)644 int32_t TonePlayerPrivate::GetSamples(uint16_t *freqs, int8_t *buffer, uint32_t reqSamples)
645 {
646 uint32_t index;
647 uint8_t *data;
648 uint16_t freqVal;
649 float pi = 3.1428f;
650 for (uint32_t i = 0; i <= TONEINFO_MAX_WAVES; i++) {
651 if (freqs[i] == 0) {
652 break;
653 }
654 freqVal = freqs[i];
655 AUDIO_DEBUG_LOG("GetSamples Freq: %{public}d sampleCount_: %{public}d", freqVal, sampleCount_);
656 index = sampleCount_;
657 data = (uint8_t*)buffer;
658 double factor = freqVal * 2 * pi / samplingRate_; // 2 is a parameter in the sine wave formula
659 for (uint32_t idx = 0; idx < reqSamples; idx++) {
660 int16_t sample = AMPLITUDE * sin(factor * index);
661 uint32_t result;
662 if (i == 0) {
663 result = (sample & 0xFF);
664 *data = result & 0xFF;
665 data++;
666 *data = ((sample & 0xFF00) >> BIT8);
667 data++;
668 } else {
669 result = *data + (sample & 0xFF);
670 *data = result & 0xFF;
671 data++;
672 *data += (result >> BIT8) + ((sample & 0xFF00) >> BIT8);
673 data++;
674 }
675 index++;
676 }
677 }
678 sampleCount_ += reqSamples;
679 return 0;
680 }
681 } // end namespace AudioStandard
682 } // end OHOS
683