Set buffer size in milliseconds for capturer and renderer

Signed-off-by: Ashish <ashish.sawakar@huawei.com>
This commit is contained in:
Ashish 2022-02-15 23:43:38 +05:30
parent d767bc1b3e
commit f06e84a07a
11 changed files with 86 additions and 6 deletions

View File

@ -43,6 +43,7 @@ public:
int32_t SetCapturerPeriodPositionCallback(int64_t frameNumber,
const std::shared_ptr<CapturerPeriodPositionCallback> &callback) override;
void UnsetCapturerPeriodPositionCallback() override;
int32_t SetBufferDuration(uint64_t bufferDuration) const override;
std::shared_ptr<AudioStream> audioStream_;
AudioCapturerInfo capturerInfo_ = {};

View File

@ -216,6 +216,15 @@ int32_t AudioCapturerPrivate::GetBufferSize(size_t &bufferSize) const
return audioStream_->GetBufferSize(bufferSize);
}
int32_t AudioCapturerPrivate::SetBufferDuration(uint64_t bufferDuration) const
{
if (bufferDuration < MINIMUM_BUFFER_SIZE_MSEC || bufferDuration > MAXIMUM_BUFFER_SIZE_MSEC) {
MEDIA_ERR_LOG("Error: Please set the buffer duration between 5ms ~ 20ms");
return ERR_INVALID_PARAM;
}
return audioStream_->SetBufferSizeInMsec(bufferDuration);
}
void AudioStreamCallbackCapturer::SaveCallback(const std::weak_ptr<AudioCapturerCallback> &callback)
{
callback_ = callback;

View File

@ -55,6 +55,7 @@ public:
int32_t SetRendererPeriodPositionCallback(int64_t frameNumber,
const std::shared_ptr<RendererPeriodPositionCallback> &callback) override;
void UnsetRendererPeriodPositionCallback() override;
int32_t SetBufferDuration(uint64_t bufferDuration) const override;
std::shared_ptr<AudioStream> audioStream_;
AudioRendererInfo rendererInfo_ = {};

View File

@ -339,6 +339,16 @@ AudioRendererRate AudioRendererPrivate::GetRenderRate() const
return audioStream_->GetRenderRate();
}
int32_t AudioRendererPrivate::SetBufferDuration(uint64_t bufferDuration) const
{
if (bufferDuration < MINIMUM_BUFFER_SIZE_MSEC || bufferDuration > MAXIMUM_BUFFER_SIZE_MSEC) {
MEDIA_ERR_LOG("Error: Please set the buffer duration between 5ms ~ 20ms");
return ERR_INVALID_PARAM;
}
return audioStream_->SetBufferSizeInMsec(bufferDuration);
}
AudioInterruptCallbackImpl::AudioInterruptCallbackImpl(const std::shared_ptr<AudioStream> &audioStream,
const AudioInterrupt &audioInterrupt)
: audioStream_(audioStream), audioInterrupt_(audioInterrupt)

View File

@ -277,6 +277,16 @@ public:
*/
virtual void UnsetCapturerPeriodPositionCallback() = 0;
/**
* @brief set the buffer duration for capturer, minimum buffer duration is 5msec
* maximum is 20msec
*
* @param bufferDuration Indicates a buffer duration to be set for capturer
* @return Returns {@link SUCCESS} if bufferDuration is successfully set; returns an error code
* defined in {@link audio_errors.h} otherwise.
*/
virtual int32_t SetBufferDuration(uint64_t bufferDuration) const = 0;
/**
* @brief Obtains the capturer supported formats.
*

View File

@ -29,6 +29,9 @@ namespace OHOS {
namespace AudioStandard {
constexpr int32_t MAX_NUM_STREAMS = 3;
constexpr int32_t RENDERER_STREAM_USAGE_SHIFT = 16;
constexpr int32_t MINIMUM_BUFFER_SIZE_MSEC = 5;
constexpr int32_t MAXIMUM_BUFFER_SIZE_MSEC = 20;
enum DeviceFlag {
/**

View File

@ -365,6 +365,16 @@ public:
*/
virtual void UnsetRendererPeriodPositionCallback() = 0;
/**
* @brief set the buffer duration for renderer, minimum buffer duration is 5msec
* maximum is 20msec
*
* @param bufferDuration Indicates a buffer duration to be set for renderer
* @return Returns {@link SUCCESS} if bufferDuration is successfully set; returns an error code
* defined in {@link audio_errors.h} otherwise.
*/
virtual int32_t SetBufferDuration(uint64_t bufferDuration) const = 0;
/**
* @brief Obtains the formats supported by renderer.
*

View File

@ -423,6 +423,14 @@ public:
*/
AudioRendererRate GetStreamRenderRate();
/**
* @brief Set the buffer duration in msec
*
* @param bufferSizeInMsec buffer size in duration.
* @return Returns {@link SUCCESS} defined in {@link audio_errors.h} otherwise.
*/
int32_t SetBufferSizeInMsec(int32_t bufferSizeInMsec);
void SaveStreamCallback(const std::weak_ptr<AudioStreamCallback> &callback);
// Audio timer callback
@ -442,6 +450,7 @@ private:
const void *internalReadBuffer;
size_t internalRdBufLen;
size_t internalRdBufIndex;
size_t setBufferSize = MINIMUM_BUFFER_SIZE;
int32_t streamCmdStatus;
int32_t streamDrainStatus;
int32_t streamFlushStatus;

View File

