Add echo-cancel properties to AudioInfo
[platform/core/api/audio-io.git] / src / cpp / CAudioInfo.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "CAudioIODef.h"
21
22
23 using namespace std;
24 using namespace tizen_media_audio;
25
26
27 /**
28  * class CAudioInfo
29  */
30 CAudioInfo::CAudioInfo() :
31     __mSampleRate(MAX_SYSTEM_SAMPLERATE),
32     __mChannel(EChannel::CHANNEL_MONO),
33     __mSampleType(ESampleType::SAMPLE_TYPE_U8),
34     __mAudioType(EAudioType::AUDIO_IN_TYPE_MEDIA),
35     __mAudioIndex(-1) {
36 }
37
38 CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
39     __mSampleRate(sampleRate),
40     __mChannel(channel),
41     __mSampleType(sampleType),
42     __mAudioType(audioType),
43     __mAudioIndex(audioIndex),
44     __mReferenceDeviceId(0) {
45     // Check to invalid AudioInfo
46     if (sampleRate < CAudioInfo::MIN_SYSTEM_SAMPLERATE ||
47         sampleRate > CAudioInfo::MAX_SYSTEM_SAMPLERATE)
48         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
49                                "The sampleRate is invalid [sampleRate:%u]", sampleRate);
50
51     if (channel < CAudioInfo::EChannel::CHANNEL_MONO ||
52         channel >= CAudioInfo::EChannel::CHANNEL_MAX)
53         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
54                                "The channel is invalid [channel:%u]", to_integral(channel));
55
56     if (sampleType < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 ||
57         sampleType >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX)
58         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
59                                "The sampleType is invalid [sampleType:%u]", to_integral(sampleType));
60
61     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
62         audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
63         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
64                                "The audioType is invalid [audioType:%u]", to_integral(audioType));
65 }
66
67 unsigned int CAudioInfo::getSampleRate() noexcept {
68     return __mSampleRate;
69 }
70
71 CAudioInfo::EChannel CAudioInfo::getChannel() noexcept {
72     return __mChannel;
73 }
74
75 CAudioInfo::ESampleType CAudioInfo::getSampleType() noexcept {
76     return __mSampleType;
77 }
78
79 CAudioInfo::EAudioType CAudioInfo::getAudioType() noexcept {
80     return __mAudioType;
81 }
82
83 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
84     __mAudioType = convertInputStreamTypeToAudioType(streamType);
85 }
86
87 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
88     __mAudioType = convertOutputStreamTypeToAudioType(streamType);
89 }
90
91 int CAudioInfo::getAudioIndex() noexcept {
92     return __mAudioIndex;
93 }
94
95 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
96     __mAudioIndex = audioIndex;
97 }
98
99 int CAudioInfo::getSampleSize() noexcept {
100     int bytes_in_sample = 0;
101
102     switch (__mSampleType) {
103     case ESampleType::SAMPLE_TYPE_U8:
104         bytes_in_sample = 1;
105         break;
106     case ESampleType::SAMPLE_TYPE_S16_LE:
107         bytes_in_sample = 2;
108         break;
109     case ESampleType::SAMPLE_TYPE_S24_LE:
110         bytes_in_sample = 3;
111         break;
112     case ESampleType::SAMPLE_TYPE_S24_32_LE:
113     case ESampleType::SAMPLE_TYPE_S32_LE:
114         bytes_in_sample = 4;
115         break;
116     default:
117         AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
118         bytes_in_sample = 2;
119         break;
120     }
121
122     return bytes_in_sample * static_cast<int>(__mChannel);
123 }
124
125 void CAudioInfo::bindEchoCancelReferenceDeviceId(int id) noexcept {
126     __mReferenceDeviceId = id;
127 }
128
129 int CAudioInfo::getEchoCancelReferenceDeviceId() noexcept {
130     return __mReferenceDeviceId;
131 }
132
133 const char* CAudioInfo::getConvertedStreamType() {
134     if (__mAudioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
135         __mAudioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
136         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
137                                "The audioType is not supported [audioType:%u]", to_integral(__mAudioType));
138
139     return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
140 }
141
142 CAudioInfo::EAudioType CAudioInfo::convertInputStreamTypeToAudioType(const char *streamType) {
143     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
144               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
145               i++) {
146         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
147             return (CAudioInfo::EAudioType)i;
148     }
149     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
150                            "The streamType of input is not supported [streamType:%s]", streamType);
151 }
152
153 CAudioInfo::EAudioType CAudioInfo::convertOutputStreamTypeToAudioType(const char *streamType) {
154     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
155               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_TYPE_MAX;
156               i++) {
157         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
158             return (CAudioInfo::EAudioType)i;
159     }
160     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
161                            "The streamType of output is not supported [streamType:%s]", streamType);
162 }