/*
 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * 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 __AUDIOINOUT_H__
#define __AUDIOINOUT_H__

#include <audio_io.h>
#include <stddef.h>
#include <stdbool.h>

typedef struct _audio_inout audio_inout;

/**
 * @brief Enumerations of audio sample rate
 */
typedef enum
{
    SR_8000_HZ = 8000,
    SR_14025_HZ = 14025,
    SR_22050_HZ = 22050,
    SR_32000_HZ = 32000,
    SR_44056_HZ = 44056,
    SR_44100_HZ = 44100,
    SR_47250_HZ = 47250,
    SR_48000_HZ = 48000
} sample_rate_e;

/**
 * @brief Main audio parameters.
 */
typedef struct
{
    audio_sample_type_e sample_type;
    audio_channel_e channel;
    sample_rate_e sample_rate;
} audio_param;

/**
 * @brief Audio buffer
 */
typedef struct
{
    void *samples;
    size_t capacity;
    size_t len;
    int counter;
} audio_buffer;

typedef void (*audio_state_cb)(audio_inout *obj, void *user_data);
typedef void (*audio_progress_cb)(audio_inout *obj, size_t processed_bytes, double elapsed_time, void *user_data);

/*
 * @brief Create new audio buffer
 * @param[in]   param   audio parameters
 * @param[in]   time    audio stream length in seconds
 * @return new audio buffer
 */
audio_buffer *audio_buffer_new(audio_param *param, unsigned time);

/*
 * @brief Get length of buffer
 * @param[in]   param   audio parameters
 * @param[in]   time    audio stream length in seconds
 * @return size in bytes
 */
size_t audio_get_buffer_len(audio_param *param, unsigned time);

/*
 * @brief Increment counter of audio_buffer
 * @param[in]   buffer   audio buffer
 */
void audio_buffer_link(audio_buffer *buffer);

/*
 * @brief Decrement counter of audio_buffer
 *        if counter == 0 buffer will be destroyed
 * @param[in]   buffer   audio buffer
 */
void audio_buffer_unlink(audio_buffer *buffer);

/*
 * @brief Get time duration
 * @param[in]   param       audio parameters
 * @param[in]   buffer_len  buffer len
 * @return size duration in sec.
 */
int audio_get_duration(audio_param *param, size_t buffer_len);

/*
 * @brief Create input audio
 * @param[in]   param   audio parameters
 * @return instance of audio_inout
 */
audio_inout *audio_input_create(audio_param *param);

/*
 * @brief Create output audio
 * @param[in]   param   audio parameters
 * @return instance of audio_inout
 */
audio_inout *audio_output_create(audio_param *param);

/*
 * @brief Destroy audio_inout object
 * @param[in]   obj   instance of audio_inout
 */
void audio_destroy(audio_inout *obj);

/*
 * @brief Set callbacks
 * @param[in]   obj   instance of audio_inout
 * @param[in]   param   audio parameters
 * @param[in]   start_cb   start callback
 * @param[in]   stop_cb    stop callback
 * @param[in]   proc_cb    processed callback
 * @param[in]   user_data  user data
 */
void audio_set_cbs(audio_inout *obj, audio_state_cb start_cb, audio_state_cb stop_cb, audio_progress_cb prog_cb, void *user_data);

/*
 * @brief Start audio process(record or play)
 * @param[in]   obj   instance of audio_inout
 */
void audio_start(audio_inout *obj);

/*
 * @brief Get audio object status
 * @param[in]   obj   instance of audio_inout
 * @return true if is started, false otherwise
 */
bool audio_is_started(const audio_inout *obj);

/*
 * @brief Set buffer
 * @param[in]   obj   instance of audio_inout
 * @param[in]   buffer   pointer audio buffer
 */
void audio_set_buffer(audio_inout *obj, audio_buffer *buffer);

/*
 * @brief Stop audio process(record or play)
 * @param[in]   obj   instance of audio_inout
 */
void audio_stop(audio_inout *obj);


#endif // __AUDIOINOUT_H__

