• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 
16 #ifndef OHOS_DMIC_CLIENT_H
17 #define OHOS_DMIC_CLIENT_H
18 
19 #include <atomic>
20 #include <condition_variable>
21 #include <cstdint>
22 #include <memory>
23 #include <sstream>
24 #include <string>
25 #include <thread>
26 
27 #include "audio_capturer.h"
28 #include "audio_info.h"
29 
30 #include "audio_data.h"
31 #include "audio_encode_transport.h"
32 #include "audio_event.h"
33 #include "audio_param.h"
34 #include "audio_status.h"
35 #include "av_sender_engine_transport.h"
36 #include "daudio_errorcode.h"
37 #include "daudio_log.h"
38 #include "iaudio_data_transport.h"
39 #include "iaudio_datatrans_callback.h"
40 #include "iaudio_event_callback.h"
41 #include "imic_client.h"
42 
43 namespace OHOS {
44 namespace DistributedHardware {
45 class DMicClient : public IAudioDataTransCallback,
46     public AudioStandard::AudioCapturerReadCallback,
47     public IMicClient, public AVSenderTransportCallback,
48     public std::enable_shared_from_this<DMicClient> {
49 public:
DMicClient(const std::string & devId,const std::shared_ptr<IAudioEventCallback> & callback)50     DMicClient(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback)
51         : devId_(devId), eventCallback_(callback) {};
52     ~DMicClient() override;
53     int32_t OnStateChange(const AudioEventType type) override;
54     int32_t OnDecodeTransDataDone(const std::shared_ptr<AudioData> &audioData) override;
55     void OnEngineTransEvent(const AVTransEvent &event) override;
56     void OnEngineTransMessage(const std::shared_ptr<AVTransMessage> &message) override;
57     int32_t InitSenderEngine(IAVEngineProvider *providerPtr) override;
58     int32_t SetUp(const AudioParam &param) override;
59     int32_t Release() override;
60     int32_t StartCapture() override;
61     int32_t StopCapture() override;
62     void SetAttrs(const std::string &devId, const std::shared_ptr<IAudioEventCallback> &callback) override;
63     int32_t SendMessage(uint32_t type, std::string content, std::string dstDevId) override;
64 
65     void OnReadData(size_t length) override;
66 private:
67     void CaptureThreadRunning();
68 
69 private:
70     constexpr static uint8_t CHANNEL_WAIT_SECONDS = 5;
71     static constexpr const char* CAPTURETHREAD = "captureThread";
72 
73     std::string devId_;
74     std::thread captureDataThread_;
75     AudioParam audioParam_;
76     std::atomic<bool> isBlocking_ = false;
77     std::atomic<bool> isCaptureReady_ = false;
78     std::mutex devMtx_;
79     AudioStatus clientStatus_ = AudioStatus::STATUS_IDLE;
80 
81     std::weak_ptr<IAudioEventCallback> eventCallback_;
82     std::unique_ptr<AudioStandard::AudioCapturer> audioCapturer_ = nullptr;
83     std::shared_ptr<IAudioDataTransport> micTrans_ = nullptr;
84 };
85 } // DistributedHardware
86 } // OHOS
87 #endif // OHOS_DMIC_CLIENT_H
88