1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License") override; 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 #ifndef REMOTE_FAST_AUDIO_CAPTURER_SOURCE 17 #define REMOTE_FAST_AUDIO_CAPTURER_SOURCE 18 19 #include <map> 20 21 #include "ashmem.h" 22 23 #include "audio_info.h" 24 #include "audio_manager.h" 25 #include "i_audio_capturer_source.h" 26 27 namespace OHOS { 28 namespace AudioStandard { 29 class RemoteFastAudioCapturerSource : public IMmapAudioCapturerSource { 30 public: 31 static IMmapAudioCapturerSource *GetInstance(const std::string& deviceNetworkId); 32 33 int32_t Init(IAudioSourceAttr &attr) override; 34 bool IsInited(void) override; 35 void DeInit(void) override; 36 37 int32_t Start(void) override; 38 int32_t Stop(void) override; 39 int32_t Flush(void) override; 40 int32_t Reset(void) override; 41 int32_t Pause(void) override; 42 int32_t Resume(void) override; 43 int32_t CaptureFrame(char *frame, uint64_t requestBytes, uint64_t &replyBytes) override; 44 int32_t SetVolume(float left, float right) override; 45 int32_t GetVolume(float &left, float &right) override; 46 int32_t SetMute(bool isMute) override; 47 int32_t GetMute(bool &isMute) override; 48 int32_t SetAudioScene(AudioScene audioScene, DeviceType activeDevice) override; 49 int32_t SetInputRoute(DeviceType inputDevice) override; 50 uint64_t GetTransactionId() override; 51 void RegisterWakeupCloseCallback(IAudioSourceCallback* callback) override; 52 void RegisterAudioCapturerSourceCallback(IAudioSourceCallback* callback) override; 53 int32_t GetMmapBufferInfo(int &fd, uint32_t &totalSizeInframe, uint32_t &spanSizeInframe, 54 uint32_t &byteSizePerFrame) override; 55 int32_t GetMmapHandlePosition(uint64_t &frames, int64_t &timeSec, int64_t &timeNanoSec) override; 56 57 private: 58 explicit RemoteFastAudioCapturerSource(const std::string& deviceNetworkId); 59 ~RemoteFastAudioCapturerSource(); 60 61 int32_t InitAudioManager(); 62 int32_t GetTargetAdapterPort(struct AudioAdapterDescriptor *descs, int32_t size, const char *networkId); 63 int32_t CreateCapture(const struct AudioPort &capturePort); 64 void InitAttrs(struct AudioSampleAttributes &attrs); 65 AudioFormat ConverToHdiFormat(AudioSampleFormat format); 66 int32_t InitAshmem(const struct AudioSampleAttributes &attrs); 67 AudioCategory GetAudioCategory(AudioScene audioScene); 68 int32_t SetInputPortPin(DeviceType inputDevice, AudioRouteNode &source); 69 uint32_t PcmFormatToBits(AudioSampleFormat format); 70 71 private: 72 static constexpr int32_t INVALID_FD = -1; 73 static constexpr int32_t INVALID_INDEX = -1; 74 static constexpr int32_t HALF_FACTOR = 2; 75 static constexpr uint32_t CAPTURE_INTERLEAVED = 1; 76 static constexpr uint32_t AUDIO_SAMPLE_RATE_48K = 48000; 77 static constexpr uint32_t DEEP_BUFFER_CAPTURER_PERIOD_SIZE = 3840; 78 static constexpr uint32_t INT_32_MAX = 0x7fffffff; 79 static constexpr uint32_t REMOTE_FAST_INPUT_STREAM_ID = 38; // 14 + 3 * 8 80 static constexpr int32_t EVENT_DES_SIZE = 60; 81 static constexpr int64_t SECOND_TO_NANOSECOND = 1000000000; 82 static constexpr int64_t CAPTURE_FIRST_FRIME_WAIT_NANO = 20000000; // 20ms 83 static constexpr int64_t CAPTURE_RESYNC_SLEEP_NANO = 2000000; // 2ms 84 static constexpr uint32_t PCM_8_BIT = 8; 85 static constexpr uint32_t PCM_16_BIT = 16; 86 static constexpr uint32_t PCM_24_BIT = 24; 87 static constexpr uint32_t PCM_32_BIT = 32; 88 #ifdef FEATURE_DISTRIBUTE_AUDIO 89 static constexpr uint32_t PARAM_VALUE_LENTH = 20; 90 #endif 91 static std::map<std::string, RemoteFastAudioCapturerSource *> allRFSources_; 92 93 std::atomic<bool> micMuteState_ = false; 94 std::atomic<bool> capturerInited_ = false; 95 std::atomic<bool> isCapturerCreated_ = false; 96 std::atomic<bool> started_ = false; 97 std::atomic<bool> paused_ = false; 98 float leftVolume_ = 0; 99 float rightVolume_ = 0; 100 int32_t routeHandle_ = -1; 101 int32_t bufferFd_ = -1; 102 uint32_t bufferTotalFrameSize_ = 0; 103 uint32_t eachReadFrameSize_ = 0; 104 struct AudioManager *audioManager_ = nullptr; 105 struct AudioAdapter *audioAdapter_ = nullptr; 106 struct AudioCapture *audioCapture_ = nullptr; 107 struct AudioPort audioPort_; 108 IAudioSourceAttr attr_ = {}; 109 std::string deviceNetworkId_; 110 111 #ifdef DEBUG_DIRECT_USE_HDI 112 sptr<Ashmem> ashmemSource_ = nullptr; 113 int32_t ashmemLen_ = 0; 114 int32_t lenPerRead_ = 0; 115 const char *audioFilePath = "/data/local/tmp/remote_fast_audio_capture.pcm"; 116 FILE *pfd_ = nullptr; 117 #endif // DEBUG_DIRECT_USE_HDI 118 }; 119 } // namespace AudioStandard 120 } // namespace OHOS 121 #endif // REMOTE_FAST_AUDIO_CAPTURER_SOURCE 122