@ -1220,6 +1220,13 @@ int32_t AudioServiceClient::ReleaseStream()
return AUDIO_CLIENT_SUCCESS;
}
int32_t AudioServiceClient::SetBufferSizeInMsec(int32_t bufferSizeInMsec)
{
size_t bufferSize = pa_usec_to_bytes(bufferSizeInMsec * PA_USEC_PER_MSEC, &sampleSpec);
setBufferSize = bufferSize;
return AUDIO_CLIENT_SUCCESS;
}
int32_t AudioServiceClient::GetMinimumBufferSize(size_t &minBufferSize)
{
CHECK_PA_STATUS_RET_IF_FAIL(mainLoop, context, paStream, AUDIO_CLIENT_PA_ERR);
@ -1232,7 +1239,7 @@ int32_t AudioServiceClient::GetMinimumBufferSize(size_t &minBufferSize)
}
if (eAudioClientType == AUDIO_SERVICE_CLIENT_PLAYBACK) {
minBufferSize = (size_t)MINIMUM_BUFFER_SIZE;
minBufferSize = setBufferSize;
}
if (eAudioClientType == AUDIO_SERVICE_CLIENT_RECORD) {
@ -1255,7 +1262,7 @@ int32_t AudioServiceClient::GetMinimumFrameCount(uint32_t &frameCount)
}
if (eAudioClientType == AUDIO_SERVICE_CLIENT_PLAYBACK) {
minBufferSize = (size_t)MINIMUM_BUFFER_SIZE;
minBufferSize = setBufferSize;
}
if (eAudioClientType == AUDIO_SERVICE_CLIENT_RECORD) {

View File

@ -24,10 +24,13 @@ using namespace std;
using namespace std::chrono;
using namespace OHOS;
using namespace OHOS::AudioStandard;
int32_t bufferMsec = 0;
namespace AudioTestConstants {
constexpr int32_t FIRST_ARG_IDX = 1;
constexpr int32_t SECOND_ARG_IDX = 2;
constexpr int32_t THIRD_ARG_IDX = 3;
constexpr int32_t FOURTH_ARG_IDX = 4;
constexpr int32_t NUM_BASE = 10;
constexpr int32_t PAUSE_BUFFER_POSITION = 128;
constexpr int32_t PAUSE_READ_TIME_SECONDS = 2;
constexpr int32_t SUCCESS = 0;
@ -192,6 +195,11 @@ public:
}
CheckSupportedParams();
MEDIA_ERR_LOG("AudioCaptruerTest: buffermsec = %{public}d", bufferMsec);
int32_t status = audioCapturer->SetBufferDuration(bufferMsec);
if (status) {
MEDIA_ERR_LOG("Failed to set buffer duration");
}
if (!InitCapture(audioCapturer)) {
MEDIA_ERR_LOG("Initialize capturer failed");
@ -240,14 +248,16 @@ int main(int argc, char *argv[])
}
MEDIA_INFO_LOG("argc=%d", argc);
MEDIA_INFO_LOG("argv[1]=%{public}s", argv[1]);
MEDIA_INFO_LOG("argv[1]=%{public}s", argv[AudioTestConstants::FIRST_ARG_IDX]);
MEDIA_INFO_LOG("argv[2]=%{public}s", argv[AudioTestConstants::SECOND_ARG_IDX]);
MEDIA_INFO_LOG("argv[3]=%{public}s", argv[AudioTestConstants::THIRD_ARG_IDX]);
int32_t samplingRate = atoi(argv[AudioTestConstants::SECOND_ARG_IDX]);
bool isBlocking = (atoi(argv[AudioTestConstants::THIRD_ARG_IDX]) == 1);
string filePath = argv[AudioTestConstants::SECOND_ARG_IDX - 1];
string filePath = argv[AudioTestConstants::FIRST_ARG_IDX];
if (argc > AudioTestConstants::FOURTH_ARG_IDX) {
bufferMsec = strtol(argv[AudioTestConstants::FOURTH_ARG_IDX], nullptr, AudioTestConstants::NUM_BASE);
}
AudioCapturerTest testObj;
bool ret = testObj.TestRecording(samplingRate, isBlocking, filePath);

View File

@ -27,6 +27,7 @@ namespace {
constexpr int32_t ARGS_INDEX_TWO = 2;
constexpr int32_t ARGS_COUNT_TWO = 2;
constexpr int32_t ARGS_COUNT_THREE = 3;
constexpr int32_t ARGS_COUNT_FOUR = 4;
constexpr int32_t SUCCESS = 0;
constexpr int32_t STOP_BUFFER_POSITION = 700000;
constexpr int32_t PAUSE_BUFFER_POSITION = 1400000;
@ -267,6 +268,10 @@ public:
contentType = static_cast<ContentType>(strtol(argv[ARGS_INDEX_TWO], NULL, numBase));
streamUsage = static_cast<StreamUsage>(strtol(argv[ARGS_INDEX_THREE], NULL, numBase));
}
int32_t bufferMsec = 0;
if (argc > ARGS_COUNT_FOUR) {
bufferMsec = strtol(argv[ARGS_COUNT_FOUR], NULL, numBase);
}
AudioRendererOptions rendererOptions = {};
rendererOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
@ -295,6 +300,11 @@ public:
GetRendererStreamInfo(audioRenderer);
CheckSupportedParams();
MEDIA_ERR_LOG("AudioRendererTest: buffermsec = %{public}d", bufferMsec);
int32_t status = audioRenderer->SetBufferDuration(bufferMsec);
if (status) {
MEDIA_ERR_LOG("Failed to set buffer duration");
}
if (!InitRender(audioRenderer)) {
MEDIA_ERR_LOG("AudioRendererTest: Init render failed");