CAudioInfo: Refactor getters to return in group 27/296627/3
authorSeungbae Shin <seungbae.shin@samsung.com>
Wed, 2 Aug 2023 09:22:20 +0000 (18:22 +0900)
committerSeungbae Shin <seungbae.shin@samsung.com>
Mon, 7 Aug 2023 02:16:08 +0000 (11:16 +0900)
[Version] 0.5.61
[Issue Type] Refactoring

Change-Id: I0b3e644436a8037164dc8148fe16ba31ee33501c

include/CAudioInfo.h
packaging/capi-media-audio-io.spec
src/cpp/CAudioInfo.cpp
src/cpp/CAudioInput.cpp
src/cpp/CAudioOutput.cpp
src/cpp/CPulseAudioClient.cpp
src/cpp/CPulseStreamSpec.cpp
src/cpp/cpp_audio_io.cpp

index 1214bf1dc98e00e25ca9b9a86379473208994b2e..fbe2439f0365961930482aded103870b37726b00 100644 (file)
 
 #include <type_traits>
 #include <string>
+#include <optional>
+#include <tuple>
+
 #include <sound_manager.h>
 #include <sound_manager_internal.h>
-#include <optional>
 
 namespace tizen_media_audio {
 
@@ -134,25 +136,27 @@ namespace tizen_media_audio {
         CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex);
         virtual ~CAudioInfo() = default;
 
-        /* Setter & Getter */
-        unsigned int getSampleRate() const noexcept;
-        EChannel getChannel() const noexcept;
-        ESampleType getSampleType() const noexcept;
-        EAudioType getAudioType() const noexcept;
-        int getAudioIndex() const noexcept;
-        void setAudioIndex(int audioIndex) noexcept;
+        /* Audio Specification */
+        std::tuple<ESampleType, unsigned int, EChannel> getSpec() const noexcept;
         int getSampleSize() const noexcept;
 
+        /* Stream Properties */
+        void setAudioIndex(int audioIndex) noexcept;
+        void setAudioTypeByInputStreamType(const char* streamType);
+        void setAudioTypeByOutputStreamType(const char* streamType);
+        EAudioType getAudioType() const noexcept;
+        std::pair<const char*, int> getStreamProperties() const;
+
         /* Effect */
         void setEffectMethod(int method) noexcept;
         std::string getEffectMethod() const noexcept;
+
         void setEffectMethodWithReference(sound_effect_method_with_reference_e method, int id) noexcept;
         std::pair<std::string, int> getEffectMethodWithReference() const noexcept;
 
-        /* Setter & Getter Utilities */
-        const char* getConvertedStreamType() const;
-        void setAudioTypeByInputStreamType(const char* streamType);
-        void setAudioTypeByOutputStreamType(const char* streamType);
+        /* Utilities */
+        size_t msToBytes(size_t milliseconds) const noexcept;
+        size_t bytesToMs(size_t bytes) const noexcept;
 
     private:
         const char *__STREAM_TYPE_TABLE[(unsigned int)EAudioType::AUDIO_TYPE_MAX] = {
index aaf63e1ef3053b7cb9731f0a14456cbc8503311a..47bbde7dc7cc2e97cce785ba3de0b0700b9d43dd 100644 (file)
@@ -1,6 +1,6 @@
 Name:           capi-media-audio-io
 Summary:        An Audio Input & Audio Output library in Tizen Native API
-Version:        0.5.60
+Version:        0.5.61
 Release:        0
 Group:          Multimedia/API
 License:        Apache-2.0
index 8a201994616693007660b5f64f8fdc65e7f3325b..8cf23d4be9e2514d44223fc9073e8e5de618dcc7 100644 (file)
@@ -36,37 +36,29 @@ CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sa
     __mAudioType(audioType),
     __mAudioIndex(audioIndex) {
     // Check to invalid AudioInfo
-    if (sampleRate < CAudioInfo::MIN_SYSTEM_SAMPLERATE ||
-        sampleRate > CAudioInfo::MAX_SYSTEM_SAMPLERATE)
+    if (sampleRate < MIN_SYSTEM_SAMPLERATE ||
+        sampleRate > MAX_SYSTEM_SAMPLERATE)
         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
                                "The sampleRate is invalid [sampleRate:%u]", sampleRate);
 
-    if (channel < CAudioInfo::EChannel::CHANNEL_MONO ||
-        channel >= CAudioInfo::EChannel::CHANNEL_MAX)
+    if (channel < EChannel::CHANNEL_MONO ||
+        channel >= EChannel::CHANNEL_MAX)
         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
                                "The channel is invalid [channel:%u]", to_integral(channel));
 
-    if (sampleType < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 ||
-        sampleType >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX)
+    if (sampleType < ESampleType::SAMPLE_TYPE_U8 ||
+        sampleType >= ESampleType::SAMPLE_TYPE_MAX)
         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
                                "The sampleType is invalid [sampleType:%u]", to_integral(sampleType));
 
