• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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 #ifndef LOG_TAG
16 #define LOG_TAG "FastAudioStream"
17 #endif
18 
19 #include <chrono>
20 #include <thread>
21 #include <vector>
22 
23 #include "audio_errors.h"
24 #include "audio_capturer_log.h"
25 
26 #include "fast_audio_stream.h"
27 
28 using namespace std;
29 
30 namespace OHOS {
31 namespace AudioStandard {
FastAudioStream(AudioStreamType eStreamType,AudioMode eMode,int32_t appUid)32 FastAudioStream::FastAudioStream(AudioStreamType eStreamType, AudioMode eMode, int32_t appUid)
33     : eStreamType_(eStreamType),
34       eMode_(eMode),
35       state_(NEW),
36       renderMode_(RENDER_MODE_CALLBACK),
37       captureMode_(CAPTURE_MODE_CALLBACK)
38 {
39     AUDIO_INFO_LOG("FastAudioStream ctor, appUID = %{public}d", appUid);
40     audioStreamTracker_ = std::make_unique<AudioStreamTracker>(eMode, appUid);
41     AUDIO_DEBUG_LOG("AudioStreamTracker created");
42 }
43 
~FastAudioStream()44 FastAudioStream::~FastAudioStream()
45 {
46     if (state_ != RELEASED && state_ != NEW) {
47         ReleaseAudioStream(false);
48     }
49     AUDIO_INFO_LOG("FastAudioStream dtor, session %{public}u", sessionId_);
50 }
51 
SetClientID(int32_t clientPid,int32_t clientUid,uint32_t appTokenId,uint64_t fullTokenId)52 void FastAudioStream::SetClientID(int32_t clientPid, int32_t clientUid, uint32_t appTokenId, uint64_t fullTokenId)
53 {
54     AUDIO_INFO_LOG("Set client PID: %{public}d, UID: %{public}d", clientPid, clientUid);
55     clientPid_ = clientPid;
56     clientUid_ = clientUid;
57     appTokenId_ = appTokenId;
58     fullTokenId_ = fullTokenId;
59 }
60 
UpdatePlaybackCaptureConfig(const AudioPlaybackCaptureConfig & config)61 int32_t FastAudioStream::UpdatePlaybackCaptureConfig(const AudioPlaybackCaptureConfig &config)
62 {
63     AUDIO_ERR_LOG("Unsupported operation!");
64     return ERR_NOT_SUPPORTED;
65 }
66 
SetRendererInfo(const AudioRendererInfo & rendererInfo)67 void FastAudioStream::SetRendererInfo(const AudioRendererInfo &rendererInfo)
68 {
69     rendererInfo_ = rendererInfo;
70     rendererInfo_.samplingRate = static_cast<AudioSamplingRate>(streamInfo_.samplingRate);
71 }
72 
SetCapturerInfo(const AudioCapturerInfo & capturerInfo)73 void FastAudioStream::SetCapturerInfo(const AudioCapturerInfo &capturerInfo)
74 {
75     capturerInfo_ = capturerInfo;
76     capturerInfo_.samplingRate = static_cast<AudioSamplingRate>(streamInfo_.samplingRate);
77 }
78 
InitializeAudioProcessConfig(AudioProcessConfig & config,const AudioStreamParams & info)79 int32_t FastAudioStream::InitializeAudioProcessConfig(AudioProcessConfig &config, const AudioStreamParams &info)
80 {
81     config.appInfo.appPid = clientPid_;
82     config.appInfo.appUid = clientUid_;
83     config.audioMode = eMode_;
84     config.streamInfo.channels = static_cast<AudioChannel>(info.channels);
85     config.streamInfo.encoding = static_cast<AudioEncodingType>(info.encoding);
86     config.streamInfo.format = static_cast<AudioSampleFormat>(info.format);
87     config.streamInfo.samplingRate = static_cast<AudioSamplingRate>(info.samplingRate);
88     config.streamType = eStreamType_;
89     config.originalSessionId = info.originalSessionId;
90     if (eMode_ == AUDIO_MODE_PLAYBACK) {
91         AUDIO_DEBUG_LOG("FastAudioStream: Initialize playback");
92         config.rendererInfo.contentType = rendererInfo_.contentType;
93         config.rendererInfo.streamUsage = rendererInfo_.streamUsage;
94         config.rendererInfo.rendererFlags = STREAM_FLAG_FAST;
95         config.rendererInfo.volumeMode = rendererInfo_.volumeMode;
96         config.rendererInfo.originalFlag = rendererInfo_.originalFlag;
97         config.rendererInfo.playerType = rendererInfo_.playerType;
98         config.rendererInfo.expectedPlaybackDurationBytes = rendererInfo_.expectedPlaybackDurationBytes;
99     } else if (eMode_ == AUDIO_MODE_RECORD) {
100         AUDIO_DEBUG_LOG("FastAudioStream: Initialize recording");
101         config.capturerInfo.sourceType = capturerInfo_.sourceType;
102         config.capturerInfo.capturerFlags = STREAM_FLAG_FAST;
103         config.capturerInfo.originalFlag = capturerInfo_.originalFlag;
104     } else {
105         return ERR_INVALID_OPERATION;
106     }
107     return SUCCESS;
108 }
109 
SetAudioStreamInfo(const AudioStreamParams info,const std::shared_ptr<AudioClientTracker> & proxyObj,const AudioPlaybackCaptureConfig & filterConfig)110 int32_t FastAudioStream::SetAudioStreamInfo(const AudioStreamParams info,
111     const std::shared_ptr<AudioClientTracker> &proxyObj,
112     const AudioPlaybackCaptureConfig &filterConfig)
113 {
114     AUDIO_INFO_LOG("FastAudioStreamInfo, Sampling rate: %{public}d, channels: %{public}d, format: %{public}d,"
115         " stream type: %{public}d", info.samplingRate, info.channels, info.format, eStreamType_);
116     CHECK_AND_RETURN_RET_LOG(processClient_ == nullptr, ERR_INVALID_OPERATION,
117         "Process is already inited, reset stream info is not supported.");
118     streamInfo_ = info;
119     if (state_ != NEW) {
120         AUDIO_INFO_LOG("FastAudioStream: State is not new, release existing stream");
121         StopAudioStream();
122         ReleaseAudioStream(false);
123     }
124     AudioProcessConfig config;
125     int32_t ret = InitializeAudioProcessConfig(config, info);
126     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Initialize failed.");
127     CHECK_AND_RETURN_RET_LOG(AudioProcessInClient::CheckIfSupport(config), ERR_INVALID_PARAM,
128         "Stream is not supported.");
129     processconfig_ = config;
130     // OS_AudioPlayCb/RecordCb should lock weak_ptr of FastAudioStream before calling OnWriteData to
131     // avoid using FastAudioStream after free in callback.
132     auto weakStream = weak_from_this();
133     processClient_ = AudioProcessInClient::Create(config, weakStream);
134     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_INVALID_PARAM,
135         "Client test creat process client fail.");
136     state_ = PREPARED;
137     proxyObj_ = proxyObj;
138 
139     if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
140         processClient_->GetSessionID(sessionId_);
141 
142         AudioRegisterTrackerInfo registerTrackerInfo;
143         UpdateRegisterTrackerInfo(registerTrackerInfo);
144         audioStreamTracker_->RegisterTracker(registerTrackerInfo, proxyObj);
145     }
146     InitCallbackHandler();
147     return SUCCESS;
148 }
149 
InitCallbackHandler()150 void FastAudioStream::InitCallbackHandler()
151 {
152     std::lock_guard<std::mutex> lock(runnerMutex_);
153     if (callbackHandler_ == nullptr) {
154         callbackHandler_ = CallbackHandler::GetInstance(shared_from_this(), "OS_AudioStateCB");
155     }
156 }
157 
SafeSendCallbackEvent(uint32_t eventCode,int64_t data)158 void FastAudioStream::SafeSendCallbackEvent(uint32_t eventCode, int64_t data)
159 {
160     std::lock_guard<std::mutex> lock(runnerMutex_);
161     AUDIO_INFO_LOG("Send callback event, code: %{public}u, data: %{public}" PRId64, eventCode, data);
162     CHECK_AND_RETURN_LOG(callbackHandler_ != nullptr && runnerReleased_ == false, "Runner is Released");
163     callbackHandler_->SendCallbackEvent(eventCode, data);
164 }
165 
OnHandle(uint32_t code,int64_t data)166 void FastAudioStream::OnHandle(uint32_t code, int64_t data)
167 {
168     AUDIO_DEBUG_LOG("On handle event, event code: %{public}u, data: %{public}" PRId64, code, data);
169     switch (code) {
170         case STATE_CHANGE_EVENT:
171             HandleStateChangeEvent(data);
172             break;
173         default:
174             break;
175     }
176 }
177 
HandleStateChangeEvent(int64_t data)178 void FastAudioStream::HandleStateChangeEvent(int64_t data)
179 {
180     State state = INVALID;
181     StateChangeCmdType cmdType = CMD_FROM_CLIENT;
182     ParamsToStateCmdType(data, state, cmdType);
183     std::unique_lock<std::mutex> lock(streamCbMutex_);
184     std::shared_ptr<AudioStreamCallback> streamCb = streamCallback_.lock();
185     if (streamCb != nullptr) {
186         state = state != STOPPING ? state : STOPPED; // client only need STOPPED
187         streamCb->OnStateChange(state, cmdType);
188     }
189 }
190 
ParamsToStateCmdType(int64_t params,State & state,StateChangeCmdType & cmdType)191 int32_t FastAudioStream::ParamsToStateCmdType(int64_t params, State &state, StateChangeCmdType &cmdType)
192 {
193     cmdType = CMD_FROM_CLIENT;
194     switch (params) {
195         case HANDLER_PARAM_NEW:
196             state = NEW;
197             break;
198         case HANDLER_PARAM_PREPARED:
199             state = PREPARED;
200             break;
201         case HANDLER_PARAM_RUNNING:
202             state = RUNNING;
203             break;
204         case HANDLER_PARAM_STOPPED:
205             state = STOPPED;
206             break;
207         case HANDLER_PARAM_RELEASED:
208             state = RELEASED;
209             break;
210         case HANDLER_PARAM_PAUSED:
211             state = PAUSED;
212             break;
213         case HANDLER_PARAM_STOPPING:
214             state = STOPPING;
215             break;
216         case HANDLER_PARAM_RUNNING_FROM_SYSTEM:
217             state = RUNNING;
218             cmdType = CMD_FROM_SYSTEM;
219             break;
220         case HANDLER_PARAM_PAUSED_FROM_SYSTEM:
221             state = PAUSED;
222             cmdType = CMD_FROM_SYSTEM;
223             break;
224         default:
225             state = INVALID;
226             break;
227     }
228     return SUCCESS;
229 }
230 
GetAudioStreamInfo(AudioStreamParams & audioStreamInfo)231 int32_t FastAudioStream::GetAudioStreamInfo(AudioStreamParams &audioStreamInfo)
232 {
233     AUDIO_INFO_LOG("GetAudioStreamInfo enter.");
234     audioStreamInfo = streamInfo_;
235     return SUCCESS;
236 }
237 
GetAudioSessionID(uint32_t & sessionID)238 int32_t FastAudioStream::GetAudioSessionID(uint32_t &sessionID)
239 {
240     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED,
241         "GetAudioSessionID failed: null process");
242     int32_t ret = processClient_->GetSessionID(sessionID);
243     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetSessionID error.");
244     return ret;
245 }
246 
GetAudioPipeType(AudioPipeType & pipeType)247 void FastAudioStream::GetAudioPipeType(AudioPipeType &pipeType)
248 {
249     pipeType = eMode_ == AUDIO_MODE_PLAYBACK ? rendererInfo_.pipeType : capturerInfo_.pipeType;
250 }
251 
GetState()252 State FastAudioStream::GetState()
253 {
254     std::lock_guard lock(switchingMutex_);
255     if (switchingInfo_.isSwitching_) {
256         AUDIO_INFO_LOG("switching, return state in switchingInfo");
257         return switchingInfo_.state_;
258     }
259     return state_;
260 }
261 
GetAudioTime(Timestamp & timestamp,Timestamp::Timestampbase base)262 bool FastAudioStream::GetAudioTime(Timestamp &timestamp, Timestamp::Timestampbase base)
263 {
264     CHECK_AND_RETURN_RET_LOG(base == Timestamp::MONOTONIC, false, "GetAudioTime failed: invalid base");
265 
266     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "GetAudioTime failed: null process");
267     int64_t timeSec = 0;
268     int64_t timeNsec = 0;
269     bool ret = processClient_->GetAudioTime(timestamp.framePosition, timeSec, timeNsec);
270     CHECK_AND_RETURN_RET_LOG(ret, false, "GetBufferSize error.");
271     timestamp.time.tv_sec = timeSec;
272     timestamp.time.tv_nsec = timeNsec;
273     return true;
274 }
275 
GetAudioPosition(Timestamp & timestamp,Timestamp::Timestampbase base)276 bool FastAudioStream::GetAudioPosition(Timestamp &timestamp, Timestamp::Timestampbase base)
277 {
278     return GetAudioTime(timestamp, base);
279 }
280 
GetBufferSize(size_t & bufferSize)281 int32_t FastAudioStream::GetBufferSize(size_t &bufferSize)
282 {
283     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "GetBufferSize failed: null process");
284     int32_t ret = processClient_->GetBufferSize(bufferSize);
285     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetBufferSize error.");
286     return ret;
287 }
288 
GetFrameCount(uint32_t & frameCount)289 int32_t FastAudioStream::GetFrameCount(uint32_t &frameCount)
290 {
291     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "GetFrameCount failed: null process");
292     int32_t ret = processClient_->GetFrameCount(frameCount);
293     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetFrameCount error.");
294     return ret;
295 }
296 
GetLatency(uint64_t & latency)297 int32_t FastAudioStream::GetLatency(uint64_t &latency)
298 {
299     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "GetLatency failed: null process");
300     int32_t ret = processClient_->GetLatency(latency);
301     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetLatency error.");
302     return ret;
303 }
304 
SetAudioStreamType(AudioStreamType audioStreamType)305 int32_t FastAudioStream::SetAudioStreamType(AudioStreamType audioStreamType)
306 {
307     // Stream type can only be set when create.
308     AUDIO_ERR_LOG("Unsupported operation: SetAudioStreamType");
309     return ERR_INVALID_OPERATION;
310 }
311 
SetVolume(float volume)312 int32_t FastAudioStream::SetVolume(float volume)
313 {
314     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "SetVolume failed: null process");
315     int32_t ret = SUCCESS;
316     ret = processClient_->SetVolume(volume);
317     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetVolume error.");
318     return ret;
319 }
320 
GetVolume()321 float FastAudioStream::GetVolume()
322 {
323     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, 1.0f, "SetVolume failed: null process"); // 1.0f for default
324     return processClient_->GetVolume();
325 }
326 
SetMute(bool mute)327 int32_t FastAudioStream::SetMute(bool mute)
328 {
329     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "SetMute failed: null process");
330     int32_t ret = processClient_->SetMute(mute);
331     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetMute error.");
332     return ret;
333 }
334 
SetSourceDuration(int64_t duration)335 int32_t FastAudioStream::SetSourceDuration(int64_t duration)
336 {
337     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "SetMute failed: null process");
338     int32_t ret = processClient_->SetSourceDuration(duration);
339     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetSourceDuration error.");
340     return ret;
341 }
342 
SetDuckVolume(float volume)343 int32_t FastAudioStream::SetDuckVolume(float volume)
344 {
345     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "SetDuckVolume failed: null process");
346     int32_t ret = processClient_->SetDuckVolume(volume);
347     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetDuckVolume error.");
348     return ret;
349 }
350 
SetSilentModeAndMixWithOthers(bool on)351 void FastAudioStream::SetSilentModeAndMixWithOthers(bool on)
352 {
353     AUDIO_PRERELEASE_LOGI("%{public}d", on);
354     silentModeAndMixWithOthers_ = on;
355     CHECK_AND_RETURN_LOG(processClient_ != nullptr, "processClient is null.");
356     processClient_->SetSilentModeAndMixWithOthers(on);
357 }
358 
GetSilentModeAndMixWithOthers()359 bool FastAudioStream::GetSilentModeAndMixWithOthers()
360 {
361     return silentModeAndMixWithOthers_;
362 }
363 
SetRenderRate(AudioRendererRate renderRate)364 int32_t FastAudioStream::SetRenderRate(AudioRendererRate renderRate)
365 {
366     CHECK_AND_RETURN_RET(RENDER_RATE_NORMAL != renderRate, SUCCESS);
367     AUDIO_ERR_LOG("Unsupported operation: SetRenderRate");
368     return ERR_INVALID_OPERATION;
369 }
370 
GetRenderRate()371 AudioRendererRate FastAudioStream::GetRenderRate()
372 {
373     return renderRate_;
374 }
375 
SetStreamCallback(const std::shared_ptr<AudioStreamCallback> & callback)376 int32_t FastAudioStream::SetStreamCallback(const std::shared_ptr<AudioStreamCallback> &callback)
377 {
378     AUDIO_INFO_LOG("SetStreamCallback enter.");
379 
380     if (callback == nullptr) {
381         AUDIO_ERR_LOG("SetStreamCallback failed. callback == nullptr");
382         return ERR_INVALID_PARAM;
383     }
384 
385     std::unique_lock<std::mutex> lock(streamCbMutex_);
386     streamCallback_ = callback;
387     lock.unlock();
388 
389     if (state_ != PREPARED) {
390         return SUCCESS;
391     }
392     SafeSendCallbackEvent(STATE_CHANGE_EVENT, PREPARED);
393     return SUCCESS;
394 }
395 
SetRenderMode(AudioRenderMode renderMode)396 int32_t FastAudioStream::SetRenderMode(AudioRenderMode renderMode)
397 {
398     CHECK_AND_RETURN_RET_LOG(renderMode == RENDER_MODE_CALLBACK && eMode_ == AUDIO_MODE_PLAYBACK,
399         ERR_INVALID_OPERATION, "SetRenderMode is not supported.");
400     return SUCCESS;
401 }
402 
GetRenderMode()403 AudioRenderMode FastAudioStream::GetRenderMode()
404 {
405     AUDIO_INFO_LOG("GetRenderMode enter.");
406     return renderMode_;
407 }
408 
SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> & callback)409 int32_t FastAudioStream::SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback)
410 {
411     AUDIO_INFO_LOG("SetRendererWriteCallback enter.");
412     CHECK_AND_RETURN_RET_LOG(callback && processClient_ && eMode_ == AUDIO_MODE_PLAYBACK,
413         ERR_INVALID_PARAM, "callback is nullptr");
414     spkProcClientCb_ = std::make_shared<FastAudioStreamRenderCallback>(callback, *this);
415     int32_t ret = processClient_->SaveDataCallback(spkProcClientCb_);
416     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Client test save data callback fail, ret %{public}d.", ret);
417     return SUCCESS;
418 }
419 
SetRendererFirstFrameWritingCallback(const std::shared_ptr<AudioRendererFirstFrameWritingCallback> & callback)420 int32_t FastAudioStream::SetRendererFirstFrameWritingCallback(
421     const std::shared_ptr<AudioRendererFirstFrameWritingCallback> &callback)
422 {
423     AUDIO_INFO_LOG("SetRendererFirstFrameWritingCallback in.");
424     CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
425     firstFrameWritingCb_ = callback;
426     return SUCCESS;
427 }
428 
SetCaptureMode(AudioCaptureMode captureMode)429 int32_t FastAudioStream::SetCaptureMode(AudioCaptureMode captureMode)
430 {
431     CHECK_AND_RETURN_RET_LOG(captureMode == CAPTURE_MODE_CALLBACK && eMode_ == AUDIO_MODE_RECORD,
432         ERR_INVALID_OPERATION, "SetCaptureMode is not supported.");
433     return SUCCESS;
434 }
435 
GetCaptureMode()436 AudioCaptureMode FastAudioStream::GetCaptureMode()
437 {
438     return captureMode_;
439 }
440 
SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> & callback)441 int32_t FastAudioStream::SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> &callback)
442 {
443     AUDIO_INFO_LOG("SetCapturerReadCallback enter.");
444     CHECK_AND_RETURN_RET_LOG(callback && processClient_ && eMode_ == AUDIO_MODE_RECORD,
445         ERR_INVALID_PARAM, "callback or client is nullptr or mode is not record.");
446     micProcClientCb_ = std::make_shared<FastAudioStreamCaptureCallback>(callback);
447     int32_t ret = processClient_->SaveDataCallback(micProcClientCb_);
448     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Client save data callback fail, ret %{public}d.", ret);
449     return SUCCESS;
450 }
451 
GetBufferDesc(BufferDesc & bufDesc)452 int32_t FastAudioStream::GetBufferDesc(BufferDesc &bufDesc)
453 {
454     AUDIO_DEBUG_LOG("GetBufferDesc enter.");
455     CHECK_AND_RETURN_RET_LOG(processClient_, ERR_INVALID_OPERATION, "spkClient is null.");
456     int32_t ret = processClient_->GetBufferDesc(bufDesc);
457     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS && bufDesc.buffer != nullptr && bufDesc.bufLength != 0,
458         -1, "GetBufferDesc failed.");
459     return SUCCESS;
460 }
461 
GetBufQueueState(BufferQueueState & bufState)462 int32_t FastAudioStream::GetBufQueueState(BufferQueueState &bufState)
463 {
464     AUDIO_INFO_LOG("GetBufQueueState enter.");
465     // note: add support
466     return SUCCESS;
467 }
468 
Enqueue(const BufferDesc & bufDesc)469 int32_t FastAudioStream::Enqueue(const BufferDesc &bufDesc)
470 {
471     AUDIO_DEBUG_LOG("Enqueue enter.");
472     CHECK_AND_RETURN_RET_LOG(processClient_, ERR_INVALID_OPERATION,
473         "spkClient is null.");
474     int32_t ret = processClient_->Enqueue(bufDesc);
475     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, -1, "Enqueue failed.");
476     return SUCCESS;
477 }
478 
SetPreferredFrameSize(int32_t frameSize)479 void FastAudioStream::SetPreferredFrameSize(int32_t frameSize)
480 {
481     std::lock_guard<std::mutex> lockSetPreferredFrameSize(setPreferredFrameSizeMutex_);
482     userSettedPreferredFrameSize_ = frameSize;
483     CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
484     processClient_->SetPreferredFrameSize(frameSize);
485 }
486 
UpdateLatencyTimestamp(std::string & timestamp,bool isRenderer)487 void FastAudioStream::UpdateLatencyTimestamp(std::string &timestamp, bool isRenderer)
488 {
489     CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
490     processClient_->UpdateLatencyTimestamp(timestamp, isRenderer);
491 }
492 
Clear()493 int32_t FastAudioStream::Clear()
494 {
495     AUDIO_INFO_LOG("Clear will do nothing.");
496 
497     return SUCCESS;
498 }
499 
SetLowPowerVolume(float volume)500 int32_t FastAudioStream::SetLowPowerVolume(float volume)
501 {
502     AUDIO_INFO_LOG("SetLowPowerVolume enter.");
503     return SUCCESS;
504 }
505 
GetLowPowerVolume()506 float FastAudioStream::GetLowPowerVolume()
507 {
508     AUDIO_INFO_LOG("GetLowPowerVolume enter.");
509     return 1.0f;
510 }
511 
SetOffloadMode(int32_t state,bool isAppBack)512 int32_t FastAudioStream::SetOffloadMode(int32_t state, bool isAppBack)
513 {
514     AUDIO_WARNING_LOG("SetOffloadMode enter.");
515     return ERR_NOT_SUPPORTED;
516 }
517 
UnsetOffloadMode()518 int32_t FastAudioStream::UnsetOffloadMode()
519 {
520     AUDIO_WARNING_LOG("UnsetOffloadMode enter.");
521     return ERR_NOT_SUPPORTED;
522 }
523 
GetSingleStreamVolume()524 float FastAudioStream::GetSingleStreamVolume()
525 {
526     AUDIO_INFO_LOG("GetSingleStreamVolume enter.");
527     return 1.0f;
528 }
529 
GetAudioEffectMode()530 AudioEffectMode FastAudioStream::GetAudioEffectMode()
531 {
532     AUDIO_ERR_LOG("GetAudioEffectMode not supported");
533     return EFFECT_NONE;
534 }
535 
SetAudioEffectMode(AudioEffectMode effectMode)536 int32_t FastAudioStream::SetAudioEffectMode(AudioEffectMode effectMode)
537 {
538     AUDIO_ERR_LOG("SetAudioEffectMode not supported");
539     return ERR_NOT_SUPPORTED;
540 }
541 
GetFramesWritten()542 int64_t FastAudioStream::GetFramesWritten()
543 {
544     int64_t result = -1; // -1 invalid frame
545     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, result, "GetFramesWritten failed: null process");
546     result = processClient_->GetFramesWritten();
547     return result;
548 }
549 
GetFramesRead()550 int64_t FastAudioStream::GetFramesRead()
551 {
552     int64_t result = -1; // -1 invalid frame
553     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, result, "GetFramesRead failed: null process");
554     result = processClient_->GetFramesRead();
555     return result;
556 }
557 
SetSpeed(float speed)558 int32_t FastAudioStream::SetSpeed(float speed)
559 {
560     AUDIO_ERR_LOG("SetSpeed is not supported");
561     return ERR_OPERATION_FAILED;
562 }
563 
GetSpeed()564 float FastAudioStream::GetSpeed()
565 {
566     AUDIO_ERR_LOG("GetSpeed is not supported");
567     return static_cast<float>(ERROR);
568 }
569 
ChangeSpeed(uint8_t * buffer,int32_t bufferSize,std::unique_ptr<uint8_t[]> & outBuffer,int32_t & outBufferSize)570 int32_t FastAudioStream::ChangeSpeed(uint8_t *buffer, int32_t bufferSize,
571     std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize)
572 {
573     AUDIO_ERR_LOG("ChangeSpeed is not supported");
574     return ERR_OPERATION_FAILED;
575 }
576 
StartAudioStream(StateChangeCmdType cmdType,AudioStreamDeviceChangeReasonExt reason)577 bool FastAudioStream::StartAudioStream(StateChangeCmdType cmdType,
578     AudioStreamDeviceChangeReasonExt reason)
579 {
580     AUDIO_PRERELEASE_LOGI("StartAudioStream enter.");
581     CHECK_AND_RETURN_RET_LOG((state_ == PREPARED) || (state_ == STOPPED) || (state_ == PAUSED),
582         false, "Illegal state:%{public}u", state_);
583 
584     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Start failed, process is null.");
585     if (spkProcClientCb_ != nullptr) {
586         AUDIO_DEBUG_LOG("StartAudioStream: reset the first frame state before starting");
587         spkProcClientCb_->ResetFirstFrameState();
588     }
589     if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
590         audioStreamTracker_->FetchOutputDeviceForTrack(sessionId_, RUNNING, clientPid_, rendererInfo_, reason);
591         audioStreamTracker_->FetchInputDeviceForTrack(sessionId_, RUNNING, clientPid_, capturerInfo_);
592     }
593     int32_t ret = ERROR;
594     if (state_ == PAUSED || state_ == STOPPED) {
595         ret = processClient_->Resume();
596     } else {
597         ret = processClient_->Start();
598     }
599     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "Client test stop fail, ret %{public}d.", ret);
600     state_ = RUNNING;
601 
602     AUDIO_DEBUG_LOG("StartAudioStream SUCCESS, sessionId: %{public}d", sessionId_);
603 
604     if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
605         AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for Running");
606         audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
607     }
608 
609     SafeSendCallbackEvent(STATE_CHANGE_EVENT, state_);
610     return true;
611 }
612 
PauseAudioStream(StateChangeCmdType cmdType)613 bool FastAudioStream::PauseAudioStream(StateChangeCmdType cmdType)
614 {
615     AUDIO_PRERELEASE_LOGI("PauseAudioStream enter.");
616     CHECK_AND_RETURN_RET_LOG(state_ == RUNNING, false,
617         "state is not RUNNING. Illegal state:%{public}u", state_);
618     State oldState = state_;
619 
620     state_ = PAUSED;
621     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Pause failed, process is null.");
622     int32_t ret = processClient_->Pause();
623     if (ret != SUCCESS) {
624         AUDIO_ERR_LOG("StreamPause fail,ret:%{public}d", ret);
625         state_ = oldState;
626         return false;
627     }
628 
629     AUDIO_DEBUG_LOG("PauseAudioStream SUCCESS, sessionId: %{public}d", sessionId_);
630     if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
631         AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for Pause");
632         audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
633     }
634 
635     SafeSendCallbackEvent(STATE_CHANGE_EVENT, state_);
636     return true;
637 }
638 
StopAudioStream()639 bool FastAudioStream::StopAudioStream()
640 {
641     CHECK_AND_RETURN_RET_LOG((state_ == RUNNING) || (state_ == PAUSED), false,
642         "State is not RUNNING. Illegal state:%{public}u", state_);
643     State oldState = state_;
644     state_ = STOPPED; // Set it before stopping as Read/Write and Stop can be called from different threads
645 
646     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Stop failed, process is null.");
647     int32_t ret = processClient_->Stop();
648     if (ret != SUCCESS) {
649         AUDIO_ERR_LOG("StreamStop fail,ret:%{public}d", ret);
650         state_ = oldState;
651         return false;
652     }
653 
654     AUDIO_INFO_LOG("StopAudioStream SUCCESS, sessionId: %{public}d", sessionId_);
655     if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
656         AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for stop");
657         audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
658     }
659     return true;
660 }
661 
FlushAudioStream()662 bool FastAudioStream::FlushAudioStream()
663 {
664     AUDIO_PRERELEASE_LOGI("FlushAudioStream enter.");
665     return true;
666 }
667 
DrainAudioStream(bool stopFlag)668 bool FastAudioStream::DrainAudioStream(bool stopFlag)
669 {
670     AUDIO_INFO_LOG("Drain stream SUCCESS");
671     return true;
672 }
673 
ReleaseAudioStream(bool releaseRunner,bool isSwitchStream)674 bool FastAudioStream::ReleaseAudioStream(bool releaseRunner, bool isSwitchStream)
675 {
676     CHECK_AND_RETURN_RET_LOG(state_ != RELEASED && state_ != NEW,
677         false, "Illegal state: state = %{public}u", state_);
678     // If state_ is RUNNING try to Stop it first and Release
679     if (state_ == RUNNING) {
680         StopAudioStream();
681     }
682 
683     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Release failed, process is null.");
684     processClient_->Release(isSwitchStream);
685     state_ = RELEASED;
686     AUDIO_INFO_LOG("ReleaseAudiostream SUCCESS, sessionId: %{public}d", sessionId_);
687     if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
688         AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for release");
689         audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
690     }
691 
692     std::unique_lock<std::mutex> lock(streamCbMutex_);
693     std::shared_ptr<AudioStreamCallback> streamCb = streamCallback_.lock();
694     if (streamCb != nullptr) {
695         AUDIO_INFO_LOG("Notify client the state is released");
696         streamCb->OnStateChange(RELEASED, CMD_FROM_CLIENT);
697     }
698     lock.unlock();
699     return true;
700 }
701 
Read(uint8_t & buffer,size_t userSize,bool isBlockingRead)702 int32_t FastAudioStream::Read(uint8_t &buffer, size_t userSize, bool isBlockingRead)
703 {
704     AUDIO_ERR_LOG("Unsupported operation: read");
705     return ERR_INVALID_OPERATION;
706 }
707 
Write(uint8_t * buffer,size_t buffer_size)708 int32_t FastAudioStream::Write(uint8_t *buffer, size_t buffer_size)
709 {
710     AUDIO_ERR_LOG("Unsupported operation: Write");
711     return ERR_INVALID_OPERATION;
712 }
713 
Write(uint8_t * pcmBuffer,size_t pcmBufferSize,uint8_t * metaBuffer,size_t metaBufferSize)714 int32_t FastAudioStream::Write(uint8_t *pcmBuffer, size_t pcmBufferSize, uint8_t *metaBuffer, size_t metaBufferSize)
715 {
716     AUDIO_ERR_LOG("Unsupported operation: Write");
717     return ERR_INVALID_OPERATION;
718 }
719 
GetUnderflowCount()720 uint32_t FastAudioStream::GetUnderflowCount()
721 {
722     AUDIO_INFO_LOG("GetUnderflowCount enter.");
723     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, 0, "process client is null.");
724     underflowCount_ = processClient_->GetUnderflowCount();
725     return underflowCount_;
726 }
727 
GetOverflowCount()728 uint32_t FastAudioStream::GetOverflowCount()
729 {
730     AUDIO_INFO_LOG("enter.");
731     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, 0, "process client is null.");
732     overflowCount_ = processClient_->GetOverflowCount();
733     return overflowCount_;
734 }
735 
SetUnderflowCount(uint32_t underflowCount)736 void FastAudioStream::SetUnderflowCount(uint32_t underflowCount)
737 {
738     CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
739     processClient_->SetUnderflowCount(underflowCount);
740 }
741 
SetOverflowCount(uint32_t overflowCount)742 void FastAudioStream::SetOverflowCount(uint32_t overflowCount)
743 {
744     CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
745     processClient_->SetOverflowCount(overflowCount);
746 }
747 
SetRendererPositionCallback(int64_t markPosition,const std::shared_ptr<RendererPositionCallback> & callback)748 void FastAudioStream::SetRendererPositionCallback(int64_t markPosition,
749     const std::shared_ptr<RendererPositionCallback> &callback)
750 {
751     AUDIO_INFO_LOG("Registering render frame position callback mark position");
752     // note: need support
753 }
754 
UnsetRendererPositionCallback()755 void FastAudioStream::UnsetRendererPositionCallback()
756 {
757     AUDIO_INFO_LOG("Unregistering render frame position callback");
758     // note: need support
759 }
760 
SetRendererPeriodPositionCallback(int64_t periodPosition,const std::shared_ptr<RendererPeriodPositionCallback> & callback)761 void FastAudioStream::SetRendererPeriodPositionCallback(int64_t periodPosition,
762     const std::shared_ptr<RendererPeriodPositionCallback> &callback)
763 {
764     AUDIO_INFO_LOG("Registering render period position callback");
765 }
766 
UnsetRendererPeriodPositionCallback()767 void FastAudioStream::UnsetRendererPeriodPositionCallback()
768 {
769     AUDIO_INFO_LOG("Unregistering render period position callback");
770 }
771 
SetCapturerPositionCallback(int64_t markPosition,const std::shared_ptr<CapturerPositionCallback> & callback)772 void FastAudioStream::SetCapturerPositionCallback(int64_t markPosition,
773     const std::shared_ptr<CapturerPositionCallback> &callback)
774 {
775     AUDIO_INFO_LOG("Registering capture frame position callback, mark position");
776 }
777 
UnsetCapturerPositionCallback()778 void FastAudioStream::UnsetCapturerPositionCallback()
779 {
780     AUDIO_INFO_LOG("Unregistering capture frame position callback");
781 }
782 
SetCapturerPeriodPositionCallback(int64_t periodPosition,const std::shared_ptr<CapturerPeriodPositionCallback> & callback)783 void FastAudioStream::SetCapturerPeriodPositionCallback(int64_t periodPosition,
784     const std::shared_ptr<CapturerPeriodPositionCallback> &callback)
785 {
786     AUDIO_INFO_LOG("Registering period position callback");
787 }
788 
UnsetCapturerPeriodPositionCallback()789 void FastAudioStream::UnsetCapturerPeriodPositionCallback()
790 {
791     AUDIO_INFO_LOG("Unregistering period position callback");
792 }
793 
SetRendererSamplingRate(uint32_t sampleRate)794 int32_t FastAudioStream::SetRendererSamplingRate(uint32_t sampleRate)
795 {
796     AUDIO_ERR_LOG("SetRendererSamplingRate  is not supported");
797 
798     return ERR_OPERATION_FAILED;
799 }
800 
GetRendererSamplingRate()801 uint32_t FastAudioStream::GetRendererSamplingRate()
802 {
803     AUDIO_INFO_LOG("GetRendererSamplingRate enter.");
804     return streamInfo_.samplingRate;
805 }
806 
SetBufferSizeInMsec(int32_t bufferSizeInMsec)807 int32_t FastAudioStream::SetBufferSizeInMsec(int32_t bufferSizeInMsec)
808 {
809     AUDIO_ERR_LOG("SetBufferSizeInMsec is not supported");
810     // note: add support
811     return ERR_NOT_SUPPORTED;
812 }
813 
SetInnerCapturerState(bool isInnerCapturer)814 void FastAudioStream::SetInnerCapturerState(bool isInnerCapturer)
815 {
816     AUDIO_ERR_LOG("SetInnerCapturerState is not supported");
817 }
818 
SetWakeupCapturerState(bool isWakeupCapturer)819 void FastAudioStream::SetWakeupCapturerState(bool isWakeupCapturer)
820 {
821     AUDIO_ERR_LOG("SetWakeupCapturerState is not supported");
822 }
823 
SetCapturerSource(int capturerSource)824 void FastAudioStream::SetCapturerSource(int capturerSource)
825 {
826     AUDIO_ERR_LOG("SetCapturerSource is not supported");
827 }
828 
SetPrivacyType(AudioPrivacyType privacyType)829 void FastAudioStream::SetPrivacyType(AudioPrivacyType privacyType)
830 {
831     AUDIO_ERR_LOG("SetPrivacyType is not supported");
832 }
833 
GetStreamClass()834 IAudioStream::StreamClass FastAudioStream::GetStreamClass()
835 {
836     return IAudioStream::StreamClass::FAST_STREAM;
837 }
838 
SetStreamTrackerState(bool trackerRegisteredState)839 void FastAudioStream::SetStreamTrackerState(bool trackerRegisteredState)
840 {
841     streamTrackerRegistered_ = trackerRegisteredState;
842 }
843 
GetSwitchInfo(IAudioStream::SwitchInfo & info)844 void FastAudioStream::GetSwitchInfo(IAudioStream::SwitchInfo& info)
845 {
846     GetAudioStreamInfo(info.params);
847     info.rendererInfo = rendererInfo_;
848     info.capturerInfo = capturerInfo_;
849     info.eStreamType = eStreamType_;
850     info.state = state_;
851     info.sessionId = sessionId_;
852 
853     info.clientPid = clientPid_;
854     info.clientUid = clientUid_;
855 
856     info.volume = GetVolume();
857     info.effectMode = GetAudioEffectMode();
858     info.renderMode = renderMode_;
859     info.captureMode = captureMode_;
860     info.renderRate = renderRate_;
861 
862     info.underFlowCount = GetUnderflowCount();
863     info.overFlowCount = GetOverflowCount();
864 
865     info.silentModeAndMixWithOthers = silentModeAndMixWithOthers_;
866     info.defaultOutputDevice = defaultOutputDevice_;
867 
868     {
869         std::lock_guard<std::mutex> lock(setPreferredFrameSizeMutex_);
870         info.userSettedPreferredFrameSize = userSettedPreferredFrameSize_;
871     }
872 
873     if (spkProcClientCb_) {
874         info.rendererWriteCallback = spkProcClientCb_->GetRendererWriteCallback();
875     }
876     if (micProcClientCb_) {
877         info.capturerReadCallback = micProcClientCb_->GetCapturerReadCallback();
878     }
879     if (firstFrameWritingCb_) {
880         info.rendererFirstFrameWritingCallback = firstFrameWritingCb_;
881     }
882 }
883 
OnFirstFrameWriting()884 void FastAudioStream::OnFirstFrameWriting()
885 {
886     CHECK_AND_RETURN_LOG(firstFrameWritingCb_!= nullptr, "firstFrameWritingCb_ is null.");
887     uint64_t latency = 0;
888     this->GetLatency(latency);
889     firstFrameWritingCb_->OnFirstFrameWriting(latency);
890 }
891 
OnHandleData(size_t length)892 void FastAudioStreamRenderCallback::OnHandleData(size_t length)
893 {
894     CHECK_AND_RETURN_LOG(rendererWriteCallback_!= nullptr, "OnHandleData failed: rendererWriteCallback_ is null.");
895     if (!hasFirstFrameWrited_) {
896         AUDIO_DEBUG_LOG("OnHandleData: send the first frame writing event to audio haptic player");
897         audioStreamImpl_.OnFirstFrameWriting();
898         hasFirstFrameWrited_ = true;
899     }
900     rendererWriteCallback_->OnWriteData(length);
901 }
902 
ResetFirstFrameState()903 void FastAudioStreamRenderCallback::ResetFirstFrameState()
904 {
905     AUDIO_DEBUG_LOG("ResetFirstFrameState: set the hasFirstFrameWrited_ to false");
906     hasFirstFrameWrited_ = false;
907 }
908 
GetRendererWriteCallback() const909 std::shared_ptr<AudioRendererWriteCallback> FastAudioStreamRenderCallback::GetRendererWriteCallback() const
910 {
911     return rendererWriteCallback_;
912 }
913 
GetCapturerReadCallback() const914 std::shared_ptr<AudioCapturerReadCallback> FastAudioStreamCaptureCallback::GetCapturerReadCallback() const
915 {
916     return captureCallback_;
917 }
918 
OnHandleData(size_t length)919 void FastAudioStreamCaptureCallback::OnHandleData(size_t length)
920 {
921     CHECK_AND_RETURN_LOG(captureCallback_!= nullptr, "OnHandleData failed: captureCallback_ is null.");
922     captureCallback_->OnReadData(length);
923 }
924 
SetChannelBlendMode(ChannelBlendMode blendMode)925 int32_t FastAudioStream::SetChannelBlendMode(ChannelBlendMode blendMode)
926 {
927     AUDIO_ERR_LOG("SetChannelBlendMode is not supported");
928     return SUCCESS;
929 }
930 
SetVolumeWithRamp(float volume,int32_t duration)931 int32_t FastAudioStream::SetVolumeWithRamp(float volume, int32_t duration)
932 {
933     AUDIO_ERR_LOG("SetVolumeWithRamp is not supported");
934     return SUCCESS;
935 }
936 
UpdateRegisterTrackerInfo(AudioRegisterTrackerInfo & registerTrackerInfo)937 void FastAudioStream::UpdateRegisterTrackerInfo(AudioRegisterTrackerInfo &registerTrackerInfo)
938 {
939     rendererInfo_.samplingRate = static_cast<AudioSamplingRate>(streamInfo_.samplingRate);
940     capturerInfo_.samplingRate = static_cast<AudioSamplingRate>(streamInfo_.samplingRate);
941 
942     registerTrackerInfo.sessionId = sessionId_;
943     registerTrackerInfo.clientPid = clientPid_;
944     registerTrackerInfo.state = state_;
945     registerTrackerInfo.rendererInfo = rendererInfo_;
946     registerTrackerInfo.capturerInfo = capturerInfo_;
947 }
948 
RestoreAudioStream(bool needStoreState)949 bool FastAudioStream::RestoreAudioStream(bool needStoreState)
950 {
951     CHECK_AND_RETURN_RET_LOG(proxyObj_ != nullptr, false, "proxyObj_ is null");
952     CHECK_AND_RETURN_RET_LOG(state_ != NEW && state_ != INVALID && state_ != RELEASED, true,
953         "state_ is %{public}d, no need for restore", state_);
954     bool result = false;
955     State oldState = state_;
956     state_ = NEW;
957     SetStreamTrackerState(false);
958     if (processClient_ != nullptr) {
959         processClient_->Stop();
960         processClient_->Release();
961         processClient_ = nullptr;
962     }
963     if (SetAudioStreamInfo(streamInfo_, proxyObj_) != SUCCESS || SetCallbacksWhenRestore() != SUCCESS) {
964         goto error;
965     }
966 
967     SetDefaultOutputDevice(defaultOutputDevice_);
968 
969     switch (oldState) {
970         case RUNNING:
971             result = StartAudioStream();
972             break;
973         case PAUSED:
974             result = StartAudioStream() && PauseAudioStream();
975             break;
976         case STOPPED:
977             [[fallthrough]];
978         case STOPPING:
979             result = StartAudioStream() && StopAudioStream();
980             break;
981         default:
982             break;
983     }
984     if (!result) {
985         goto error;
986     }
987     return result;
988 error:
989     AUDIO_ERR_LOG("RestoreAudioStream failed");
990     state_ = oldState;
991     return false;
992 }
993 
GetOffloadEnable()994 bool FastAudioStream::GetOffloadEnable()
995 {
996     AUDIO_WARNING_LOG("not supported in fast audio stream");
997     return false;
998 }
999 
GetSpatializationEnabled()1000 bool FastAudioStream::GetSpatializationEnabled()
1001 {
1002     AUDIO_WARNING_LOG("not supported in fast audio stream");
1003     return false;
1004 }
1005 
GetHighResolutionEnabled()1006 bool FastAudioStream::GetHighResolutionEnabled()
1007 {
1008     AUDIO_WARNING_LOG("not supported in fast audio stream");
1009     return false;
1010 }
1011 
SetDefaultOutputDevice(const DeviceType defaultOutputDevice)1012 int32_t FastAudioStream::SetDefaultOutputDevice(const DeviceType defaultOutputDevice)
1013 {
1014     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "set failed: null process");
1015     int32_t ret = processClient_->SetDefaultOutputDevice(defaultOutputDevice);
1016     CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetDefaultOutputDevice error.");
1017     defaultOutputDevice_ = defaultOutputDevice;
1018     return SUCCESS;
1019 }
1020 
GetDefaultOutputDevice()1021 DeviceType FastAudioStream::GetDefaultOutputDevice()
1022 {
1023     return defaultOutputDevice_;
1024 }
1025 
1026 // diffrence from GetAudioPosition only when set speed
GetAudioTimestampInfo(Timestamp & timestamp,Timestamp::Timestampbase base)1027 int32_t FastAudioStream::GetAudioTimestampInfo(Timestamp &timestamp, Timestamp::Timestampbase base)
1028 {
1029     return GetAudioTime(timestamp, base);
1030 }
1031 
SetSwitchingStatus(bool isSwitching)1032 void FastAudioStream::SetSwitchingStatus(bool isSwitching)
1033 {
1034     std::lock_guard lock(switchingMutex_);
1035     if (isSwitching) {
1036         switchingInfo_ = {true, state_};
1037     } else {
1038         switchingInfo_ = {false, INVALID};
1039     }
1040 }
1041 
SetCallbacksWhenRestore()1042 int32_t FastAudioStream::SetCallbacksWhenRestore()
1043 {
1044     int32_t ret = SUCCESS;
1045     CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERROR_INVALID_PARAM, "processClient_ is null");
1046     if (eMode_ == AUDIO_MODE_PLAYBACK) {
1047         ret = processClient_->SaveDataCallback(spkProcClientCb_);
1048     } else if (eMode_ == AUDIO_MODE_RECORD) {
1049         ret = processClient_->SaveDataCallback(micProcClientCb_);
1050     }
1051     return ret;
1052 }
1053 
GetRestoreInfo(RestoreInfo & restoreInfo)1054 void FastAudioStream::GetRestoreInfo(RestoreInfo &restoreInfo)
1055 {
1056     processClient_->GetRestoreInfo(restoreInfo);
1057     return;
1058 }
1059 
SetRestoreInfo(RestoreInfo & restoreInfo)1060 void FastAudioStream::SetRestoreInfo(RestoreInfo &restoreInfo)
1061 {
1062     processClient_->SetRestoreInfo(restoreInfo);
1063     return;
1064 }
1065 
CheckRestoreStatus()1066 RestoreStatus FastAudioStream::CheckRestoreStatus()
1067 {
1068     return processClient_->CheckRestoreStatus();
1069 }
1070 
SetRestoreStatus(RestoreStatus restoreStatus)1071 RestoreStatus FastAudioStream::SetRestoreStatus(RestoreStatus restoreStatus)
1072 {
1073     return processClient_->SetRestoreStatus(restoreStatus);
1074 }
1075 
FetchDeviceForSplitStream()1076 void FastAudioStream::FetchDeviceForSplitStream()
1077 {
1078     AUDIO_WARNING_LOG("Fast stream does not support split stream");
1079     if (processClient_) {
1080         processClient_->SetRestoreStatus(NO_NEED_FOR_RESTORE);
1081     }
1082 }
1083 } // namespace AudioStandard
1084 } // namespace OHOS
1085