////////////////////////////////////////////////////////////////////////////////
// SmackerView.h
// Class for playing back SMK media files
// Copyright Bjoern Ganster
////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "Audio.h"
#include "BGBase.h"
#include "BGStringTemplates.h"
#include "GLWindow.h"
#include "MemoryBlock.h"
#include "Texture.h"

#include "../libsmacker-1.1.1r35/smacker.h"

#include <iostream>

class SmackerView: public GLWindow, public AudioProvider {
public:
	// Constructor
	SmackerView (GLWindow* _parent = NULL, int _left = 0, int _top = 0, 
		int _width = 0, int _height = 0);

	// Destructor
	~SmackerView() OVERRIDE;

	// Open SMK file, given its path
	bool open(const char* path);

	void close ();

	bool done() const
	{
		return !mVideoRemaining;
	}

	void render(int x, int y, int w, int h);

	void decodeAudio(Uint8* stream, int len);

	// Provide audio via SDL callback
	void provideAudio(void* userdata, Uint8* stream, int len) OVERRIDE;

private:
	bool mVideoRemaining;
	smk mSmk;
	Texture* mTexture;

	// file meta-info 
	unsigned int mFrame;
	unsigned long mVideoWidth, mVideoHeight, mFrames;

	// microseconds per frame
	double usf;

	DWORD mVideoStartTicks;

	// Arrays for audio track metadata
	unsigned char mTrackMask, mChannels[7], mDepth[7];
	unsigned long mRate[7];

	// Convert smk decoded into the format required by the audio device
	void processAudio(const unsigned char* audioData, int dataSize);

	// SDL audio playback via callbacks
	MemoryBlock mAudioBuf;
	size_t mAudioStreamOffset;
	SDL_mutex* mSoundMutex;
};
