Skip to content

Commit

Permalink
add unsubclasswindow && wait_for_completion && current_frame_rate && …
Browse files Browse the repository at this point in the history
…current_bit_rate implement...
  • Loading branch information
Jackarain committed May 13, 2012
1 parent b4b3687 commit 6957af6
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 44 deletions.
11 changes: 2 additions & 9 deletions avcore/avcore.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="2"
CharacterSet="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
Expand Down Expand Up @@ -98,7 +98,7 @@
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="2"
CharacterSet="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Expand Down Expand Up @@ -183,13 +183,6 @@
<File
RelativePath=".\avplayer.cpp"
>
<FileConfiguration
Name="Debug|Win32"
>
<Tool
Name="VCCLCompilerTool"
/>
</FileConfiguration>
</File>
<File
RelativePath=".\player_impl.cpp"
Expand Down
10 changes: 10 additions & 0 deletions avcore/avplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ BOOL avplayer::subclasswindow(HWND hwnd, BOOL in_process/* = TRUE*/)
return m_impl->subclasswindow(hwnd, in_process);
}

BOOL avplayer::unsubclasswindow(HWND hwnd)
{
return m_impl->unsubclasswindow(hwnd);
}

BOOL avplayer::open(LPCTSTR movie, int media_type)
{
return m_impl->open(movie, media_type);
Expand All @@ -48,6 +53,11 @@ BOOL avplayer::resume()
return m_impl->resume();
}

BOOL avplayer::wait_for_completion()
{
return m_impl->wait_for_completion();
}

BOOL avplayer::stop()
{
return m_impl->stop();
Expand Down
6 changes: 6 additions & 0 deletions avcore/avplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class EXPORT_API avplayer
// 子类化一个存在的窗口, in_process参数表示窗口是否在同一进程中.
BOOL subclasswindow(HWND hwnd, BOOL in_process = TRUE);

// 撤消子类化.
BOOL unsubclasswindow(HWND hwnd);

public:
// 打开一个媒体文件, movie是文件名, media_type可以是MEDIA_TYPE_FILE,
// 也可以是MEDIA_TYPE_BT, 注意, 这个函数只打开文件, 但并不播放.
Expand All @@ -62,6 +65,9 @@ class EXPORT_API avplayer
// 停止播放.
BOOL stop();

// 等待播放直到完成.
BOOL wait_for_completion();

// 关闭媒体, 如果打开的是一个bt文件, 那么
// 在这个bt文件中的所有视频文件将被关闭.
BOOL close();
Expand Down
33 changes: 32 additions & 1 deletion avcore/player_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,22 @@ BOOL player_impl::subclasswindow(HWND hwnd, BOOL in_process)
return TRUE;
}

BOOL player_impl::unsubclasswindow(HWND hwnd)
{
if (!IsWindow(m_hwnd) || !IsWindow(hwnd)
|| m_hwnd != hwnd || !m_old_win_proc)
{
return FALSE;
}

// 设置为原来的窗口过程.
::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)m_old_win_proc);
win_data_ptr->remove_window(hwnd);
m_hwnd = NULL;

return TRUE;
}

LRESULT CALLBACK player_impl::static_win_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
player_impl* this_ptr = win_data_ptr->lookup_window(hwnd);
Expand Down Expand Up @@ -720,6 +736,10 @@ BOOL player_impl::open(LPCTSTR movie, int media_type)
m_video_height = m_avplay->m_video_ctx->height;
}

// 打开视频实时码率和帧率计算.
enable_calc_frame_rate(m_avplay);
enable_calc_bit_rate(m_avplay);

return TRUE;

} while (0);
Expand All @@ -744,7 +764,7 @@ BOOL player_impl::play(int index /*= 0*/)
return FALSE;

// 如果是文件数据, 则直接播放.
if (start(m_avplay, index) != 0)
if (::start(m_avplay, index) != 0)
return FALSE;
m_cur_index = index;

Expand Down Expand Up @@ -785,6 +805,17 @@ BOOL player_impl::stop()
return FALSE;
}

BOOL player_impl::wait_for_completion()
{
if (m_avplay)
{
::wait_for_completion(m_avplay);
return TRUE;
}

return FALSE;
}

