• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "audio_errors.h"
17 #include "audio_policy_manager.h"
18 #include "audio_renderer_gateway.h"
19 #include "audio_container_stream_base.h"
20 #include "audio_log.h"
21 #include "audio_renderer.h"
22 
23 namespace OHOS {
24 namespace AudioStandard {
25 std::map<pid_t, std::map<AudioStreamType, AudioInterrupt>> AudioRendererGateway::sharedInterrupts_;
26 
27 AudioRenderer::~AudioRenderer() = default;
~AudioRendererGateway()28 AudioRendererGateway::~AudioRendererGateway()
29 {
30     RendererState state = GetStatus();
31     if (state != RENDERER_RELEASED && state != RENDERER_NEW) {
32         Release();
33     }
34 }
35 
AudioRendererGateway(AudioStreamType audioStreamType,const AppInfo & appInfo)36 AudioRendererGateway::AudioRendererGateway(AudioStreamType audioStreamType, const AppInfo &appInfo)
37 {
38     appInfo_ = appInfo;
39     if (!(appInfo_.appPid)) {
40         appInfo_.appPid = getpid();
41     }
42 
43     if (appInfo_.appUid < 0) {
44         appInfo_.appUid = static_cast<int32_t>(getuid());
45     }
46 
47     audioStream_ = std::make_shared<AudioContainerRenderStream>(audioStreamType, AUDIO_MODE_PLAYBACK, appInfo_.appUid);
48     if (audioStream_) {
49         AUDIO_DEBUG_LOG("AudioRendererGateway::Audio stream created");
50         // Initializing with default values
51         rendererInfo_.contentType = CONTENT_TYPE_MUSIC;
52         rendererInfo_.streamUsage = STREAM_USAGE_MEDIA;
53     }
54 
55     audioInterrupt_.streamType = audioStreamType;
56     sharedInterrupt_.streamType = audioStreamType;
57 }
58 
InitAudioInterruptCallback()59 int32_t AudioRendererGateway::InitAudioInterruptCallback()
60 {
61     AUDIO_INFO_LOG("AudioRendererGateway::InitAudioInterruptCallback in");
62     AudioInterrupt interrupt;
63     switch (mode_) {
64         case InterruptMode::SHARE_MODE:
65             if (InitSharedInterrupt() != 0) {
66                 AUDIO_ERR_LOG("InitAudioInterruptCallback::GetAudioSessionID failed for SHARE_MODE");
67                 return ERR_INVALID_INDEX;
68             }
69             interrupt = sharedInterrupt_;
70             break;
71         case InterruptMode::INDEPENDENT_MODE:
72             if (audioStream_->GetAudioSessionID(audioInterrupt_.sessionID) != 0) {
73                 AUDIO_ERR_LOG("InitAudioInterruptCallback::GetAudioSessionID failed for INDEPENDENT_MODE");
74                 return ERR_INVALID_INDEX;
75             }
76             interrupt = audioInterrupt_;
77             break;
78         default:
79             AUDIO_ERR_LOG("InitAudioInterruptCallback::Invalid interrupt mode!");
80             return ERR_INVALID_PARAM;
81     }
82     sessionID_ = interrupt.sessionID;
83 
84     AUDIO_INFO_LOG("InitAudioInterruptCallback::interruptMode %{public}d, streamType %{public}d, sessionID %{public}d",
85         mode_, interrupt.streamType, interrupt.sessionID);
86 
87     if (audioInterruptCallback_ == nullptr) {
88         audioInterruptCallback_ = std::make_shared<AudioInterruptCallbackGateway>(audioStream_, interrupt);
89         if (audioInterruptCallback_ == nullptr) {
90             AUDIO_ERR_LOG("InitAudioInterruptCallback::Failed to allocate memory for audioInterruptCallback_");
91             return ERROR;
92         }
93     }
94     return AudioPolicyManager::GetInstance().SetAudioInterruptCallback(sessionID_, audioInterruptCallback_);
95 }
96 
InitSharedInterrupt()97 int32_t AudioRendererGateway::InitSharedInterrupt()
98 {
99     if (AudioRendererGateway::sharedInterrupts_.find(appInfo_.appPid) ==
100         AudioRendererGateway::sharedInterrupts_.end()) {
101         AUDIO_INFO_LOG("InitSharedInterrupt: appInfo_.appPid %{public}d create new sharedInterrupt", appInfo_.appPid);
102         std::map<AudioStreamType, AudioInterrupt> interrupts;
103         std::vector<AudioStreamType> types;
104         types.push_back(AudioStreamType::STREAM_DEFAULT);
105         types.push_back(AudioStreamType::STREAM_VOICE_CALL);
106         types.push_back(AudioStreamType::STREAM_MUSIC);
107         types.push_back(AudioStreamType::STREAM_RING);
108         types.push_back(AudioStreamType::STREAM_MEDIA);
109         types.push_back(AudioStreamType::STREAM_VOICE_ASSISTANT);
110         types.push_back(AudioStreamType::STREAM_SYSTEM);
111         types.push_back(AudioStreamType::STREAM_ALARM);
112         types.push_back(AudioStreamType::STREAM_NOTIFICATION);
113         types.push_back(AudioStreamType::STREAM_BLUETOOTH_SCO);
114         types.push_back(AudioStreamType::STREAM_ENFORCED_AUDIBLE);
115         types.push_back(AudioStreamType::STREAM_DTMF);
116         types.push_back(AudioStreamType::STREAM_TTS);
117         types.push_back(AudioStreamType::STREAM_ACCESSIBILITY);
118         for (auto type : types) {
119             uint32_t interruptId;
120             if (audioStream_->GetAudioSessionID(interruptId) != 0) {
121                 AUDIO_ERR_LOG("AudioRendererGateway::GetAudioSessionID interruptId Failed");
122                 return ERR_INVALID_INDEX;
123             }
124             AudioInterrupt interrupt = {STREAM_USAGE_UNKNOWN, CONTENT_TYPE_UNKNOWN, sharedInterrupt_.streamType,
125                 interruptId};
126             interrupts.insert(std::make_pair(type, interrupt));
127         }
128         AudioRendererGateway::sharedInterrupts_.insert(std::make_pair(appInfo_.appPid, interrupts));
129     } else {
130         AUDIO_INFO_LOG("InitSharedInterrupt: sharedInterrupt of appInfo_.appPid %{public}d existed", appInfo_.appPid);
131     }
132 
133     sharedInterrupt_ = AudioRendererGateway::sharedInterrupts_.find(appInfo_.appPid)
134         ->second.find(sharedInterrupt_.streamType)->second;
135     return SUCCESS;
136 }
137 
GetFrameCount(uint32_t & frameCount) const138 int32_t AudioRendererGateway::GetFrameCount(uint32_t &frameCount) const
139 {
140     return audioStream_->GetFrameCount(frameCount);
141 }
142 
GetLatency(uint64_t & latency) const143 int32_t AudioRendererGateway::GetLatency(uint64_t &latency) const
144 {
145     return audioStream_->GetLatency(latency);
146 }
147 
SetParams(const AudioRendererParams params)148 int32_t AudioRendererGateway::SetParams(const AudioRendererParams params)
149 {
150     audioStream_->SetRendererInfo(rendererInfo_);
151     AudioStreamParams audioStreamParams;
152     audioStreamParams.format = params.sampleFormat;
153     audioStreamParams.samplingRate = params.sampleRate;
154     audioStreamParams.channels = params.channelCount;
155     audioStreamParams.encoding = params.encodingType;
156 
157     audioStream_->SetClientID(appInfo_.appPid, appInfo_.appUid);
158 
159     int32_t ret = audioStream_->SetAudioStreamInfo(audioStreamParams);
160     if (ret) {
161         AUDIO_ERR_LOG("AudioRendererGateway::SetParams SetAudioStreamInfo Failed");
162         return ret;
163     }
164     AUDIO_INFO_LOG("AudioRendererGateway::SetParams SetAudioStreamInfo Success");
165 
166     return InitAudioInterruptCallback();
167 }
168 
GetParams(AudioRendererParams & params) const169 int32_t AudioRendererGateway::GetParams(AudioRendererParams &params) const
170 {
171     AudioStreamParams audioStreamParams;
172     int32_t result = audioStream_->GetAudioStreamInfo(audioStreamParams);
173     if (!result) {
174         params.sampleFormat = static_cast<AudioSampleFormat>(audioStreamParams.format);
175         params.sampleRate = static_cast<AudioSamplingRate>(audioStreamParams.samplingRate);
176         params.channelCount = static_cast<AudioChannel>(audioStreamParams.channels);
177         params.encodingType = static_cast<AudioEncodingType>(audioStreamParams.encoding);
178     }
179 
180     return result;
181 }
182 
GetRendererInfo(AudioRendererInfo & rendererInfo) const183 int32_t AudioRendererGateway::GetRendererInfo(AudioRendererInfo &rendererInfo) const
184 {
185     rendererInfo = rendererInfo_;
186 
187     return SUCCESS;
188 }
189 
GetStreamInfo(AudioStreamInfo & streamInfo) const190 int32_t AudioRendererGateway::GetStreamInfo(AudioStreamInfo &streamInfo) const
191 {
192     AudioStreamParams audioStreamParams;
193     int32_t result = audioStream_->GetAudioStreamInfo(audioStreamParams);
194     if (!result) {
195         streamInfo.format = static_cast<AudioSampleFormat>(audioStreamParams.format);
196         streamInfo.samplingRate = static_cast<AudioSamplingRate>(audioStreamParams.samplingRate);
197         streamInfo.channels = static_cast<AudioChannel>(audioStreamParams.channels);
198         streamInfo.encoding = static_cast<AudioEncodingType>(audioStreamParams.encoding);
199     }
200 
201     return result;
202 }
203 
SetRendererCallback(const std::shared_ptr<AudioRendererCallback> & callback)204 int32_t AudioRendererGateway::SetRendererCallback(const std::shared_ptr<AudioRendererCallback> &callback)
205 {
206     // If the client is using the deprecated SetParams API. SetRendererCallback must be invoked, after SetParams.
207     // In general, callbacks can only be set after the renderer state is PREPARED.
208     RendererState state = GetStatus();
209     if (state == RENDERER_NEW || state == RENDERER_RELEASED) {
210         AUDIO_DEBUG_LOG("AudioRendererGateway::SetRendererCallback incorrect state:%{public}d to register cb", state);
211         return ERR_ILLEGAL_STATE;
212     }
213     if (callback == nullptr) {
214         AUDIO_ERR_LOG("AudioRendererGateway::SetRendererCallback callback param is null");
215         return ERR_INVALID_PARAM;
216     }
217 
218     // Save reference for interrupt callback
219     if (audioInterruptCallback_ == nullptr) {
220         AUDIO_ERR_LOG("AudioRendererGateway::SetRendererCallback audioInterruptCallback_ == nullptr");
221         return ERROR;
222     }
223     std::shared_ptr<AudioInterruptCallbackGateway> cbInterrupt =
224         std::static_pointer_cast<AudioInterruptCallbackGateway>(audioInterruptCallback_);
225     cbInterrupt->SaveCallback(callback);
226 
227     // Save and Set reference for stream callback. Order is important here.
228     if (audioStreamCallback_ == nullptr) {
229         audioStreamCallback_ = std::make_shared<AudioStreamRenderCallback>();
230         if (audioStreamCallback_ == nullptr) {
231             AUDIO_ERR_LOG("AudioRendererGateway::Failed to allocate memory for audioStreamCallback_");
232             return ERROR;
233         }
234     }
235 
236     std::shared_ptr<AudioStreamRenderCallback> cbStream =
237         std::static_pointer_cast<AudioStreamRenderCallback>(audioStreamCallback_);
238     cbStream->SaveCallback(callback);
239     AUDIO_INFO_LOG("AudioRendererGateway::SetRendererCallback callback audioStreamCallback_");
240     (void)audioStream_->SetStreamCallback(audioStreamCallback_);
241 
242     return SUCCESS;
243 }
244 
SetRendererPositionCallback(int64_t markPosition,const std::shared_ptr<RendererPositionCallback> & callback)245 int32_t AudioRendererGateway::SetRendererPositionCallback(int64_t markPosition,
246     const std::shared_ptr<RendererPositionCallback> &callback)
247 {
248     if ((callback == nullptr) || (markPosition <= 0)) {
249         AUDIO_ERR_LOG("AudioRendererGateway::SetRendererPositionCallback input param is invalid");
250         return ERR_INVALID_PARAM;
251     }
252 
253     audioStream_->SetRendererPositionCallback(markPosition, callback);
254 
255     return SUCCESS;
256 }
257 
UnsetRendererPositionCallback()258 void AudioRendererGateway::UnsetRendererPositionCallback()
259 {
260     audioStream_->UnsetRendererPositionCallback();
261 }
262 
SetRendererPeriodPositionCallback(int64_t frameNumber,const std::shared_ptr<RendererPeriodPositionCallback> & callback)263 int32_t AudioRendererGateway::SetRendererPeriodPositionCallback(int64_t frameNumber,
264     const std::shared_ptr<RendererPeriodPositionCallback> &callback)
265 {
266     if ((callback == nullptr) || (frameNumber <= 0)) {
267         AUDIO_ERR_LOG("AudioRendererGateway::SetRendererPeriodPositionCallback input param is invalid");
268         return ERR_INVALID_PARAM;
269     }
270 
271     audioStream_->SetRendererPeriodPositionCallback(frameNumber, callback);
272 
273     return SUCCESS;
274 }
275 
UnsetRendererPeriodPositionCallback()276 void AudioRendererGateway::UnsetRendererPeriodPositionCallback()
277 {
278     audioStream_->UnsetRendererPeriodPositionCallback();
279 }
280 
Start(StateChangeCmdType cmdType) const281 bool AudioRendererGateway::Start(StateChangeCmdType cmdType) const
282 {
283     AUDIO_INFO_LOG("AudioRendererGateway::Start()");
284     RendererState state = GetStatus();
285     if ((state != RENDERER_PREPARED) && (state != RENDERER_STOPPED) && (state != RENDERER_PAUSED)) {
286         AUDIO_ERR_LOG("AudioRendererGateway::Start() Illegal state:%{public}u, Start failed", state);
287         return false;
288     }
289     AudioInterrupt audioInterrupt;
290     switch (mode_) {
291         case InterruptMode::SHARE_MODE:
292             audioInterrupt = sharedInterrupt_;
293             break;
294         case InterruptMode::INDEPENDENT_MODE:
295             audioInterrupt = audioInterrupt_;
296             break;
297         default:
298             break;
299     }
300 
301     AUDIO_INFO_LOG("AudioRenderer::Start::interruptMode: %{public}d, streamType: %{public}d, sessionID: %{public}d",
302         mode_, audioInterrupt.streamType, audioInterrupt.sessionID);
303 
304     if (audioInterrupt.streamType == STREAM_DEFAULT || audioInterrupt.sessionID == INVALID_SESSION) {
305         return false;
306     }
307 
308     int32_t ret = AudioPolicyManager::GetInstance().ActivateAudioInterrupt(audioInterrupt);
309     if (ret != 0) {
310         AUDIO_ERR_LOG("AudioRendererGateway::ActivateAudioInterrupt Failed");
311         return false;
312     }
313 
314     return audioStream_->StartAudioStream();
315 }
316 
Write(uint8_t * buffer,size_t bufferSize)317 int32_t AudioRendererGateway::Write(uint8_t *buffer, size_t bufferSize)
318 {
319     return audioStream_->Write(buffer, bufferSize);
320 }
321 
GetStatus() const322 RendererState AudioRendererGateway::GetStatus() const
323 {
324     return static_cast<RendererState>(audioStream_->GetState());
325 }
326 
GetAudioTime(Timestamp & timestamp,Timestamp::Timestampbase base) const327 bool AudioRendererGateway::GetAudioTime(Timestamp &timestamp, Timestamp::Timestampbase base) const
328 {
329     return audioStream_->GetAudioTime(timestamp, base);
330 }
331 
Drain() const332 bool AudioRendererGateway::Drain() const
333 {
334     return audioStream_->DrainAudioStream();
335 }
336 
Flush() const337 bool AudioRendererGateway::Flush() const
338 {
339     return audioStream_->FlushAudioStream();
340 }
341 
Pause(StateChangeCmdType cmdType) const342 bool AudioRendererGateway::Pause(StateChangeCmdType cmdType) const
343 {
344     bool result = audioStream_->PauseAudioStream();
345     AudioInterrupt audioInterrupt;
346     switch (mode_) {
347         case InterruptMode::SHARE_MODE:
348             audioInterrupt = sharedInterrupt_;
349             break;
350         case InterruptMode::INDEPENDENT_MODE:
351             audioInterrupt = audioInterrupt_;
352             break;
353         default:
354             break;
355     }
356     // When user is intentionally pausing , Deactivate to remove from active/pending owners list
357     int32_t ret = AudioPolicyManager::GetInstance().DeactivateAudioInterrupt(audioInterrupt);
358     if (ret != 0) {
359         AUDIO_ERR_LOG("AudioRendererGateway: DeactivateAudioInterrupt Failed");
360     }
361 
362     return result;
363 }
364 
Stop() const365 bool AudioRendererGateway::Stop() const
366 {
367     bool result = audioStream_->StopAudioStream();
368     AudioInterrupt audioInterrupt;
369     switch (mode_) {
370         case InterruptMode::SHARE_MODE:
371             audioInterrupt = sharedInterrupt_;
372             break;
373         case InterruptMode::INDEPENDENT_MODE:
374             audioInterrupt = audioInterrupt_;
375             break;
376         default:
377             break;
378     }
379     int32_t ret = AudioPolicyManager::GetInstance().DeactivateAudioInterrupt(audioInterrupt);
380     if (ret != 0) {
381         AUDIO_ERR_LOG("AudioRendererGateway: DeactivateAudioInterrupt Failed");
382     }
383 
384     return result;
385 }
386 
Release() const387 bool AudioRendererGateway::Release() const
388 {
389     // If Stop call was skipped, Release to take care of Deactivation
390     (void)AudioPolicyManager::GetInstance().DeactivateAudioInterrupt(audioInterrupt_);
391 
392     // Unregister the callaback in policy server
393     (void)AudioPolicyManager::GetInstance().UnsetAudioInterruptCallback(sessionID_);
394 
395     return audioStream_->ReleaseAudioStream();
396 }
397 
GetBufferSize(size_t & bufferSize) const398 int32_t AudioRendererGateway::GetBufferSize(size_t &bufferSize) const
399 {
400     return audioStream_->GetBufferSize(bufferSize);
401 }
402 
GetAudioStreamId(uint32_t & sessionID) const403 int32_t AudioRendererGateway::GetAudioStreamId(uint32_t &sessionID) const
404 {
405     return audioStream_->GetAudioSessionID(sessionID);
406 }
407 
SetAudioRendererDesc(AudioRendererDesc audioRendererDesc) const408 int32_t AudioRendererGateway::SetAudioRendererDesc(AudioRendererDesc audioRendererDesc) const
409 {
410     ContentType contentType = audioRendererDesc.contentType;
411     StreamUsage streamUsage = audioRendererDesc.streamUsage;
412     AudioStreamType audioStreamType = audioStream_->GetStreamType(contentType, streamUsage);
413     return audioStream_->SetAudioStreamType(audioStreamType);
414 }
415 
SetStreamType(AudioStreamType audioStreamType) const416 int32_t AudioRendererGateway::SetStreamType(AudioStreamType audioStreamType) const
417 {
418     return audioStream_->SetAudioStreamType(audioStreamType);
419 }
420 
SetVolume(float volume) const421 int32_t AudioRendererGateway::SetVolume(float volume) const
422 {
423     return audioStream_->SetVolume(volume);
424 }
425 
GetVolume() const426 float AudioRendererGateway::GetVolume() const
427 {
428     return audioStream_->GetVolume();
429 }
430 
SetRenderRate(AudioRendererRate renderRate) const431 int32_t AudioRendererGateway::SetRenderRate(AudioRendererRate renderRate) const
432 {
433     return audioStream_->SetRenderRate(renderRate);
434 }
435 
GetRenderRate() const436 AudioRendererRate AudioRendererGateway::GetRenderRate() const
437 {
438     return audioStream_->GetRenderRate();
439 }
440 
SetBufferDuration(uint64_t bufferDuration) const441 int32_t AudioRendererGateway::SetBufferDuration(uint64_t bufferDuration) const
442 {
443     if (bufferDuration < MINIMUM_BUFFER_SIZE_MSEC || bufferDuration > MAXIMUM_BUFFER_SIZE_MSEC) {
444         AUDIO_ERR_LOG("Error: Please set the buffer duration between 5ms ~ 20ms");
445         return ERR_INVALID_PARAM;
446     }
447 
448     return audioStream_->SetBufferSizeInMsec(bufferDuration);
449 }
450 
AudioInterruptCallbackGateway(const std::shared_ptr<AudioContainerRenderStream> & audioStream,const AudioInterrupt & audioInterrupt)451 AudioInterruptCallbackGateway::AudioInterruptCallbackGateway(
452     const std::shared_ptr<AudioContainerRenderStream> &audioStream, const AudioInterrupt &audioInterrupt)
453     : audioStream_(audioStream), audioInterrupt_(audioInterrupt)
454 {
455     AUDIO_INFO_LOG("AudioInterruptCallbackGateway constructor");
456 }
457 
~AudioInterruptCallbackGateway()458 AudioInterruptCallbackGateway::~AudioInterruptCallbackGateway()
459 {
460     AUDIO_DEBUG_LOG("AudioInterruptCallbackGateway: instance destroy");
461 }
462 
SaveCallback(const std::weak_ptr<AudioRendererCallback> & callback)463 void AudioInterruptCallbackGateway::SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback)
464 {
465     callback_ = callback;
466 }
467 
NotifyEvent(const InterruptEvent & interruptEvent)468 void AudioInterruptCallbackGateway::NotifyEvent(const InterruptEvent &interruptEvent)
469 {
470     AUDIO_DEBUG_LOG("AudioRendererGateway: NotifyEvent: Hint: %{public}d", interruptEvent.hintType);
471     AUDIO_DEBUG_LOG("AudioRendererGateway: NotifyEvent: eventType: %{public}d", interruptEvent.eventType);
472 
473     if (cb_ != nullptr) {
474         cb_->OnInterrupt(interruptEvent);
475         AUDIO_DEBUG_LOG("AudioRendererGateway: OnInterrupt : NotifyEvent to app complete");
476     } else {
477         AUDIO_DEBUG_LOG("AudioRendererGateway: cb_ == nullptr cannont NotifyEvent to app");
478     }
479 }
480 
HandleForceDucking(const InterruptEventInternal & interruptEvent)481 bool AudioInterruptCallbackGateway::HandleForceDucking(const InterruptEventInternal &interruptEvent)
482 {
483     float streamVolume = AudioPolicyManager::GetInstance().GetStreamVolume(audioInterrupt_.streamType);
484     float duckVolume = interruptEvent.duckVolume;
485     int32_t ret = 0;
486 
487     if (streamVolume <= duckVolume || FLOAT_COMPARE_EQ(streamVolume, 0.0f)) {
488         AUDIO_INFO_LOG("AudioRendererGateway: StreamVolume: %{public}f <= duckVolume: %{public}f",
489                        streamVolume, duckVolume);
490         AUDIO_INFO_LOG("AudioRendererGateway: No need to duck further return");
491         return false;
492     }
493 
494     instanceVolBeforeDucking_ = audioStream_->GetVolume();
495     float duckInstanceVolume = duckVolume / streamVolume;
496     if (FLOAT_COMPARE_EQ(instanceVolBeforeDucking_, 0.0f) || instanceVolBeforeDucking_ < duckInstanceVolume) {
497         AUDIO_INFO_LOG("AudioRendererGateway: No need to duck further return");
498         return false;
499     }
500 
501     ret = audioStream_->SetVolume(duckInstanceVolume);
502     if (ret) {
503         AUDIO_DEBUG_LOG("AudioRendererGateway: set duckVolume(instance) %{pubic}f failed", duckInstanceVolume);
504         return false;
505     }
506 
507     AUDIO_DEBUG_LOG("AudioRendererGateway: set duckVolume(instance) %{pubic}f succeeded", duckInstanceVolume);
508     return true;
509 }
510 
NotifyForcePausedToResume(const InterruptEventInternal & interruptEvent)511 void AudioInterruptCallbackGateway::NotifyForcePausedToResume(const InterruptEventInternal &interruptEvent)
512 {
513     // Change InterruptForceType to Share, Since app will take care of resuming
514     InterruptEvent interruptEventResume {interruptEvent.eventType, INTERRUPT_SHARE,
515                                          interruptEvent.hintType};
516     NotifyEvent(interruptEventResume);
517 }
518 
HandleAndNotifyForcedEvent(const InterruptEventInternal & interruptEvent)519 void AudioInterruptCallbackGateway::HandleAndNotifyForcedEvent(const InterruptEventInternal &interruptEvent)
520 {
521     InterruptHint hintType = interruptEvent.hintType;
522     AUDIO_DEBUG_LOG("AudioRendererGateway ForceType: INTERRUPT_FORCE, Force handle the event and notify the app");
523     AUDIO_DEBUG_LOG("AudioRendererGateway: HandleAndNotifyForcedEvent: Hint: %{public}d eventType: %{public}d",
524         interruptEvent.hintType, interruptEvent.eventType);
525 
526     switch (hintType) {
527         case INTERRUPT_HINT_PAUSE:
528             if (audioStream_->GetState() != RUNNING) {
529                 AUDIO_DEBUG_LOG("AudioRendererGateway::OnInterrupt state is not running no need to pause");
530                 return;
531             }
532             (void)audioStream_->PauseAudioStream(); // Just Pause, do not deactivate here
533             isForcePaused_ = true;
534             break;
535         case INTERRUPT_HINT_RESUME:
536             if (audioStream_->GetState() != PAUSED || !isForcePaused_) {
537                 AUDIO_DEBUG_LOG("AudioRendererGateway::OnInterrupt state is not paused or not forced paused");
538                 return;
539             }
540             isForcePaused_ = false;
541             NotifyForcePausedToResume(interruptEvent);
542             return; // return, sending callback is taken care in NotifyForcePausedToResume
543         case INTERRUPT_HINT_STOP:
544             (void)audioStream_->StopAudioStream();
545             break;
546         case INTERRUPT_HINT_DUCK:
547             if (!HandleForceDucking(interruptEvent)) {
548                 AUDIO_DEBUG_LOG("AudioRendererGateway:: It is not forced ducked, no need notify app, return");
549                 return;
550             }
551             isForceDucked_ = true;
552             break;
553         case INTERRUPT_HINT_UNDUCK:
554             if (!isForceDucked_) {
555                 AUDIO_DEBUG_LOG("AudioRendererGateway:: It is not forced ducked, no need to unduck or notify app");
556                 return;
557             }
558             (void)audioStream_->SetVolume(instanceVolBeforeDucking_);
559             AUDIO_DEBUG_LOG("AudioRendererGateway: unduck Volume(instance) complete: %{public}f",
560                             instanceVolBeforeDucking_);
561             isForceDucked_ = false;
562             break;
563         default:
564             break;
565     }
566     // Notify valid forced event callbacks to app
567     InterruptEvent interruptEventForced {interruptEvent.eventType, interruptEvent.forceType, interruptEvent.hintType};
568     NotifyEvent(interruptEventForced);
569 }
570 
OnInterrupt(const InterruptEventInternal & interruptEvent)571 void AudioInterruptCallbackGateway::OnInterrupt(const InterruptEventInternal &interruptEvent)
572 {
573     cb_ = callback_.lock();
574     InterruptForceType forceType = interruptEvent.forceType;
575     AUDIO_DEBUG_LOG("AudioRendererGateway: OnInterrupt InterruptForceType: %{public}d", forceType);
576 
577     if (forceType != INTERRUPT_FORCE) { // INTERRUPT_SHARE
578         AUDIO_DEBUG_LOG("AudioRendererGateway ForceType: INTERRUPT_SHARE. Let app handle the event");
579         InterruptEvent interruptEventShared {interruptEvent.eventType, interruptEvent.forceType,
580                                              interruptEvent.hintType};
581         NotifyEvent(interruptEventShared);
582         return;
583     }
584 
585     if (audioStream_ == nullptr) {
586         AUDIO_DEBUG_LOG("AudioInterruptCallbackGateway::OnInterrupt: stream is null. No need to take forced action");
587         return;
588     }
589 
590     HandleAndNotifyForcedEvent(interruptEvent);
591 }
592 
SaveCallback(const std::weak_ptr<AudioRendererCallback> & callback)593 void AudioStreamRenderCallback::SaveCallback(const std::weak_ptr<AudioRendererCallback> &callback)
594 {
595     std::shared_ptr<AudioRendererCallback> cb = callback.lock();
596     AUDIO_ERR_LOG("AudioStreamRenderCallback::SaveCallback cb");
597     callback_ = callback;
598 }
599 
OnStateChange(const State state,StateChangeCmdType cmdType)600 void AudioStreamRenderCallback::OnStateChange(const State state, StateChangeCmdType cmdType)
601 {
602     std::shared_ptr<AudioRendererCallback> cb = callback_.lock();
603     if (cb == nullptr) {
604         AUDIO_ERR_LOG("AudioStreamRenderCallback::OnStateChange cb == nullptr.");
605         return;
606     }
607     AUDIO_ERR_LOG("AudioStreamRenderCallback::OnStateChange cb");
608     cb->OnStateChange(static_cast<RendererState>(state));
609 }
610 
SetRenderMode(AudioRenderMode renderMode) const611 int32_t AudioRendererGateway::SetRenderMode(AudioRenderMode renderMode) const
612 {
613     return audioStream_->SetRenderMode(renderMode);
614 }
615 
GetRenderMode() const616 AudioRenderMode AudioRendererGateway::GetRenderMode() const
617 {
618     return audioStream_->GetRenderMode();
619 }
620 
GetBufferDesc(BufferDesc & bufDesc) const621 int32_t AudioRendererGateway::GetBufferDesc(BufferDesc &bufDesc) const
622 {
623     return audioStream_->GetBufferDesc(bufDesc);
624 }
625 
Enqueue(const BufferDesc & bufDesc) const626 int32_t AudioRendererGateway::Enqueue(const BufferDesc &bufDesc) const
627 {
628     return audioStream_->Enqueue(bufDesc);
629 }
630 
Clear() const631 int32_t AudioRendererGateway::Clear() const
632 {
633     return audioStream_->Clear();
634 }
635 
GetBufQueueState(BufferQueueState & bufState) const636 int32_t AudioRendererGateway::GetBufQueueState(BufferQueueState &bufState) const
637 {
638     return audioStream_->GetBufQueueState(bufState);
639 }
640 
SetApplicationCachePath(const std::string cachePath)641 void AudioRendererGateway::SetApplicationCachePath(const std::string cachePath)
642 {
643     audioStream_->SetApplicationCachePath(cachePath);
644 }
645 
SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> & callback)646 int32_t AudioRendererGateway::SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback)
647 {
648     return audioStream_->SetRendererWriteCallback(callback);
649 }
650 
SetInterruptMode(InterruptMode mode)651 void AudioRendererGateway::SetInterruptMode(InterruptMode mode)
652 {
653     AUDIO_INFO_LOG("AudioRendererGateway::SetInterruptMode: InterruptMode %{pubilc}d", mode);
654     if (mode_ == mode) {
655         return;
656     } else if (mode != SHARE_MODE && mode != INDEPENDENT_MODE) {
657         AUDIO_ERR_LOG("AudioRendererGateway::SetInterruptMode: Invalid interrupt mode!");
658         return;
659     }
660     mode_ = mode;
661 
662     if (AudioPolicyManager::GetInstance().UnsetAudioInterruptCallback(sessionID_) != 0) {
663         AUDIO_ERR_LOG("AudioRendererGateway::SetInterruptMode: UnsetAudioInterruptCallback failed!");
664         return;
665     }
666     if (InitAudioInterruptCallback() != 0) {
667         AUDIO_ERR_LOG("AudioRendererGateway::SetInterruptMode: InitAudioInterruptCallback failed!");
668         return;
669     }
670 }
671 
SetLowPowerVolume(float volume) const672 int32_t AudioRendererGateway::SetLowPowerVolume(float volume) const
673 {
674     return 0;
675 }
676 
GetLowPowerVolume() const677 float AudioRendererGateway::GetLowPowerVolume() const
678 {
679     return 0;
680 }
681 
GetSingleStreamVolume() const682 float AudioRendererGateway::GetSingleStreamVolume() const
683 {
684     return 0;
685 }
686 }  // namespace AudioStandard
687 }  // namespace OHOS
688