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

SubPicQueueImpl.h « SubPic « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 630bd074b9d60e225ca76026687654ebdbcecb5c (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
/*
 * (C) 2003-2006 Gabest
 * (C) 2006-2014 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/>.
 *
 */

#pragma once

#include <memory>
#include <mutex>
#include <condition_variable>

#include "ISubPic.h"
#include "SubPicQueueSettings.h"

class CSubPicQueueImpl : public CUnknown, public ISubPicQueue
{
    static const double DEFAULT_FPS;

protected:
    struct SubPicProviderWithSharedLock {
    private:
        unsigned int m_nRef = 0;
        std::mutex m_mutex;

    public:
        const CComPtr<ISubPicProvider> pSubPicProvider;

        SubPicProviderWithSharedLock(const CComPtr<ISubPicProvider>& pSubPicProvider)
            : pSubPicProvider(pSubPicProvider) {
        }

        HRESULT Lock() {
            HRESULT hr;

            if (pSubPicProvider) {
                std::lock_guard<std::mutex> lock(m_mutex);

                hr = S_OK;
                if ((m_nRef == 0 && SUCCEEDED(hr = pSubPicProvider->Lock()))
                        || m_nRef > 0) {
                    m_nRef++;
                }
            } else {
                hr = E_POINTER;
            }

            return hr;
        }

        HRESULT Unlock() {
            HRESULT hr;

            if (pSubPicProvider) {
                std::lock_guard<std::mutex> lock(m_mutex);

                hr = S_OK;
                if ((m_nRef == 1 && SUCCEEDED(hr = pSubPicProvider->Unlock()))
                        || m_nRef > 1) {
                    m_nRef--;
                }
            } else {
                hr = E_POINTER;
            }

            return hr;
        }
    };

private:
    CCritSec m_csSubPicProvider;
    std::shared_ptr<SubPicProviderWithSharedLock> m_pSubPicProviderWithSharedLock;

protected:
    double m_fps;
    REFERENCE_TIME m_rtTimePerFrame;
    REFERENCE_TIME m_rtTimePerSubFrame;
    REFERENCE_TIME m_rtNow;

    SubPicQueueSettings m_settings;

    CComPtr<ISubPicAllocator> m_pAllocator;

    std::shared_ptr<SubPicProviderWithSharedLock> GetSubPicProviderWithSharedLock() {
        CAutoLock cAutoLock(&m_csSubPicProvider);
        return m_pSubPicProviderWithSharedLock;
    }

    HRESULT RenderTo(ISubPic* pSubPic, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop, double fps, BOOL bIsAnimated);

public:
    CSubPicQueueImpl(SubPicQueueSettings settings, ISubPicAllocator* pAllocator, HRESULT* phr);
    virtual ~CSubPicQueueImpl();

    DECLARE_IUNKNOWN;
    STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void** ppv);

    // ISubPicQueue

    STDMETHODIMP SetSubPicProvider(ISubPicProvider* pSubPicProvider);
    STDMETHODIMP GetSubPicProvider(ISubPicProvider** pSubPicProvider);

    STDMETHODIMP SetFPS(double fps);
    STDMETHODIMP SetTime(REFERENCE_TIME rtNow);
    /*
    STDMETHODIMP Invalidate(REFERENCE_TIME rtInvalidate = -1) PURE;
    STDMETHODIMP_(bool) LookupSubPic(REFERENCE_TIME rtNow, ISubPic** ppSubPic) PURE;
    STDMETHODIMP_(bool) LookupSubPic(REFERENCE_TIME rtNow, bool bAdviseBlocking, CComPtr<ISubPic>& pSubPic);

    STDMETHODIMP GetStats(int& nSubPics, REFERENCE_TIME& rtNow, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop) PURE;
    STDMETHODIMP GetStats(int nSubPics, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop) PURE;
    */
};

class CSubPicQueue : public CSubPicQueueImpl, protected CAMThread
{
protected:
    bool m_bExitThread;

    CComPtr<ISubPic> m_pSubPic;
    CInterfaceList<ISubPic> m_queue;

    std::mutex m_mutexSubpic; // to protect m_pSubPic
    std::mutex m_mutexQueue; // to protect m_queue
    std::condition_variable m_condQueueFull;
    std::condition_variable m_condQueueReady;

    CAMEvent m_runQueueEvent;

    REFERENCE_TIME m_rtNowLast;

    bool m_bInvalidate;
    REFERENCE_TIME m_rtInvalidate;

    bool EnqueueSubPic(CComPtr<ISubPic>& pSubPic, bool bBlocking);
    REFERENCE_TIME GetCurrentRenderingTime();

    // CAMThread
    virtual DWORD ThreadProc();

public:
    CSubPicQueue(SubPicQueueSettings settings, ISubPicAllocator* pAllocator, HRESULT* phr);
    virtual ~CSubPicQueue();

    // ISubPicQueue

    STDMETHODIMP SetFPS(double fps);
    STDMETHODIMP SetTime(REFERENCE_TIME rtNow);

    STDMETHODIMP Invalidate(REFERENCE_TIME rtInvalidate = -1);
    STDMETHODIMP_(bool) LookupSubPic(REFERENCE_TIME rtNow, CComPtr<ISubPic>& pSubPic);
    STDMETHODIMP_(bool) LookupSubPic(REFERENCE_TIME rtNow, bool bAdviseBlocking, CComPtr<ISubPic>& pSubPic);

    STDMETHODIMP GetStats(int& nSubPics, REFERENCE_TIME& rtNow, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
    STDMETHODIMP GetStats(int nSubPic, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
};

class CSubPicQueueNoThread : public CSubPicQueueImpl
{
protected:
    CCritSec m_csLock;
    CComPtr<ISubPic> m_pSubPic;

public:
    CSubPicQueueNoThread(SubPicQueueSettings settings, ISubPicAllocator* pAllocator, HRESULT* phr);
    virtual ~CSubPicQueueNoThread();

    // ISubPicQueue

    STDMETHODIMP SetFPS(double fps);

    STDMETHODIMP Invalidate(REFERENCE_TIME rtInvalidate = -1);
    STDMETHODIMP_(bool) LookupSubPic(REFERENCE_TIME rtNow, CComPtr<ISubPic>& pSubPic);
    STDMETHODIMP_(bool) LookupSubPic(REFERENCE_TIME rtNow, bool bAdviseBlocking, CComPtr<ISubPic>& pSubPic);

    STDMETHODIMP GetStats(int& nSubPics, REFERENCE_TIME& rtNow, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
    STDMETHODIMP GetStats(int nSubPic, REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop);
};