STDMETHODIMP
CPosPassThru::GetPositions(LONGLONG *pCurrent, LONGLONG * pStop)
{
    IMediaSeeking* pMS;
    HRESULT hr = GetPeerSeeking(&pMS);
    if (FAILED(hr)) {
	return hr;
    }

    hr = pMS->GetPositions(pCurrent,pStop);
    pMS->Release();
    return hr;
}
Beispiel #2
0
bool gui::dx::audio_playerX::play(const char * url, char* clipBegin, char* clipEnd) {

	EnterCriticalSection(&m_csSequence);

	TRACE(L"%s", L"\n####### -- PLAY DX\n");
	amis::util::Log* p_log = amis::util::Log::Instance();
	p_log->writeTrace("Play DX", "audio_playerX::play");

	m_url.assign(url);

	if (m_graph_builder != NULL)
	{ 
		stop(true);
	}

	if (m_graph_builder == NULL)
	{
		HRESULT hr = CoCreateInstance(CLSID_FilterGraph,0,CLSCTX_INPROC_SERVER,
			IID_IGraphBuilder,(void**)&m_graph_builder);
		if(FAILED(hr))
		{
			win_report_error("CoCreateInstance(CLSID_FilterGraph, ...)", hr);	

			LeaveCriticalSection(&m_csSequence);
			return false;
		}
	}

	//WCHAR wsz[MAX_PATH];
	//MultiByteToWideChar(CP_ACP,0, url.c_str(), -1, wsz, MAX_PATH);

	std::string strFileName;
	strFileName.assign(url);

	USES_CONVERSION;
	LPCWSTR str = A2CW(strFileName.c_str());

	HRESULT hr = m_graph_builder->RenderFile(str, 0);
	if(FAILED(hr)){
		
		amis::util::Log* p_log = amis::util::Log::Instance();
		string log_msg = "CANNOT PLAY: ";	
		log_msg.append(strFileName);
		p_log->writeError(log_msg, "audio_playerX::play");

		//m_graph_builder->Release();
		//m_graph_builder = 0;
		/* 
		if (hr == 0x800c000d)  // XXX This value experimentally determined:-)
		errorX("%s: Unsupported URL protocol", url.c_str());
		else if (hr == VFW_E_CANNOT_CONNECT)
		errorX("%s: Unsupported video format", url.c_str());
		else
		errorX("%s: DirectX error 0x%x", url.c_str(), hr);
		*/
		release_player();
		LeaveCriticalSection(&m_csSequence);
		return false;
	}
#ifdef WITH_TPB_AUDIO_SPEEDUP
	initialize_speedup_filter();
#endif

	if (m_media_control == NULL) { 
		HRESULT hr = m_graph_builder->QueryInterface(IID_IMediaControl, (void **) &m_media_control);
		if(FAILED(hr)) {
			win_report_error("QueryInterface(IID_IMediaControl, ...)", hr);	

			LeaveCriticalSection(&m_csSequence);
			return false;
		}
	}

	if (m_media_position == NULL) { 
		m_graph_builder->QueryInterface(IID_IMediaPosition, (void **) &m_media_position);
		if(FAILED(hr)) {
			win_report_error("QueryInterface(IID_IMediaPosition, ...)", hr);	

			LeaveCriticalSection(&m_csSequence);
			return false;
		}
	}

	if (m_media_event == NULL) { 
		m_graph_builder->QueryInterface(IID_IMediaEvent, (void **) &m_media_event);
		if(FAILED(hr)) {
			win_report_error("QueryInterface(IID_IMediaEvent, ...)", hr);	

			LeaveCriticalSection(&m_csSequence);
			return false;
		}
	}

	if (m_basic_audio == NULL) { 
		m_graph_builder->QueryInterface(IID_IBasicAudio, (void **) &m_basic_audio);
		if(FAILED(hr)) {
			win_report_error("QueryInterface(IID_IBasicAudio, ...)", hr);	
		}
	}

	SmilTimeCode startStop(clipBegin, clipBegin, clipEnd);
	unsigned long begin = startStop.getStart();
	unsigned long end = startStop.getEnd();

	LONGLONG llDuration = 0;

	IMediaSeeking *pIMS;
	if (m_graph_builder->QueryInterface(IID_IMediaSeeking, (void**) &pIMS) == S_OK)
	{

		if (pIMS->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME) == S_OK) {
			if (pIMS->GetPositions(NULL, &llDuration) != S_OK) llDuration = -10000;
		}
		else
		{
			llDuration = - 10000;
		}

		long duration = (long) ((llDuration / 10000) & 0xFFFFFFFF);	  

		if (! (begin == 0 && end == 0)) {

			if (begin >= 0 && begin < duration) {

				LONGLONG Value = (LONGLONG) (begin);
				Value *= 10000;
				if (pIMS->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME) == S_OK) {
					if (pIMS->SetPositions(&Value, AM_SEEKING_AbsolutePositioning, NULL, AM_SEEKING_NoPositioning) != S_OK){}
				}
			}
			
			if (end >= 0 && end > begin && end < duration) {
				
				LONGLONG Value = (LONGLONG) (end);
				Value *= 10000;
				if (pIMS->SetTimeFormat(&TIME_FORMAT_MEDIA_TIME) == S_OK) {
					if (pIMS->SetPositions(NULL, AM_SEEKING_NoPositioning, &Value, AM_SEEKING_AbsolutePositioning) != S_OK) {}
				}
			}
		}
		pIMS->Release();
	}

	if (hEventHandler == NULL) {
		unsigned long lpdwThreadID;
		//hEventHandler = (HANDLE) _beginthreadex(NULL, 0, eventHandler, (void*) this, 0, &lpdwThreadID);
		hEventHandler = CreateThread(NULL, 0, &eventHandler, this, 0, &lpdwThreadID);
		//GetCurrentThreadId
		TRACE("\nTHREAD ID (DX_AUDIO_PLAYER): %x\n", lpdwThreadID);
		
		amis::util::Log* p_log = amis::util::Log::Instance();
		string log_msg = "Thread ID: ";
		char strID[10];
		sprintf(strID, "%x", lpdwThreadID);			
		log_msg.append(strID);
		p_log->writeTrace(log_msg, "audio_playerX::play");
	}
#ifdef SINGLE_THREAD_HACK
	SetEvent(m_hEventWakeup);
#endif

	set_volume(s_current_volume);

	hr = m_media_control->Run();
	if(FAILED(hr)) {
		win_report_error("IMediaControl::run()", hr);	
	}

	LeaveCriticalSection(&m_csSequence);
	return true;
}