1 /*
2 * Copyright (c) 2021-2022 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 #include <cstdio>
16 #include <iostream>
17
18 #include "audio_service_client.h"
19 #include "audio_log.h"
20
21 using namespace OHOS::AudioStandard;
22
23 namespace {
24 constexpr uint8_t DEFAULT_FORMAT = SAMPLE_S16LE;
25 constexpr uint8_t DEFAULT_CHANNELS = 2;
26 } // namespace
27
28 class RecordTest : public AudioCapturerCallbacks {
29 public:
OnSourceDeviceUpdatedCb() const30 void OnSourceDeviceUpdatedCb() const
31 {
32 AUDIO_INFO_LOG("My custom callback OnSourceDeviceUpdatedCb");
33 }
34 // Need to check required state changes to update applications
OnStreamStateChangeCb() const35 virtual void OnStreamStateChangeCb() const
36 {
37 AUDIO_INFO_LOG("My custom callback OnStreamStateChangeCb");
38 }
39
OnStreamBufferUnderFlowCb() const40 virtual void OnStreamBufferUnderFlowCb() const {}
OnStreamBufferOverFlowCb() const41 virtual void OnStreamBufferOverFlowCb() const {}
OnErrorCb(AudioServiceErrorCodes error) const42 virtual void OnErrorCb(AudioServiceErrorCodes error) const {}
OnEventCb(AudioServiceEventTypes error) const43 virtual void OnEventCb(AudioServiceEventTypes error) const {}
44 };
45
InitClient(std::unique_ptr<AudioServiceClient> & client,uint32_t samplingRate)46 static bool InitClient(std::unique_ptr<AudioServiceClient> &client, uint32_t samplingRate)
47 {
48 AudioStreamParams audioParams;
49 audioParams.format = DEFAULT_FORMAT;
50 audioParams.samplingRate = samplingRate;
51 audioParams.channels = DEFAULT_CHANNELS;
52
53 AUDIO_INFO_LOG("Initializing of AudioServiceClient");
54 if (client->Initialize(AUDIO_SERVICE_CLIENT_RECORD) < 0)
55 return false;
56
57 RecordTest customCb;
58 client->RegisterAudioCapturerCallbacks(customCb);
59
60 AUDIO_INFO_LOG("Creating Stream");
61 if (client->CreateStream(audioParams, STREAM_MUSIC) < 0) {
62 client->ReleaseStream();
63 return false;
64 }
65
66 AUDIO_INFO_LOG("Starting Stream");
67 if (client->StartStream() < 0)
68 return false;
69
70 return true;
71 }
72
main(int argc,char * argv[])73 int main(int argc, char* argv[])
74 {
75 std::unique_ptr client = std::make_unique<AudioServiceClient>();
76 if (client == nullptr) {
77 return -1;
78 }
79
80 int32_t rateArgIndex = 2;
81 if (!InitClient(client, atoi(argv[rateArgIndex]))) {
82 AUDIO_ERR_LOG("Initialize client failed");
83 return -1;
84 }
85
86 size_t bufferLen;
87 if (client->GetMinimumBufferSize(bufferLen) < 0) {
88 return -1;
89 }
90
91 AUDIO_DEBUG_LOG("minimum buffer length: %{public}zu", bufferLen);
92
93 uint8_t* buffer = nullptr;
94 buffer = (uint8_t *) malloc(bufferLen);
95 if (buffer == nullptr) {
96 AUDIO_ERR_LOG("Failed to allocate buffer");
97 return -1;
98 }
99
100 FILE *pFile = fopen(argv[1], "wb");
101
102 size_t size = 1;
103 size_t numBuffersToCapture = 1024;
104 uint64_t timeStamp;
105 StreamBuffer stream;
106 stream.buffer = buffer;
107 stream.bufferLen = bufferLen;
108 int32_t bytesRead = 0;
109
110 while (numBuffersToCapture) {
111 bytesRead = client->ReadStream(stream, false);
112 if (bytesRead < 0) {
113 break;
114 }
115
116 if (bytesRead > 0) {
117 fwrite(stream.buffer, size, bytesRead, pFile);
118 if (client->GetCurrentTimeStamp(timeStamp) >= 0)
119 AUDIO_DEBUG_LOG("current timestamp: %{public}" PRIu64, timeStamp);
120 numBuffersToCapture--;
121 }
122 }
123
124 fflush(pFile);
125 client->FlushStream();
126 client->StopStream();
127 client->ReleaseStream();
128 free(buffer);
129 fclose(pFile);
130 AUDIO_INFO_LOG("Exit from test app");
131 return 0;
132 }
133