summaryrefslogtreecommitdiffstats
path: root/dom/media/directshow/SampleSink.cpp
blob: fa5dc8d19c23f51f3d49bf4906a725badbbad0a6 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "SampleSink.h"
#include "AudioSinkFilter.h"
#include "AudioSinkInputPin.h"
#include "VideoUtils.h"
#include "mozilla/Logging.h"

using namespace mozilla::media;

namespace mozilla {

static LazyLogModule gDirectShowLog("DirectShowDecoder");
#define LOG(...) MOZ_LOG(gDirectShowLog, mozilla::LogLevel::Debug, (__VA_ARGS__))

SampleSink::SampleSink()
  : mMonitor("SampleSink"),
    mIsFlushing(false),
    mAtEOS(false)
{
  MOZ_COUNT_CTOR(SampleSink);
}

SampleSink::~SampleSink()
{
  MOZ_COUNT_DTOR(SampleSink);
}

void
SampleSink::SetAudioFormat(const WAVEFORMATEX* aInFormat)
{
  NS_ENSURE_TRUE(aInFormat, );
  ReentrantMonitorAutoEnter mon(mMonitor);
  memcpy(&mAudioFormat, aInFormat, sizeof(WAVEFORMATEX));
}

void
SampleSink::GetAudioFormat(WAVEFORMATEX* aOutFormat)
{
  MOZ_ASSERT(aOutFormat);
  ReentrantMonitorAutoEnter mon(mMonitor);
  memcpy(aOutFormat, &mAudioFormat, sizeof(WAVEFORMATEX));
}

HRESULT
SampleSink::Receive(IMediaSample* aSample)
{
  ReentrantMonitorAutoEnter mon(mMonitor);

  while (true) {
    if (mIsFlushing) {
      return S_FALSE;
    }
    if (!mSample) {
      break;
    }
    if (mAtEOS) {
      return E_UNEXPECTED;
    }
    // Wait until the consumer thread consumes the sample.
    mon.Wait();
  }

  if (MOZ_LOG_TEST(gDirectShowLog, LogLevel::Debug)) {
    REFERENCE_TIME start = 0, end = 0;
    HRESULT hr = aSample->GetMediaTime(&start, &end);
    LOG("SampleSink::Receive() [%4.2lf-%4.2lf]",
        (double)RefTimeToUsecs(start) / USECS_PER_S,
        (double)RefTimeToUsecs(end) / USECS_PER_S);
  }

  mSample = aSample;
  // Notify the signal, to awaken the consumer thread in WaitForSample()
  // if necessary.
  mon.NotifyAll();
  return S_OK;
}

HRESULT
SampleSink::Extract(RefPtr<IMediaSample>& aOutSample)
{
  ReentrantMonitorAutoEnter mon(mMonitor);
  // Loop until we have a sample, or we should abort.
  while (true) {
    if (mIsFlushing) {
      return S_FALSE;
    }
    if (mSample) {
      break;
    }
    if (mAtEOS) {
      // Order is important here, if we have a sample, we should return it
      // before reporting EOS.
      return E_UNEXPECTED;
    }
    // Wait until the producer thread gives us a sample.
    mon.Wait();
  }
  aOutSample = mSample;

  if (MOZ_LOG_TEST(gDirectShowLog, LogLevel::Debug)) {
    int64_t start = 0, end = 0;
    mSample->GetMediaTime(&start, &end);
    LOG("SampleSink::Extract() [%4.2lf-%4.2lf]",
        (double)RefTimeToUsecs(start) / USECS_PER_S,
        (double)RefTimeToUsecs(end) / USECS_PER_S);
  }

  mSample = nullptr;
  // Notify the signal, to awaken the producer thread in Receive()
  // if necessary.
  mon.NotifyAll();
  return S_OK;
}

void
SampleSink::Flush()
{
  LOG("SampleSink::Flush()");
  ReentrantMonitorAutoEnter mon(mMonitor);
  mIsFlushing = true;
  mSample = nullptr;
  mon.NotifyAll();
}

void
SampleSink::Reset()
{
  LOG("SampleSink::Reset()");
  ReentrantMonitorAutoEnter mon(mMonitor);
  mIsFlushing = false;
  mAtEOS = false;
}

void
SampleSink::SetEOS()
{
  LOG("SampleSink::SetEOS()");
  ReentrantMonitorAutoEnter mon(mMonitor);
  mAtEOS = true;
  // Notify to unblock any threads waiting for samples in
  // Extract() or Receive(). Now that we're at EOS, no more samples
  // will come!
  mon.NotifyAll();
}

bool
SampleSink::AtEOS()
{
  ReentrantMonitorAutoEnter mon(mMonitor);
  return mAtEOS && !mSample;
}

} // namespace mozilla