This commit is contained in:
yangshuai0222 2022-02-22 19:45:46 +08:00
commit 2113d586dc
21 changed files with 1028 additions and 175 deletions

150
README.md
View File

@ -79,7 +79,7 @@ You can use APIs provided in this repository to convert audio data into audible
3. (Optional) use audioRenderer->**GetRendererInfo**(AudioRendererInfo &) and audioRenderer->**GetStreamInfo**(AudioStreamInfo &) to retrieve the current renderer configuration values.
4. Inorder to listen to Audio Interrupt events, it would be required to Register to **RendererCallbacks** using audioRenderer->**SetRendererCallback**
4. Inorder to listen to Audio Interrupt and state change events, it would be required to register to **RendererCallbacks** using audioRenderer->**SetRendererCallback**
```
class AudioRendererCallbackImpl : public AudioRendererCallback {
void OnInterrupt(const InterruptEvent &interruptEvent) override
@ -104,6 +104,22 @@ You can use APIs provided in this repository to convert audio data into audible
}
}
}
void OnStateChange(const RendererState state) override
{
switch (state) {
case RENDERER_PREPARED:
// Renderer prepared
case RENDERER_RUNNING:
// Renderer in running state
case RENDERER_STOPPED:
// Renderer stopped
case RENDERER_RELEASED:
// Renderer released
case RENDERER_PAUSED:
// Renderer paused
}
}
}
std::shared_ptr<AudioRendererCallback> audioRendererCB = std::make_shared<AudioRendererCallbackImpl>();
@ -115,6 +131,36 @@ You can use APIs provided in this repository to convert audio data into audible
This will have information on the audio interrupt forced action taken by the Audio framework and also the action hints to be handled by the application. Refer to **audio_renderer.h** and **audio_info.h** for more details.
Similarly, renderer state change callbacks can be received by overriding **OnStateChange** function in **AudioRendererCallback** class. Refer to **audio_renderer.h** for the list of renderer states.
5. In order to get callbacks for frame mark position and/or frame period position, register for the corresponding callbacks in audio renderer using audioRenderer->**SetRendererPositionCallback** and/or audioRenderer->**SetRendererPeriodPositionCallback** functions respectively.
```
class RendererPositionCallbackImpl : public RendererPositionCallback {
void OnMarkReached(const int64_t &framePosition) override
{
// frame mark reached
// framePosition is the frame mark number
}
}
std::shared_ptr<RendererPositionCallback> framePositionCB = std::make_shared<RendererPositionCallbackImpl>();
//markPosition is the frame mark number for which callback is requested.
audioRenderer->SetRendererPositionCallback(markPosition, framePositionCB);
class RendererPeriodPositionCallbackImpl : public RendererPeriodPositionCallback {
void OnPeriodReached(const int64_t &frameNumber) override
{
// frame period reached
// frameNumber is the frame period number
}
}
std::shared_ptr<RendererPeriodPositionCallback> periodPositionCB = std::make_shared<RendererPeriodPositionCallbackImpl>();
//framePeriodNumber is the frame period number for which callback is requested.
audioRenderer->SetRendererPeriodPositionCallback(framePeriodNumber, periodPositionCB);
```
For unregistering the position callbacks, call the corresponding audioRenderer->**UnsetRendererPositionCallback** and/or audioRenderer->**UnsetRendererPeriodPositionCallback** APIs.
6. Call audioRenderer->**Start**() function on the AudioRenderer instance to start the playback task.
7. Get the buffer length to be written, using **GetBufferSize** API.
```
@ -177,13 +223,63 @@ You can use the APIs provided in this repository for your application to record
4. (Optional) use audioCapturer->**GetCapturerInfo**(AudioCapturerInfo &) and audioCapturer->**GetStreamInfo**(AudioStreamInfo &) to retrieve the current capturer configuration values.
5. Call audioCapturer->**Start**() function on the AudioCapturer instance to start the recording task.
5. Capturer state change callbacks can be received by overriding **OnStateChange** function in **AudioCapturerCallback** class, and registering the callback instance using audioCapturer->**SetCapturerCallback** API.
```
class AudioCapturerCallbackImpl : public AudioCapturerCallback {
void OnStateChange(const CapturerState state) override
{
switch (state) {
case CAPTURER_PREPARED:
// Capturer prepared
case CAPTURER_RUNNING:
// Capturer in running state
case CAPTURER_STOPPED:
// Capturer stopped
case CAPTURER_RELEASED:
// Capturer released
}
}
}
6. Get the buffer length to be read, using **GetBufferSize** API.
std::shared_ptr<AudioCapturerCallback> audioCapturerCB = std::make_shared<AudioCapturerCallbackImpl>();
audioCapturer->SetCapturerCallback(audioCapturerCB);
```
6. In order to get callbacks for frame mark position and/or frame period position, register for the corresponding callbacks in audio capturer using audioCapturer->**SetCapturerPositionCallback** and/or audioCapturer->**SetCapturerPeriodPositionCallback** functions respectively.
```
class CapturerPositionCallbackImpl : public CapturerPositionCallback {
void OnMarkReached(const int64_t &framePosition) override
{
// frame mark reached
// framePosition is the frame mark number
}
}
std::shared_ptr<CapturerPositionCallback> framePositionCB = std::make_shared<CapturerPositionCallbackImpl>();
//markPosition is the frame mark number for which callback is requested.
audioCapturer->SetCapturerPositionCallback(markPosition, framePositionCB);
class CapturerPeriodPositionCallbackImpl : public CapturerPeriodPositionCallback {
void OnPeriodReached(const int64_t &frameNumber) override
{
// frame period reached
// frameNumber is the frame period number
}
}
std::shared_ptr<CapturerPeriodPositionCallback> periodPositionCB = std::make_shared<CapturerPeriodPositionCallbackImpl>();
//framePeriodNumber is the frame period number for which callback is requested.
audioCapturer->SetCapturerPeriodPositionCallback(framePeriodNumber, periodPositionCB);
```
For unregistering the position callbacks, call the corresponding audioCapturer->**UnsetCapturerPositionCallback** and/or audioCapturer->**UnsetCapturerPeriodPositionCallback** APIs.
7. Call audioCapturer->**Start**() function on the AudioCapturer instance to start the recording task.
8. Get the buffer length to be read, using **GetBufferSize** API.
```
audioCapturer->GetBufferSize(bufferLen);
```
7. Read the captured audio data and convert it to a byte stream. Call the read function repeatedly to read data untill you want to stop recording
9. Read the captured audio data and convert it to a byte stream. Call the read function repeatedly to read data untill you want to stop recording
```
// set isBlocking = true/false for blocking/non-blocking read
bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlocking);
@ -197,9 +293,9 @@ You can use the APIs provided in this repository for your application to record
}
}
```
8. (Optional) Call audioCapturer->**Flush**() to flush the capture buffer of this stream.
9. Call the audioCapturer->**Stop**() function on the AudioCapturer instance to stop the recording.
10. After the recording task is complete, call the audioCapturer->**Release**() function on the AudioCapturer instance to release the stream resources.
10. (Optional) Call audioCapturer->**Flush**() to flush the capture buffer of this stream.
11. Call the audioCapturer->**Stop**() function on the AudioCapturer instance to stop the recording.
12. After the recording task is complete, call the audioCapturer->**Release**() function on the AudioCapturer instance to release the stream resources.
Provided the basic recording usecase above. Please refer [**audio_capturer.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocapturer/include/audio_capturer.h) and [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h) for more APIs.
@ -238,12 +334,12 @@ You can use the APIs provided in [**audio_system_manager.h**](https://gitee.com/
#### Device control
7. Use **GetDevices**, **deviceType_** and **deviceRole_** APIs to get audio I/O devices information. For DeviceFlag, DeviceType and DeviceRole enums refer [**audio_info.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiocommon/include/audio_info.h).
```
DeviceFlag deviceFlag = OUTPUT_DEVICES_FLAG;
vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors
= audioSystemMgr->GetDevices(deviceFlag);
sptr<AudioDeviceDescriptor> audioDeviceDescriptor = audioDeviceDescriptors[0];
cout << audioDeviceDescriptor->deviceType_;
cout << audioDeviceDescriptor->deviceRole_;
DeviceFlag deviceFlag = ALL_DEVICES_FLAG;
vector<sptr<AudioDeviceDescriptor>> audioDeviceDescriptors = audioSystemMgr->GetDevices(deviceFlag);
for (auto &audioDeviceDescriptor : audioDeviceDescriptors) {
cout << audioDeviceDescriptor->deviceType_ << endl;
cout << audioDeviceDescriptor->deviceRole_ << endl;
}
```
8. Use **SetDeviceActive** and **IsDeviceActive** APIs to Actiavte/Deactivate the device and to check if the device is active.
```
@ -251,9 +347,33 @@ You can use the APIs provided in [**audio_system_manager.h**](https://gitee.com/
int32_t result = audioSystemMgr->SetDeviceActive(deviceType, true);
bool isDevActive = audioSystemMgr->IsDeviceActive(deviceType);
```
9. Other useful APIs such as **IsStreamActive**, **SetAudioParameter** and **GetAudioParameter** are also provided. Please refer [**audio_system_manager.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h) for more details
10. Applications can register for change in system volume using **AudioManagerNapi::On**. Here when an application registers to volume change event, whenever there is change in volume, the application is notified with following parameters:
9. Use **SetDeviceChangeCallback** API to register for device change events. Clients will recieve callback when a device is connected/disconnected.
**OnDeviceChange** function will be called and client will receive **DeviceChangeAction** object, which will contain following parameters:\
*type* : **DeviceChangeType** which specifies whether device is connected or disconnected.\
*deviceDescriptors* : Array of **AudioDeviceDescriptor** object which specifies the type of device and its role(input/output device).
```
class DeviceChangeCallback : public AudioManagerDeviceChangeCallback {
public:
DeviceChangeCallback = default;
~DeviceChangeCallback = default;
void OnDeviceChange(const DeviceChangeAction &deviceChangeAction) override
{
cout << deviceChangeAction.type << endl;
for (auto &audioDeviceDescriptor : deviceChangeAction.deviceDescriptors) {
cout << audioDeviceDescriptor->deviceType_ << endl;
cout << audioDeviceDescriptor->deviceRole_ << endl;
}
}
};
auto callback = std::make_shared<DeviceChangeCallback>();
audioSystemMgr->SetDeviceChangeCallback(callback);
```
10. Other useful APIs such as **IsStreamActive**, **SetAudioParameter** and **GetAudioParameter** are also provided. Please refer [**audio_system_manager.h**](https://gitee.com/openharmony/multimedia_audio_standard/blob/master/interfaces/inner_api/native/audiomanager/include/audio_system_manager.h) for more details
11. Applications can register for change in system volume using **AudioManagerNapi::On**. Here when an application registers to volume change event, whenever there is change in volume, the application is notified with following parameters:
volumeType : The AudioVolumeType for which volume is updated
volume : The curret volume level set.
updateUi : Whether the volume change details need to be shown or not. (If volume is updated through volume key up/down we set the updateUi flag to true, in other scenarios the updateUi is set as false).

View File

@ -153,6 +153,11 @@ int32_t AudioCapturerPrivate::GetStreamInfo(AudioStreamInfo &streamInfo) const
int32_t AudioCapturerPrivate::SetCapturerPositionCallback(int64_t markPosition,
const std::shared_ptr<CapturerPositionCallback> &callback)
{
if ((callback == nullptr) || (markPosition <= 0)) {
MEDIA_ERR_LOG("AudioCapturerPrivate::SetCapturerPositionCallback input param is invalid");
return ERR_INVALID_PARAM;
}
audioStream_->SetCapturerPositionCallback(markPosition, callback);
return SUCCESS;
@ -166,6 +171,11 @@ void AudioCapturerPrivate::UnsetCapturerPositionCallback()
int32_t AudioCapturerPrivate::SetCapturerPeriodPositionCallback(int64_t frameNumber,
const std::shared_ptr<CapturerPeriodPositionCallback> &callback)
{
if ((callback == nullptr) || (frameNumber <= 0)) {
MEDIA_ERR_LOG("AudioCapturerPrivate::SetCapturerPeriodPositionCallback input param is invalid");
return ERR_INVALID_PARAM;
}
audioStream_->SetCapturerPeriodPositionCallback(frameNumber, callback);
return SUCCESS;

View File

@ -196,6 +196,11 @@ int32_t AudioRendererPrivate::SetRendererCallback(const std::shared_ptr<AudioRen
int32_t AudioRendererPrivate::SetRendererPositionCallback(int64_t markPosition,
const std::shared_ptr<RendererPositionCallback> &callback)
{
if ((callback == nullptr) || (markPosition <= 0)) {
MEDIA_ERR_LOG("AudioRendererPrivate::SetRendererPositionCallback input param is invalid");
return ERR_INVALID_PARAM;
}
audioStream_->SetRendererPositionCallback(markPosition, callback);
return SUCCESS;
@ -209,6 +214,11 @@ void AudioRendererPrivate::UnsetRendererPositionCallback()
int32_t AudioRendererPrivate::SetRendererPeriodPositionCallback(int64_t frameNumber,
const std::shared_ptr<RendererPeriodPositionCallback> &callback)
{
if ((callback == nullptr) || (frameNumber <= 0)) {
MEDIA_ERR_LOG("AudioRendererPrivate::SetRendererPeriodPositionCallback input param is invalid");
return ERR_INVALID_PARAM;
}
audioStream_->SetRendererPeriodPositionCallback(frameNumber, callback);
return SUCCESS;

View File

@ -128,6 +128,10 @@ config("audio_policy_public_config") {
if (target_cpu == "arm") {
cflags += [ "-DBINDER_IPC_32BIT" ]
}
if (device_name == "baltimore") {
cflags += [ "-DDEVICE_BALTIMORE" ]
}
}
ohos_shared_library("audio_dump") {

View File

@ -16,9 +16,9 @@
<audioPolicyConfiguration version="1.0" xmlns:xi="http://www.w3.org/2001/XInclude">
<deviceclass name="primary">
<modules>
<module name="Speaker" lib="libmodule-hdi-sink.z.so" role="sink" channels="2" rate="48000" buffer_size="4096">
<module name="Speaker" lib="libmodule-hdi-sink.z.so" role="sink" channels="2" rate="44100" buffer_size="4096">
<Ports>
<Port adapter_name="internal" id="0" channels="2" rate="48000" buffer_size="4096"/>
<Port adapter_name="internal" id="0" channels="2" rate="44100" buffer_size="4096"/>
</Ports>
</module>
<module name="Built_in_mic" lib="libmodule-hdi-source.z.so" role="source" channels="2" rate="44100" buffer_size="8192">

View File

@ -78,6 +78,7 @@ public:
void OnAudioInterruptEnable(bool enable);
void OnDeviceStatusUpdated(DeviceType deviceType, bool connected, void *privData);
void OnServiceConnected();
int32_t SetAudioSessionCallback(AudioSessionCallback *callback);

View File

@ -23,6 +23,7 @@ namespace AudioStandard {
class IDeviceStatusObserver {
public:
virtual void OnDeviceStatusUpdated(DeviceType deviceType, bool connected, void *privData) = 0;
virtual void OnServiceConnected() = 0;
};
} // namespace AudioStandard
} // namespace OHOS

View File

@ -283,19 +283,6 @@ AudioScene AudioPolicyService::GetAudioScene() const
void AudioPolicyService::OnAudioPortAvailable(const AudioModuleInfo &moduleInfo)
{
MEDIA_INFO_LOG("Port detected for [%{public}s]", moduleInfo.name.c_str());
AudioIOHandle ioHandle = mAudioPolicyManager.OpenAudioPort(moduleInfo);
auto devType = GetDeviceType(moduleInfo.name);
if (devType == DeviceType::DEVICE_TYPE_SPEAKER || devType == DeviceType::DEVICE_TYPE_MIC) {
mAudioPolicyManager.SetDeviceActive(ioHandle, devType, moduleInfo.name, true);
// add new device into active device list
auto audioDescriptor = new(std::nothrow) AudioDeviceDescriptor(devType, GetDeviceRole(moduleInfo.role));
mActiveDevices.insert(mActiveDevices.begin(), audioDescriptor);
}
mIOHandles[moduleInfo.name] = ioHandle;
return;
}
@ -340,6 +327,30 @@ void AudioPolicyService::OnDeviceStatusUpdated(DeviceType devType, bool isConnec
MEDIA_INFO_LOG("output device list = [%{public}zu]", mActiveDevices.size());
}
void AudioPolicyService::OnServiceConnected()
{
MEDIA_INFO_LOG("HDI service started: load modules");
auto primaryModulesPos = deviceClassInfo_.find(ClassType::TYPE_PRIMARY);
if (primaryModulesPos != deviceClassInfo_.end()) {
auto moduleInfoList = primaryModulesPos->second;
for (auto &moduleInfo : moduleInfoList) {
MEDIA_INFO_LOG("Load modules: %{public}s", moduleInfo.name.c_str());
AudioIOHandle ioHandle = mAudioPolicyManager.OpenAudioPort(moduleInfo);
auto devType = GetDeviceType(moduleInfo.name);
if (devType == DeviceType::DEVICE_TYPE_SPEAKER || devType == DeviceType::DEVICE_TYPE_MIC) {
mAudioPolicyManager.SetDeviceActive(ioHandle, devType, moduleInfo.name, true);
// add new device into active device list
auto audioDescriptor = new(std::nothrow) AudioDeviceDescriptor(devType, GetDeviceRole(moduleInfo.role));
mActiveDevices.insert(mActiveDevices.begin(), audioDescriptor);
}
mIOHandles[moduleInfo.name] = ioHandle;
}
}
}
// Parser callbacks
void AudioPolicyService::OnXmlParsingCompleted(const std::unordered_map<ClassType, std::list<AudioModuleInfo>> &xmlData)
{

View File

@ -180,11 +180,6 @@ void XMLParser::ParsePort(xmlNode &node, AudioModuleInfo &moduleInfo)
}
portInfoList.push_back(moduleInfo);
// Open the ports for internal devices
if (deviceClassType_ == ClassType::TYPE_PRIMARY) {
mPortObserver.OnAudioPortAvailable(moduleInfo);
}
}
portNode = portNode->next;
}

View File

@ -22,6 +22,14 @@
namespace OHOS {
namespace AudioStandard {
namespace {
#ifdef DEVICE_BALTIMORE
const std::string AUDIO_HDI_SERVICE_NAME = "audio_adapter_service";
#else
const std::string AUDIO_HDI_SERVICE_NAME = "audio_hdi_service";
#endif
}
static DeviceType GetDeviceTypeByName(std::string deviceName)
{
DeviceType deviceType = DEVICE_TYPE_INVALID;
@ -44,6 +52,13 @@ static void OnServiceStatusReceived(struct ServiceStatusListener *listener,
MEDIA_DEBUG_LOG("[DeviceStatusListener] OnServiceStatusReceived in");
MEDIA_DEBUG_LOG("[DeviceStatusListener]: service name: %{public}s", serviceStatus->serviceName);
if (!AUDIO_HDI_SERVICE_NAME.compare(std::string(serviceStatus->serviceName))) {
if (serviceStatus->status == SERVIE_STATUS_START) {
DeviceStatusListener *deviceStatusListener = reinterpret_cast<DeviceStatusListener *>(listener->priv);
deviceStatusListener->deviceObserver_.OnServiceConnected();
}
}
DeviceType deviceType = GetDeviceTypeByName(serviceStatus->info);
if (deviceType != DEVICE_TYPE_INVALID) {
if ((serviceStatus->status == SERVIE_STATUS_START) || (serviceStatus->status == SERVIE_STATUS_STOP)) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

View File

@ -16,6 +16,7 @@ group("audio_unit_test") {
deps = [
"unittest/capturer_test:audio_capturer_unit_test",
"unittest/manager_test:audio_manager_unit_test",
"unittest/opensles_test:audio_opensles_unit_test",
"unittest/renderer_test:audio_renderer_unit_test",
"unittest/volume_change_test:audio_volume_change_unit_test",

View File

@ -20,6 +20,21 @@
namespace OHOS {
namespace AudioStandard {
class CapturerPositionCallbackTest : public CapturerPositionCallback {
public:
void OnMarkReached(const int64_t &framePosition) override {}
};
class CapturerPeriodPositionCallbackTest : public CapturerPeriodPositionCallback {
public:
void OnPeriodReached(const int64_t &frameNumber) override {}
};
class AudioCapturerCallbackTest : public AudioCapturerCallback {
public:
void OnStateChange(const CapturerState state) override {}
};
class AudioCapturerUnitTest : public testing::Test {
public:
// SetUpTestCase: Called before all test cases

View File

@ -32,10 +32,16 @@ namespace {
const string AUDIO_TIME_STABILITY_TEST_FILE = "/data/audiocapture_getaudiotime_stability_test.pcm";
const string AUDIO_FLUSH_STABILITY_TEST_FILE = "/data/audiocapture_flush_stability_test.pcm";
const int32_t READ_BUFFERS_COUNT = 128;
const int32_t VALUE_NEGATIVE = -1;
const int32_t VALUE_ZERO = 0;
const int32_t VALUE_HUNDRED = 100;
const int32_t VALUE_THOUSAND = 1000;
const int32_t CAPTURER_FLAG = 0;
constexpr uint64_t BUFFER_DURATION_FIVE = 5;
constexpr uint64_t BUFFER_DURATION_TEN = 10;
constexpr uint64_t BUFFER_DURATION_FIFTEEN = 15;
constexpr uint64_t BUFFER_DURATION_TWENTY = 20;
} // namespace
void AudioCapturerUnitTest::SetUpTestCase(void) {}
@ -1940,5 +1946,331 @@ HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStreamInfo_001, TestSize.Level1)
EXPECT_EQ(AudioSampleFormat::SAMPLE_U8, streamInfo.format);
EXPECT_EQ(AudioChannel::MONO, streamInfo.channels);
}
/**
* @tc.name : Test SetBufferDuration API
* @tc.number: Audio_Capturer_SetBufferDuration_001
* @tc.desc : Test SetBufferDuration interface. Check whether valid parameters are accepted.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetBufferDuration_001, TestSize.Level1)
{
int32_t ret = -1;
AudioCapturerOptions capturerOptions;
capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
capturerOptions.streamInfo.channels = AudioChannel::MONO;
capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
ASSERT_NE(nullptr, audioCapturer);
ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_FIVE);
EXPECT_EQ(SUCCESS, ret);
ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_TEN);
EXPECT_EQ(SUCCESS, ret);
ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_FIFTEEN);
EXPECT_EQ(SUCCESS, ret);
ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_TWENTY);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetBufferDuration API
* @tc.number: Audio_Capturer_SetBufferDuration_002
* @tc.desc : Test SetBufferDuration interface. Check whether invalid parameters are rejected.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetBufferDuration_002, TestSize.Level1)
{
int32_t ret = -1;
AudioCapturerOptions capturerOptions;
capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
capturerOptions.streamInfo.channels = AudioChannel::MONO;
capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
ASSERT_NE(nullptr, audioCapturer);
ret = audioCapturer->SetBufferDuration(VALUE_NEGATIVE);
EXPECT_NE(SUCCESS, ret);
ret = audioCapturer->SetBufferDuration(VALUE_ZERO);
EXPECT_NE(SUCCESS, ret);
ret = audioCapturer->SetBufferDuration(VALUE_HUNDRED);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPositionCallback_001
* @tc.desc : Test SetCapturerPositionCallback interface to check set position callback is success for valid callback.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_001, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<CapturerPositionCallbackTest> positionCB = std::make_shared<CapturerPositionCallbackTest>();
ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, positionCB);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPositionCallback_002
* @tc.desc : Test SetCapturerPositionCallback interface again after unregister.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_002, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<CapturerPositionCallbackTest> positionCB1 = std::make_shared<CapturerPositionCallbackTest>();
ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, positionCB1);
EXPECT_EQ(SUCCESS, ret);
audioCapturer->UnsetCapturerPositionCallback();
shared_ptr<CapturerPositionCallbackTest> positionCB2 = std::make_shared<CapturerPositionCallbackTest>();
ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, positionCB2);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPositionCallback_003
* @tc.desc : Test SetCapturerPositionCallback interface with null callback.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_003, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, nullptr);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPositionCallback_004
* @tc.desc : Test SetCapturerPositionCallback interface with invalid parameter.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_004, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<CapturerPositionCallbackTest> positionCB = std::make_shared<CapturerPositionCallbackTest>();
ret = audioCapturer->SetCapturerPositionCallback(VALUE_ZERO, positionCB);
EXPECT_NE(SUCCESS, ret);
ret = audioCapturer->SetCapturerPositionCallback(VALUE_NEGATIVE, positionCB);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPeriodPositionCallback API
* @tc.number: SetCapturerPeriodPositionCallback_001
* @tc.desc : Test SetCapturerPeriodPositionCallback interface to check set period position
* callback is success for valid callback.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_001, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<CapturerPeriodPositionCallbackTest> positionCB = std::make_shared<CapturerPeriodPositionCallbackTest>();
ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, positionCB);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPeriodPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPeriodPositionCallback_002
* @tc.desc : Test SetCapturerPeriodPositionCallback interface again after unregister.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_002, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<CapturerPeriodPositionCallbackTest> positionCB1 = std::make_shared<CapturerPeriodPositionCallbackTest>();
ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, positionCB1);
EXPECT_EQ(SUCCESS, ret);
audioCapturer->UnsetCapturerPeriodPositionCallback();
shared_ptr<CapturerPeriodPositionCallbackTest> positionCB2 = std::make_shared<CapturerPeriodPositionCallbackTest>();
ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, positionCB2);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPeriodPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPeriodPositionCallback_003
* @tc.desc : Test SetCapturerPeriodPositionCallback interface with null callback.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_003, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, nullptr);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerPeriodPositionCallback API
* @tc.number: Audio_Capturer_SetCapturerPeriodPositionCallback_004
* @tc.desc : Test SetCapturerPeriodPositionCallback interface with invalid parameter.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_004, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<CapturerPeriodPositionCallbackTest> positionCB = std::make_shared<CapturerPeriodPositionCallbackTest>();
ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_ZERO, positionCB);
EXPECT_NE(SUCCESS, ret);
ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_NEGATIVE, positionCB);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerCallback with null pointer.
* @tc.number: Audio_Capturer_SetCapturerCallback_001
* @tc.desc : Test SetCapturerCallback interface. Returns error code, if null pointer is set.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_001, TestSize.Level1)
{
int32_t ret = -1;
AudioCapturerOptions capturerOptions;
capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
capturerOptions.streamInfo.channels = AudioChannel::MONO;
capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
ASSERT_NE(nullptr, audioCapturer);
ret = audioCapturer->SetCapturerCallback(nullptr);
EXPECT_NE(SUCCESS, ret);
EXPECT_EQ(ERR_INVALID_PARAM, ret);
}
/**
* @tc.name : Test SetCapturerCallback with valid callback pointer.
* @tc.number: Audio_Capturer_SetCapturerCallback_002
* @tc.desc : Test SetCapturerCallback interface. Returns success, if valid callback is set.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_002, TestSize.Level1)
{
int32_t ret = -1;
AudioCapturerOptions capturerOptions;
capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
capturerOptions.streamInfo.channels = AudioChannel::MONO;
capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
ASSERT_NE(nullptr, audioCapturer);
shared_ptr<AudioCapturerCallbackTest> audioCapturerCB = std::make_shared<AudioCapturerCallbackTest>();
ret = audioCapturer->SetCapturerCallback(audioCapturerCB);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetCapturerCallback via illegal state, CAPTURER_RELEASED: After RELEASED
* @tc.number: Audio_Capturer_SetCapturerCallback_003
* @tc.desc : Test SetCapturerCallback interface. Returns error, if callback is set in released state.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_003, TestSize.Level1)
{
int32_t ret = -1;
AudioCapturerOptions capturerOptions;
capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
capturerOptions.streamInfo.channels = AudioChannel::MONO;
capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
ASSERT_NE(nullptr, audioCapturer);
bool isReleased = audioCapturer->Release();
EXPECT_EQ(true, isReleased);
CapturerState state = audioCapturer->GetStatus();
EXPECT_EQ(CAPTURER_RELEASED, state);
shared_ptr<AudioCapturerCallbackTest> audioCapturerCB = std::make_shared<AudioCapturerCallbackTest>();
ret = audioCapturer->SetCapturerCallback(audioCapturerCB);
EXPECT_NE(SUCCESS, ret);
EXPECT_EQ(ERR_ILLEGAL_STATE, ret);
}
/**
* @tc.name : Test SetCapturerCallback via legal state, CAPTURER_PREPARED: After PREPARED
* @tc.number: Audio_Capturer_SetCapturerCallback_004
* @tc.desc : Test SetCapturerCallback interface. Returns success, if callback is set in proper state.
*/
HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_004, TestSize.Level1)
{
int32_t ret = -1;
AudioCapturerOptions capturerOptions;
capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
capturerOptions.streamInfo.channels = AudioChannel::MONO;
capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
ASSERT_NE(nullptr, audioCapturer);
CapturerState state = audioCapturer->GetStatus();
EXPECT_EQ(CAPTURER_PREPARED, state);
shared_ptr<AudioCapturerCallbackTest> audioCapturerCB = std::make_shared<AudioCapturerCallbackTest>();
ret = audioCapturer->SetCapturerCallback(audioCapturerCB);
EXPECT_EQ(SUCCESS, ret);
}
} // namespace AudioStandard
} // namespace OHOS

View File

@ -0,0 +1,37 @@
# Copyright (C) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/test.gni")
module_output_path = "audio_standard/audio_manager"
ohos_unittest("audio_manager_unit_test") {
module_out_path = module_output_path
include_dirs = [
"./include",
"//foundation/multimedia/audio_standard/interfaces/inner_api/native/audiocommon/include",
"//foundation/multimedia/audio_standard/frameworks/native/common/include",
]
cflags = [
"-Wall",
"-Werror",
]
sources = [ "src/audio_manager_unit_test.cpp" ]
deps = [
"//foundation/multimedia/audio_standard/interfaces/inner_api/native/audiomanager:audio_client",
"//utils/native/base:utils",
]
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef AUDIO_MANAGER_UNIT_TEST_H
#define AUDIO_MANAGER_UNIT_TEST_H
#include "gtest/gtest.h"
namespace OHOS {
namespace AudioStandard {
class AudioManagerUnitTest : public testing::Test {
public:
// SetUpTestCase: Called before all test cases
static void SetUpTestCase(void);
// TearDownTestCase: Called after all test case
static void TearDownTestCase(void);
// SetUp: Called before each test cases
void SetUp(void);
// TearDown: Called after each test cases
void TearDown(void);
};
} // namespace AudioStandard
} // namespace OHOS
#endif // AUDIO_MANAGER_UNIT_TEST_H

View File

@ -0,0 +1,107 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "audio_manager_unit_test.h"
#include "audio_errors.h"
#include "audio_info.h"
#include "audio_system_manager.h"
using namespace std;
using namespace testing::ext;
namespace OHOS {
namespace AudioStandard {
namespace {
const uint32_t MIN_DEVICE_COUNT = 2;
const uint32_t MIN_INPUT_DEVICE_COUNT = 1;
const uint32_t MIN_OUTPUT_DEVICE_COUNT = 1;
}
void AudioManagerUnitTest::SetUpTestCase(void) {}
void AudioManagerUnitTest::TearDownTestCase(void) {}
void AudioManagerUnitTest::SetUp(void) {}
void AudioManagerUnitTest::TearDown(void) {}
/**
* @tc.name : Test GetDevices API
* @tc.number: GetConnectedDevicesList_001
* @tc.desc : Test GetDevices interface. Returns list of all input and output devices
*/
HWTEST(AudioManagerUnitTest, GetConnectedDevicesList_001, TestSize.Level0)
{
auto audioDeviceDescriptors = AudioSystemManager::GetInstance()->GetDevices(DeviceFlag::ALL_DEVICES_FLAG);
auto deviceCount = audioDeviceDescriptors.size();
EXPECT_GE(deviceCount, MIN_DEVICE_COUNT);
}
/**
* @tc.name : Test GetDevices API
* @tc.number: GetConnectedDevicesList_002
* @tc.desc : Test GetDevices interface. Returns list of input devices
*/
HWTEST(AudioManagerUnitTest, GetConnectedDevicesList_002, TestSize.Level0)
{
auto audioDeviceDescriptors = AudioSystemManager::GetInstance()->GetDevices(DeviceFlag::INPUT_DEVICES_FLAG);
auto deviceCount = audioDeviceDescriptors.size();
EXPECT_GE(deviceCount, MIN_INPUT_DEVICE_COUNT);
for (const auto &device : audioDeviceDescriptors) {
EXPECT_EQ(device->deviceRole_, DeviceRole::INPUT_DEVICE);
}
}
/**
* @tc.name : Test GetDevices API
* @tc.number: GetConnectedDevicesList_003
* @tc.desc : Test GetDevices interface. Returns list of output devices
*/
HWTEST(AudioManagerUnitTest, GetConnectedDevicesList_003, TestSize.Level0)
{
auto audioDeviceDescriptors = AudioSystemManager::GetInstance()->GetDevices(DeviceFlag::OUTPUT_DEVICES_FLAG);
auto deviceCount = audioDeviceDescriptors.size();
EXPECT_GE(deviceCount, MIN_OUTPUT_DEVICE_COUNT);
for (const auto &device : audioDeviceDescriptors) {
EXPECT_EQ(device->deviceRole_, DeviceRole::OUTPUT_DEVICE);
}
}
/**
* @tc.name : Test SetDeviceActive API
* @tc.number: SetDeviceActive_001
* @tc.desc : Test SetDeviceActive interface. Makes a device active
*/
HWTEST(AudioManagerUnitTest, SetDeviceActive_001, TestSize.Level0)
{
auto ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::SPEAKER, true);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test IsDeviceActive API
* @tc.number: IsDeviceActive_001
* @tc.desc : Test IsDeviceActive interface. Returns whether the mentioned device is currently active
*/
HWTEST(AudioManagerUnitTest, IsDeviceActive_001, TestSize.Level0)
{
auto ret = AudioSystemManager::GetInstance()->SetDeviceActive(ActiveDeviceType::SPEAKER, true);
EXPECT_EQ(SUCCESS, ret);
auto isActive = AudioSystemManager::GetInstance()->IsDeviceActive(ActiveDeviceType::SPEAKER);
EXPECT_TRUE(isActive);
}
} // namespace AudioStandard
} // namespace OHOS

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2021 Huawei Device Co., Ltd.
* Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

View File

@ -22,10 +22,20 @@ namespace OHOS {
namespace AudioStandard {
class AudioRendererCallbackTest : public AudioRendererCallback {
public:
void OnInterrupt(const InterruptEvent &interruptEvent) override;
void OnInterrupt(const InterruptEvent &interruptEvent) override {}
void OnStateChange(const RendererState state) override {}
};
class RendererPositionCallbackTest : public RendererPositionCallback {
public:
void OnMarkReached(const int64_t &framePosition) override {}
};
class RendererPeriodPositionCallbackTest : public RendererPeriodPositionCallback {
public:
void OnPeriodReached(const int64_t &frameNumber) override {}
};
class AudioRendererUnitTest : public testing::Test {
public:
// SetUpTestCase: Called before all test cases

View File

@ -28,6 +28,7 @@ namespace OHOS {
namespace AudioStandard {
namespace {
const string AUDIORENDER_TEST_FILE_PATH = "/data/test_44100_2.wav";
const int32_t VALUE_NEGATIVE = -1;
const int32_t VALUE_ZERO = 0;
const int32_t VALUE_HUNDRED = 100;
const int32_t VALUE_THOUSAND = 1000;
@ -36,9 +37,12 @@ namespace {
const int32_t WRITE_BUFFERS_COUNT = 500;
constexpr int32_t PAUSE_BUFFER_POSITION = 400000;
constexpr int32_t PAUSE_RENDER_TIME_SECONDS = 1;
} // namespace
void AudioRendererCallbackTest::OnInterrupt(const InterruptEvent &interruptEvent) {}
constexpr uint64_t BUFFER_DURATION_FIVE = 5;
constexpr uint64_t BUFFER_DURATION_TEN = 10;
constexpr uint64_t BUFFER_DURATION_FIFTEEN = 15;
constexpr uint64_t BUFFER_DURATION_TWENTY = 20;
} // namespace
void AudioRendererUnitTest::SetUpTestCase(void) {}
void AudioRendererUnitTest::TearDownTestCase(void) {}
@ -2714,11 +2718,21 @@ HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererCallback_001, TestSize.L
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
AudioRendererOptions rendererOptions;
rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
rendererOptions.streamInfo.channels = AudioChannel::STEREO;
rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
rendererOptions.rendererInfo.rendererFlags = RENDERER_FLAG;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
ASSERT_NE(nullptr, audioRenderer);
ret = audioRenderer->SetRendererCallback(nullptr);
EXPECT_NE(SUCCESS, ret);
EXPECT_EQ(ERR_INVALID_PARAM, ret);
}
/**
@ -2730,167 +2744,83 @@ HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererCallback_002, TestSize.L
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
AudioRendererOptions rendererOptions;
rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
rendererOptions.streamInfo.channels = AudioChannel::STEREO;
rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
rendererOptions.rendererInfo.rendererFlags = RENDERER_FLAG;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<AudioRendererCallbackTest> audioRendererCB = std::make_shared<AudioRendererCallbackTest>();
ret = audioRenderer->SetRendererCallback(audioRendererCB);
EXPECT_EQ(SUCCESS, ret);
ret = audioRenderer->SetRendererCallback(nullptr);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test start for 2 streams with both stream types as STREAM_MUSIC.
* @tc.name : Test SetRendererCallback via illegal state, RENDERER_RELEASED: After RELEASED
* @tc.number: Audio_Renderer_SetRendererCallback_003
* @tc.desc : Test interrupt interfaces. Allow concurrent streams if stream types are both STREAM_MUSIC.
* @tc.desc : Test SetRendererCallback interface. Returns error, if callback is set in released state.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererCallback_003, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer1 = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer1);
AudioRendererOptions rendererOptions;
rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
rendererOptions.streamInfo.channels = AudioChannel::STEREO;
rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
rendererOptions.rendererInfo.rendererFlags = RENDERER_FLAG;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
ASSERT_NE(nullptr, audioRenderer);
bool isReleased = audioRenderer->Release();
EXPECT_EQ(true, isReleased);
RendererState state = audioRenderer->GetStatus();
EXPECT_EQ(RENDERER_RELEASED, state);
shared_ptr<AudioRendererCallbackTest> audioRendererCB = std::make_shared<AudioRendererCallbackTest>();
ret = audioRenderer1->SetRendererCallback(audioRendererCB);
EXPECT_EQ(SUCCESS, ret);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer1);
EXPECT_EQ(SUCCESS, ret);
bool isStarted = audioRenderer1->Start();
EXPECT_EQ(true, isStarted);
unique_ptr<AudioRenderer> audioRenderer2 = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer2);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer2);
EXPECT_EQ(SUCCESS, ret);
isStarted = audioRenderer2->Start();
EXPECT_EQ(true, isStarted);
bool isReleased = audioRenderer1->Release();
EXPECT_EQ(true, isReleased);
isReleased = audioRenderer2->Release();
EXPECT_EQ(true, isReleased);
ret = audioRenderer->SetRendererCallback(audioRendererCB);
EXPECT_NE(SUCCESS, ret);
EXPECT_EQ(ERR_ILLEGAL_STATE, ret);
}
/**
* @tc.name : Test start for 2 streams with both stream types as STREAM_VOICE_CALL.
* @tc.name : Test SetRendererCallback via legal state, RENDERER_PREPARED: After PREPARED
* @tc.number: Audio_Renderer_SetRendererCallback_004
* @tc.desc : Test interrupt interfaces. Do not allow concurrent streams if stream types are both STREAM_VOICE_CALL.
* @tc.desc : Test SetRendererCallback interface. Returns success, if callback is set in proper state.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererCallback_004, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer1 = AudioRenderer::Create(STREAM_VOICE_CALL);
ASSERT_NE(nullptr, audioRenderer1);
AudioRendererOptions rendererOptions;
rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
rendererOptions.streamInfo.channels = AudioChannel::STEREO;
rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
rendererOptions.rendererInfo.rendererFlags = RENDERER_FLAG;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
ASSERT_NE(nullptr, audioRenderer);
RendererState state = audioRenderer->GetStatus();
EXPECT_EQ(RENDERER_PREPARED, state);
shared_ptr<AudioRendererCallbackTest> audioRendererCB = std::make_shared<AudioRendererCallbackTest>();
ret = audioRenderer1->SetRendererCallback(audioRendererCB);
ret = audioRenderer->SetRendererCallback(audioRendererCB);
EXPECT_EQ(SUCCESS, ret);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer1);
EXPECT_EQ(SUCCESS, ret);
bool isStarted = audioRenderer1->Start();
EXPECT_EQ(true, isStarted);
unique_ptr<AudioRenderer> audioRenderer2 = AudioRenderer::Create(STREAM_VOICE_CALL);
ASSERT_NE(nullptr, audioRenderer2);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer2);
EXPECT_EQ(SUCCESS, ret);
isStarted = audioRenderer2->Start();
EXPECT_EQ(false, isStarted);
bool isReleased = audioRenderer1->Release();
EXPECT_EQ(true, isReleased);
isReleased = audioRenderer2->Release();
EXPECT_EQ(true, isReleased);
}
/**
* @tc.name : Test start for 2 streams with both stream types as STREAM_RING.
* @tc.number: Audio_Renderer_SetRendererCallback_005
* @tc.desc : Test interrupt interfaces. Do not allow concurrent streams if stream types are both STREAM_RING.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererCallback_005, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer1 = AudioRenderer::Create(STREAM_RING);
ASSERT_NE(nullptr, audioRenderer1);
shared_ptr<AudioRendererCallbackTest> audioRendererCB = std::make_shared<AudioRendererCallbackTest>();
ret = audioRenderer1->SetRendererCallback(audioRendererCB);
EXPECT_EQ(SUCCESS, ret);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer1);
EXPECT_EQ(SUCCESS, ret);
bool isStarted = audioRenderer1->Start();
EXPECT_EQ(true, isStarted);
unique_ptr<AudioRenderer> audioRenderer2 = AudioRenderer::Create(STREAM_RING);
ASSERT_NE(nullptr, audioRenderer2);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer2);
EXPECT_EQ(SUCCESS, ret);
isStarted = audioRenderer2->Start();
EXPECT_EQ(false, isStarted);
bool isReleased = audioRenderer1->Release();
EXPECT_EQ(true, isReleased);
isReleased = audioRenderer2->Release();
EXPECT_EQ(true, isReleased);
}
/**
* @tc.name : Test start of STREAM_VOICE_CALL and STREAM_RING.
* @tc.number: Audio_Renderer_SetRendererCallback_006
* @tc.desc : Test interrupt interfaces. Do not allow STREAM_RING to start if STREAM_VOICE_CALL already running.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererCallback_006, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer1 = AudioRenderer::Create(STREAM_VOICE_CALL);
ASSERT_NE(nullptr, audioRenderer1);
shared_ptr<AudioRendererCallbackTest> audioRendererCB = std::make_shared<AudioRendererCallbackTest>();
ret = audioRenderer1->SetRendererCallback(audioRendererCB);
EXPECT_EQ(SUCCESS, ret);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer1);
EXPECT_EQ(SUCCESS, ret);
bool isStarted = audioRenderer1->Start();
EXPECT_EQ(true, isStarted);
unique_ptr<AudioRenderer> audioRenderer2 = AudioRenderer::Create(STREAM_RING);
ASSERT_NE(nullptr, audioRenderer2);
ret = AudioRendererUnitTest::InitializeRenderer(audioRenderer2);
EXPECT_EQ(SUCCESS, ret);
isStarted = audioRenderer2->Start();
EXPECT_EQ(false, isStarted);
bool isReleased = audioRenderer1->Release();
EXPECT_EQ(true, isReleased);
isReleased = audioRenderer2->Release();
EXPECT_EQ(true, isReleased);
}
/**
@ -2947,5 +2877,223 @@ HWTEST(AudioRendererUnitTest, Audio_Renderer_GetStreamInfo_001, TestSize.Level1)
EXPECT_EQ(AudioSampleFormat::SAMPLE_U8, streamInfo.format);
EXPECT_EQ(AudioChannel::MONO, streamInfo.channels);
}
/**
* @tc.name : Test SetBufferDuration API
* @tc.number: Audio_Renderer_SetBufferDuration_001
* @tc.desc : Test SetBufferDuration interface. Check whether valid parameters are accepted.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetBufferDuration_001, TestSize.Level1)
{
int32_t ret = -1;
AudioRendererOptions rendererOptions;
rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
rendererOptions.streamInfo.channels = AudioChannel::MONO;
rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
rendererOptions.rendererInfo.rendererFlags = RENDERER_FLAG;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
EXPECT_NE(nullptr, audioRenderer);
ret = audioRenderer->SetBufferDuration(BUFFER_DURATION_FIVE);
EXPECT_EQ(SUCCESS, ret);
ret = audioRenderer->SetBufferDuration(BUFFER_DURATION_TEN);
EXPECT_EQ(SUCCESS, ret);
ret = audioRenderer->SetBufferDuration(BUFFER_DURATION_FIFTEEN);
EXPECT_EQ(SUCCESS, ret);
ret = audioRenderer->SetBufferDuration(BUFFER_DURATION_TWENTY);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetBufferDuration API
* @tc.number: Audio_Renderer_SetBufferDuration_002
* @tc.desc : Test SetBufferDuration interface. Check whether invalid parameters are rejected.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetBufferDuration_002, TestSize.Level1)
{
int32_t ret = -1;
AudioRendererOptions rendererOptions;
rendererOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
rendererOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
rendererOptions.streamInfo.channels = AudioChannel::MONO;
rendererOptions.rendererInfo.contentType = ContentType::CONTENT_TYPE_MUSIC;
rendererOptions.rendererInfo.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
rendererOptions.rendererInfo.rendererFlags = RENDERER_FLAG;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(rendererOptions);
EXPECT_NE(nullptr, audioRenderer);
ret = audioRenderer->SetBufferDuration(VALUE_NEGATIVE);
EXPECT_NE(SUCCESS, ret);
ret = audioRenderer->SetBufferDuration(VALUE_ZERO);
EXPECT_NE(SUCCESS, ret);
ret = audioRenderer->SetBufferDuration(VALUE_HUNDRED);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPositionCallback_001
* @tc.desc : Test SetRendererPositionCallback interface to check set position callback is success for valid callback.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPositionCallback_001, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<RendererPositionCallbackTest> positionCB = std::make_shared<RendererPositionCallbackTest>();
ret = audioRenderer->SetRendererPositionCallback(VALUE_THOUSAND, positionCB);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPositionCallback_002
* @tc.desc : Test SetRendererPositionCallback interface again after unregister.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPositionCallback_002, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<RendererPositionCallbackTest> positionCB1 = std::make_shared<RendererPositionCallbackTest>();
ret = audioRenderer->SetRendererPositionCallback(VALUE_THOUSAND, positionCB1);
EXPECT_EQ(SUCCESS, ret);
audioRenderer->UnsetRendererPositionCallback();
shared_ptr<RendererPositionCallbackTest> positionCB2 = std::make_shared<RendererPositionCallbackTest>();
ret = audioRenderer->SetRendererPositionCallback(VALUE_THOUSAND, positionCB2);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPositionCallback_003
* @tc.desc : Test SetRendererPositionCallback interface with null callback.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPositionCallback_003, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
ret = audioRenderer->SetRendererPositionCallback(VALUE_THOUSAND, nullptr);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPositionCallback_004
* @tc.desc : Test SetRendererPositionCallback interface with invalid parameter.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPositionCallback_004, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<RendererPositionCallbackTest> positionCB = std::make_shared<RendererPositionCallbackTest>();
ret = audioRenderer->SetRendererPositionCallback(VALUE_ZERO, positionCB);
EXPECT_NE(SUCCESS, ret);
ret = audioRenderer->SetRendererPositionCallback(VALUE_NEGATIVE, positionCB);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPeriodPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPeriodPositionCallback_001
* @tc.desc : Test SetRendererPeriodPositionCallback interface to check set period position
* callback is success for valid callback.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPeriodPositionCallback_001, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<RendererPeriodPositionCallbackTest> positionCB = std::make_shared<RendererPeriodPositionCallbackTest>();
ret = audioRenderer->SetRendererPeriodPositionCallback(VALUE_THOUSAND, positionCB);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPeriodPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPeriodPositionCallback_002
* @tc.desc : Test SetRendererPeriodPositionCallback interface again after unregister.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPeriodPositionCallback_002, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<RendererPeriodPositionCallbackTest> positionCB1 = std::make_shared<RendererPeriodPositionCallbackTest>();
ret = audioRenderer->SetRendererPeriodPositionCallback(VALUE_THOUSAND, positionCB1);
EXPECT_EQ(SUCCESS, ret);
audioRenderer->UnsetRendererPeriodPositionCallback();
shared_ptr<RendererPeriodPositionCallbackTest> positionCB2 = std::make_shared<RendererPeriodPositionCallbackTest>();
ret = audioRenderer->SetRendererPeriodPositionCallback(VALUE_THOUSAND, positionCB2);
EXPECT_EQ(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPeriodPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPeriodPositionCallback_003
* @tc.desc : Test SetRendererPeriodPositionCallback interface with null callback.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPeriodPositionCallback_003, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
ret = audioRenderer->SetRendererPeriodPositionCallback(VALUE_THOUSAND, nullptr);
EXPECT_NE(SUCCESS, ret);
}
/**
* @tc.name : Test SetRendererPeriodPositionCallback API
* @tc.number: Audio_Renderer_SetRendererPeriodPositionCallback_004
* @tc.desc : Test SetRendererPeriodPositionCallback interface with invalid parameter.
*/
HWTEST(AudioRendererUnitTest, Audio_Renderer_SetRendererPeriodPositionCallback_004, TestSize.Level1)
{
int32_t ret = -1;
unique_ptr<AudioRenderer> audioRenderer = AudioRenderer::Create(STREAM_MUSIC);
ASSERT_NE(nullptr, audioRenderer);
shared_ptr<RendererPeriodPositionCallbackTest> positionCB = std::make_shared<RendererPeriodPositionCallbackTest>();
ret = audioRenderer->SetRendererPeriodPositionCallback(VALUE_ZERO, positionCB);
EXPECT_NE(SUCCESS, ret);
ret = audioRenderer->SetRendererPeriodPositionCallback(VALUE_NEGATIVE, positionCB);
EXPECT_NE(SUCCESS, ret);
}
} // namespace AudioStandard
} // namespace OHOS