// 使用Window下多线程API以及信号量来控制视频的解码顺序
#include <tchar.h>
#include "QueryPerformance.h" // time counter
#include "windows.h"
#include <stdio.h>
#include <process.h>
#include "cudaDecodeInterface.h"
#pragma comment(lib,"cudaDecode.lib")
typedef unsigned (WINAPI *PBEGINTHREADEX_THREADFUNC)
(LPVOID lpThreadParameter);
typedef unsigned *PBEGINTHREADEX_THREADID;
HANDLE hand_cuda[6] = {NULL, NULL, NULL, NULL,NULL,NULL};
bool cuda_decodeEnd[6] = {false, false, false, false,false,false};
static int counter[6] = {0, 0, 0, 0,0,0};
FILE *fp0 = fopen("../decodePic/cudadecode0.yuv", "wb");
FILE *fp1 = fopen("../decodePic/cudadecode1.yuv", "wb");
FILE *fp2 = fopen("../decodePic/cudadecode2.yuv", "wb");
FILE *fp3 = fopen("../decodePic/cudadecode3.yuv", "wb");
FILE *fp4 = fopen("../decodePic/cudadecode4.yuv", "wb");
FILE *fp5 = fopen("../decodePic/cudadecode5.yuv", "wb");
// front claim
int CudaDecodeNotify0(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);
int CudaDecodeNotify1(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);
int CudaDecodeNotify2(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);
int CudaDecodeNotify3(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);
int CudaDecodeNotify4(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);
int CudaDecodeNotify5(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof);
DWORD WINAPI OnCuda0();
DWORD WINAPI OnCuda1();
DWORD WINAPI OnCuda2();
DWORD WINAPI OnCuda3();
DWORD WINAPI OnCuda4();
DWORD WINAPI OnCuda5();
HANDLE handle_video[6] = {NULL, NULL, NULL, NULL,NULL,NULL};
HANDLE hand_semaphore[6] = {NULL, NULL, NULL, NULL,NULL,NULL};
int main(int argc, char* argv[])
{
printf("Cuda Decoder......\n");
// Initialzie semaphore
hand_semaphore[0] = CreateSemaphore(NULL, 1, 1, NULL);
hand_semaphore[1] = CreateSemaphore(NULL, 0, 1, NULL);
hand_semaphore[2] = CreateSemaphore(NULL, 0, 1, NULL);
hand_semaphore[3] = CreateSemaphore(NULL, 0, 1, NULL);
hand_semaphore[4] = CreateSemaphore(NULL, 0, 1, NULL);
hand_semaphore[5] = CreateSemaphore(NULL, 0, 1, NULL);
// Initialize hand
// DWORD begin_time = GetTickCount64();
CStopwatch stopwatch;
WaitForSingleObject(hand_semaphore[0], INFINITE);
handle_video[0] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda0, NULL, 0, NULL);
WaitForSingleObject(hand_semaphore[1], INFINITE);
handle_video[1] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda1, NULL, 0, NULL);
WaitForSingleObject(hand_semaphore[2], INFINITE);
handle_video[2] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda2, NULL, 0, NULL);
WaitForSingleObject(hand_semaphore[3], INFINITE);
handle_video[3] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda3, NULL, 0, NULL);
WaitForSingleObject(hand_semaphore[4], INFINITE);
handle_video[4] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda4, NULL, 0, NULL);
WaitForSingleObject(hand_semaphore[5], INFINITE);
handle_video[5] = (HANDLE)_beginthreadex(NULL, 0, (PBEGINTHREADEX_THREADFUNC)OnCuda5, NULL, 0, NULL);
WaitForMultipleObjects(6, handle_video, TRUE, INFINITE);
__int64 qwElapsedTime = stopwatch.Now();
printf("总用时为:%d<ms>\n", qwElapsedTime);
// DWORD end_time = GetTickCount64();
// printf("总用时为:%d<ms>\n", end_time - begin_time);
for (int i=0; i<6; i++)
{
CloseHandle(handle_video[i]);
}
fflush(stdin);
system("pause");
return 0;
}
// OnCuda函数
DWORD WINAPI OnCuda0()
{
bool nstate = Cuda_SupportDecode();
hand_cuda[0] = Cuda_CreateDecoder();
DWORD StartTime = 0; // 起始的时间
DWORD ElapsedTime = 0; //消耗的时间
if( Cuda_OpenFile(hand_cuda[0], "../data/720P/video0.m2v", CudaDecodeNotify0)==1 )
{
StartTime = GetTickCount();
printf("文件<1>打开成功\n");
}
else
printf("文件<1>打开失败\n");
while ( !cuda_decodeEnd[0] )
{
Sleep(1);
}
ElapsedTime = GetTickCount() - StartTime;
// printf("文件<1>解码完成 nTime1 = %d<ms>\n", ElapsedTime);
Cuda_DestroyDecoder(hand_cuda[0]);
return 0;
}
DWORD WINAPI OnCuda1()
{
bool nstate = Cuda_SupportDecode();
hand_cuda[1] = Cuda_CreateDecoder();
DWORD StartTime = 0; // 起始的时间
DWORD ElapsedTime = 0; //消耗的时间
if( Cuda_OpenFile(hand_cuda[1], "../data/720P/video1.m2v", CudaDecodeNotify1)==1 )
{
StartTime = GetTickCount();
printf("文件<2>打开成功\n");
}
else
printf("文件<2>打开失败\n");
while ( !cuda_decodeEnd[1] )
{
Sleep(1);
}
ElapsedTime = GetTickCount() - StartTime;
// printf("文件<2>解码完成 nTime2 = %d<ms>\n", ElapsedTime);
Cuda_DestroyDecoder(hand_cuda[1]);
return 0;
}
DWORD WINAPI OnCuda2()
{
bool nstate = Cuda_SupportDecode();
hand_cuda[2] = Cuda_CreateDecoder();
DWORD StartTime = 0; // 起始的时间
DWORD ElapsedTime = 0; //消耗的时间
if( Cuda_OpenFile(hand_cuda[2], "../data/720P/video2.m2v", CudaDecodeNotify2)==1 )
{
StartTime = GetTickCount();
printf("文件<3>打开成功\n");
}
else
printf("文件<3>打开失败\n");
while ( !cuda_decodeEnd[2] )
{
Sleep(1);
}
ElapsedTime = GetTickCount() - StartTime;
// printf("文件<3>解码完成 nTime3 = %d<ms>\n", ElapsedTime);
Cuda_DestroyDecoder(hand_cuda[2]);
return 0;
}
DWORD WINAPI OnCuda3()
{
bool nstate = Cuda_SupportDecode();
hand_cuda[3] = Cuda_CreateDecoder();
DWORD StartTime = 0; // 起始的时间
DWORD ElapsedTime = 0; //消耗的时间
if( Cuda_OpenFile(hand_cuda[3], "../data/720P/video3.m2v", CudaDecodeNotify3)==1 )
{
StartTime = GetTickCount();
printf("文件<4>打开成功\n");
}
else
printf("文件<4>打开失败\n");
while ( !cuda_decodeEnd[3] )
{
Sleep(1);
}
ElapsedTime = GetTickCount() - StartTime;
// printf("文件<4>解码完成 nTime4 = %d<ms>\n", ElapsedTime);
Cuda_DestroyDecoder(hand_cuda[3]);
return 0;
}
DWORD WINAPI OnCuda4()
{
bool nstate = Cuda_SupportDecode();
hand_cuda[4] = Cuda_CreateDecoder();
DWORD StartTime = 0; // 起始的时间
DWORD ElapsedTime = 0; //消耗的时间
if( Cuda_OpenFile(hand_cuda[4], "../data/720P/video4.m2v", CudaDecodeNotify4)==1 )
{
StartTime = GetTickCount();
printf("文件<1>打开成功\n");
}
else
printf("文件<1>打开失败\n");
while ( !cuda_decodeEnd[4] )
{
Sleep(1);
}
ElapsedTime = GetTickCount() - StartTime;
// printf("文件<1>解码完成 nTime1 = %d<ms>\n", ElapsedTime);
Cuda_DestroyDecoder(hand_cuda[4]);
return 0;
}
DWORD WINAPI OnCuda5()
{
bool nstate = Cuda_SupportDecode();
hand_cuda[5] = Cuda_CreateDecoder();
DWORD StartTime = 0; // 起始的时间
DWORD ElapsedTime = 0; //消耗的时间
if( Cuda_OpenFile(hand_cuda[5], "../data/720P/video5.m2v", CudaDecodeNotify5)==1 )
{
StartTime = GetTickCount();
printf("文件<1>打开成功\n");
}
else
printf("文件<1>打开失败\n");
while ( !cuda_decodeEnd[5] )
{
Sleep(1);
}
ElapsedTime = GetTickCount() - StartTime;
// printf("文件<1>解码完成 nTime1 = %d<ms>\n", ElapsedTime);
Cuda_DestroyDecoder(hand_cuda[5]);
return 0;
}
// cudaDecodeNotify函数,拷贝解码后的数据到硬盘
int CudaDecodeNotify0(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof)
{
counter[0]++;
// fwrite(pDecodeData,1,nDataSize,fp0);
if(nEof)
{
OutputDebugString("thread 1 读包结束!!!\n");
printf("====== thread 1 读包结束!!!\n");
// fclose(fp0);
cuda_decodeEnd[0] = true;
}
#if 0
printf(" \n------ video 1:%d ------\n", counter[0]);
FILE *file = fopen("../order.txt", "at");
fprintf(file, " ------ 视频1:%d ------\n", counter[0]);
fclose(file);
#endif
ReleaseSemaphore(hand_semaphore[1], 1, NULL);
Sleep(1);
return 0;
}
int CudaDecodeNotify1(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof)
{
counter[1]++;
// fwrite(pDecodeData,1,nDataSize,fp1);
if(nEof)
{
OutputDebugString("thread 2 读包结束!!!\n");
printf("====== thread 2 读包结束!!!\n");
// fclose(fp4);
cuda_decodeEnd[1] = true;
}
#if 0
printf(" \n------ video 2 : %d ------\n", counter[1]);
FILE *file = fopen("../order_new.txt", "at");
fprintf(file, " ------ 视频2:%d ------\n", counter[1]);
fclose(file);
#endif
ReleaseSemaphore(hand_semaphore[2], 1, NULL);
Sleep(1);
return 0;
}
int CudaDecodeNotify2(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof)
{
counter[2]++;
// fwrite(pDecodeData,1,nDataSize,fp2);
if(nEof)
{
OutputDebugString("thread 3 读包结束!!!\n");
printf("====== thread 3 读包结束!!!\n");
// fclose(fp4);
cuda_decodeEnd[2] = true;
}
#if 0
printf(" \n------ video 3:%d ------\n", counter[2]);
FILE *file = fopen("../order.txt", "at");
fprintf(file, " ------ 视频3:%d ------\n", counter[2]);
fclose(file);
#endif
ReleaseSemaphore(hand_semaphore[3], 1, NULL);
Sleep(1);
return 0;
}
int CudaDecodeNotify3(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof)
{
counter[3]++;
// fwrite(pDecodeData,1,nDataSize,fp3);
if(nEof)
{
OutputDebugString("thread 4 读包结束!!!\n");
printf("====== thread 4 读包结束!!!\n");
// fclose(fp4);
cuda_decodeEnd[3] = true;
}
#if 0
printf(" \n------ video 4 :%d ------\n\n", counter[3]);
FILE *file = fopen("../order.txt", "at");
fprintf(file, " ------ 视频4:%d ------\n", counter[3]);
fclose(file);
#endif
ReleaseSemaphore(hand_semaphore[4], 1, NULL);
Sleep(1);
return 0;
}
int CudaDecodeNotify4(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof)
{
counter[4]++;
// fwrite(pDecodeData,1,nDataSize,fp0);
if(nEof)
{
OutputDebugString("thread 5 读包结束!!!\n");
printf("====== thread 5 读包结束!!!\n");
// fclose(fp0);
cuda_decodeEnd[4] = true;
}
#if 0
printf(" \n------ video 1:%d ------\n", counter[0]);
FILE *file = fopen("../order.txt", "at");
fprintf(file, " ------ 视频1:%d ------\n", counter[0]);
fclose(file);
#endif
ReleaseSemaphore(hand_semaphore[5], 1, NULL);
Sleep(1);
return 0;
}
int CudaDecodeNotify5(unsigned char* pDecodeData, int nDataSize, DWORD timestamp, int nEof)
{
counter[5]++;
// fwrite(pDecodeData,1,nDataSize,fp0);
if(nEof)
{
OutputDebugString("thread 6 读包结束!!!\n");
printf("====== thread 6 读包结束!!!\n");
// fclose(fp0);
cuda_decodeEnd[5] = true;
}
#if 0
printf(" \n------ video 1:%d ------\n", counter[0]);
FILE *file = fopen("../order.txt", "at");
fprintf(file, " ------ 视频1:%d ------\n", counter[0]);
fclose(file);
#endif
ReleaseSemaphore(hand_semaphore[0], 1, NULL);
Sleep(1);
return 0;
}