BOOL player_impl::close()
{
if (m_avplay)
Expand Down
6 changes: 6 additions & 0 deletions avcore/player_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class player_impl
// 子类化一个存在的窗口, in_process参数表示窗口是否在同一进程中.
BOOL subclasswindow(HWND hwnd, BOOL in_process);

// 撤消子类化.
BOOL unsubclasswindow(HWND hwnd);

public:
// 打开一个媒体文件, movie是文件名, media_type可以是MEDIA_TYPE_FILE,
// 也可以是MEDIA_TYPE_BT, 注意, 这个函数只打开文件, 但并不播放.
Expand All @@ -52,6 +55,9 @@ class player_impl
// 停止播放.
BOOL stop();

// 等待播放直到完成.
BOOL wait_for_completion();

// 关闭媒体, 如果打开的是一个bt文件, 那么
// 在这个bt文件中的所有视频文件将被关闭.
BOOL close();
Expand Down
8 changes: 4 additions & 4 deletions avplayer/avplayer.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
Expand Down Expand Up @@ -62,7 +62,7 @@
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="2"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Expand Down Expand Up @@ -95,7 +95,7 @@
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
CharacterSet="2"
WholeProgramOptimization="1"
>
<Tool
Expand Down Expand Up @@ -135,7 +135,7 @@
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="2"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
Expand Down
63 changes: 36 additions & 27 deletions avplayer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// Windows 头文件:
#include <windows.h>
#include <ShellAPI.h>
#include <process.h>

// C 运行时头文件
#include <stdio.h>
Expand All @@ -30,6 +31,7 @@
#include <string>
#include "avplayer.h"

// 获得文件名后辍.
template <typename T>
static T extension(const T &filename)
{
Expand All @@ -41,50 +43,44 @@ static T extension(const T &filename)
: T(filename.c_str() + pos);
}

#ifdef UNICODE
typedef std::wstring auto_string;
#else
typedef std::string auto_string;
#endif // UNICODE

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
setlocale(LC_ALL, "chs");

auto_string filename;
MSG msg;
int ret = NULL;
void play_thread(void *param);

if (_tcslen(lpCmdLine) == 0)
int main(int argc, char* argv[])
{
// 判断播放参数是否足够.
if (argc != 2)
{
printf("usage: avplayer.exe <video>\n");
return -1;
}

filename.resize(_tcslen(lpCmdLine));
_tcscpy((TCHAR*)filename.data(), lpCmdLine);
// 设置语言环境为"中文".
setlocale(LC_ALL, "chs");

// 创建播放器.
avplayer win;

if (win.create_window(_T("main")) == NULL)
if (win.create_window("main") == NULL)
return -1;

auto_string ext = extension(filename);
if (ext == _T(".torrent"))
// 判断打开的媒体类型, 根据媒体文件类型选择不同的方式打开.
std::string filename = std::string(argv[1]);
std::string ext = extension<std::string>(filename);
if (ext == ".torrent")
{
if (!win.open(filename.c_str(), MEDIA_TYPE_BT))
return -1;
}
else
{
auto_string str = filename;
auto_string is_url = str.substr(0, 7);
if (is_url == _T("http://"))
std::string str = filename;
std::string is_url = str.substr(0, 7);
if (is_url == "http://")
{
if (!win.open(filename.c_str(), MEDIA_TYPE_HTTP))
return -1;
}
else if (is_url == _T("rtsp://"))
else if (is_url == "rtsp://")
{
if (!win.open(filename.c_str(), MEDIA_TYPE_RTSP))
return -1;
Expand All @@ -96,8 +92,11 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
}
}

win.play();
// 在线程中运行或直接运行win.play();
_beginthread(play_thread, NULL, (void*)&win);

// 消息循环.
MSG msg;
while (true)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
Expand All @@ -116,7 +115,17 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
}
}

// 关闭播放器.
win.close();

return 0;
}

void play_thread(void *param)
{
avplayer *play = (avplayer*)param;
play->play();
// 一直等待直到播放完成.
play->wait_for_completion();
// 播放完成后, 处理各种事件.
}
Loading

0 comments on commit 6957af6

Please sign in to comment.