• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_stream.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 constexpr int32_t INIT_CLIENT_ERROR = -1;
27 } // namespace
28 
29 class RecordTest : public AudioCapturerCallbacks {
30 public:
OnSourceDeviceUpdatedCb() const31     void OnSourceDeviceUpdatedCb() const
32     {
33         AUDIO_INFO_LOG("My custom callback OnSourceDeviceUpdatedCb");
34     }
35     // Need to check required state changes to update applications
OnStreamStateChangeCb() const36     virtual void OnStreamStateChangeCb() const
37     {
38         AUDIO_INFO_LOG("My custom callback OnStreamStateChangeCb");
39     }
40 
OnStreamBufferUnderFlowCb() const41     virtual void OnStreamBufferUnderFlowCb() const {}
OnStreamBufferOverFlowCb() const42     virtual void OnStreamBufferOverFlowCb() const {}
OnErrorCb(AudioServiceErrorCodes error) const43     virtual void OnErrorCb(AudioServiceErrorCodes error) const {}
OnEventCb(AudioServiceEventTypes error) const44     virtual void OnEventCb(AudioServiceEventTypes error) const {}
45 };
46 
InitClient(std::unique_ptr<AudioServiceClient> & client,uint32_t samplingRate)47 static bool InitClient(std::unique_ptr<AudioServiceClient> &client, uint32_t samplingRate)
48 {
49     AudioStreamParams audioParams;
50     audioParams.format = DEFAULT_FORMAT;
51     audioParams.samplingRate = samplingRate;
52     audioParams.channels = DEFAULT_CHANNELS;
53 
54     AUDIO_INFO_LOG("Initializing of AudioServiceClient");
55     if (client->Initialize(AUDIO_SERVICE_CLIENT_RECORD) < 0)
56         return false;
57 
58     RecordTest customCb;
59     client->RegisterAudioCapturerCallbacks(customCb);
60 
61     AUDIO_INFO_LOG("Creating Stream");
62     if (client->CreateStream(audioParams, STREAM_MUSIC) < 0) {
63         client->ReleaseStream();
64         return false;
65     }
66 
67     AUDIO_INFO_LOG("Starting Stream");
68     if (client->StartStream() < 0)
69         return false;
70 
71     return true;
72 }
73 
main(int argc,char * argv[])74 int main(int argc, char* argv[])
75 {
76     std::unique_ptr<AudioServiceClient> client = std::make_unique<AudioStream>(STREAM_MUSIC, AUDIO_MODE_RECORD,
77         getuid());
78     CHECK_AND_RETURN_RET_LOG(client != nullptr, INIT_CLIENT_ERROR, "AudioStream create error");
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