1 /*
2 * Copyright (c) 2023 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 #include "audio_utils.h"
26
27 #include "fast_audio_stream.h"
28
29 using namespace std;
30
31 namespace OHOS {
32 namespace AudioStandard {
FastAudioStream(AudioStreamType eStreamType,AudioMode eMode,int32_t appUid)33 FastAudioStream::FastAudioStream(AudioStreamType eStreamType, AudioMode eMode, int32_t appUid)
34 : eStreamType_(eStreamType),
35 eMode_(eMode),
36 state_(NEW),
37 renderMode_(RENDER_MODE_CALLBACK),
38 captureMode_(CAPTURE_MODE_CALLBACK)
39 {
40 AUDIO_INFO_LOG("FastAudioStream ctor, appUID = %{public}d", appUid);
41 audioStreamTracker_ = std::make_unique<AudioStreamTracker>(eMode, appUid);
42 AUDIO_DEBUG_LOG("AudioStreamTracker created");
43 }
44
~FastAudioStream()45 FastAudioStream::~FastAudioStream()
46 {
47 if (state_ != RELEASED && state_ != NEW) {
48 ReleaseAudioStream(false);
49 }
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.originalFlag = rendererInfo_.originalFlag;
96 } else if (eMode_ == AUDIO_MODE_RECORD) {
97 AUDIO_DEBUG_LOG("FastAudioStream: Initialize recording");
98 config.capturerInfo.sourceType = capturerInfo_.sourceType;
99 config.capturerInfo.capturerFlags = STREAM_FLAG_FAST;
100 config.capturerInfo.originalFlag = capturerInfo_.originalFlag;
101 } else {
102 return ERR_INVALID_OPERATION;
103 }
104 return SUCCESS;
105 }
106
SetAudioStreamInfo(const AudioStreamParams info,const std::shared_ptr<AudioClientTracker> & proxyObj)107 int32_t FastAudioStream::SetAudioStreamInfo(const AudioStreamParams info,
108 const std::shared_ptr<AudioClientTracker> &proxyObj)
109 {
110 AUDIO_INFO_LOG("FastAudioStreamInfo, Sampling rate: %{public}d, channels: %{public}d, format: %{public}d,"
111 " stream type: %{public}d", info.samplingRate, info.channels, info.format, eStreamType_);
112 CHECK_AND_RETURN_RET_LOG(processClient_ == nullptr, ERR_INVALID_OPERATION,
113 "Process is already inited, reset stream info is not supported.");
114 streamInfo_ = info;
115 if (state_ != NEW) {
116 AUDIO_INFO_LOG("FastAudioStream: State is not new, release existing stream");
117 StopAudioStream();
118 ReleaseAudioStream(false);
119 }
120 AudioProcessConfig config;
121 int32_t ret = InitializeAudioProcessConfig(config, info);
122 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Initialize failed.");
123 CHECK_AND_RETURN_RET_LOG(AudioProcessInClient::CheckIfSupport(config), ERR_INVALID_PARAM,
124 "Stream is not supported.");
125 processconfig_ = config;
126 processClient_ = AudioProcessInClient::Create(config);
127 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_INVALID_PARAM,
128 "Client test creat process client fail.");
129 state_ = PREPARED;
130 proxyObj_ = proxyObj;
131
132 if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
133 processClient_->GetSessionID(sessionId_);
134
135 AudioRegisterTrackerInfo registerTrackerInfo;
136 UpdateRegisterTrackerInfo(registerTrackerInfo);
137 audioStreamTracker_->RegisterTracker(registerTrackerInfo, proxyObj);
138 }
139 return SUCCESS;
140 }
141
GetAudioStreamInfo(AudioStreamParams & audioStreamInfo)142 int32_t FastAudioStream::GetAudioStreamInfo(AudioStreamParams &audioStreamInfo)
143 {
144 AUDIO_INFO_LOG("GetAudioStreamInfo enter.");
145 audioStreamInfo = streamInfo_;
146 return SUCCESS;
147 }
148
CheckRecordingCreate(uint32_t appTokenId,uint64_t appFullTokenId,int32_t appUid,SourceType sourceType)149 bool FastAudioStream::CheckRecordingCreate(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
150 SourceType sourceType)
151 {
152 AUDIO_ERR_LOG("Not supported operation");
153 return false;
154 }
155
CheckRecordingStateChange(uint32_t appTokenId,uint64_t appFullTokenId,int32_t appUid,AudioPermissionState state)156 bool FastAudioStream::CheckRecordingStateChange(uint32_t appTokenId, uint64_t appFullTokenId, int32_t appUid,
157 AudioPermissionState state)
158 {
159 AUDIO_ERR_LOG("Not supported operation");
160 return false;
161 }
162
GetAudioSessionID(uint32_t & sessionID)163 int32_t FastAudioStream::GetAudioSessionID(uint32_t &sessionID)
164 {
165 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED,
166 "GetAudioSessionID failed: null process");
167 int32_t ret = processClient_->GetSessionID(sessionID);
168 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetSessionID error.");
169 return ret;
170 }
171
GetAudioPipeType(AudioPipeType & pipeType)172 void FastAudioStream::GetAudioPipeType(AudioPipeType &pipeType)
173 {
174 pipeType = eMode_ == AUDIO_MODE_PLAYBACK ? rendererInfo_.pipeType : capturerInfo_.pipeType;
175 }
176
GetState()177 State FastAudioStream::GetState()
178 {
179 return state_;
180 }
181
GetAudioTime(Timestamp & timestamp,Timestamp::Timestampbase base)182 bool FastAudioStream::GetAudioTime(Timestamp ×tamp, Timestamp::Timestampbase base)
183 {
184 CHECK_AND_RETURN_RET_LOG(base == Timestamp::MONOTONIC, false, "GetAudioTime failed: invalid base");
185
186 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "GetAudioTime failed: null process");
187 int64_t timeSec = 0;
188 int64_t timeNsec = 0;
189 bool ret = processClient_->GetAudioTime(timestamp.framePosition, timeSec, timeNsec);
190 CHECK_AND_RETURN_RET_LOG(ret, false, "GetBufferSize error.");
191 timestamp.time.tv_sec = timeSec;
192 timestamp.time.tv_nsec = timeNsec;
193 return true;
194 }
195
GetAudioPosition(Timestamp & timestamp,Timestamp::Timestampbase base)196 bool FastAudioStream::GetAudioPosition(Timestamp ×tamp, Timestamp::Timestampbase base)
197 {
198 return GetAudioTime(timestamp, base);
199 }
200
GetBufferSize(size_t & bufferSize)201 int32_t FastAudioStream::GetBufferSize(size_t &bufferSize)
202 {
203 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "GetBufferSize failed: null process");
204 int32_t ret = processClient_->GetBufferSize(bufferSize);
205 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetBufferSize error.");
206 return ret;
207 }
208
GetFrameCount(uint32_t & frameCount)209 int32_t FastAudioStream::GetFrameCount(uint32_t &frameCount)
210 {
211 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "GetFrameCount failed: null process");
212 int32_t ret = processClient_->GetFrameCount(frameCount);
213 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetFrameCount error.");
214 return ret;
215 }
216
GetLatency(uint64_t & latency)217 int32_t FastAudioStream::GetLatency(uint64_t &latency)
218 {
219 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "GetLatency failed: null process");
220 int32_t ret = processClient_->GetLatency(latency);
221 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "GetLatency error.");
222 return ret;
223 }
224
SetAudioStreamType(AudioStreamType audioStreamType)225 int32_t FastAudioStream::SetAudioStreamType(AudioStreamType audioStreamType)
226 {
227 // Stream type can only be set when create.
228 AUDIO_ERR_LOG("Unsupported operation: SetAudioStreamType");
229 return ERR_INVALID_OPERATION;
230 }
231
SetVolume(float volume)232 int32_t FastAudioStream::SetVolume(float volume)
233 {
234 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "SetVolume failed: null process");
235 int32_t ret = SUCCESS;
236 ret = processClient_->SetVolume(volume);
237 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetVolume error.");
238 return ret;
239 }
240
GetVolume()241 float FastAudioStream::GetVolume()
242 {
243 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, 1.0f, "SetVolume failed: null process"); // 1.0f for default
244 return processClient_->GetVolume();
245 }
246
SetDuckVolume(float volume)247 int32_t FastAudioStream::SetDuckVolume(float volume)
248 {
249 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, ERR_OPERATION_FAILED, "SetDuckVolume failed: null process");
250 int32_t ret = processClient_->SetDuckVolume(volume);
251 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "SetDuckVolume error.");
252 return ret;
253 }
254
SetSilentModeAndMixWithOthers(bool on)255 void FastAudioStream::SetSilentModeAndMixWithOthers(bool on)
256 {
257 AUDIO_PRERELEASE_LOGI("%{public}d", on);
258 silentModeAndMixWithOthers_ = on;
259 CHECK_AND_RETURN_LOG(processClient_ != nullptr, "processClient is null.");
260 processClient_->SetSilentModeAndMixWithOthers(on);
261 }
262
GetSilentModeAndMixWithOthers()263 bool FastAudioStream::GetSilentModeAndMixWithOthers()
264 {
265 return silentModeAndMixWithOthers_;
266 }
267
SetRenderRate(AudioRendererRate renderRate)268 int32_t FastAudioStream::SetRenderRate(AudioRendererRate renderRate)
269 {
270 CHECK_AND_RETURN_RET(RENDER_RATE_NORMAL != renderRate, SUCCESS);
271 AUDIO_ERR_LOG("Unsupported operation: SetRenderRate");
272 return ERR_INVALID_OPERATION;
273 }
274
GetRenderRate()275 AudioRendererRate FastAudioStream::GetRenderRate()
276 {
277 return renderRate_;
278 }
279
SetStreamCallback(const std::shared_ptr<AudioStreamCallback> & callback)280 int32_t FastAudioStream::SetStreamCallback(const std::shared_ptr<AudioStreamCallback> &callback)
281 {
282 AUDIO_INFO_LOG("SetStreamCallback enter.");
283 // note: need add support
284 return SUCCESS;
285 }
286
SetRenderMode(AudioRenderMode renderMode)287 int32_t FastAudioStream::SetRenderMode(AudioRenderMode renderMode)
288 {
289 CHECK_AND_RETURN_RET_LOG(renderMode == RENDER_MODE_CALLBACK && eMode_ == AUDIO_MODE_PLAYBACK,
290 ERR_INVALID_OPERATION, "SetRenderMode is not supported.");
291 return SUCCESS;
292 }
293
GetRenderMode()294 AudioRenderMode FastAudioStream::GetRenderMode()
295 {
296 AUDIO_INFO_LOG("GetRenderMode enter.");
297 return renderMode_;
298 }
299
SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> & callback)300 int32_t FastAudioStream::SetRendererWriteCallback(const std::shared_ptr<AudioRendererWriteCallback> &callback)
301 {
302 AUDIO_INFO_LOG("SetRendererWriteCallback enter.");
303 CHECK_AND_RETURN_RET_LOG(callback && processClient_ && eMode_ == AUDIO_MODE_PLAYBACK,
304 ERR_INVALID_PARAM, "callback is nullptr");
305 spkProcClientCb_ = std::make_shared<FastAudioStreamRenderCallback>(callback, *this);
306 int32_t ret = processClient_->SaveDataCallback(spkProcClientCb_);
307 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Client test save data callback fail, ret %{public}d.", ret);
308 return SUCCESS;
309 }
310
SetRendererFirstFrameWritingCallback(const std::shared_ptr<AudioRendererFirstFrameWritingCallback> & callback)311 int32_t FastAudioStream::SetRendererFirstFrameWritingCallback(
312 const std::shared_ptr<AudioRendererFirstFrameWritingCallback> &callback)
313 {
314 AUDIO_INFO_LOG("SetRendererFirstFrameWritingCallback in.");
315 CHECK_AND_RETURN_RET_LOG(callback != nullptr, ERR_INVALID_PARAM, "callback is nullptr");
316 firstFrameWritingCb_ = callback;
317 return SUCCESS;
318 }
319
SetCaptureMode(AudioCaptureMode captureMode)320 int32_t FastAudioStream::SetCaptureMode(AudioCaptureMode captureMode)
321 {
322 CHECK_AND_RETURN_RET_LOG(captureMode == CAPTURE_MODE_CALLBACK && eMode_ == AUDIO_MODE_RECORD,
323 ERR_INVALID_OPERATION, "SetCaptureMode is not supported.");
324 return SUCCESS;
325 }
326
GetCaptureMode()327 AudioCaptureMode FastAudioStream::GetCaptureMode()
328 {
329 return captureMode_;
330 }
331
SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> & callback)332 int32_t FastAudioStream::SetCapturerReadCallback(const std::shared_ptr<AudioCapturerReadCallback> &callback)
333 {
334 AUDIO_INFO_LOG("SetCapturerReadCallback enter.");
335 CHECK_AND_RETURN_RET_LOG(callback && processClient_ && eMode_ == AUDIO_MODE_RECORD,
336 ERR_INVALID_PARAM, "callback or client is nullptr or mode is not record.");
337 micProcClientCb_ = std::make_shared<FastAudioStreamCaptureCallback>(callback);
338 int32_t ret = processClient_->SaveDataCallback(micProcClientCb_);
339 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, ret, "Client save data callback fail, ret %{public}d.", ret);
340 return SUCCESS;
341 }
342
GetBufferDesc(BufferDesc & bufDesc)343 int32_t FastAudioStream::GetBufferDesc(BufferDesc &bufDesc)
344 {
345 AUDIO_DEBUG_LOG("GetBufferDesc enter.");
346 CHECK_AND_RETURN_RET_LOG(processClient_, ERR_INVALID_OPERATION, "spkClient is null.");
347 int32_t ret = processClient_->GetBufferDesc(bufDesc);
348 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS && bufDesc.buffer != nullptr && bufDesc.bufLength != 0,
349 -1, "GetBufferDesc failed.");
350 return SUCCESS;
351 }
352
GetBufQueueState(BufferQueueState & bufState)353 int32_t FastAudioStream::GetBufQueueState(BufferQueueState &bufState)
354 {
355 AUDIO_INFO_LOG("GetBufQueueState enter.");
356 // note: add support
357 return SUCCESS;
358 }
359
Enqueue(const BufferDesc & bufDesc)360 int32_t FastAudioStream::Enqueue(const BufferDesc &bufDesc)
361 {
362 AUDIO_DEBUG_LOG("Enqueue enter.");
363 CHECK_AND_RETURN_RET_LOG(processClient_, ERR_INVALID_OPERATION,
364 "spkClient is null.");
365 int32_t ret = processClient_->Enqueue(bufDesc);
366 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, -1, "Enqueue failed.");
367 return SUCCESS;
368 }
369
SetPreferredFrameSize(int32_t frameSize)370 void FastAudioStream::SetPreferredFrameSize(int32_t frameSize)
371 {
372 std::lock_guard<std::mutex> lockSetPreferredFrameSize(setPreferredFrameSizeMutex_);
373 userSettedPreferredFrameSize_ = frameSize;
374 CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
375 processClient_->SetPreferredFrameSize(frameSize);
376 }
377
UpdateLatencyTimestamp(std::string & timestamp,bool isRenderer)378 void FastAudioStream::UpdateLatencyTimestamp(std::string ×tamp, bool isRenderer)
379 {
380 CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
381 processClient_->UpdateLatencyTimestamp(timestamp, isRenderer);
382 }
383
Clear()384 int32_t FastAudioStream::Clear()
385 {
386 AUDIO_INFO_LOG("Clear will do nothing.");
387
388 return SUCCESS;
389 }
390
SetLowPowerVolume(float volume)391 int32_t FastAudioStream::SetLowPowerVolume(float volume)
392 {
393 AUDIO_INFO_LOG("SetLowPowerVolume enter.");
394 return SUCCESS;
395 }
396
GetLowPowerVolume()397 float FastAudioStream::GetLowPowerVolume()
398 {
399 AUDIO_INFO_LOG("GetLowPowerVolume enter.");
400 return 1.0f;
401 }
402
SetOffloadMode(int32_t state,bool isAppBack)403 int32_t FastAudioStream::SetOffloadMode(int32_t state, bool isAppBack)
404 {
405 AUDIO_WARNING_LOG("SetOffloadMode enter.");
406 return ERR_NOT_SUPPORTED;
407 }
408
UnsetOffloadMode()409 int32_t FastAudioStream::UnsetOffloadMode()
410 {
411 AUDIO_WARNING_LOG("UnsetOffloadMode enter.");
412 return ERR_NOT_SUPPORTED;
413 }
414
GetSingleStreamVolume()415 float FastAudioStream::GetSingleStreamVolume()
416 {
417 AUDIO_INFO_LOG("GetSingleStreamVolume enter.");
418 return 1.0f;
419 }
420
GetAudioEffectMode()421 AudioEffectMode FastAudioStream::GetAudioEffectMode()
422 {
423 AUDIO_ERR_LOG("GetAudioEffectMode not supported");
424 return EFFECT_NONE;
425 }
426
SetAudioEffectMode(AudioEffectMode effectMode)427 int32_t FastAudioStream::SetAudioEffectMode(AudioEffectMode effectMode)
428 {
429 AUDIO_ERR_LOG("SetAudioEffectMode not supported");
430 return ERR_NOT_SUPPORTED;
431 }
432
GetFramesWritten()433 int64_t FastAudioStream::GetFramesWritten()
434 {
435 int64_t result = -1; // -1 invalid frame
436 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, result, "GetFramesWritten failed: null process");
437 result = processClient_->GetFramesWritten();
438 return result;
439 }
440
GetFramesRead()441 int64_t FastAudioStream::GetFramesRead()
442 {
443 int64_t result = -1; // -1 invalid frame
444 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, result, "GetFramesRead failed: null process");
445 result = processClient_->GetFramesRead();
446 return result;
447 }
448
SetSpeed(float speed)449 int32_t FastAudioStream::SetSpeed(float speed)
450 {
451 AUDIO_ERR_LOG("SetSpeed is not supported");
452 return ERR_OPERATION_FAILED;
453 }
454
GetSpeed()455 float FastAudioStream::GetSpeed()
456 {
457 AUDIO_ERR_LOG("GetSpeed is not supported");
458 return static_cast<float>(ERROR);
459 }
460
ChangeSpeed(uint8_t * buffer,int32_t bufferSize,std::unique_ptr<uint8_t[]> & outBuffer,int32_t & outBufferSize)461 int32_t FastAudioStream::ChangeSpeed(uint8_t *buffer, int32_t bufferSize,
462 std::unique_ptr<uint8_t []> &outBuffer, int32_t &outBufferSize)
463 {
464 AUDIO_ERR_LOG("ChangeSpeed is not supported");
465 return ERR_OPERATION_FAILED;
466 }
467
StartAudioStream(StateChangeCmdType cmdType,AudioStreamDeviceChangeReasonExt reason)468 bool FastAudioStream::StartAudioStream(StateChangeCmdType cmdType,
469 AudioStreamDeviceChangeReasonExt reason)
470 {
471 AUDIO_PRERELEASE_LOGI("StartAudioStream enter.");
472 CHECK_AND_RETURN_RET_LOG((state_ == PREPARED) || (state_ == STOPPED) || (state_ == PAUSED),
473 false, "Illegal state:%{public}u", state_);
474
475 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Start failed, process is null.");
476 if (spkProcClientCb_ != nullptr) {
477 AUDIO_DEBUG_LOG("StartAudioStream: reset the first frame state before starting");
478 spkProcClientCb_->ResetFirstFrameState();
479 }
480 if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
481 audioStreamTracker_->FetchOutputDeviceForTrack(sessionId_, RUNNING, clientPid_, rendererInfo_, reason);
482 audioStreamTracker_->FetchInputDeviceForTrack(sessionId_, RUNNING, clientPid_, capturerInfo_);
483 }
484 int32_t ret = ERROR;
485 if (state_ == PAUSED || state_ == STOPPED) {
486 ret = processClient_->Resume();
487 } else {
488 ret = processClient_->Start();
489 }
490 CHECK_AND_RETURN_RET_LOG(ret == SUCCESS, false, "Client test stop fail, ret %{public}d.", ret);
491 state_ = RUNNING;
492
493 AUDIO_DEBUG_LOG("StartAudioStream SUCCESS, sessionId: %{public}d", sessionId_);
494
495 if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
496 AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for Running");
497 audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
498 }
499
500 return true;
501 }
502
PauseAudioStream(StateChangeCmdType cmdType)503 bool FastAudioStream::PauseAudioStream(StateChangeCmdType cmdType)
504 {
505 AUDIO_PRERELEASE_LOGI("PauseAudioStream enter.");
506 CHECK_AND_RETURN_RET_LOG(state_ == RUNNING, false,
507 "state is not RUNNING. Illegal state:%{public}u", state_);
508 State oldState = state_;
509
510 state_ = PAUSED;
511 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Pause failed, process is null.");
512 int32_t ret = processClient_->Pause();
513 if (ret != SUCCESS) {
514 AUDIO_ERR_LOG("StreamPause fail,ret:%{public}d", ret);
515 state_ = oldState;
516 return false;
517 }
518
519 AUDIO_DEBUG_LOG("PauseAudioStream SUCCESS, sessionId: %{public}d", sessionId_);
520 if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
521 AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for Pause");
522 audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
523 }
524 return true;
525 }
526
StopAudioStream()527 bool FastAudioStream::StopAudioStream()
528 {
529 CHECK_AND_RETURN_RET_LOG((state_ == RUNNING) || (state_ == PAUSED), false,
530 "State is not RUNNING. Illegal state:%{public}u", state_);
531 State oldState = state_;
532 state_ = STOPPED; // Set it before stopping as Read/Write and Stop can be called from different threads
533
534 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Stop failed, process is null.");
535 int32_t ret = processClient_->Stop();
536 if (ret != SUCCESS) {
537 AUDIO_ERR_LOG("StreamStop fail,ret:%{public}d", ret);
538 state_ = oldState;
539 return false;
540 }
541
542 AUDIO_INFO_LOG("StopAudioStream SUCCESS, sessionId: %{public}d", sessionId_);
543 if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
544 AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for stop");
545 audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
546 }
547 return true;
548 }
549
FlushAudioStream()550 bool FastAudioStream::FlushAudioStream()
551 {
552 AUDIO_PRERELEASE_LOGI("FlushAudioStream enter.");
553 return true;
554 }
555
DrainAudioStream(bool stopFlag)556 bool FastAudioStream::DrainAudioStream(bool stopFlag)
557 {
558 AUDIO_INFO_LOG("Drain stream SUCCESS");
559 return true;
560 }
561
ReleaseAudioStream(bool releaseRunner,bool isSwitchStream)562 bool FastAudioStream::ReleaseAudioStream(bool releaseRunner, bool isSwitchStream)
563 {
564 CHECK_AND_RETURN_RET_LOG(state_ != RELEASED && state_ != NEW,
565 false, "Illegal state: state = %{public}u", state_);
566 // If state_ is RUNNING try to Stop it first and Release
567 if (state_ == RUNNING) {
568 StopAudioStream();
569 }
570
571 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "Release failed, process is null.");
572 processClient_->Release(isSwitchStream);
573 state_ = RELEASED;
574 AUDIO_INFO_LOG("ReleaseAudiostream SUCCESS, sessionId: %{public}d", sessionId_);
575 if (audioStreamTracker_ != nullptr && audioStreamTracker_.get()) {
576 AUDIO_DEBUG_LOG("AudioStream:Calling Update tracker for release");
577 audioStreamTracker_->UpdateTracker(sessionId_, state_, clientPid_, rendererInfo_, capturerInfo_);
578 }
579 return true;
580 }
581
Read(uint8_t & buffer,size_t userSize,bool isBlockingRead)582 int32_t FastAudioStream::Read(uint8_t &buffer, size_t userSize, bool isBlockingRead)
583 {
584 AUDIO_ERR_LOG("Unsupported operation: read");
585 return ERR_INVALID_OPERATION;
586 }
587
Write(uint8_t * buffer,size_t buffer_size)588 int32_t FastAudioStream::Write(uint8_t *buffer, size_t buffer_size)
589 {
590 AUDIO_ERR_LOG("Unsupported operation: Write");
591 return ERR_INVALID_OPERATION;
592 }
593
Write(uint8_t * pcmBuffer,size_t pcmBufferSize,uint8_t * metaBuffer,size_t metaBufferSize)594 int32_t FastAudioStream::Write(uint8_t *pcmBuffer, size_t pcmBufferSize, uint8_t *metaBuffer, size_t metaBufferSize)
595 {
596 AUDIO_ERR_LOG("Unsupported operation: Write");
597 return ERR_INVALID_OPERATION;
598 }
599
GetUnderflowCount()600 uint32_t FastAudioStream::GetUnderflowCount()
601 {
602 AUDIO_INFO_LOG("GetUnderflowCount enter.");
603 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, 0, "process client is null.");
604 underflowCount_ = processClient_->GetUnderflowCount();
605 return underflowCount_;
606 }
607
GetOverflowCount()608 uint32_t FastAudioStream::GetOverflowCount()
609 {
610 AUDIO_INFO_LOG("enter.");
611 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, 0, "process client is null.");
612 overflowCount_ = processClient_->GetOverflowCount();
613 return overflowCount_;
614 }
615
SetUnderflowCount(uint32_t underflowCount)616 void FastAudioStream::SetUnderflowCount(uint32_t underflowCount)
617 {
618 CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
619 processClient_->SetUnderflowCount(underflowCount);
620 }
621
SetOverflowCount(uint32_t overflowCount)622 void FastAudioStream::SetOverflowCount(uint32_t overflowCount)
623 {
624 CHECK_AND_RETURN_LOG(processClient_ != nullptr, "process client is null.");
625 processClient_->SetOverflowCount(overflowCount);
626 }
627
SetRendererPositionCallback(int64_t markPosition,const std::shared_ptr<RendererPositionCallback> & callback)628 void FastAudioStream::SetRendererPositionCallback(int64_t markPosition,
629 const std::shared_ptr<RendererPositionCallback> &callback)
630 {
631 AUDIO_INFO_LOG("Registering render frame position callback mark position");
632 // note: need support
633 }
634
UnsetRendererPositionCallback()635 void FastAudioStream::UnsetRendererPositionCallback()
636 {
637 AUDIO_INFO_LOG("Unregistering render frame position callback");
638 // note: need support
639 }
640
SetRendererPeriodPositionCallback(int64_t periodPosition,const std::shared_ptr<RendererPeriodPositionCallback> & callback)641 void FastAudioStream::SetRendererPeriodPositionCallback(int64_t periodPosition,
642 const std::shared_ptr<RendererPeriodPositionCallback> &callback)
643 {
644 AUDIO_INFO_LOG("Registering render period position callback");
645 }
646
UnsetRendererPeriodPositionCallback()647 void FastAudioStream::UnsetRendererPeriodPositionCallback()
648 {
649 AUDIO_INFO_LOG("Unregistering render period position callback");
650 }
651
SetCapturerPositionCallback(int64_t markPosition,const std::shared_ptr<CapturerPositionCallback> & callback)652 void FastAudioStream::SetCapturerPositionCallback(int64_t markPosition,
653 const std::shared_ptr<CapturerPositionCallback> &callback)
654 {
655 AUDIO_INFO_LOG("Registering capture frame position callback, mark position");
656 }
657
UnsetCapturerPositionCallback()658 void FastAudioStream::UnsetCapturerPositionCallback()
659 {
660 AUDIO_INFO_LOG("Unregistering capture frame position callback");
661 }
662
SetCapturerPeriodPositionCallback(int64_t periodPosition,const std::shared_ptr<CapturerPeriodPositionCallback> & callback)663 void FastAudioStream::SetCapturerPeriodPositionCallback(int64_t periodPosition,
664 const std::shared_ptr<CapturerPeriodPositionCallback> &callback)
665 {
666 AUDIO_INFO_LOG("Registering period position callback");
667 }
668
UnsetCapturerPeriodPositionCallback()669 void FastAudioStream::UnsetCapturerPeriodPositionCallback()
670 {
671 AUDIO_INFO_LOG("Unregistering period position callback");
672 }
673
SetRendererSamplingRate(uint32_t sampleRate)674 int32_t FastAudioStream::SetRendererSamplingRate(uint32_t sampleRate)
675 {
676 AUDIO_ERR_LOG("SetRendererSamplingRate is not supported");
677
678 return ERR_OPERATION_FAILED;
679 }
680
GetRendererSamplingRate()681 uint32_t FastAudioStream::GetRendererSamplingRate()
682 {
683 AUDIO_INFO_LOG("GetRendererSamplingRate enter.");
684 return streamInfo_.samplingRate;
685 }
686
SetBufferSizeInMsec(int32_t bufferSizeInMsec)687 int32_t FastAudioStream::SetBufferSizeInMsec(int32_t bufferSizeInMsec)
688 {
689 AUDIO_ERR_LOG("SetBufferSizeInMsec is not supported");
690 // note: add support
691 return ERR_NOT_SUPPORTED;
692 }
693
SetApplicationCachePath(const std::string cachePath)694 void FastAudioStream::SetApplicationCachePath(const std::string cachePath)
695 {
696 AUDIO_INFO_LOG("SetApplicationCachePath to %{public}s", cachePath.c_str());
697
698 cachePath_ = cachePath;
699 }
SetInnerCapturerState(bool isInnerCapturer)700 void FastAudioStream::SetInnerCapturerState(bool isInnerCapturer)
701 {
702 AUDIO_ERR_LOG("SetInnerCapturerState is not supported");
703 }
704
SetWakeupCapturerState(bool isWakeupCapturer)705 void FastAudioStream::SetWakeupCapturerState(bool isWakeupCapturer)
706 {
707 AUDIO_ERR_LOG("SetWakeupCapturerState is not supported");
708 }
709
SetCapturerSource(int capturerSource)710 void FastAudioStream::SetCapturerSource(int capturerSource)
711 {
712 AUDIO_ERR_LOG("SetCapturerSource is not supported");
713 }
714
SetPrivacyType(AudioPrivacyType privacyType)715 void FastAudioStream::SetPrivacyType(AudioPrivacyType privacyType)
716 {
717 AUDIO_ERR_LOG("SetPrivacyType is not supported");
718 }
719
GetStreamClass()720 IAudioStream::StreamClass FastAudioStream::GetStreamClass()
721 {
722 return IAudioStream::StreamClass::FAST_STREAM;
723 }
724
SetStreamTrackerState(bool trackerRegisteredState)725 void FastAudioStream::SetStreamTrackerState(bool trackerRegisteredState)
726 {
727 streamTrackerRegistered_ = trackerRegisteredState;
728 }
729
GetSwitchInfo(IAudioStream::SwitchInfo & info)730 void FastAudioStream::GetSwitchInfo(IAudioStream::SwitchInfo& info)
731 {
732 GetAudioStreamInfo(info.params);
733 info.rendererInfo = rendererInfo_;
734 info.capturerInfo = capturerInfo_;
735 info.eStreamType = eStreamType_;
736 info.state = state_;
737 info.sessionId = sessionId_;
738 info.cachePath = cachePath_;
739
740 info.clientPid = clientPid_;
741 info.clientUid = clientUid_;
742
743 info.volume = GetVolume();
744 info.effectMode = GetAudioEffectMode();
745 info.renderMode = renderMode_;
746 info.captureMode = captureMode_;
747 info.renderRate = renderRate_;
748
749 info.underFlowCount = GetUnderflowCount();
750 info.overFlowCount = GetOverflowCount();
751
752 info.silentModeAndMixWithOthers = silentModeAndMixWithOthers_;
753
754 {
755 std::lock_guard<std::mutex> lock(setPreferredFrameSizeMutex_);
756 info.userSettedPreferredFrameSize = userSettedPreferredFrameSize_;
757 }
758
759 if (spkProcClientCb_) {
760 info.rendererWriteCallback = spkProcClientCb_->GetRendererWriteCallback();
761 }
762 if (micProcClientCb_) {
763 info.capturerReadCallback = micProcClientCb_->GetCapturerReadCallback();
764 }
765 if (firstFrameWritingCb_) {
766 info.rendererFirstFrameWritingCallback = firstFrameWritingCb_;
767 }
768 }
769
OnFirstFrameWriting()770 void FastAudioStream::OnFirstFrameWriting()
771 {
772 CHECK_AND_RETURN_LOG(firstFrameWritingCb_!= nullptr, "firstFrameWritingCb_ is null.");
773 uint64_t latency = 0;
774 this->GetLatency(latency);
775 firstFrameWritingCb_->OnFirstFrameWriting(latency);
776 }
777
OnHandleData(size_t length)778 void FastAudioStreamRenderCallback::OnHandleData(size_t length)
779 {
780 CHECK_AND_RETURN_LOG(rendererWriteCallback_!= nullptr, "OnHandleData failed: rendererWriteCallback_ is null.");
781 if (!hasFirstFrameWrited_) {
782 AUDIO_DEBUG_LOG("OnHandleData: send the first frame writing event to audio haptic player");
783 audioStreamImpl_.OnFirstFrameWriting();
784 hasFirstFrameWrited_ = true;
785 }
786 rendererWriteCallback_->OnWriteData(length);
787 }
788
ResetFirstFrameState()789 void FastAudioStreamRenderCallback::ResetFirstFrameState()
790 {
791 AUDIO_DEBUG_LOG("ResetFirstFrameState: set the hasFirstFrameWrited_ to false");
792 hasFirstFrameWrited_ = false;
793 }
794
GetRendererWriteCallback() const795 std::shared_ptr<AudioRendererWriteCallback> FastAudioStreamRenderCallback::GetRendererWriteCallback() const
796 {
797 return rendererWriteCallback_;
798 }
799
GetCapturerReadCallback() const800 std::shared_ptr<AudioCapturerReadCallback> FastAudioStreamCaptureCallback::GetCapturerReadCallback() const
801 {
802 return captureCallback_;
803 }
804
OnHandleData(size_t length)805 void FastAudioStreamCaptureCallback::OnHandleData(size_t length)
806 {
807 CHECK_AND_RETURN_LOG(captureCallback_!= nullptr, "OnHandleData failed: captureCallback_ is null.");
808 captureCallback_->OnReadData(length);
809 }
810
SetChannelBlendMode(ChannelBlendMode blendMode)811 int32_t FastAudioStream::SetChannelBlendMode(ChannelBlendMode blendMode)
812 {
813 AUDIO_ERR_LOG("SetChannelBlendMode is not supported");
814 return SUCCESS;
815 }
816
SetVolumeWithRamp(float volume,int32_t duration)817 int32_t FastAudioStream::SetVolumeWithRamp(float volume, int32_t duration)
818 {
819 AUDIO_ERR_LOG("SetVolumeWithRamp is not supported");
820 return SUCCESS;
821 }
822
UpdateRegisterTrackerInfo(AudioRegisterTrackerInfo & registerTrackerInfo)823 void FastAudioStream::UpdateRegisterTrackerInfo(AudioRegisterTrackerInfo ®isterTrackerInfo)
824 {
825 rendererInfo_.samplingRate = static_cast<AudioSamplingRate>(streamInfo_.samplingRate);
826 capturerInfo_.samplingRate = static_cast<AudioSamplingRate>(streamInfo_.samplingRate);
827
828 registerTrackerInfo.sessionId = sessionId_;
829 registerTrackerInfo.clientPid = clientPid_;
830 registerTrackerInfo.state = state_;
831 registerTrackerInfo.rendererInfo = rendererInfo_;
832 registerTrackerInfo.capturerInfo = capturerInfo_;
833 }
834
RestoreAudioStream(bool needStoreState)835 bool FastAudioStream::RestoreAudioStream(bool needStoreState)
836 {
837 CHECK_AND_RETURN_RET_LOG(proxyObj_ != nullptr, false, "proxyObj_ is null");
838 CHECK_AND_RETURN_RET_LOG(state_ != NEW && state_ != INVALID && state_ != RELEASED, true,
839 "state_ is %{public}d, no need for restore", state_);
840 bool result = false;
841 State oldState = state_;
842 state_ = NEW;
843 SetStreamTrackerState(false);
844 if (processClient_ != nullptr) {
845 processClient_->Stop();
846 processClient_->Release();
847 processClient_ = nullptr;
848 }
849 int32_t ret = SetAudioStreamInfo(streamInfo_, proxyObj_);
850 if (ret != SUCCESS) {
851 goto error;
852 }
853 switch (oldState) {
854 case RUNNING:
855 CHECK_AND_RETURN_RET_LOG(processClient_ != nullptr, false, "processClient_ is null");
856 ret = processClient_->SaveDataCallback(spkProcClientCb_);
857 if (ret != SUCCESS) {
858 goto error;
859 }
860 result = StartAudioStream();
861 break;
862 case PAUSED:
863 result = StartAudioStream() && PauseAudioStream();
864 break;
865 case STOPPED:
866 case STOPPING:
867 result = StartAudioStream() && StopAudioStream();
868 break;
869 default:
870 break;
871 }
872 if (!result) {
873 goto error;
874 }
875 return result;
876
877 error:
878 AUDIO_ERR_LOG("RestoreAudioStream failed");
879 state_ = oldState;
880 return false;
881 }
882
GetOffloadEnable()883 bool FastAudioStream::GetOffloadEnable()
884 {
885 AUDIO_WARNING_LOG("not supported in fast audio stream");
886 return false;
887 }
888
GetSpatializationEnabled()889 bool FastAudioStream::GetSpatializationEnabled()
890 {
891 AUDIO_WARNING_LOG("not supported in fast audio stream");
892 return false;
893 }
894
GetHighResolutionEnabled()895 bool FastAudioStream::GetHighResolutionEnabled()
896 {
897 AUDIO_WARNING_LOG("not supported in fast audio stream");
898 return false;
899 }
900 } // namespace AudioStandard
901 } // namespace OHOS
902