Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PPageFileMediaInfo.cpp « mpc-hc « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: faf485d0f536ea61dbd9b3998ed74faf66876584 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * (C) 2009-2017 see Authors.txt
 *
 * This file is part of MPC-HC.
 *
 * MPC-HC 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.
 *
 * MPC-HC 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

// PPageFileMediaInfo.cpp : implementation file


#include "stdafx.h"
#include "mplayerc.h"
#include "MainFrm.h"
#include "PPageFileMediaInfo.h"
#include "WinAPIUtils.h"

#if USE_STATIC_MEDIAINFO
#include "MediaInfo/MediaInfo.h"
using namespace MediaInfoLib;
#define MediaInfo_int64u ZenLib::int64u
#else
#include "MediaInfoDLL/MediaInfoDLL.h"
using namespace MediaInfoDLL;
#endif

#define MEDIAINFO_BUFFER_SIZE 1024 * 256

// CPPageFileMediaInfo dialog

IMPLEMENT_DYNAMIC(CPPageFileMediaInfo, CPropertyPage)
CPPageFileMediaInfo::CPPageFileMediaInfo(CString path, IFileSourceFilter* pFSF, IDvdInfo2* pDVDI, CMainFrame* pMainFrame)
    : CPropertyPage(CPPageFileMediaInfo::IDD, CPPageFileMediaInfo::IDD)
    , m_fn(path)
    , m_path(path)
    , m_bSyncAnalysis(false)
{
    CComQIPtr<IAsyncReader> pAR;
    if (pFSF) {
        CComHeapPtr<OLECHAR> pFN;
        if (SUCCEEDED(pFSF->GetCurFile(&pFN, nullptr))) {
            m_fn = pFN;
        }

        if (CComQIPtr<IBaseFilter> pBF = pFSF) {
            BeginEnumPins(pBF, pEP, pPin) {
                if (pAR = pPin) {
                    break;
                }
            }
            EndEnumPins;
        }
    } else if (pDVDI) {
        ULONG len = 0;
        if (SUCCEEDED(pDVDI->GetDVDDirectory(m_path.GetBufferSetLength(MAX_PATH), MAX_PATH, &len)) && len) {
            m_path.ReleaseBuffer();
            m_fn = m_path += _T("\\VIDEO_TS.IFO");
        }
    }

    if (m_path.IsEmpty()) {
        m_path = m_fn;
    }

    if (m_path.GetLength() > 1 && m_path[1] == _T(':')
            && GetDriveType(m_path.Left(2) + _T('\\')) == DRIVE_CDROM) {
        // If we are playing from an optical drive, we do the analysis synchronously
        // when the user chooses to display the MediaInfo tab. We keep a reference
        // on the async reader filter but it will not cause any issue even if the
        // filter graph is destroyed before the analysis.
        m_bSyncAnalysis = true;
    }

    m_futureMIText = std::async(m_bSyncAnalysis ? std::launch::deferred : std::launch::async, [ = ]() {
#if USE_STATIC_MEDIAINFO
        MediaInfoLib::String filename = m_path;
        MediaInfoLib::MediaInfo MI;
#else
        MediaInfoDLL::String filename = m_path;
        MediaInfo MI;
#endif
        // If we do a synchronous analysis on an optical drive, we pause the video during
        // the analysis to avoid concurrent accesses to the drive. Note that due to the
        // synchronous nature of the analysis, we are sure that the graph state will not
        // change during the analysis.
        bool bUnpause = false;
        if (m_bSyncAnalysis && pMainFrame->GetMediaState() == State_Running) {
            pMainFrame->SendMessage(WM_COMMAND, ID_PLAY_PAUSE);
            bUnpause = true;
        }

        MI.Option(_T("ParseSpeed"), _T("0.5"));
        MI.Option(_T("Complete"));
        MI.Option(_T("Language"), _T("  Config_Text_ColumnSize;30"));

        LONGLONG llSize, llAvailable;
        if (pAR && SUCCEEDED(pAR->Length(&llSize, &llAvailable))) {
            size_t ret = MI.Open_Buffer_Init((MediaInfo_int64u)llSize);

            std::vector<BYTE> buffer(MEDIAINFO_BUFFER_SIZE);
            LONGLONG llPosition = 0;
            while ((ret & 0x1) && !(ret & 0x8) && llPosition < llAvailable) { // While accepted and not finished
                size_t szLength = (size_t)std::min(llAvailable - llPosition, (LONGLONG)buffer.size());
                if (pAR->SyncRead(llPosition, (LONG)szLength, buffer.data()) != S_OK) {
                    break;
                }

                ret = MI.Open_Buffer_Continue(buffer.data(), szLength);

                // Seek to a different position if needed
                MediaInfo_int64u uiNeeded = MI.Open_Buffer_Continue_GoTo_Get();
                if (uiNeeded != MediaInfo_int64u(-1)) {
                    llPosition = (LONGLONG)uiNeeded;
                    // Inform MediaInfo of the seek
                    MI.Open_Buffer_Init((MediaInfo_int64u)llSize, (MediaInfo_int64u)llPosition);
                } else {
                    llPosition += (LONGLONG)szLength;
                }

                if (FAILED(pAR->Length(&llSize, &llAvailable))) {
                    break;
                }
            }
            MI.Open_Buffer_Finalize();
        } else {
            MI.Open(filename);
        }

        if (bUnpause) {
            pMainFrame->SendMessage(WM_COMMAND, ID_PLAY_PLAY);
        }

        CString info = MI.Inform().c_str();

        if (info.IsEmpty() || !info.Find(_T("Unable to load"))) {
            info.LoadString(IDS_MEDIAINFO_NO_INFO_AVAILABLE);
        }

        return info;
    }).share();
}

