summaryrefslogtreecommitdiffstats
path: root/plugins/pluginWinMF/internals/mf_display_watcher.cxx
blob: 38f36871e8afa78449451f6de9952e54b4e731eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright (C) 2013 Mamadou DIOP
* Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*/
#include "mf_display_watcher.h"
#include "mf_utils.h"

#include "tsk_debug.h"

#include <assert.h>

DisplayWatcher::DisplayWatcher(HWND hWnd, IMFMediaSink* pMediaSink, HRESULT &hr)
    : m_pDisplayControl(NULL)
    , m_hWnd(hWnd)
    , m_pWndProc(NULL)
    , m_bStarted(FALSE)
    , m_bFullScreen(FALSE)
{
    IMFGetService *pService = NULL;

    CHECK_HR(hr = pMediaSink->QueryInterface(__uuidof(IMFGetService), (void**)&pService));
    CHECK_HR(hr = pService->GetService(MR_VIDEO_RENDER_SERVICE, __uuidof(IMFVideoDisplayControl), (void**)&m_pDisplayControl));
    CHECK_HR(hr = m_pDisplayControl->SetAspectRatioMode(MFVideoARMode_PreservePicture));
bail:
    SafeRelease(&pService);
}

DisplayWatcher::~DisplayWatcher()
{
    Stop();

    SafeRelease(&m_pDisplayControl);
}

HRESULT DisplayWatcher::Start()
{
    HRESULT hr = S_OK;
    HWND hWnd = m_hWnd; // save()
    CHECK_HR(hr = Stop());

    if((m_hWnd = hWnd) && m_pDisplayControl) {
        CHECK_HR(hr = m_pDisplayControl->SetVideoWindow(hWnd));

        BOOL ret = SetPropA(m_hWnd, "This", this);
        assert(ret);

#if _M_X64
        m_pWndProc = (WNDPROC)SetWindowLongPtr(m_hWnd, GWLP_WNDPROC, (LONG_PTR)DisplayWatcher::WndProc);
#else
        m_pWndProc = (WNDPROC)SetWindowLongPtr(m_hWnd, GWL_WNDPROC, (LONG)DisplayWatcher::WndProc);
#endif

        UpdatePosition(); // black screen if attached later
    }
    m_bStarted = TRUE;
bail:
    return hr;
}

HRESULT DisplayWatcher::SetFullscreen(BOOL bEnabled)
{
    if(m_pDisplayControl) {
        HRESULT hr =  m_pDisplayControl->SetFullscreen(bEnabled);
        m_bFullScreen = SUCCEEDED(hr);
        return hr;
    }

    return E_FAIL;
}

HRESULT DisplayWatcher::SetHwnd(HWND hWnd)
{
    BOOL bWasStarted = m_bStarted;
    Stop();
    m_hWnd = hWnd;
    if(bWasStarted) {
        return Start();
    }
    return S_OK;
}

HRESULT DisplayWatcher::Stop()
{
    if(m_hWnd && m_pWndProc) {
        // Restore

#if _M_X64
        SetWindowLongPtr(m_hWnd, GWLP_WNDPROC, (LONG_PTR)m_pWndProc);
#else
        SetWindowLongPtr(m_hWnd, GWL_WNDPROC, (LONG)m_pWndProc);
#endif
    }
    m_hWnd = NULL;
    m_pWndProc = NULL;
    m_bStarted = FALSE;
    return S_OK;
}

void DisplayWatcher::UpdatePosition()
{
    if(m_pDisplayControl && m_hWnd) {
        RECT rcDst = { 0, 0, 0, 0 };
        GetClientRect(m_hWnd, &rcDst);
        m_pDisplayControl->SetVideoPosition(NULL, &rcDst);
    }
}

LRESULT CALLBACK DisplayWatcher::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg) {
    case WM_CREATE:
    case WM_SIZE:
    case WM_MOVE: {
        DisplayWatcher* This = dynamic_cast<DisplayWatcher*>((DisplayWatcher*)GetPropA(hWnd, "This"));
        if(This) {
            This->UpdatePosition();
        }
        break;
    }

    case WM_CHAR:
    case WM_KEYUP: {
        DisplayWatcher* This = dynamic_cast<DisplayWatcher*>((DisplayWatcher*)GetPropA(hWnd, "This"));
        if(This) {
            if(This->m_bFullScreen && (wParam == 0x1B || wParam == VK_ESCAPE)) {
                This->SetFullscreen(FALSE);
            }
        }

        break;
    }
    }

    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
OpenPOWER on IntegriCloud