CAudioInfo: Initialize __mReferenceDeviceId
[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     __mReferenceDeviceId(0) {
37 }
38
39 CAudioInfo::CAudioInfo(unsigned int sampleRate, EChannel channel, ESampleType sampleType, EAudioType audioType, int audioIndex) :
40     __mSampleRate(sampleRate),
41     __mChannel(channel),
42     __mSampleType(sampleType),
43     __mAudioType(audioType),
44     __mAudioIndex(audioIndex),
45     __mReferenceDeviceId(0) {
46     // Check to invalid AudioInfo
47     if (sampleRate < CAudioInfo::MIN_SYSTEM_SAMPLERATE ||
48         sampleRate > CAudioInfo::MAX_SYSTEM_SAMPLERATE)
49         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
50                                "The sampleRate is invalid [sampleRate:%u]", sampleRate);
51
52     if (channel < CAudioInfo::EChannel::CHANNEL_MONO ||
53         channel >= CAudioInfo::EChannel::CHANNEL_MAX)
54         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
55                                "The channel is invalid [channel:%u]", to_integral(channel));
56
57     if (sampleType < CAudioInfo::ESampleType::SAMPLE_TYPE_U8 ||
58         sampleType >= CAudioInfo::ESampleType::SAMPLE_TYPE_MAX)
59         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
60                                "The sampleType is invalid [sampleType:%u]", to_integral(sampleType));
61
62     if (audioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
63         audioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
64         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_INVALID_ARGUMENT,
65                                "The audioType is invalid [audioType:%u]", to_integral(audioType));
66 }
67
68 unsigned int CAudioInfo::getSampleRate() noexcept {
69     return __mSampleRate;
70 }
71
72 CAudioInfo::EChannel CAudioInfo::getChannel() noexcept {
73     return __mChannel;
74 }
75
76 CAudioInfo::ESampleType CAudioInfo::getSampleType() noexcept {
77     return __mSampleType;
78 }
79
80 CAudioInfo::EAudioType CAudioInfo::getAudioType() noexcept {
81     return __mAudioType;
82 }
83
84 void CAudioInfo::setAudioTypeByInputStreamType(const char* streamType) {
85     __mAudioType = convertInputStreamTypeToAudioType(streamType);
86 }
87
88 void CAudioInfo::setAudioTypeByOutputStreamType(const char* streamType) {
89     __mAudioType = convertOutputStreamTypeToAudioType(streamType);
90 }
91
92 int CAudioInfo::getAudioIndex() noexcept {
93     return __mAudioIndex;
94 }
95
96 void CAudioInfo::setAudioIndex(int audioIndex) noexcept {
97     __mAudioIndex = audioIndex;
98 }
99
100 int CAudioInfo::getSampleSize() noexcept {
101     int bytes_in_sample = 0;
102
103     switch (__mSampleType) {
104     case ESampleType::SAMPLE_TYPE_U8:
105         bytes_in_sample = 1;
106         break;
107     case ESampleType::SAMPLE_TYPE_S16_LE:
108         bytes_in_sample = 2;
109         break;
110     case ESampleType::SAMPLE_TYPE_S24_LE:
111         bytes_in_sample = 3;
112         break;
113     case ESampleType::SAMPLE_TYPE_S24_32_LE:
114     case ESampleType::SAMPLE_TYPE_S32_LE:
115         bytes_in_sample = 4;
116         break;
117     default:
118         AUDIO_IO_LOGW("As unrecognized sample type %d, let's assume S16_LE", static_cast<int>(__mSampleType));
119         bytes_in_sample = 2;
120         break;
121     }
122
123     return bytes_in_sample * static_cast<int>(__mChannel);
124 }
125
126 void CAudioInfo::bindEchoCancelReferenceDeviceId(int id) noexcept {
127     __mReferenceDeviceId = id;
128 }
129
130 int CAudioInfo::getEchoCancelReferenceDeviceId() noexcept {
131     return __mReferenceDeviceId;
132 }
133
134 const char* CAudioInfo::getConvertedStreamType() {
135     if (__mAudioType < CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA ||
136         __mAudioType >= CAudioInfo::EAudioType::AUDIO_TYPE_MAX)
137         THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
138                                "The audioType is not supported [audioType:%u]", to_integral(__mAudioType));
139
140     return __STREAM_TYPE_TABLE[(unsigned int)__mAudioType];
141 }
142
143 CAudioInfo::EAudioType CAudioInfo::convertInputStreamTypeToAudioType(const char *streamType) {
144     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_IN_TYPE_MEDIA;
145               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
146               i++) {
147         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
148             return (CAudioInfo::EAudioType)i;
149     }
150     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
151                            "The streamType of input is not supported [streamType:%s]", streamType);
152 }
153
154 CAudioInfo::EAudioType CAudioInfo::convertOutputStreamTypeToAudioType(const char *streamType) {
155     for (auto i = (unsigned int)CAudioInfo::EAudioType::AUDIO_OUT_TYPE_MEDIA;
156               i < (unsigned int)CAudioInfo::EAudioType::AUDIO_TYPE_MAX;
157               i++) {
158         if (!strcmp(__STREAM_TYPE_TABLE[i], streamType))
159             return (CAudioInfo::EAudioType)i;
160     }
161     THROW_ERROR_MSG_FORMAT(CAudioError::EError::ERROR_NOT_SUPPORTED_TYPE,
162                            "The streamType of output is not supported [streamType:%s]", streamType);
163 }