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

MyPin.cpp « src - github.com/mpc-hc/sanear.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0c828d15bd37b46383561eb64e74f35abec7ac93 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "pch.h"
#include "MyPin.h"

#include "AudioRenderer.h"

namespace SaneAudioRenderer
{
    MyPin::MyPin(AudioRenderer& renderer, CBaseFilter* pFilter, HRESULT& result)
        : CBaseInputPin(L"SaneAudioRenderer::MyPin", pFilter, this, &result, L"Input0")
        , m_bufferFilled(TRUE/*manual reset*/)
        , m_renderer(renderer)
    {
        if (FAILED(result))
            return;

        if (static_cast<HANDLE>(m_bufferFilled) == NULL)
            result = E_OUTOFMEMORY;
    }

    HRESULT MyPin::CheckMediaType(const CMediaType* pmt)
    {
        CheckPointer(pmt, E_POINTER);

        if (pmt->majortype == MEDIATYPE_Audio &&
            pmt->formattype == FORMAT_WaveFormatEx)
        {
            auto pFormat = reinterpret_cast<const WAVEFORMATEX*>(pmt->pbFormat);

            if (!pFormat ||
                pmt->cbFormat < sizeof(WAVEFORMATEX) ||
                pmt->cbFormat != sizeof(WAVEFORMATEX) + pFormat->cbSize)
            {
                return E_INVALIDARG;
            }

            try
            {
                if (m_renderer.CheckFormat(CopyWaveFormat(*pFormat), m_live))
                    return S_OK;
            }
            catch (std::bad_alloc&)
            {
                return E_OUTOFMEMORY;
            }
        }

        return S_FALSE;
    }

    HRESULT MyPin::SetMediaType(const CMediaType* pmt)
    {
        assert(CritCheckIn(this));

        ReturnIfFailed(CBaseInputPin::SetMediaType(pmt));

        auto pFormat = reinterpret_cast<const WAVEFORMATEX*>(pmt->pbFormat);

        // No point in doing integrity checks, that was done in CheckMediaType().
        assert(pFormat);
        assert(pmt->cbFormat == sizeof(WAVEFORMATEX) + pFormat->cbSize);

        m_live = CheckLive(m_Connected);

        try
        {
            m_renderer.SetFormat(CopyWaveFormat(*pFormat), m_live);
        }
        catch (std::bad_alloc&)
        {
            return E_OUTOFMEMORY;
        }

        return S_OK;
    }

    HRESULT MyPin::CheckConnect(IPin* pPin)
    {
        assert(CritCheckIn(this));

        ReturnIfFailed(CBaseInputPin::CheckConnect(pPin));

        m_live = CheckLive(pPin);

        return S_OK;
    }

    STDMETHODIMP MyPin::NewSegment(REFERENCE_TIME startTime, REFERENCE_TIME stopTime, double rate)
    {
        CAutoLock receiveLock(&m_receiveMutex);
        CAutoLock objectLock(this);

        CBaseInputPin::NewSegment(startTime, stopTime, rate);
        m_renderer.NewSegment(rate);

        return S_OK;
    }

    STDMETHODIMP MyPin::Receive(IMediaSample* pSample)
    {
        CAutoLock receiveLock(&m_receiveMutex);

        {
            CAutoLock objectLock(this);

            if (m_state == State_Stopped)
                return VFW_E_WRONG_STATE;

            ReturnIfNotEquals(S_OK, CBaseInputPin::Receive(pSample));

            if (m_SampleProps.dwSampleFlags & AM_SAMPLE_TYPECHANGED)
            {
                // TODO: don't recreate the device when possible
                m_renderer.Finish(false, &m_bufferFilled);
                ReturnIfFailed(SetMediaType(static_cast<CMediaType*>(m_SampleProps.pMediaType)));
            }

            if (m_eosUp)
                return S_FALSE;
        }

        // Raise Receive() thread priority, once.
        if (m_hReceiveThread != GetCurrentThread())
        {
            m_hReceiveThread = GetCurrentThread();
            if (GetThreadPriority(m_hReceiveThread) < THREAD_PRIORITY_ABOVE_NORMAL)
                SetThreadPriority(m_hReceiveThread, THREAD_PRIORITY_ABOVE_NORMAL);
        }

        // Push() returns 'false' in case of interruption.
        return m_renderer.Push(pSample, m_SampleProps, &m_bufferFilled) ? S_OK : S_FALSE;
    }

    STDMETHODIMP MyPin::EndOfStream()
    {
        CAutoLock receiveLock(&m_receiveMutex);

        {
            CAutoLock objectLock(this);

            if (m_state == State_Stopped)
                return VFW_E_WRONG_STATE;

            if (m_bFlushing)
                return S_FALSE;

            m_eosUp = true;
        }

        // We ask audio renderer to block until all samples are played.
        // Finish() returns 'false' in case of interruption.
        bool eosDown = m_renderer.Finish(true, &m_bufferFilled);

        {
            CAutoLock objectLock(this);

            m_eosDown = eosDown;

            if (m_eosDown)
                m_pFilter->NotifyEvent(EC_COMPLETE, S_OK, (LONG_PTR)m_pFilter);
        }

        return S_OK;
    }

