• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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_source_capturer.h"
17 #include <media_description.h>
18 #include <iostream>
19 #include <mutex>
20 #include <queue>
21 #include <thread>
22 #include <unistd.h>
23 #include "const_def.h"
24 #include "frame.h"
25 #include "sharing_log.h"
26 #include "utils/data_buffer.h"
27 
28 using namespace OHOS;
29 using namespace OHOS::AudioStandard;
30 using namespace OHOS::MediaAVCodec;
31 using namespace std;
32 
33 namespace OHOS {
34 namespace Sharing {
35 
~AudioSourceCapturer()36 AudioSourceCapturer::~AudioSourceCapturer()
37 {
38     audioEncoder_ = nullptr;
39 }
40 
InitAudioCapture()41 bool AudioSourceCapturer::InitAudioCapture()
42 {
43     SHARING_LOGD("trace.");
44 
45     AudioStandard::AudioCapturerOptions options;
46     options.capturerInfo.capturerFlags = 0;
47     options.capturerInfo.sourceType = AudioStandard::SourceType::SOURCE_TYPE_PLAYBACK_CAPTURE;
48     options.streamInfo.channels = AudioStandard::AudioChannel::STEREO;
49     options.streamInfo.encoding = AudioStandard::AudioEncodingType::ENCODING_PCM;
50     options.streamInfo.format = AudioStandard::AudioSampleFormat::SAMPLE_S16LE;
51     options.streamInfo.samplingRate = AudioStandard::AudioSamplingRate::SAMPLE_RATE_48000;
52 
53     AudioStandard::AppInfo appInfo;
54     appInfo.appUid = 0;
55 
56     audioCapturer_ = AudioStandard::AudioCapturer::Create(options, appInfo);
57     if (audioCapturer_ == nullptr) {
58         SHARING_LOGE("create AudioCapturer failed!");
59         return false;
60     }
61 
62     return true;
63 }
64 
StartAudioCapture()65 bool AudioSourceCapturer::StartAudioCapture()
66 {
67     SHARING_LOGD("trace.");
68     if (audioCapturer_ == nullptr) {
69         SHARING_LOGE("start capture fail - invalid capturer!");
70         return false;
71     }
72 
73     if (!audioCapturer_->Start()) {
74         SHARING_LOGE("audioCapturer Start() failed!");
75         return false;
76     }
77 
78     if (audioCapturer_->GetBufferSize(audioBufferLen_) != 0 || audioBufferLen_ == 0) {
79         audioBufferLen_ = 0;
80         SHARING_LOGE("audioCapturer GetBufferSize failed!");
81         return false;
82     }
83 
84     isAudioCapturing_ = true;
85     audioCaptureThread_ = std::make_unique<std::thread>(&AudioSourceCapturer::AudioCaptureThreadWorker, this);
86 
87     SHARING_LOGD("Audio capture start successful.");
88     return true;
89 }
90 
StopAudioCapture()91 bool AudioSourceCapturer::StopAudioCapture()
92 {
93     SHARING_LOGD("trace.");
94     isAudioCapturing_ = false;
95 
96     if (audioCaptureThread_ != nullptr && audioCaptureThread_->joinable()) {
97         audioCaptureThread_->join();
98         audioCaptureThread_.reset();
99         audioCaptureThread_ = nullptr;
100     }
101 
102     if (audioCapturer_) {
103         audioCapturer_->Flush();
104         audioCapturer_->Stop();
105     }
106 
107     return true;
108 }
109 
AudioCaptureThreadWorker()110 void AudioSourceCapturer::AudioCaptureThreadWorker()
111 {
112     SHARING_LOGD("trace.");
113     int32_t bytesRead = 0;
114     bool isBlockingRead = true;
115 
116     auto pcmFrame = FrameImpl::Create();
117     pcmFrame->codecId_ = CODEC_PCM;
118 
119     SHARING_LOGI("audio capture buffer size: %{public}zu.", audioBufferLen_);
120     uint8_t *frame = (uint8_t *)malloc(audioBufferLen_);
121     if (!frame) {
122         SHARING_LOGE("malloc buffer failed.");
123         return;
124     }
125 
126     while (isAudioCapturing_) {
127         bytesRead = audioCapturer_->Read(*frame, audioBufferLen_, isBlockingRead);
128         SHARING_LOGD("audio capture thread get pcm data, size: %{public}d.", bytesRead);
129         if (bytesRead > 0 && audioEncoder_) {
130             pcmFrame->Clear();
131             pcmFrame->Assign((char *)frame, static_cast<uint32_t>(bytesRead));
132 
133             audioEncoder_->OnFrame(pcmFrame);
134         }
135     }
136 
137     if (frame) {
138         free(frame);
139     }
140 
141     SHARING_LOGD("audio capture thread exit.");
142 }
143 } // namespace Sharing
144 } // namespace OHOS
145