//
//  HA_WavFileObject.h
//  HA_server
//
//  Created by hark on 2013/07/06.
//  Copyright (c) 2013年 hark. All rights reserved.
//

#ifndef __HA_server__HA_WavFileObject__
#define __HA_server__HA_WavFileObject__

#ifdef __cplusplus
#include <iostream>
#include "HA_Object.h"
#include "HA_buffer.h"
#include "HA_device.h"

typedef struct _WAV_FILE_
{
    short channels;
    short bits;
    //int   bytes;
    int   rate;
} WAV_FILE_t;

namespace HARKAndroid {
    
    class WavFileObject : public DeviceObject {
    private:
        // AudioStreamFromWave.c からいただき
        //const std::string filenameWithFullpath;
        // constructor の初期化で const 設定のみだと後でファイル名を変更できないので SocketDeviceObject とインターフェースを統一
        std::string filename;
        WAV_FILE_t wav_file_info;
        FILE *fp;
    public:
        BufferObject *buffer;
        
    private:
        off_t start_pos;
        
        pthread_t thread_id;
        ::pthread_mutex_t Being_Stopped_mutex;
        bool BeingStopped;
        
    public:
        WavFileObject(const WavFileObject&);
        WavFileObject();
        WavFileObject(const char * const filename);
        virtual ~WavFileObject();
        WavFileObject& operator = (const WavFileObject&);
        
        int open();
        int start(BufferObject&);
        int stop();
        int close();
        
        int SetBeingStopped();
        int ResetBeingStopped();
        bool isBeingStopped();
        
        int SetFileName(const char * const argfilename) {
            filename = argfilename;
            return(0);
        };

        int rewinddatatop() {
            if (fp) {
                ::fseeko(fp, start_pos, SEEK_SET);
                return(0);
            } else {
                return(-1);
            }
        }
        
        size_t read_a_data(unsigned char *buf, const int len) {
            return(::fread(buf, sizeof(char), len, fp));
        }
        
        bool iseof() {
	  return((fp && (feof(fp) != 0)) ? true : false);
        }
        
        std::string getFileName() const {
            //return(filenameWithFullpath);
            return(filename);
        }
        int GetChannels(){
        	return wav_file_info.channels;
        }
        int GetBits(){
        	return wav_file_info.bits;
        }
    };
} // end of namespace

#endif // __cpusplus

#endif /* defined(__HA_server__HA_WavFileObject__) */