-    if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
-        audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
+    if (audioType < EAudioType::AUDIO_IN_TYPE_MEDIA ||
+        audioType >= EAudioType::AUDIO_TYPE_MAX)
         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
                                "The audioType is invalid [audioType:%u]", to_integral(audioType));
 }
 
-unsigned int CAudioInfo::getSampleRate() const noexcept {
-    return __mSampleRate;
-}
-
-CAudioInfo::EChannel CAudioInfo::getChannel() const noexcept {
-    return __mChannel;
-}
-
-CAudioInfo::ESampleType CAudioInfo::getSampleType() const noexcept {
-    return __mSampleType;
+std::tuple<CAudioInfo::ESampleType, unsigned int, CAudioInfo::EChannel> CAudioInfo::getSpec() const noexcept {
+    return { __mSampleType, __mSampleRate, __mChannel };
 }
 
 CAudioInfo::EAudioType CAudioInfo::getAudioType() const noexcept {
@@ -81,10 +73,6 @@ void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
     __mAudioType = convertOutputStreamTypeToAudioType(streamType);
 }
 
-int CAudioInfo::getAudioIndex() const noexcept {
-    return __mAudioIndex;
-}
-
 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
     __mAudioIndex = audioIndex;
 }
@@ -115,6 +103,15 @@ int CAudioInfo::getSampleSize() const noexcept {
     return bytes_in_sample * static_cast<int>(__mChannel);
 }
 
+
+size_t CAudioInfo::msToBytes(size_t milliseconds) const noexcept {
+    return milliseconds * __mSampleRate * getSampleSize() / 1000;
+}
+
+size_t CAudioInfo::bytesToMs(size_t bytes) const noexcept {
+    return bytes * 1000 / getSampleSize() / __mSampleRate;
+}
+
 void CAudioInfo::setEffectMethod(int method) noexcept {
     __mEffectMethod = method;
 }
@@ -155,32 +152,32 @@ std::pair<std::string, int> CAudioInfo::getEffectMethodWithReference() const noe
     return make_pair(method, __mEffectMethodReferenceDeviceId);
 }
 
