1 | //////////////////////////////////////////////////////////// |
---|
2 | // Copyright (C) Roman Ryltsov, 2008-2012 |
---|
3 | // Created by Roman Ryltsov roman@alax.info |
---|
4 | // |
---|
5 | // $Id: AvCodecLockManager.cpp 113 2012-09-05 07:23:48Z roman $ |
---|
6 | |
---|
7 | #include "stdafx.h" |
---|
8 | #include <atlbase.h> |
---|
9 | #include <atlsync.h> |
---|
10 | #include <atlcoll.h> |
---|
11 | |
---|
12 | extern "C" |
---|
13 | { |
---|
14 | #include "libavcodec\avcodec.h" |
---|
15 | } |
---|
16 | |
---|
17 | #pragma comment(lib, "avcodec.lib") |
---|
18 | |
---|
19 | class CModule |
---|
20 | { |
---|
21 | public: |
---|
22 | INT m_nMode; |
---|
23 | |
---|
24 | private: |
---|
25 | CEvent m_StartEvent; |
---|
26 | CComAutoCriticalSection m_CriticalSection; |
---|
27 | |
---|
28 | public: |
---|
29 | // CModule |
---|
30 | static int LockOperation(void** ppvLock, enum AVLockOp Operation) |
---|
31 | { |
---|
32 | ATLTRACE(atlTraceGeneral, 4, _T("ppvLock 0x%p, Operation %d\n"), ppvLock, Operation); |
---|
33 | ATLASSERT(ppvLock); |
---|
34 | CComCriticalSection* pCriticalSection = static_cast<CComCriticalSection*>(*ppvLock); |
---|
35 | switch(Operation) |
---|
36 | { |
---|
37 | case AV_LOCK_CREATE: |
---|
38 | { |
---|
39 | *ppvLock = new CComCriticalSection; |
---|
40 | if(!*ppvLock) |
---|
41 | return 1; // Failure |
---|
42 | CComCriticalSection* pCriticalSection = static_cast<CComCriticalSection*>(*ppvLock); |
---|
43 | ATLVERIFY(SUCCEEDED(pCriticalSection->Init())); |
---|
44 | } |
---|
45 | return 0; |
---|
46 | case AV_LOCK_OBTAIN: |
---|
47 | ATLVERIFY(SUCCEEDED(pCriticalSection->Lock())); |
---|
48 | return 0; |
---|
49 | case AV_LOCK_RELEASE: |
---|
50 | ATLVERIFY(SUCCEEDED(pCriticalSection->Unlock())); |
---|
51 | return 0; |
---|
52 | case AV_LOCK_DESTROY: |
---|
53 | ATLVERIFY(SUCCEEDED(pCriticalSection->Term())); |
---|
54 | delete pCriticalSection; |
---|
55 | *ppvLock = NULL; |
---|
56 | return 0; |
---|
57 | } |
---|
58 | return 1; // Failure |
---|
59 | } |
---|
60 | DWORD ThreadProc() |
---|
61 | { |
---|
62 | AVCodecContext* pContext = avcodec_alloc_context(); |
---|
63 | AVCodec* pCodec = avcodec_find_decoder_by_name("h264"); |
---|
64 | ATLASSERT(pContext); |
---|
65 | ATLVERIFY(WaitForSingleObject(m_StartEvent, INFINITE) == WAIT_OBJECT_0); |
---|
66 | for(; ; ) |
---|
67 | { |
---|
68 | if(m_nMode == 1) |
---|
69 | m_CriticalSection.Lock(); |
---|
70 | const INT nOpenResult = avcodec_open(pContext, pCodec); |
---|
71 | if(m_nMode == 1) |
---|
72 | m_CriticalSection.Unlock(); |
---|
73 | if(nOpenResult) |
---|
74 | _tprintf(_T("GetCurrentThreadId() %d, nOpenResult %d\n"), GetCurrentThreadId(), nOpenResult); |
---|
75 | if(m_nMode == 1) |
---|
76 | m_CriticalSection.Lock(); |
---|
77 | const INT nCloseResult = avcodec_close(pContext); |
---|
78 | if(m_nMode == 1) |
---|
79 | m_CriticalSection.Unlock(); |
---|
80 | if(nCloseResult) |
---|
81 | _tprintf(_T("GetCurrentThreadId() %d, nCloseResult %d\n"), GetCurrentThreadId(), nCloseResult); |
---|
82 | } |
---|
83 | return 0; |
---|
84 | } |
---|
85 | static DWORD __stdcall ThreadProc(CModule* pModule) |
---|
86 | { |
---|
87 | return pModule->ThreadProc(); |
---|
88 | } |
---|
89 | VOID Run() |
---|
90 | { |
---|
91 | if(m_nMode == 2) |
---|
92 | av_lockmgr_register(&CModule::LockOperation); |
---|
93 | ATLVERIFY(m_StartEvent.Create(NULL, TRUE, FALSE, NULL)); |
---|
94 | CAtlArray<CHandle> ThreadArray; |
---|
95 | static const SIZE_T g_nThreadCount = 8; |
---|
96 | for(SIZE_T nIndex = 0; nIndex < g_nThreadCount; nIndex++) |
---|
97 | { |
---|
98 | CHandle Thread; |
---|
99 | Thread.Attach(AtlCreateThread<CModule>(&CModule::ThreadProc, this)); |
---|
100 | ATLASSERT(Thread); |
---|
101 | ThreadArray.Add(Thread); |
---|
102 | Thread.Detach(); |
---|
103 | } |
---|
104 | avcodec_register_all(); |
---|
105 | m_StartEvent.Set(); |
---|
106 | _tprintf(_T("Started")); |
---|
107 | Sleep(60 * 1000); |
---|
108 | _tprintf(_T("Lucky")); |
---|
109 | } |
---|
110 | }; |
---|
111 | |
---|
112 | int _tmain(int argc, _TCHAR* argv[]) |
---|
113 | { |
---|
114 | #if defined(_DEBUG) |
---|
115 | AtlTraceLoadSettings(NULL); |
---|
116 | #endif // defined(_DEBUG) |
---|
117 | CModule Module; |
---|
118 | Module.m_nMode = 0; |
---|
119 | if(argc > 1) |
---|
120 | { |
---|
121 | if(_tcschr(argv[1], _T('1'))) |
---|
122 | Module.m_nMode = 1; |
---|
123 | else if(_tcschr(argv[1], _T('2'))) |
---|
124 | Module.m_nMode = 2; |
---|
125 | } |
---|
126 | _tprintf(_T("Mode %d\n"), Module.m_nMode); |
---|
127 | Module.Run(); |
---|
128 | return 0; |
---|
129 | } |
---|
130 | |
---|