• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "audio_errors.h"
17 #include "audio_policy_manager.h"
18 #include "audio_renderer_private.h"
19 #include "audio_stream.h"
20 #include "media_log.h"
21 
22 #include "audio_renderer.h"
23 
24 namespace OHOS {
25 namespace AudioStandard {
26 AudioRenderer::~AudioRenderer() = default;
~AudioRendererPrivate()27 AudioRendererPrivate::~AudioRendererPrivate()
28 {
29     RendererState state = GetStatus();
30     if (state != RENDERER_RELEASED && state != RENDERER_NEW) {
31         Release();
32     }
33 }
34 
Create(AudioStreamType audioStreamType)35 std::unique_ptr<AudioRenderer> AudioRenderer::Create(AudioStreamType audioStreamType)
36 {
37     return std::make_unique<AudioRendererPrivate>(audioStreamType);
38 }
39 
Create(const AudioRendererOptions & rendererOptions)40 std::unique_ptr<AudioRenderer> AudioRenderer::Create(const AudioRendererOptions &rendererOptions)
41 {
42     return Create("", rendererOptions);
43 }
44 
Create(const std::string cachePath,const AudioRendererOptions & rendererOptions)45 std::unique_ptr<AudioRenderer> AudioRenderer::Create(const std::string cachePath,
46     const AudioRendererOptions &rendererOptions)
47 {
48     ContentType contentType = rendererOptions.rendererInfo.contentType;
49     CHECK_AND_RETURN_RET_LOG(contentType >= CONTENT_TYPE_UNKNOWN && contentType <= CONTENT_TYPE_RINGTONE, nullptr,
50                              "Invalid content type");
51 
52     StreamUsage streamUsage = rendererOptions.rendererInfo.streamUsage;
53     CHECK_AND_RETURN_RET_LOG(streamUsage >= STREAM_USAGE_UNKNOWN && streamUsage <= STREAM_USAGE_NOTIFICATION_RINGTONE,
54                              nullptr, "Invalid stream usage");
55 
56     AudioStreamType audioStreamType = AudioStream::GetStreamType(contentType, streamUsage);
57     auto audioRenderer = std::make_unique<AudioRendererPrivate>(audioStreamType);
58     CHECK_AND_RETURN_RET_LOG(audioRenderer != nullptr, nullptr, "Failed to create renderer object");
59     if (!cachePath.empty()) {
60         MEDIA_INFO_LOG("Set application cache path");
61         audioRenderer->SetApplicationCachePath(cachePath);
62     }
63 
64     audioRenderer->rendererInfo_.contentType = contentType;
65     audioRenderer->rendererInfo_.streamUsage = streamUsage;
66     audioRenderer->rendererInfo_.rendererFlags = rendererOptions.rendererInfo.rendererFlags;
67 
68     AudioRendererParams params;
69     params.sampleFormat = rendererOptions.streamInfo.format;
70     params.sampleRate = rendererOptions.streamInfo.samplingRate;
71     params.channelCount = rendererOptions.streamInfo.channels;
72     params.encodingType = rendererOptions.streamInfo.encoding;
73 
74     if (audioRenderer->SetParams(params) != SUCCESS) {
75         MEDIA_ERR_LOG("SetParams failed in renderer");
76         audioRenderer = nullptr;
77         return nullptr;
78     }
79 
80     return audioRenderer;
81 }
82 
AudioRendererPrivate(AudioStreamType audioStreamType)83 AudioRendererPrivate::AudioRendererPrivate(AudioStreamType audioStreamType)
84 {
85     audioStream_ = std::make_shared<AudioStream>(audioStreamType, AUDIO_MODE_PLAYBACK);
86     audioInterrupt_.streamType = audioStreamType;
87 }
88 
GetFrameCount(uint32_t & frameCount) const89 int32_t AudioRendererPrivate::GetFrameCount(uint32_t &frameCount) const
90 {
91     return audioStream_->GetFrameCount(frameCount);
92 }
93 
GetLatency(uint64_t & latency) const94 int32_t AudioRendererPrivate::GetLatency(uint64_t &latency) const
95 {
96     return audioStream_->GetLatency(latency);
97 }
98 
SetParams(const AudioRendererParams params)99 int32_t AudioRendererPrivate::SetParams(const AudioRendererParams params)
100 {
101     AudioStreamParams audioStreamParams;
102     audioStreamParams.format = params.sampleFormat;
103     audioStreamParams.samplingRate = params.sampleRate;
104     audioStreamParams.channels = params.channelCount;
105     audioStreamParams.encoding = params.encodingType;
106 
107     int32_t ret = audioStream_->SetAudioStreamInfo(audioStreamParams);
108 
109     MEDIA_INFO_LOG("AudioRendererPrivate::SetParams SetAudioStreamInfo Success");
110     if (ret) {
111         MEDIA_ERR_LOG("AudioRendererPrivate::SetParams SetAudioStreamInfo Failed");
112         return ret;
113     }
114 
115     if (audioStream_->GetAudioSessionID(sessionID_) != 0) {
116         MEDIA_ERR_LOG("AudioRendererPrivate::GetAudioSessionID Failed");
117         return ERR_INVALID_INDEX;
118     }
119     audioInterrupt_.sessionID = sessionID_;
120 
121     if (audioInterruptCallback_ == nullptr) {
122         audioInterruptCallback_ = std::make_shared<AudioInterruptCallbackImpl>(audioStream_, audioInterrupt_);
123         if (audioInterruptCallback_ == nullptr) {
124             MEDIA_ERR_LOG("AudioRendererPrivate::Failed to allocate memory for audioInterruptCallback_");
125             return ERROR;
126         }
127     }
128 
129     return AudioPolicyManager::GetInstance().SetAudioInterruptCallback(sessionID_, audioInterruptCallback_);
130 }
131 
GetParams(AudioRendererParams & params) const132 int32_t AudioRendererPrivate::GetParams(AudioRendererParams &params) const
133 {
134     AudioStreamParams audioStreamParams;
135     int32_t result = audioStream_->GetAudioStreamInfo(audioStreamParams);
136     if (!result) {
137         params.sampleFormat = static_cast<AudioSampleFormat>(audioStreamParams.format);
138         params.sampleRate = static_cast<AudioSamplingRate>(audioStreamParams.samplingRate);
139         params.channelCount = static_cast<AudioChannel>(audioStreamParams.channels);
140         params.encodingType = static_cast<AudioEncodingType>(audioStreamParams.encoding);
141     }
142 
143     return result;
144 }
145 
GetRendererInfo(AudioRendererInfo & rendererInfo) const146 int32_t AudioRendererPrivate::GetRendererInfo(AudioRendererInfo &rendererInfo) const
147 {
148     rendererInfo = rendererInfo_;
149 
150     return SUCCESS;
151 }
152 
GetStreamInfo(AudioStreamInfo & streamInfo) const153 int32_t AudioRendererPrivate::GetStreamInfo(AudioStreamInfo &streamInfo) const
154 {
155     AudioStreamParams audioStreamParams;
156     int32_t result = audioStream_->GetAudioStreamInfo(audioStreamParams);
157     if (!result) {
158         streamInfo.format = static_cast<AudioSampleFormat>(audioStreamParams.format);
159         streamInfo.samplingRate = static_cast<AudioSamplingRate>(audioStreamParams.samplingRate);
160         streamInfo.channels = static_cast<AudioChannel>(audioStreamParams.channels);
161         streamInfo.encoding = static_cast<AudioEncodingType>(audioStreamParams.encoding);
162     }
163 
164     return result;
165 }
166 
SetRendererCallback(const std::shared_ptr<AudioRendererCallback> & callback)167 int32_t AudioRendererPrivate::SetRendererCallback(const std::shared_ptr<AudioRendererCallback> &callback)
168 {
169     // If the client is using the deprecated SetParams API. SetRendererCallback must be invoked, after SetParams.
170     // In general, callbacks can only be set after the renderer state is  PREPARED.
171     RendererState state = GetStatus();
172     if (state == RENDERER_NEW || state == RENDERER_RELEASED) {
173         MEDIA_DEBUG_LOG("AudioRendererPrivate::SetRendererCallback incorrect state:%{public}d to register cb", state);
174         return ERR_ILLEGAL_STATE;
175     }
176     if (callback == nullptr) {
177         MEDIA_ERR_LOG("AudioRendererPrivate::SetRendererCallback callback param is null");
178         return ERR_INVALID_PARAM;
179     }
180 
181     // Save reference for interrupt callback
182     if (audioInterruptCallback_ == nullptr) {
183         MEDIA_ERR_LOG("AudioRendererPrivate::SetRendererCallback audioInterruptCallback_ == nullptr");
184         return ERROR;
185     }
186     std::shared_ptr<AudioInterruptCallbackImpl> cbInterrupt =
187         std::static_pointer_cast<AudioInterruptCallbackImpl>(audioInterruptCallback_);
188     cbInterrupt->SaveCallback(callback);
189 
190     // Save and Set reference for stream callback. Order is important here.
191     if (audioStreamCallback_ == nullptr) {
192         audioStreamCallback_ = std::make_shared<AudioStreamCallbackRenderer>();
193         if (audioStreamCallback_ == nullptr) {
194             MEDIA_ERR_LOG("AudioRendererPrivate::Failed to allocate memory for audioStreamCallback_");
195             return ERROR;
196         }
197     }
198     std::shared_ptr<AudioStreamCallbackRenderer> cbStream =
199         std::static_pointer_cast<AudioStreamCallbackRenderer>(audioStreamCallback_);
200     cbStream->SaveCallback(callback);
201     (void)audioStream_->SetStreamCallback(audioStreamCallback_);
202 
203     return SUCCESS;
204 }
205 
SetRendererPositionCallback(int64_t markPosition,const std::shared_ptr<RendererPositionCallback> & callback)206 int32_t AudioRendererPrivate::SetRendererPositionCallback(int64_t markPosition,
207     const std::shared_ptr<RendererPositionCallback> &callback)
208 {
209     if ((callback == nullptr) || (markPosition <= 0)) {
210         MEDIA_ERR_LOG("AudioRendererPrivate::SetRendererPositionCallback input param is invalid");
211         return ERR_INVALID_PARAM;
212     }
213 
214     audioStream_->SetRendererPositionCallback(markPosition, callback);
215 
216     return SUCCESS;
217 }
218 
UnsetRendererPositionCallback()219 void AudioRendererPrivate::UnsetRendererPositionCallback()
220 {
221     audioStream_->UnsetRendererPositionCallback();
222 }
223 
SetRendererPeriodPositionCallback(int64_t frameNumber,const std::shared_ptr<RendererPeriodPositionCallback> & callback)224 int32_t AudioRendererPrivate::SetRendererPeriodPositionCallback(int64_t frameNumber,
225     const std::shared_ptr<RendererPeriodPositionCallback> &callback)
226 {
227     if ((callback == nullptr) || (frameNumber <= 0)) {
228         MEDIA_ERR_LOG("AudioRendererPrivate::SetRendererPeriodPositionCallback input param is invalid");
229         return ERR_INVALID_PARAM;
230     }
231 
232     audioStream_->SetRendererPeriodPositionCallback(frameNumber, callback);
233 
234     return SUCCESS;
235 }
236 
UnsetRendererPeriodPositionCallback()237 void AudioRendererPrivate::UnsetRendererPeriodPositionCallback()
238 {
239     audioStream_->UnsetRendererPeriodPositionCallback();
240 }
241 
Start()242 bool AudioRendererPrivate::Start()
243 {
244     RendererState state = GetStatus();
245     if ((state != RENDERER_PREPARED) && (state != RENDERER_STOPPED) && (state != RENDERER_PAUSED)) {
246         MEDIA_ERR_LOG("AudioRendererPrivate::Start() Illegal state:%{public}u, Start failed", state);
247         return false;
248     }
249 
250     if (audioInterrupt_.streamType == STREAM_DEFAULT || audioInterrupt_.sessionID == INVALID_SESSION_ID) {
251         return false;
252     }
253 
254     int32_t ret = AudioPolicyManager::GetInstance().ActivateAudioInterrupt(audioInterrupt_);
255     if (ret != 0) {
256         MEDIA_ERR_LOG("AudioRendererPrivate::ActivateAudioInterrupt Failed");
257         return false;
258     }
259 
260     return audioStream_->StartAudioStream();
261 }
262 
Write(uint8_t * buffer,size_t bufferSize)263 int32_t AudioRendererPrivate::Write(uint8_t *buffer, size_t bufferSize)
264 {
265     return audioStream_->Write(buffer, bufferSize);
266 }
267 
GetStatus() const268 RendererState AudioRendererPrivate::GetStatus() const
269 {
270     return static_cast<RendererState>(audioStream_->GetState());
271 }
272 
GetAudioTime(Timestamp & timestamp,Timestamp::Timestampbase base) const273 bool AudioRendererPrivate::GetAudioTime(Timestamp &timestamp, Timestamp::Timestampbase base) const
274 {
275     return audioStream_->GetAudioTime(timestamp, base);
276 }
277 
Drain() const278 bool AudioRendererPrivate::Drain() const
279 {
280     return audioStream_->DrainAudioStream();
281 }
282 
Flush() const283 bool AudioRendererPrivate::Flush() const
284 {
285     return audioStream_->FlushAudioStream();
286 }
287 
Pause() const288 bool AudioRendererPrivate::Pause() const
289 {
290     bool result = audioStream_->PauseAudioStream();
291 
292     // When user is intentionally pausing , Deactivate to remove from active/pending owners list
293     int32_t ret = AudioPolicyManager::GetInstance().DeactivateAudioInterrupt(audioInterrupt_);
294     if (ret != 0) {
295         MEDIA_ERR_LOG("AudioRenderer: DeactivateAudioInterrupt Failed");
296     }
297 
298     return result;
299 }
300 
Stop() const301 bool AudioRendererPrivate::Stop() const
302 {
303     bool result = audioStream_->StopAudioStream();
304 
305     int32_t ret = AudioPolicyManager::GetInstance().DeactivateAudioInterrupt(audioInterrupt_);
306     if (ret != 0) {
307         MEDIA_ERR_LOG("AudioRenderer: DeactivateAudioInterrupt Failed");
308     }
309 
310     return result;
311 }
312 
Release() const313 bool AudioRendererPrivate::Release() const
314 {
315     // If Stop call was skipped, Release to take care of Deactivation
316     (void)AudioPolicyManager::GetInstance().DeactivateAudioInterrupt(audioInterrupt_);
317 
318     // Unregister the callaback in policy server
319     (void)AudioPolicyManager::GetInstance().UnsetAudioInterruptCallback(sessionID_);
320 
321     return audioStream_->ReleaseAudioStream();
322 }
323 
GetBufferSize(size_t & bufferSize) const324 int32_t AudioRendererPrivate::GetBufferSize(size_t &bufferSize) const
325 {
326     return audioStream_->GetBufferSize(bufferSize);
327 }
328 
SetAudioRendererDesc(AudioRendererDesc audioRendererDesc) const329 int32_t AudioRendererPrivate::SetAudioRendererDesc(AudioRendererDesc audioRendererDesc) const
330 {
331     ContentType contentType = audioRendererDesc.contentType;
332     StreamUsage streamUsage = audioRendererDesc.streamUsage;
333     AudioStreamType audioStreamType = audioStream_->GetStreamType(contentType, streamUsage);
334     return audioStream_->SetAudioStreamType(audioStreamType);
335 }
336 
SetStreamType(AudioStreamType audioStreamType) const337 int32_t AudioRendererPrivate::SetStreamType(AudioStreamType audioStreamType) const
338 {
339     return audioStream_->SetAudioStreamType(audioStreamType);
340 }
341 
SetVolume(float volume) const342 int32_t AudioRendererPrivate::SetVolume(float volume) const
343 {
344     return audioStream_->SetVolume(volume);
345 }
346 
GetVolume() const347 float AudioRendererPrivate::GetVolume() const
348 {
349     return audioStream_->GetVolume();
350 }
351 
SetRenderRate(AudioRendererRate renderRate) const352 int32_t AudioRendererPrivate::SetRenderRate(AudioRendererRate renderRate) const
353 {
354     return audioStream_->SetRenderRate(renderRate);
355 }
356 
GetRenderRate() const357 AudioRendererRate AudioRendererPrivate::GetRenderRate() const
358 {
359     return audioStream_->GetRenderRate();
360 }
361 
SetBufferDuration(uint64_t bufferDuration) const362 int32_t AudioRendererPrivate::SetBufferDuration(uint64_t bufferDuration) const
363 {
364     if (bufferDuration < MINIMUM_BUFFER_SIZE_MSEC || bufferDuration > MAXIMUM_BUFFER_SIZE_MSEC) {
365         MEDIA_ERR_LOG("Error: Please set the buffer duration between 5ms ~ 20ms");
366         return ERR_INVALID_PARAM;
367     }
368 
369     return audioStream_->SetBufferSizeInMsec(bufferDuration);
370 }
371 
AudioInterruptCallbackImpl(const std::shared_ptr<AudioStream> & audioStream,const AudioInterrupt & audioInterrupt)372 AudioInterruptCallbackImpl::AudioInterruptCallbackImpl(const std::shared_ptr<AudioStream> &audioStream,
373     const AudioInterrupt &audioInterrupt)
374     : audioStream_(audioStream), audioInterrupt_(audioInterrupt)
375 {
376     MEDIA_INFO_LOG("AudioInterruptCallbackImpl constructor");
377 }
378 
~AudioInterruptCallbackImpl()379 AudioInterruptCallbackImpl::~AudioInterruptCallbackImpl()
380 {
381     MEDIA_DEBUG_LOG("AudioInterruptCallbackImpl: instance destroy");
382 }
383 
SaveCallback(const std::weak_ptr<AudioRendererCallback> & callback)384 void AudioInterruptCallbackImpl::SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback)
385 {
386     callback_ = callback;
387 }
388 
NotifyEvent(const InterruptEvent & interruptEvent)389 void AudioInterruptCallbackImpl::NotifyEvent(const InterruptEvent &interruptEvent)
390 {
391     MEDIA_DEBUG_LOG("AudioRendererPrivate: NotifyEvent: Hint: %{public}d", interruptEvent.hintType);
392     MEDIA_DEBUG_LOG("AudioRendererPrivate: NotifyEvent: eventType: %{public}d", interruptEvent.eventType);
393 
394     if (cb_ != nullptr) {
395         cb_->OnInterrupt(interruptEvent);
396         MEDIA_DEBUG_LOG("AudioRendererPrivate: OnInterrupt : NotifyEvent to app complete");
397     } else {
398         MEDIA_DEBUG_LOG("AudioRendererPrivate: cb_ == nullptr cannont NotifyEvent to app");
399     }
400 }
401 
HandleForceDucking(const InterruptEventInternal & interruptEvent)402 bool AudioInterruptCallbackImpl::HandleForceDucking(const InterruptEventInternal &interruptEvent)
403 {
404     float streamVolume = AudioPolicyManager::GetInstance().GetStreamVolume(audioInterrupt_.streamType);
405     float duckVolume = interruptEvent.duckVolume;
406     int32_t ret = 0;
407 
408     if (streamVolume <= duckVolume || FLOAT_COMPARE_EQ(streamVolume, 0.0f)) {
409         MEDIA_INFO_LOG("AudioRendererPrivate: StreamVolume: %{public}f <= duckVolume: %{public}f",
410                        streamVolume, duckVolume);
411         MEDIA_INFO_LOG("AudioRendererPrivate: No need to duck further return");
412         return false;
413     }
414 
415     instanceVolBeforeDucking_ = audioStream_->GetVolume();
416     float duckInstanceVolume = duckVolume / streamVolume;
417     if (FLOAT_COMPARE_EQ(instanceVolBeforeDucking_, 0.0f) || instanceVolBeforeDucking_ < duckInstanceVolume) {
418         MEDIA_INFO_LOG("AudioRendererPrivate: No need to duck further return");
419         return false;
420     }
421 
422     ret = audioStream_->SetVolume(duckInstanceVolume);
423     if (ret) {
424         MEDIA_DEBUG_LOG("AudioRendererPrivate: set duckVolume(instance) %{pubic}f failed", duckInstanceVolume);
425         return false;
426     }
427 
428     MEDIA_DEBUG_LOG("AudioRendererPrivate: set duckVolume(instance) %{pubic}f success", duckInstanceVolume);
429     return true;
430 }
431 
NotifyForcePausedToResume(const InterruptEventInternal & interruptEvent)432 void AudioInterruptCallbackImpl::NotifyForcePausedToResume(const InterruptEventInternal &interruptEvent)
433 {
434     // Change InterruptForceType to Share, Since app will take care of resuming
435     InterruptEvent interruptEventResume {interruptEvent.eventType, INTERRUPT_SHARE,
436                                          interruptEvent.hintType};
437     NotifyEvent(interruptEventResume);
438 }
439 
HandleAndNotifyForcedEvent(const InterruptEventInternal & interruptEvent)440 void AudioInterruptCallbackImpl::HandleAndNotifyForcedEvent(const InterruptEventInternal &interruptEvent)
441 {
442     InterruptHint hintType = interruptEvent.hintType;
443     MEDIA_DEBUG_LOG("AudioRendererPrivate ForceType: INTERRUPT_FORCE, Force handle the event and notify the app");
444     MEDIA_DEBUG_LOG("AudioRendererPrivate: HandleAndNotifyForcedEvent: Hint: %{public}d eventType: %{public}d",
445         interruptEvent.hintType, interruptEvent.eventType);
446 
447     switch (hintType) {
448         case INTERRUPT_HINT_PAUSE:
449             if (audioStream_->GetState() != RUNNING) {
450                 MEDIA_DEBUG_LOG("AudioRendererPrivate::OnInterrupt state is not running no need to pause");
451                 return;
452             }
453             (void)audioStream_->PauseAudioStream(); // Just Pause, do not deactivate here
454             isForcePaused_ = true;
455             break;
456         case INTERRUPT_HINT_RESUME:
457             if (audioStream_->GetState() != PAUSED || !isForcePaused_) {
458                 MEDIA_DEBUG_LOG("AudioRendererPrivate::OnInterrupt state is not paused or not forced paused");
459                 return;
460             }
461             isForcePaused_ = false;
462             NotifyForcePausedToResume(interruptEvent);
463             return; // return, sending callback is taken care in NotifyForcePausedToResume
464         case INTERRUPT_HINT_STOP:
465             (void)audioStream_->StopAudioStream();
466             break;
467         case INTERRUPT_HINT_DUCK:
468             if (!HandleForceDucking(interruptEvent)) {
469                 MEDIA_DEBUG_LOG("AudioRendererPrivate:: It is not forced ducked, no need notify app, return");
470                 return;
471             }
472             isForceDucked_ = true;
473             break;
474         case INTERRUPT_HINT_UNDUCK:
475             if (!isForceDucked_) {
476                 MEDIA_DEBUG_LOG("AudioRendererPrivate:: It is not forced ducked, no need to unduck or notify app");
477                 return;
478             }
479             (void)audioStream_->SetVolume(instanceVolBeforeDucking_);
480             MEDIA_DEBUG_LOG("AudioRendererPrivate: unduck Volume(instance) complete: %{public}f",
481                             instanceVolBeforeDucking_);
482             isForceDucked_ = false;
483             break;
484         default:
485             break;
486     }
487     // Notify valid forced event callbacks to app
488     InterruptEvent interruptEventForced {interruptEvent.eventType, interruptEvent.forceType, interruptEvent.hintType};
489     NotifyEvent(interruptEventForced);
490 }
491 
OnInterrupt(const InterruptEventInternal & interruptEvent)492 void AudioInterruptCallbackImpl::OnInterrupt(const InterruptEventInternal &interruptEvent)
493 {
494     cb_ = callback_.lock();
495     InterruptForceType forceType = interruptEvent.forceType;
496     MEDIA_DEBUG_LOG("AudioRendererPrivate: OnInterrupt InterruptForceType: %{public}d", forceType);
497 
498     if (forceType != INTERRUPT_FORCE) { // INTERRUPT_SHARE
499         MEDIA_DEBUG_LOG("AudioRendererPrivate ForceType: INTERRUPT_SHARE. Let app handle the event");
500         InterruptEvent interruptEventShared {interruptEvent.eventType, interruptEvent.forceType,
501                                              interruptEvent.hintType};
502         NotifyEvent(interruptEventShared);
503         return;
504     }
505 
506     if (audioStream_ == nullptr) {
507         MEDIA_DEBUG_LOG("AudioInterruptCallbackImpl::OnInterrupt stream is not alive. No need to take forced action");
508         return;
509     }
510 
511     HandleAndNotifyForcedEvent(interruptEvent);
512 }
513 
SaveCallback(const std::weak_ptr<AudioRendererCallback> & callback)514 void AudioStreamCallbackRenderer::SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback)
515 {
516     callback_ = callback;
517 }
518 
OnStateChange(const State state)519 void AudioStreamCallbackRenderer::OnStateChange(const State state)
520 {
521     std::shared_ptr<AudioRendererCallback> cb = callback_.lock();
522     if (cb == nullptr) {
523         MEDIA_ERR_LOG("AudioStreamCallbackRenderer::OnStateChange cb == nullptr.");
524         return;
525     }
526 
527     cb->OnStateChange(static_cast<RendererState>(state));
528 }
529 
GetSupportedFormats()530 std::vector<AudioSampleFormat> AudioRenderer::GetSupportedFormats()
531 {
532     return AUDIO_SUPPORTED_FORMATS;
533 }
534 
GetSupportedSamplingRates()535 std::vector<AudioSamplingRate> AudioRenderer::GetSupportedSamplingRates()
536 {
537     return AUDIO_SUPPORTED_SAMPLING_RATES;
538 }
539 
GetSupportedChannels()540 std::vector<AudioChannel> AudioRenderer::GetSupportedChannels()
541 {
542     return AUDIO_SUPPORTED_CHANNELS;
543 }
544 
GetSupportedEncodingTypes()545 std::vector<AudioEncodingType> AudioRenderer::GetSupportedEncodingTypes()
546 {
547     return AUDIO_SUPPORTED_ENCODING_TYPES;
548 }
549 
SetRenderMode(AudioRenderMode renderMode) const550 int32_t AudioRendererPrivate::SetRenderMode(AudioRenderMode renderMode) const
551 {
552     return audioStream_->SetRenderMode(renderMode);
553 }
554 
GetRenderMode() const555 AudioRenderMode AudioRendererPrivate::GetRenderMode() const
556 {
557     return audioStream_->GetRenderMode();
558 }
559 
GetBufferDesc(BufferDesc & bufDesc) const560 int32_t AudioRendererPrivate::GetBufferDesc(BufferDesc &bufDesc) const
561 {
562     return audioStream_->GetBufferDesc(bufDesc);
563 }
564 
Enqueue(const BufferDesc & bufDesc) const565 int32_t AudioRendererPrivate::Enqueue(const BufferDesc &bufDesc) const
566 {
567     return audioStream_->Enqueue(bufDesc);
568 }
569 
Clear() const570 int32_t AudioRendererPrivate::Clear() const
571 {
572     return audioStream_->Clear();
573 }
574 
GetBufQueueState(BufferQueueState & bufState) const575 int32_t AudioRendererPrivate::GetBufQueueState(BufferQueueState &bufState) const
576 {
577     return SUCCESS;
578 }
579 
SetApplicationCachePath(const std::string cachePath)580 void AudioRendererPrivate::SetApplicationCachePath(const std::string cachePath)
581 {
582     audioStream_->SetApplicationCachePath(cachePath);
583 }
584 
SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> & callback)585 int32_t AudioRendererPrivate::SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback)
586 {
587     return audioStream_->SetRendererWriteCallback(callback);
588 }
589 }  // namespace AudioStandard
590 }  // namespace OHOS
591