-const char* CAudioInfo::getConvertedStreamType() const {
-    if (__mAudioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
-        __mAudioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
+std::pair<const char*, int> CAudioInfo::getStreamProperties() const {
+    if (__mAudioType < EAudioType::AUDIO_IN_TYPE_MEDIA ||
+        __mAudioType >= EAudioType::AUDIO_TYPE_MAX)
         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
                                "The audioType is not supported [audioType:%u]", to_integral(__mAudioType));
 
-    return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
+    return std::make_pair(__STREAM_TYPE_TABLE[(unsigned int)__mAudioType], __mAudioIndex);
 }
 
 CAudioInfo::EAudioType CAudioInfo::convertInputStreamTypeToAudioType(const char *streamType) {
-    for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
-              i < (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
+    for (auto i = (unsigned int)EAudioType::AUDIO_IN_TYPE_MEDIA;
+              i < (unsigned int)EAudioType::AUDIO_OUT_TYPE_MEDIA;
               i++) {
         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
-            return (CAudioInfo::EAudioType)i;
+            return (EAudioType)i;
     }
     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
                            "The streamType of input is not supported [streamType:%s]", streamType);
 }
 
 CAudioInfo::EAudioType CAudioInfo::convertOutputStreamTypeToAudioType(const char *streamType) {
-    for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
-              i < (unsigned int)CAudioInfo::EAudioType::AUDIO_TYPE_MAX;
+    for (auto i = (unsigned int)EAudioType::AUDIO_OUT_TYPE_MEDIA;
+              i < (unsigned int)EAudioType::AUDIO_TYPE_MAX;
               i++) {
         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
-            return (CAudioInfo::EAudioType)i;
+            return (EAudioType)i;
     }
     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
                            "The streamType of output is not supported [streamType:%s]", streamType);
index 201585faf0261e3b32428e6e58ae4d467451d5ac..e00ec0724e964db739ce459044615478e53abd1b 100644 (file)
@@ -238,7 +238,7 @@ int CAudioInput::getBufferSize() {
         THROW_ERROR_MSG(CAudioError::EError::ERROR_NOT_INITIALIZED, "Did not initialize CAudioInput"); //LCOV_EXCL_LINE
 
     /* FIXME : return calculated size here to satisfy backward compatibility */
-    return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
+    return mAudioInfo.msToBytes(DEFAULT_PERIOD_SIZE);
 }
 
 void CAudioInput::setStreamCallback(SStreamCallback callback) {
index 2aa032e594eadfb17333574ff7f3e66baf3f99d1..0cd8e4e747b900e35f676ed4661991cac29928f6 100644 (file)
@@ -276,7 +276,7 @@ int CAudioOutput::getBufferSize() {
                         "Did not initialize or prepare CAudioOutput"); //LCOV_EXCL_LINE
 
     /* FIXME : return calculated size here to satisfy backward compatibility */
-    return (mAudioInfo.getSampleRate() * DEFAULT_PERIOD_SIZE) / 1000 * mAudioInfo.getSampleSize();
+    return mAudioInfo.msToBytes(DEFAULT_PERIOD_SIZE);
 }
 
 size_t CAudioOutput::write(const void* buffer, size_t length) {
@@ -385,8 +385,7 @@ size_t CAudioOutput::write(const void* buffer, size_t length) {
 
 void CAudioOutput::__dumpStat() noexcept {
     AUDIO_IO_LOGD("pClient[%p] : total written %5" PRIu64 " times, %10" PRIu64 " bytes, %7" PRIu64 " ms",
-                  mpPulseAudioClient, __mTotalWrittenCount, __mTotalWrittenBytes,
-                  __mTotalWrittenBytes * 1000 / mAudioInfo.getSampleSize() / mAudioInfo.getSampleRate());
+                  mpPulseAudioClient, __mTotalWrittenCount, __mTotalWrittenBytes, mAudioInfo.bytesToMs(__mTotalWrittenBytes));
 }
 
 void CAudioOutput::__dumpStat(size_t length) noexcept {
index 08221ec2d32d3444b15ec14fe9b6254014473cb8..7c1796a1ff28b2ca662e38407862e9b48adcfe3c 100644 (file)
@@ -392,11 +392,11 @@ void CPulseAudioClient::initialize() {
             THROW_ERROR_MSG(CAudioError::EError::ERROR_OUT_OF_MEMORY, "Failed pa_proplist_new()"); //LCOV_EXCL_LINE
 
         // Adds values on proplist for delivery to PULSEAUDIO
-        pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, __mSpec.getAudioInfo().getConvertedStreamType());
+        auto [ streamType, streamIndex ] = __mSpec.getAudioInfo().getStreamProperties();
 
-        int index = __mSpec.getAudioInfo().getAudioIndex();
-        if (index >= 0)
-            pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%u", (unsigned int) index);
+        pa_proplist_sets(__mpPropList, PA_PROP_MEDIA_ROLE, streamType);
+        if (streamIndex >= 0)
+            pa_proplist_setf(__mpPropList, PA_PROP_MEDIA_PARENT_ID, "%d", streamIndex);
 
         if (__mDirection == EStreamDirection::STREAM_DIRECTION_RECORD) {
             /* Noise-suppression effect should be set first. */
index 0d295b931ecdb73b076ffe9f6c13eb2924ae2975..9dcf29f778379e29b6133c2a1b3050e88a010bdd 100644 (file)
@@ -49,14 +49,17 @@ CPulseStreamSpec::CPulseStreamSpec(EStreamLatency latency, CAudioInfo& audioInfo
 }
 
 void CPulseStreamSpec::__adjustSpec() noexcept {
+
+    auto [ sampletype, samplerate, channels ] = __mAudioInfo.getSpec();
+
     // Sets a sampleRate
-    __mSampleSpec.rate = __mAudioInfo.getSampleRate();
+    __mSampleSpec.rate = samplerate;
 
     // Convert channels for PA
-    __mSampleSpec.channels = static_cast<uint8_t>(__mAudioInfo.getChannel());
+    __mSampleSpec.channels = static_cast<uint8_t>(channels);
 
     // Convert format for PA
-    switch (__mAudioInfo.getSampleType()) {
+    switch (sampletype) {
     case CAudioInfo::ESampleType::SAMPLE_TYPE_U8:
         __mSampleSpec.format = PA_SAMPLE_U8;
         break;
index 86fbf78fbd20539ff96337856be52558c09f01eb..6cdd1e1405a982654147de1371463213c9212d01 100644 (file)
@@ -273,6 +273,24 @@ static void __handle_safe_free(audio_io_s* handle, void *obj, bool is_output) {
     VALID_POINTER_END
 }
 
+static int __get_samplerate(const CAudioInfo& info) {
+    unsigned int samplerate = 0;
+    std::tie(std::ignore, samplerate, std::ignore) = info.getSpec();
+    return static_cast<int>(samplerate);
+}
+
+static audio_channel_e __get_channels(const CAudioInfo& info) {
+    CAudioInfo::EChannel channels;
+    std::tie(std::ignore, std::ignore, channels) = info.getSpec();
+    return __convert_audio_info_channel_to_channel(channels);
+}
+
+static audio_sample_type_e __get_sampletype(const CAudioInfo& info) {
+    CAudioInfo::ESampleType sampletype;
+    std::tie(sampletype, std::ignore, std::ignore) = info.getSpec();
+    return __convert_audio_info_sample_type_to_sample_type(sampletype);
+}
+
 /**
  * Implements CAPI functions
  */
@@ -528,7 +546,7 @@ int cpp_audio_in_get_sample_rate(audio_in_h input, int *sample_rate) {
                                    "Parameters are NULL input:%p, sample_rate:%p", input, sample_rate);
         assert(handle->audioIoHandle);
 
-        *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
+        *sample_rate = __get_samplerate(handle->audioIoHandle->getAudioInfo());
     } catch (const CAudioError& e) {
         AUDIO_IO_LOGE("%s", e.getErrorMsg());
         return __convert_audio_io_error(e.getError());
@@ -545,7 +563,7 @@ int cpp_audio_in_get_channel(audio_in_h input, audio_channel_e *channel) {
                                    "Parameters are NULL input:%p, channel:%p", input, channel);
         assert(handle->audioIoHandle);
 
-        *channel = __convert_audio_info_channel_to_channel(handle->audioIoHandle->getAudioInfo().getChannel());
+        *channel = __get_channels(handle->audioIoHandle->getAudioInfo());
     } catch (const CAudioError& e) {
         AUDIO_IO_LOGE("%s", e.getErrorMsg());
         return __convert_audio_io_error(e.getError());
@@ -562,7 +580,7 @@ int cpp_audio_in_get_sample_type(audio_in_h input, audio_sample_type_e *type) {
                                    "Parameters are NULL input:%p, type:%p", input, type);
         assert(handle->audioIoHandle);
 
-        *type = __convert_audio_info_sample_type_to_sample_type(handle->audioIoHandle->getAudioInfo().getSampleType());
+        *type = __get_sampletype(handle->audioIoHandle->getAudioInfo());
     } catch (const CAudioError& e) {
         AUDIO_IO_LOGE("%s", e.getErrorMsg());
         return __convert_audio_io_error(e.getError());
@@ -1066,7 +1084,7 @@ int cpp_audio_out_get_sample_rate(audio_out_h output, int *sample_rate) {
                                    "Parameters are NULL output:%p, sample_rate:%p", output, sample_rate);
         assert(handle->audioIoHandle);
 
-        *sample_rate = handle->audioIoHandle->getAudioInfo().getSampleRate();
+        *sample_rate = __get_samplerate(handle->audioIoHandle->getAudioInfo());
     } catch (const CAudioError& e) {
         AUDIO_IO_LOGE("%s", e.getErrorMsg());
         return __convert_audio_io_error(e.getError());
@@ -1083,7 +1101,7 @@ int cpp_audio_out_get_channel(audio_out_h output, audio_channel_e *channel) {
                                    "Parameters are NULL output:%p, channel:%p", output, channel);
         assert(handle->audioIoHandle);
 
-        *channel = __convert_audio_info_channel_to_channel(handle->audioIoHandle->getAudioInfo().getChannel());
+        *channel = __get_channels(handle->audioIoHandle->getAudioInfo());
     } catch (const CAudioError& e) {
         AUDIO_IO_LOGE("%s", e.getErrorMsg());
         return __convert_audio_io_error(e.getError());
@@ -1100,7 +1118,7 @@ int cpp_audio_out_get_sample_type(audio_out_h output, audio_sample_type_e *type)
                                    "Parameters are NULL output:%p, type:%p", output, type);
         assert(handle->audioIoHandle);
 
-        *type = __convert_audio_info_sample_type_to_sample_type(handle->audioIoHandle->getAudioInfo().getSampleType());
+        *type = __get_sampletype(handle->audioIoHandle->getAudioInfo());
     } catch (const CAudioError& e) {
         AUDIO_IO_LOGE("%s", e.getErrorMsg());
         return __convert_audio_io_error(e.getError());