CPPageFileMediaInfo::~CPPageFileMediaInfo()
{
}

void CPPageFileMediaInfo::DoDataExchange(CDataExchange* pDX)
{
    __super::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_MIEDIT, m_mediainfo);
}

BOOL CPPageFileMediaInfo::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN && pMsg->hwnd == m_mediainfo) {
        if (OnKeyDownInEdit(pMsg)) {
            return TRUE;
        }
    }

    return __super::PreTranslateMessage(pMsg);
}

BEGIN_MESSAGE_MAP(CPPageFileMediaInfo, CPropertyPage)
    ON_WM_SHOWWINDOW()
    ON_WM_DESTROY()
    ON_MESSAGE_VOID(WM_MEDIAINFO_READY, OnMediaInfoReady)
END_MESSAGE_MAP()

// CPPageFileMediaInfo message handlers

BOOL CPPageFileMediaInfo::OnInitDialog()
{
    __super::OnInitDialog();

    LOGFONT lf;
    ZeroMemory(&lf, sizeof(lf));
    lf.lfPitchAndFamily = DEFAULT_PITCH | FF_MODERN;
    // The empty string will fall back to the first font that matches the other specified attributes.
    LPCTSTR fonts[] = { _T("Lucida Console"), _T("Courier New"), _T("") };
    // Use a negative value to match the character height instead of the cell height.
    const int fonts_size[] = { 10, 11, 11 };
    size_t i = 0;
    bool bSuccess;
    DpiHelper dpi;
    do {
        _tcscpy_s(lf.lfFaceName, fonts[i]);
        lf.lfHeight = -dpi.ScaleY(fonts_size[i]);
        bSuccess = IsFontInstalled(fonts[i]) && m_font.CreateFontIndirect(&lf);
        i++;
    } while (!bSuccess && i < _countof(fonts));
    m_mediainfo.SetFont(&m_font);

    GetParent()->GetDlgItem(IDC_BUTTON_MI)->EnableWindow(FALSE); // Initially disable the "Save As" button

    if (m_bSyncAnalysis) { // Wait until the analysis is finished
        OnMediaInfoReady();
    } else { // Spawn a thread that will asynchronously update the window
        m_mediainfo.SetWindowText(ResStr(IDS_MEDIAINFO_ANALYSIS_IN_PROGRESS));
        m_threadSetText = std::thread([this]() {
            m_futureMIText.wait(); // Wait for the info to be ready
            PostMessage(WM_MEDIAINFO_READY); // then notify the window that MediaInfo analysis finished
        });
    }

    return TRUE;  // return TRUE unless you set the focus to a control
    // EXCEPTION: OCX Property Pages should return FALSE
}

void CPPageFileMediaInfo::OnShowWindow(BOOL bShow, UINT nStatus)
{
    __super::OnShowWindow(bShow, nStatus);

    GetParent()->GetDlgItem(IDC_BUTTON_MI)->ShowWindow(bShow ? SW_SHOW : SW_HIDE);
}

void CPPageFileMediaInfo::OnDestroy()
{
    if (m_threadSetText.joinable()) {
        m_threadSetText.join();
    }
}

void CPPageFileMediaInfo::OnMediaInfoReady()
{
    if (m_futureMIText.get() != ResStr(IDS_MEDIAINFO_NO_INFO_AVAILABLE)) {
        GetParent()->GetDlgItem(IDC_BUTTON_MI)->EnableWindow(TRUE);
    }
    m_mediainfo.SetWindowText(m_futureMIText.get());
}

bool CPPageFileMediaInfo::OnKeyDownInEdit(MSG* pMsg)
{
    bool bHandled = false;

    if ((LOWORD(pMsg->wParam) == _T('A') || LOWORD(pMsg->wParam) == _T('a'))
            && (GetKeyState(VK_CONTROL) < 0)) {
        m_mediainfo.SetSel(0, -1, TRUE);
        bHandled = true;
    }

    return bHandled;
}

#if !USE_STATIC_MEDIAINFO
bool CPPageFileMediaInfo::HasMediaInfo()
{
    MediaInfo MI;
    return MI.IsReady();
}
#endif

void CPPageFileMediaInfo::OnSaveAs()
{
    CString fn = m_fn;

    fn.TrimRight(_T('/'));
    int i = std::max(fn.ReverseFind(_T('\\')), fn.ReverseFind(_T('/')));
    if (i >= 0 && i < fn.GetLength() - 1) {
        fn = fn.Mid(i + 1);
    }
    fn.Append(_T(".MediaInfo.txt"));

    CFileDialog fileDlg(FALSE, _T("*.txt"), fn,
                        OFN_EXPLORER | OFN_ENABLESIZING | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR,
                        _T("Text Files (*.txt)|*.txt|All Files (*.*)|*.*||"), this, 0);

    if (fileDlg.DoModal() == IDOK) { // user has chosen a file
        CFile file;
        if (file.Open(fileDlg.GetPathName(), CFile::modeCreate | CFile::modeWrite)) {
            TCHAR bom = (TCHAR)0xFEFF;
            file.Write(&bom, sizeof(TCHAR));
            file.Write(LPCTSTR(m_futureMIText.get()), m_futureMIText.get().GetLength() * sizeof(TCHAR));
            file.Close();
        }
    }
}