///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Winnov L.P., 1996.  All rights reserved.
// mute.cpp: Mute output class implementation.
///////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include "debug.h"
#include "mute.h"
#include "wnvreg32.h"
#include "registry.h"
#include "nlstring.h"
#include "wnvasrc.h"

#define START_MUTE  5

///////////////////////////////////////////////////////////////////////////////

CMute::CMute ()
{
    m_dwCount = 0;		// count up to mute transition
    m_fWaveOutPlaying = FALSE;	// current state of wave out
    m_fMuteInput = FALSE;	// current state in input mute
    m_nBoard = 0;		// board number from open
    m_pShared = NULL;		// pointer to shared memory
    m_pRegistry = NULL;		// registry class
    m_fRegistryOpen = FALSE;	// registry not available
    m_hKey = NULL;		// key for registry
    m_dwAsrc = 0;
}

CMute::~CMute ()
{
}

///////////////////////////////////////////////////////////////////////////////
// input mute handlers
// This is one of the factors in controlling the ASCO output mute.

BOOL CMute::GetMuteInput (void)
{
    return m_fMuteInput;
}

void CMute::SetMuteInput (BOOL fMuteInput)
{
    m_pShared->fMuteInput = fMuteInput;
    m_fMuteInput = fMuteInput;
    AsrcSet (m_dwAsrc, WNVASRC_MUTEIN, fMuteInput);
    if (m_fRegistryOpen)
	m_pRegistry->Write (m_hKey, NLS_MUTEINPUT,  fMuteInput);
}

BOOL CMute::GetCodecMute (void)
{
	return AsrcGet (m_dwAsrc, WNVASRC_CODECMUTE);
}

///////////////////////////////////////////////////////////////////////////////
// wave output state tracking

void CMute::SetWaveOutPlaying (BOOL fWaveOutPlaying)
{
	TraceString ("\nCMute::SetWaveOutPlaying: ");
	TraceLong (fWaveOutPlaying);

    m_fWaveOutPlaying = fWaveOutPlaying;
    AsrcSet (m_dwAsrc, WNVASRC_WAVEOUTSTATE, m_fWaveOutPlaying);
}

///////////////////////////////////////////////////////////////////////////////
// Dummy write, do not mute, ASCO pass thru is active

void CMute::DummyWrite (void)
{
}

void CMute::WriteZero (void)
{
}

void CMute::WriteNormal (void)
{
    m_dwCount = 0;
}

void CMute::Prime (void)
{
    //!!!hack m_dwCount = 0;
}

///////////////////////////////////////////////////////////////////////////////

void CMute::Tick (void)
{
    BOOL fWaveOutPlaying;

    //
    // Update input mute
    //
    if (m_fMuteInput != (!!m_pShared->fMuteInput))
	SetMuteInput (!!m_pShared->fMuteInput);
    //
    // Update output mute
    //
    if (m_dwCount > START_MUTE)
    {	// wave output is not active
	fWaveOutPlaying = FALSE;
    }
    else
    {	// wave output is active
	fWaveOutPlaying = TRUE;
	m_dwCount++;
    }

    if (m_fWaveOutPlaying != fWaveOutPlaying)
	SetWaveOutPlaying (fWaveOutPlaying);
}

///////////////////////////////////////////////////////////////////////////////

#define DEF_MUTEINPUT	TRUE

DWORD CMute::Open (UINT nBoard, SHARED_HEADER *pShared, BOOL fMuteInput)
{
    m_nBoard = nBoard;
    m_pShared = pShared;
    m_dwCount = 0;

    m_dwAsrc = AsrcConstructor (m_nBoard);

    // init wave output state
    SetWaveOutPlaying (FALSE);

    // init mute input in shared mem
    m_pRegistry = new CRegistry;
    m_pRegistry->Open (m_nBoard);
    m_hKey = m_pRegistry->OpenKey (NLS_WNVIRQ);
    m_fRegistryOpen = TRUE;
    
	// Mute input, except for VideumCam case
	if (!fMuteInput)
		fMuteInput = m_pRegistry->Read (m_hKey, NLS_MUTEINPUT,  DEF_MUTEINPUT);
	SetMuteInput (fMuteInput);

    return 0;	// pass
}

void CMute::Close (void)
{
    // cleanup registry class
    if (m_pRegistry)
    {
	m_fRegistryOpen = FALSE;
	if (m_hKey)
	{
	    m_pRegistry->CloseKey (m_hKey);
	    m_hKey = NULL;
	}
	m_pRegistry->Close ();
	delete m_pRegistry;
	m_pRegistry = NULL;
    }

    SetWaveOutPlaying (FALSE);
    SetMuteInput (TRUE);

    if (m_dwAsrc)
    {
	AsrcDestructor (m_dwAsrc);
	m_dwAsrc = NULL;
    }
}

///////////////////////////////////////////////////////////////////////////////
//				    End of File
///////////////////////////////////////////////////////////////////////////////
