File: gstwasapi2activator.cpp

package info (click to toggle)
gst-plugins-bad1.0 1.28.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,252 kB
  • sloc: ansic: 744,658; cpp: 300,297; objc: 3,559; xml: 3,351; sh: 1,095; python: 565; makefile: 181; java: 75
file content (171 lines) | stat: -rw-r--r-- 4,295 bytes parent folder | download | duplicates (3)
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
/* GStreamer
 * Copyright (C) 2025 Seungha Yang <seungha@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "gstwasapi2activator.h"
#include <objidl.h>

GST_DEBUG_CATEGORY_EXTERN (gst_wasapi2_debug);
#define GST_CAT_DEFAULT gst_wasapi2_debug

/* *INDENT-OFF* */
using namespace Microsoft::WRL;

void
Wasapi2ActivationHandler::CreateInstance (Wasapi2ActivationHandler ** handler,
    const wchar_t * device_id,
    const AUDIOCLIENT_ACTIVATION_PARAMS * params)
{
  auto self = new Wasapi2ActivationHandler ();
  self->device_id_ = device_id;

  if (params) {
    self->params_ = *params;
    self->prop_.vt = VT_BLOB;
    self->prop_.blob.cbSize = sizeof (AUDIOCLIENT_ACTIVATION_PARAMS);
    self->prop_.blob.pBlobData = (BYTE *) &self->params_;
    self->have_params_ = true;
  }

  *handler = self;
}

STDMETHODIMP_ (ULONG)
Wasapi2ActivationHandler::AddRef (void)
{
  return InterlockedIncrement (&refcount_);
}

STDMETHODIMP_ (ULONG)
Wasapi2ActivationHandler::Release (void)
{
  ULONG ref_count;

  ref_count = InterlockedDecrement (&refcount_);

  if (ref_count == 0)
    delete this;

  return ref_count;
}

STDMETHODIMP
Wasapi2ActivationHandler::QueryInterface (REFIID riid, void ** object)
{
  if (riid == __uuidof(IUnknown) || riid == __uuidof(IAgileObject)) {
    *object = static_cast<IUnknown *>(static_cast<Wasapi2ActivationHandler*>(this));
  } else if (riid == __uuidof(IActivateAudioInterfaceCompletionHandler)) {
    *object = static_cast<IActivateAudioInterfaceCompletionHandler *>(
        static_cast<Wasapi2ActivationHandler*>(this));
  } else if (riid == IID_Wasapi2ActivationHandler) {
    *object = this;
  } else {
    *object = nullptr;
    return E_NOINTERFACE;
  }

  AddRef ();

  return S_OK;
}

STDMETHODIMP
Wasapi2ActivationHandler::ActivateCompleted (IActivateAudioInterfaceAsyncOperation * op)
{
  ComPtr<IUnknown> iface;
  HRESULT hr = S_OK;
  HRESULT activate_hr = S_OK;
  hr = op->GetActivateResult (&activate_hr, &iface);
  if (!gst_wasapi2_result (hr))
    GST_WARNING ("Couldn't get activate result, hr: 0x%x", (guint) hr);

  if (!gst_wasapi2_result (activate_hr)) {
    GST_WARNING ("GetActivateResult failed, hr: 0x%x", (guint) activate_hr);
    hr = activate_hr;
  }

  if (SUCCEEDED (hr) && !iface) {
    GST_ERROR ("Couldn't get inteface from asyncop");
    hr = E_FAIL;
  }

  if (FAILED (hr)) {
    activate_hr_ = hr;
    SetEvent (event_);
    return hr;
  }

  {
    std::lock_guard<std::mutex> lk (lock_);
    hr = iface.As (&client_);
    activate_hr_ = hr;
  }

  GST_LOG ("Activation result 0x%x", (guint) hr);

  SetEvent (event_);

  return hr;
}

HRESULT
Wasapi2ActivationHandler::ActivateAsync ()
{
  ComPtr<IActivateAudioInterfaceAsyncOperation> async_op;
  auto hr = ActivateAudioInterfaceAsync (device_id_.c_str (),
      __uuidof (IAudioClient), have_params_ ? &prop_ : nullptr,
      this, &async_op);
  if (!gst_wasapi2_result (hr)) {
    activate_hr_ = hr;
    SetEvent (event_);
  }

  return hr;
}

HRESULT
Wasapi2ActivationHandler::GetClient (IAudioClient ** client, DWORD timeout)
{
  WaitForSingleObject (event_, timeout);
  auto hr = activate_hr_.load ();
  if (!gst_wasapi2_result (hr))
    return hr;

  if (!client)
    return S_OK;

  std::lock_guard<std::mutex> lk (lock_);
  if (!client_)
    return E_FAIL;

  *client = client_.Get ();
  (*client)->AddRef ();

  return S_OK;
}

Wasapi2ActivationHandler::Wasapi2ActivationHandler ()
{
  event_ = CreateEvent (nullptr, TRUE, FALSE, nullptr);
}

Wasapi2ActivationHandler::~Wasapi2ActivationHandler ()
{
  CloseHandle (event_);
}
/* *INDENT-ON* */