/*
 * Copyright (c) 1998-2006 Apple Computer, Inc. All rights reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 * 
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 * 
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 * 
 * @APPLE_LICENSE_HEADER_END@
 */
 
//--------------------------------------------------------------------------------
//
//	File:		AppleUSBAudioStream.h
//
//	Contains:	Support for the USB Audio Class Stream Interface.
//
//	Technology:	OS X
//
//--------------------------------------------------------------------------------

#ifndef _APPLEUSBAUDIOCOMMON_H
#define _APPLEUSBAUDIOCOMMON_H

#include <libkern/OSTypes.h>

#ifdef DEBUGLOGGING
	#define DEBUG_LEVEL 1
	#ifdef DEBUGLOG_TO_USBLOG
		#include <IOKit/usb/IOUSBLog.h>
	#endif
	#ifdef DEBUGLOG_TO_FIRELOG
		#include <IOKit/firewire/IOFireLog.h>
	#endif
#endif


//	-----------------------------------------------------------------
#define SoundAssertionMessage( cond, file, line, handler ) \
    "Sound assertion \"" #cond "\" failed in " #file " at line " #line " goto " #handler ""

#define SoundAssertionFailed( cond, file, line, handler ) \
	{debugIOLog( SoundAssertionMessage( cond, file, line, handler )); IOSleep(20);};

//	-----------------------------------------------------------------
#define	FailIf( cond, handler )										\
    if( cond ){														\
        SoundAssertionFailed( cond, __FILE__, __LINE__, handler )	\
        goto handler; 												\
    }

//	-----------------------------------------------------------------
#define	FailWithAction( cond, action, handler )						\
    if( cond ){														\
        SoundAssertionFailed( cond, __FILE__, __LINE__, handler )	\
            { action; }												\
        goto handler; 												\
    }

//	-----------------------------------------------------------------
#define FailMessage(cond, handler)									\
	if (cond) {														\
		SoundAssertionFailed(cond, __FILE__, __LINE__, handler)		\
		goto handler;												\
	}

//  -----------------------------------------------------------------
//
// System Logging or USB Prober Logging
//
//#define sleepTime 20
#define sleepTime 0

#ifdef DEBUGLOGGING /* { */
	#ifdef CONSOLELOGGING /* { */
		#define debugIOLog( message... ) \
			do {IOLog( #message "\n", message ); IOSleep(sleepTime);} while (0)
	#elif defined (DEBUGLOG_TO_KPRINTF) /* }{ */
			#define debugIOLog( message... ) \
				do { kprintf ( message ); kprintf ( "\n" ); } while (0)
	#elif defined (DEBUGLOG_TO_FIRELOG) /* }{ */
			#define debugIOLog( message... ) \
				do { FireLog( message ); FireLog( "\n" ); } while (0)
	#else /* }{ */
		#define debugIOLog( message... ) \
			do {USBLog( DEBUG_LEVEL_DEVELOPMENT, message );} while (0)
	#endif /* } */
#else /* }{ */
	#define debugIOLog( message... ) ;
#endif /* } */

// Following are a list of precompiler variables that affect the way that AppleUSBAudio executes, logs, and compiles.

// kUSBInputRecoveryTimeFraction represents the denominator (with 1 as the numerator) of the fractional multiple of a USB frame we must wait
// before data read via isoc in is available for converting. This must be greater than zero.
#define	kUSBInputRecoveryTimeFraction		4

// DEBUGLATENCY enables methods that allow the tracking of how long clipped samples stay in the output buffer prior to DMA
#define DEBUGLATENCY				FALSE

// PRIMEISOCINPUT queues kNumIsocFramesToPrime USB frames to be read and disregarded before attempting to stream samples to CoreAudio
#define PRIMEISOCINPUT				TRUE
#define	kNumIsocFramesToPrime		12

// LOGTIMESTAMPS prints the timestamp in nanoseconds whenever takeTimeStamp it is called
#define LOGTIMESTAMPS				FALSE

// RESETAFTERSLEEP causes a device reset to be issued after waking from sleep for all devices.
#define	RESETAFTERSLEEP				TRUE

// DEBUGANCHORS prints out the last kAnchorsToAccumulate anchors whenever the list fills; used to check anchor accuracy.
#define	DEBUGANCHORS				FALSE
#define	kAnchorsToAccumulate		10

// LOGWALLTIMEPERUSBCYCLE will display mWallTimePerUSBCycle * kExtraPrecision each time updateWallTimePerUSBCycle is executed.

#define LOGWALLTIMEPERUSBCYCLE		FALSE

// LOGDEVICEREQUESTS shows each device request and its result after it has been issued
#define	LOGDEVICEREQUESTS			FALSE

// DEBUGTIMER shows the entry and exit of all USB rate timer functions
#define DEBUGTIMER					FALSE

// DEBUGCONVERT shows the entry and exit of all calls to convertInputSamples
#define	DEBUGCONVERT				FALSE

// The following return codes are used by AppleUSBAudioDevice to detail the status of a format change.
enum 
{
	kAUAFormatChangeNormal				= kIOReturnSuccess,
	kAUAFormatChangeForced,
	kAUAFormatChangeForceFailure,
	kAUAFormatChangeError				= kIOReturnError
	
};

#endif /* _APPLEUSBAUDIOCOMMON_H */