    STDMETHODIMP MyPin::BeginFlush()
    {
        // Parent method locks the object before modifying it, all is good.
        CBaseInputPin::BeginFlush();

        m_renderer.BeginFlush();

        // Barrier for any present Receive() and EndOfStream() calls.
        // Subsequent ones will be rejected because m_bFlushing == TRUE.
        CAutoLock receiveLock(&m_receiveMutex);

        m_bufferFilled.Reset();

        {
            CAutoLock objectLock(this);

            m_eosUp = false;
            m_eosDown = false;
        }

        m_hReceiveThread = NULL;

        m_renderer.EndFlush();

        return S_OK;
    }

    STDMETHODIMP MyPin::EndFlush()
    {
        // Parent method locks the object before modifying it, all is good.
        CBaseInputPin::EndFlush();

        return S_OK;
    }

    HRESULT MyPin::Active()
    {
        CAutoLock objectLock(this);

        assert(m_state != State_Paused);
        m_state = State_Paused;

        if (IsConnected())
        {
            m_renderer.Pause();
        }
        else
        {
            m_eosUp = true;
            m_eosDown = true;
        }

        return S_OK;
    }

    HRESULT MyPin::Run(REFERENCE_TIME startTime)
    {
        CAutoLock objectLock(this);

        assert(m_state == State_Paused);
        m_state = State_Running;

        if (m_eosDown)
        {
            m_pFilter->NotifyEvent(EC_COMPLETE, S_OK, (LONG_PTR)m_pFilter);
        }
        else
        {
            assert(IsConnected());
            m_renderer.Play(startTime);
        }

        return S_OK;
    }

    HRESULT MyPin::Inactive()
    {
        {
            CAutoLock objectLock(this);

            assert(m_state != State_Stopped);
            m_state = State_Stopped;

            CBaseInputPin::Inactive();
        }

        m_renderer.BeginFlush();

        // Barrier for any present Receive() and EndOfStream() calls.
        // Subsequent ones will be rejected because m_state == State_Stopped.
        CAutoLock receiveLock(&m_receiveMutex);

        m_bufferFilled.Reset();

        {
            CAutoLock objectLock(this);

            m_eosUp = false;
            m_eosDown = false;
        }

        m_hReceiveThread = NULL;

        m_renderer.Stop();
        m_renderer.EndFlush();

        return S_OK;
    }

    bool MyPin::StateTransitionFinished(uint32_t timeoutMilliseconds)
    {
        {
            CAutoLock objectLock(this);

            if (!IsConnected() || m_state != State_Paused)
                return true;
        }

        // Don't lock the object, we don't want to block Receive() method.

        // There won't be any state transitions during the wait,
        // because MyFilter always locks itself before calling this method.

        return !!m_bufferFilled.Wait(timeoutMilliseconds);
    }

    bool MyPin::CheckLive(IPin* pPin)
    {
        assert(pPin);

        bool live = false;

        IAMGraphStreamsPtr graphStreams;
        IAMPushSourcePtr pushSource;
        if (SUCCEEDED(m_pFilter->GetFilterGraph()->QueryInterface(IID_PPV_ARGS(&graphStreams))) &&
            SUCCEEDED(graphStreams->FindUpstreamInterface(pPin, IID_PPV_ARGS(&pushSource), AM_INTF_SEARCH_OUTPUT_PIN)))
        {
            live = true;

            ULONG flags;
            if (SUCCEEDED(pushSource->GetPushSourceFlags(&flags)))
            {
                if (flags & AM_PUSHSOURCECAPS_INTERNAL_RM)
                    DebugOut("MyPin upstream live pin has AM_PUSHSOURCECAPS_INTERNAL_RM flag");

                if (flags & AM_PUSHSOURCECAPS_NOT_LIVE)
                {
                    DebugOut("MyPin upstream live pin has AM_PUSHSOURCECAPS_NOT_LIVE flag");
                    live = false;
                }

                if (flags & AM_PUSHSOURCECAPS_PRIVATE_CLOCK)
                    DebugOut("MyPin upstream live pin has AM_PUSHSOURCECAPS_PRIVATE_CLOCK flag");

                if (flags & AM_PUSHSOURCEREQS_USE_STREAM_CLOCK)
                    DebugOut("MyPin upstream live pin has AM_PUSHSOURCEREQS_USE_STREAM_CLOCK flag");

                if (!flags)
                    DebugOut("MyPin upstream live pin has no flags");
            }
        }

        return live;
    }
}