DWORD WINAPI startOddcast(LPVOID lpParameter) 
{

	
	initializeencoder();
	if (connectToServer()) {
		setCurrentSongTitle("");
		UpdateWhatsPlaying();
	}
	else {
		MessageBox(NULL, "Unable to Connect", NULL, MB_OK);
		return(0);
	}
	if (getLiveRecordingSetFlag()) {
		if (!initLiveRecording()) {
			MessageBox(NULL, "Fail to open input device", NULL, MB_OK);
			disconnectFromServer();
			return 0;
		}
		else {
			char	buf[255] = "";
			sprintf(buf, "%dHz/Stereo PCM", getLiveInSamplerate());
			setSourceDescription(buf);
			int songChange = setCurrentSongTitle(getServerDesc());
			startRecording();
		}
	}
	return 1;
}
void 
UpdateWhatsPlaying()
{
	char	buf[4097] = "";


	if (getIsConnected()) {
		if(api->core_getCurrent(0) != NULL) {
			memset(buf, '\000', sizeof(buf));
			api->metadb_getMetaData(api->core_getCurrent(0), MT_NAME, buf, 4096, MDT_STRINGZ);
			if (setCurrentSongTitle((char *)buf)) {
				pOddcast->m_MetadataValue->setName((char *)buf);
			}
		}
//		const char *szurl = api->core_getCurrent(0);
	}

	/*
	if (szurl == NULL || strstr(szurl, "http://") == NULL)
	{
		m_titleText->setName(NO_STREAM_MSG);
		return;
	}
	std::string msg = "Press start to rip ";
	msg +=  szurl;
	m_titleText->setName(msg.c_str());
	*/
}
void * startDecodeThread(void *url) {

	LogMessage(&gMain, LOG_DEBUG, "Starting decode thread");

	char	currentMetadata[2046] = "";
	
	if (sourceMedia == MP3_FORMAT) {
		/* MP3 */
		mp3dec_init(&mp3Decoder);
	}
	if (sourceMedia == OGGVORBIS_FORMAT) {
		/* Ogg Vorbis */
    	devorb_init (&vorbisDecoder);
	}
	decoderLoop = 1;
	while (decoderLoop) {
		int	read = 0;
		long inbuffer = 0;
		char *pStreamData = NULL;
		
        inbuffer = cbuffer_get_used(&sourceCBuffer);

		if (decoderLoop) {
			if (inbuffer > 0) {
				pStreamData = (char*)malloc(inbuffer);
				 
				if (cbuffer_extract(&sourceCBuffer, pStreamData, inbuffer)) {
	//				fprintf(stdout, "Extracted %d from the circular buffer\n", inbuffer);

					pthread_mutex_lock(&encoders_mutex);
					if (sourceMedia == MP3_FORMAT) {

						mp3dec_feed_stream(&mp3Decoder, pStreamData, inbuffer);
						if (mp3dec_get_stream_size(&mp3Decoder) > MIN_MP3_STREAM_SIZE) {
							if (!mp3dec_decode(&mp3Decoder)) {
								pthread_mutex_unlock(&encoders_mutex);
								LogMessage(&gMain, LOG_ERROR, "Problem with mp3 decoding.");
								pthread_exit((void *)1);
								return 0;
							}
						}

						/* MP3 */
					}
					if (sourceMedia == OGGVORBIS_FORMAT) {
						/* Vorbis */
						devorb_read (&vorbisDecoder, pStreamData, inbuffer);
						while (devorb_decode (&vorbisDecoder)) {
							if (vorbisDecoder.headers_changed) {
								char	StreamTitle[2046] = "";
								char	Artist[1024] = "";
								char	Title[1024] = "";

								char **ptr=vorbisDecoder.vc.user_comments;

								while(*ptr){
									char *pData = *ptr;
									if (!strncasecmp(pData, "ARTIST=", strlen("ARTIST="))) {
										strncpy(Artist, pData + strlen("ARTIST="), sizeof(Artist)-1);
									}
									if (!strncasecmp(pData, "TITLE=", strlen("TITLE="))) {
										strncpy(Title, pData + strlen("TITLE="), sizeof(Title)-1);
									}
									++ptr;
								}

								sprintf(StreamTitle, "%s - %s", Artist, Title);

								inputMetadataCallback(&gMain, StreamTitle);

								for(int i = 0; i < gMain.gNumEncoders; i++) {
									setCurrentSongTitle(g[i], StreamTitle);
								}
								vorbisDecoder.headers_changed = 0;
							}
						}
					}
					pthread_mutex_unlock(&encoders_mutex);
				}
				if (pStreamData) {
					free(pStreamData);
				}
			}
			else {
	#ifdef WIN32
				Sleep(100);
	#else
				usleep(250);
	#endif
			}
		}
	}
	LogMessage(&gMain, LOG_DEBUG, "Breaking out of decoder loop.");
	
	pthread_exit((void *)1);
	return 0;
}
int 
OddcastWnd::handleChildNotify (int msg, int objId, int param1, int param2)
{

	if (msg != ChildNotify::BUTTON_LEFTPUSH)
		return 0;
	int ret;

	switch(objId) 
	{
	case ODDCAST_VU:
		if (m_vu_on_off) {
			m_vu_on_off = 0;
		}
		else {
			m_vu_on_off = 1;
		}
		break;

	case ODDCAST_CONNECT:
		if (getIsConnected()) {
			disconnectFromServer();
			setForceStop(1);
			if (getLiveRecordingSetFlag()) {
				stopRecording();
			}
			setCurrentSongTitle("");
			m_connect->setButtonText("Connect", EXB_BUTTON_FONT_SIZE);
			gAvgRight = 0;
			gAvgLeft = 0;
			invalidate();

		}
		else {
			char	message[1024] = "";
			char	error[1024] = "";
			int ret = 1;
#ifdef WIN32
			if (getWMAFlag()) {
				ret = startWMAServer(message,error);
			}
#endif
			if (ret == 0) {
				MessageBox(NULL,message, NULL, MB_OK);
				MessageBox(NULL,error, NULL, MB_OK);
			}
			else {
				if (startOddcast(NULL)) {
//				gThreadHandle = CreateThread(NULL, 0, startOddcast, NULL, 0, &gThreadID);
					m_connect->setButtonText("Stop", EXB_BUTTON_FONT_SIZE);
				}
			}
		}
		break;
	case ODDCAST_CONFIG:
		if (!getIsConnected()) {
			STARTUPINFO si = {0};
			PROCESS_INFORMATION pi = {0};

			si.cb = sizeof (si);
			si.lpReserved = si.lpDesktop = si.lpTitle = NULL;
			si.dwFlags = STARTF_USESHOWWINDOW;
			si.wShowWindow = SW_SHOW;
			si.cbReserved2 = 0;
			si.lpReserved2 = 0;
			DWORD dwError;

			CreateProcess (NULL, "oddcast_editConfig.exe", 
			  NULL, NULL, 
			  TRUE, CREATE_NEW_CONSOLE, 
			  NULL, NULL, 
			  &si, &pi); 

			WaitForSingleObject (pi.hProcess, INFINITE);
			initializeGlobals();
			transcode_init();
		}
		else {
			MessageBox(NULL, "Sorry, but you cannot edit the settings while connected", "Error", MB_OK);
		}
		break;
	}
	return 1;
}
OddcastWnd::~OddcastWnd() 
{
	if (getIsConnected()) {
		disconnectFromServer();
		setForceStop(1);
		if (getLiveRecordingSetFlag()) {
			stopRecording();
		}
		setCurrentSongTitle("");
		m_connect->setButtonText("Connect", EXB_BUTTON_FONT_SIZE);
		gAvgRight = 0;
		gAvgLeft = 0;
		invalidate();

	}

/*
	if (m_ServerDescText) {
		delete m_ServerDescText;
	}
	if (m_ServerDescValue) {
		delete m_ServerDescValue;
	}
	if (m_DestinationText) {
		delete m_DestinationText;
	}
	if (m_DestinationValue) {
		delete m_DestinationValue;
	}
	if (m_MetadataText) {
		delete m_MetadataText;
	}
	if (m_MetadataValue) {
		delete m_MetadataValue;
	}
	if (m_BPSText) {
		delete m_BPSText;
	}
	if (m_BPSValue) {
		delete m_BPSValue;
	}
	if (m_BitrateText) {
		delete m_BitrateText;
	}
	if (m_BitrateValue) {
		delete m_BitrateValue;
	}
	if (m_StatusText) {
		delete m_StatusText;
	}
	if (m_StatusValue) {
		delete m_StatusValue;
	}
	if (m_LiveRecordingText) {
		delete m_LiveRecordingText;
	}


	if (m_connect) {
		delete m_connect;
	}
	if (m_config) {
		delete m_config;
	}
	if (m_vu) {
		delete m_vu;
	}
*/
	api->core_delCallback(0, this);
	m_pcurInst = NULL;
}