audir 0.1.0

Low-level audio library
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
#![allow(non_upper_case_globals)]

pub mod com;
mod fence;

use self::fence::*;

pub use winapi::shared::winerror::HRESULT;
pub type WasapiResult<T> = (T, HRESULT);

use com::{Guid, WeakPtr};
use std::{
    collections::HashMap, ffi::OsString, mem, os::windows::ffi::OsStringExt, ptr, slice,
    sync::Mutex,
};
use winapi::shared::{
    devpkey::*, ksmedia, minwindef::DWORD, mmreg::*, winerror, wtypes::PROPERTYKEY,
};
use winapi::um::{
    audioclient::*, audiosessiontypes::*, combaseapi::*, coml2api::STGM_READ, mmdeviceapi::*,
    objbase::COINIT_MULTITHREADED, propsys::*, winnt::*,
};
use winapi::Interface;

use crate::{
    api::{self, Result},
    handle::Handle,
};

unsafe fn string_from_wstr(os_str: *const WCHAR) -> String {
    let mut len = 0;
    while *os_str.offset(len) != 0 {
        len += 1;
    }
    let string: OsString = OsStringExt::from_wide(slice::from_raw_parts(os_str, len as _));
    string.into_string().unwrap()
}

#[repr(C)]
#[derive(com_impl::ComImpl)]
#[interfaces(IMMNotificationClient)]
pub struct NotificationClient {
    vtbl: com_impl::VTable<IMMNotificationClientVtbl>,
    refcount: com_impl::Refcount,
    cb: Box<dyn FnMut(api::Event)>,
}

#[com_impl::com_impl]
unsafe impl IMMNotificationClient for NotificationClient {
    unsafe fn on_device_state_changed(&self, pwstrDeviceId: LPCWSTR, state: DWORD) -> HRESULT {
        println!("changed {} to {}", string_from_wstr(pwstrDeviceId), state);
        winerror::S_OK
    }

    unsafe fn on_device_added(&self, pwstrDeviceId: LPCWSTR) -> HRESULT {
        println!("added {}", string_from_wstr(pwstrDeviceId));
        winerror::S_OK
    }

    unsafe fn on_device_removed(&self, pwstrDeviceId: LPCWSTR) -> HRESULT {
        println!("removed {}", string_from_wstr(pwstrDeviceId));
        winerror::S_OK
    }

    unsafe fn on_default_device_changed(
        &self,
        _flow: EDataFlow,
        role: ERole,
        pwstrDefaultDeviceId: LPCWSTR,
    ) -> HRESULT {
        if role == eConsole {
            println!("default {:?} ({})", pwstrDefaultDeviceId, role);
        }

        winerror::S_OK
    }

    unsafe fn on_property_value_changed(
        &self,
        _pwstrDeviceId: LPCWSTR,
        _key: PROPERTYKEY,
    ) -> HRESULT {
        winerror::S_OK
    }
}

fn map_frame_desc(frame_desc: &api::FrameDesc) -> Option<WAVEFORMATEXTENSIBLE> {
    let (format_tag, sub_format, bytes_per_sample) = match frame_desc.format {
        api::Format::F32 => (
            WAVE_FORMAT_EXTENSIBLE,
            ksmedia::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT,
            4,
        ),
        api::Format::U32 => return None,
        _ => unimplemented!(),
    };

    let mut channel_mask = 0;
    {
        let channels = frame_desc.channels;
        if channels.contains(api::ChannelMask::FRONT_LEFT) {
            channel_mask |= SPEAKER_FRONT_LEFT;
        }
        if channels.contains(api::ChannelMask::FRONT_RIGHT) {
            channel_mask |= SPEAKER_FRONT_RIGHT;
        }
        if channels.contains(api::ChannelMask::FRONT_CENTER) {
            channel_mask |= SPEAKER_FRONT_CENTER;
        }
    }

    let num_channels = frame_desc.num_channels();
    let bits_per_sample = 8 * bytes_per_sample;

    let format = WAVEFORMATEX {
        wFormatTag: format_tag,
        nChannels: num_channels as _,
        nSamplesPerSec: frame_desc.sample_rate as _,
        nAvgBytesPerSec: (num_channels * frame_desc.sample_rate * bytes_per_sample) as _,
        nBlockAlign: (num_channels * bytes_per_sample) as _,
        wBitsPerSample: bits_per_sample as _,
        cbSize: (mem::size_of::<WAVEFORMATEXTENSIBLE>() - mem::size_of::<WAVEFORMATEX>()) as _,
    };

    Some(WAVEFORMATEXTENSIBLE {
        Format: format,
        Samples: bits_per_sample as _,
        dwChannelMask: channel_mask,
        SubFormat: sub_format,
    })
}

unsafe fn map_waveformat(format: *const WAVEFORMATEX) -> Result<api::FrameDesc> {
    let wave_format = &*format;
    match wave_format.wFormatTag {
        WAVE_FORMAT_EXTENSIBLE => {
            let wave_format_ex = &*(format as *const WAVEFORMATEXTENSIBLE);
            let subformat = Guid(wave_format_ex.SubFormat);
            let samples = wave_format_ex.Samples;
            let format =
                if subformat == Guid(ksmedia::KSDATAFORMAT_SUBTYPE_IEEE_FLOAT) && samples == 32 {
                    api::Format::F32
                } else {
                    return Err(api::Error::Internal { cause: "unsupported format".into() }); // TODO
                };

            let mut channels = api::ChannelMask::empty();
            if wave_format_ex.dwChannelMask & SPEAKER_FRONT_LEFT != 0 {
                channels |= api::ChannelMask::FRONT_LEFT;
            }
            if wave_format_ex.dwChannelMask & SPEAKER_FRONT_RIGHT != 0 {
                channels |= api::ChannelMask::FRONT_RIGHT;
            }
            if wave_format_ex.dwChannelMask & SPEAKER_FRONT_CENTER != 0 {
                channels |= api::ChannelMask::FRONT_CENTER;
            }

            Ok(api::FrameDesc {
                format,
                channels,
                sample_rate: wave_format.nSamplesPerSec as _,
            })
        }
        _ => Err(api::Error::Internal { cause: "unsupported wave format".into() }), // TODO
    }
}

fn map_sharing_mode(sharing: api::SharingMode) -> AUDCLNT_SHAREMODE {
    match sharing {
        api::SharingMode::Exclusive => AUDCLNT_SHAREMODE_EXCLUSIVE,
        api::SharingMode::Concurrent => AUDCLNT_SHAREMODE_SHARED,
    }
}

type InstanceRaw = WeakPtr<IMMDeviceEnumerator>;
type PhysicalDeviceRaw = WeakPtr<IMMDevice>;
struct PhysicalDevice {
    device: PhysicalDeviceRaw,
    audio_client: WeakPtr<IAudioClient>,
    streams: api::StreamFlags,
}

impl PhysicalDevice {
    unsafe fn state(&self) -> u32 {
        let mut state = 0;
        self.device.GetState(&mut state);
        state
    }
}

type PhysicalDeviceId = String;
type PhysialDeviceMap = HashMap<PhysicalDeviceId, Handle<PhysicalDevice>>;

pub struct Session(Option<audio_thread_priority::RtPriorityHandle>);

impl std::ops::Drop for Session {
    fn drop(&mut self) {
        if let Some(handle) = self.0.take() {
            audio_thread_priority::demote_current_thread_from_real_time(handle).unwrap();
        }
    }
}

pub struct Instance {
    raw: InstanceRaw,
    physical_devices: Mutex<PhysialDeviceMap>,
    notifier: WeakPtr<NotificationClient>,
}

impl api::Instance for Instance {
    type Device = Device;
    type Session = Session;

    unsafe fn properties() -> api::InstanceProperties {
        api::InstanceProperties {
            driver_id: api::DriverId::Wasapi,
            stream_mode: api::StreamMode::Polling,
            sharing: api::SharingModeFlags::CONCURRENT | api::SharingModeFlags::EXCLUSIVE,
        }
    }

    unsafe fn create(_: &str) -> Self {
        CoInitializeEx(ptr::null_mut(), COINIT_MULTITHREADED);

        let mut instance = InstanceRaw::null();
        let _hr = CoCreateInstance(
            &CLSID_MMDeviceEnumerator,
            ptr::null_mut(),
            CLSCTX_ALL,
            &IMMDeviceEnumerator::uuidof(),
            instance.mut_void(),
        );

        let mut physical_devices = HashMap::new();
        Self::enumerate_physical_devices_by_flow(&mut physical_devices, instance, eCapture);
        Self::enumerate_physical_devices_by_flow(&mut physical_devices, instance, eRender);

        Instance {
            raw: instance,
            physical_devices: Mutex::new(physical_devices),
            notifier: WeakPtr::null(),
        }
    }

    unsafe fn enumerate_physical_devices(&self) -> Vec<api::PhysicalDevice> {
        let mut physical_devices = self.physical_devices.lock().unwrap();

        Self::enumerate_physical_devices_by_flow(&mut physical_devices, self.raw, eCapture);
        Self::enumerate_physical_devices_by_flow(&mut physical_devices, self.raw, eRender);

        physical_devices
            .values()
            .filter_map(|device| {
                if device.state() & DEVICE_STATE_ACTIVE != 0 {
                    Some(device.raw())
                } else {
                    None
                }
            })
            .collect()
    }

    unsafe fn default_physical_input_device(&self) -> Option<api::PhysicalDevice> {
        let mut device = PhysicalDeviceRaw::null();
        let _hr = self
            .raw
            .GetDefaultAudioEndpoint(eCapture, eConsole, device.mut_void() as *mut _);
        if device.is_null() {
            None
        } else {
            let id = Self::get_physical_device_id(device);
            Some(self.physical_devices.lock().unwrap()[&id].raw())
        }
    }

    unsafe fn default_physical_output_device(&self) -> Option<api::PhysicalDevice> {
        let mut device = PhysicalDeviceRaw::null();
        let _hr = self
            .raw
            .GetDefaultAudioEndpoint(eRender, eConsole, device.mut_void() as *mut _);
        if device.is_null() {
            None
        } else {
            let id = Self::get_physical_device_id(device);
            Some(self.physical_devices.lock().unwrap()[&id].raw())
        }
    }

    unsafe fn physical_device_properties(
        &self,
        physical_device: api::PhysicalDevice,
    ) -> Result<api::PhysicalDeviceProperties> {
        type PropertyStore = WeakPtr<IPropertyStore>;

        let physical_device = Handle::<PhysicalDevice>::from_raw(physical_device);

        let mut store = PropertyStore::null();
        physical_device
            .device
            .OpenPropertyStore(STGM_READ, store.mut_void() as *mut _);

        let device_name = {
            let mut value = mem::MaybeUninit::uninit();
            store.GetValue(
                &DEVPKEY_Device_FriendlyName as *const _ as *const _,
                value.as_mut_ptr(),
            );
            let os_str = *value.assume_init().data.pwszVal();
            string_from_wstr(os_str)
        };

        let _form_factor = {
            let mut value = mem::MaybeUninit::uninit();
            store.GetValue(
                &PKEY_AudioEndpoint_FormFactor as *const _ as *const _,
                value.as_mut_ptr(),
            );
            *value.assume_init().data.uintVal()
        };

        Ok(api::PhysicalDeviceProperties {
            device_name,
            form_factor: api::FormFactor::Unknown, // todo
            streams: physical_device.streams,
        })
    }

    unsafe fn physical_device_default_concurrent_format(
        &self,
        physical_device: api::PhysicalDevice,
    ) -> Result<api::FrameDesc> {
        let physical_device = Handle::<PhysicalDevice>::from_raw(physical_device);

        let mut mix_format = ptr::null_mut();
        physical_device.audio_client.GetMixFormat(&mut mix_format);
        map_waveformat(mix_format)
    }

    unsafe fn create_device(
        &self,
        desc: api::DeviceDesc,
        channels: api::Channels,
        callback: api::StreamCallback,
    ) -> Result<Device> {
        if !channels.input.is_empty() && !channels.output.is_empty() {
            // no duplex
            return api::Error::validation("Duplex not supported");
        }

        let use_default_sample_rate = desc.sample_desc.sample_rate == api::DEFAULT_SAMPLE_RATE;
        if use_default_sample_rate && desc.sharing == api::SharingMode::Exclusive {
            return api::Error::validation("Default sample rate can't be used with exclusive sharing mode");
        }

        let physical_device = Handle::<PhysicalDevice>::from_raw(desc.physical_device);
        let sharing = map_sharing_mode(desc.sharing);

        let fence = Fence::create(false, false);

        let sample_rate = if use_default_sample_rate {
            self.physical_device_default_concurrent_format(desc.physical_device)?.sample_rate
        } else {
            desc.sample_desc.sample_rate
        };

        let frame_desc = api::FrameDesc {
            format: desc.sample_desc.format,
            channels: if !channels.input.is_empty() {
                channels.input
            } else {
                channels.output
            },
            sample_rate,
        };
        let mix_format = map_frame_desc(&frame_desc).unwrap(); // todo
        let _hr = physical_device.audio_client.Initialize(
            sharing,
            AUDCLNT_STREAMFLAGS_EVENTCALLBACK,
            0,
            0,
            &mix_format as *const _ as _,
            ptr::null(),
        );

        physical_device.audio_client.SetEventHandle(fence.0);

        let mut mix_format = ptr::null_mut();
        physical_device.audio_client.GetMixFormat(&mut mix_format);
        let frame_desc = map_waveformat(mix_format).unwrap();

        let (properties, device_stream) = if !channels.input.is_empty() {
            let mut capture_client = WeakPtr::<IAudioCaptureClient>::null();
            physical_device.audio_client.GetService(
                &IAudioCaptureClient::uuidof(),
                capture_client.mut_void() as _,
            );
            let buffer_size = {
                let mut size = 0;
                physical_device.audio_client.GetBufferSize(&mut size);
                size
            };

            let properties = api::StreamProperties {
                channels: frame_desc.channels,
                sample_rate: frame_desc.sample_rate,
                buffer_size: buffer_size as _,
            };
            let device_stream = DeviceStream::Input {
                client: capture_client,
            };

            (properties, device_stream)
        } else {
            let mut render_client = WeakPtr::<IAudioRenderClient>::null();
            physical_device
                .audio_client
                .GetService(&IAudioRenderClient::uuidof(), render_client.mut_void() as _);
            let buffer_size = {
                let mut size = 0;
                physical_device.audio_client.GetBufferSize(&mut size);
                size
            };

            let properties = api::StreamProperties {
                channels: frame_desc.channels,
                sample_rate: frame_desc.sample_rate,
                buffer_size: buffer_size as _,
            };
            let device_stream = DeviceStream::Output {
                client: render_client,
                buffer_size,
            };

            (properties, device_stream)
        };

        Ok(Device {
            client: physical_device.audio_client,
            fence,
            device_stream,
            callback,
            properties,
        })
    }

    unsafe fn create_session(&self, sample_rate: usize) -> Result<Session> {
        if sample_rate == api::DEFAULT_SAMPLE_RATE {
            return api::Error::validation("Default sample rate can't be used for session creation");
        }

        let rt_handle =
            audio_thread_priority::promote_current_thread_to_real_time(0, sample_rate as _)
                .unwrap();
        Ok(Session(Some(rt_handle)))
    }

    unsafe fn set_event_callback<F>(&mut self, callback: Option<F>) -> Result<()>
    where
        F: FnMut(api::Event) + Send + 'static,
    {
        if !self.notifier.is_null() {
            self.raw
                .UnregisterEndpointNotificationCallback(self.notifier.as_mut_ptr() as *mut _);
            self.notifier.as_unknown().Release();
        }

        if let Some(callback) = callback {
            self.notifier = WeakPtr::from_raw(NotificationClient::create_raw(Box::new(callback)));
            self.raw
                .RegisterEndpointNotificationCallback(self.notifier.as_mut_ptr() as *mut _);
        }

        Ok(())
    }

    unsafe fn physical_device_supports_format(
        &self,
        physical_device: api::PhysicalDevice,
        sharing: api::SharingMode,
        frame_desc: api::FrameDesc,
    ) -> bool {
        let physical_device = Handle::<PhysicalDevice>::from_raw(physical_device);

        let wave_format = map_frame_desc(&frame_desc).unwrap(); // todo
        let sharing = map_sharing_mode(sharing);

        let mut closest_format = ptr::null_mut();
        let hr = physical_device.audio_client.IsFormatSupported(
            sharing,
            &wave_format as *const _ as _,
            &mut closest_format
        );

        hr == winerror::S_OK
    }
}

impl Instance {
    unsafe fn get_physical_device_id(device: PhysicalDeviceRaw) -> String {
        let mut str_id = ptr::null_mut();
        device.GetId(&mut str_id);
        let mut len = 0;
        while *str_id.offset(len) != 0 {
            len += 1;
        }
        let name: OsString = OsStringExt::from_wide(slice::from_raw_parts(str_id, len as _));
        name.into_string().unwrap()
    }

    unsafe fn enumerate_physical_devices_by_flow(
        physical_devices: &mut PhysialDeviceMap,
        instance: InstanceRaw,
        ty: EDataFlow,
    ) {
        type DeviceCollection = WeakPtr<IMMDeviceCollection>;

        let stream_flags = match ty {
            eCapture => api::StreamFlags::INPUT,
            eRender => api::StreamFlags::OUTPUT,
            _ => unreachable!(),
        };

        let collection = {
            let mut collection = DeviceCollection::null();
            let _hr = instance.EnumAudioEndpoints(
                ty,
                DEVICE_STATEMASK_ALL,
                collection.mut_void() as *mut _,
            );
            collection
        };

        let num_items = {
            let mut num = 0;
            collection.GetCount(&mut num);
            num
        };

        for i in 0..num_items {
            let mut device = PhysicalDeviceRaw::null();
            collection.Item(i, device.mut_void() as *mut _);
            let id = Self::get_physical_device_id(device);

            let state = {
                let mut state = 0;
                device.GetState(&mut state);
                state
            };

            physical_devices
                .entry(id)
                .and_modify(|device| {
                    device.streams |= stream_flags;
                })
                .or_insert_with(|| {
                    let mut audio_client = WeakPtr::<IAudioClient>::null();

                    if state & DEVICE_STATE_ACTIVE != 0 {
                        device.Activate(
                            &IAudioClient::uuidof(),
                            CLSCTX_ALL,
                            ptr::null_mut(),
                            audio_client.mut_void() as *mut _,
                        );
                    }

                    Handle::new(PhysicalDevice {
                        device,
                        audio_client,
                        streams: stream_flags,
                    })
                });
        }

        collection.Release();
    }
}

impl std::ops::Drop for Instance {
    fn drop(&mut self) {
        unsafe {
            self.raw.Release();
            if !self.notifier.is_null() {
                WeakPtr::from_raw(self.notifier.as_mut_ptr() as *mut IMMNotificationClient)
                    .Release();
            }
            // TODO: drop audio clients
        }
    }
}

pub enum DeviceStream {
    Input {
        client: WeakPtr<IAudioCaptureClient>,
    },
    Output {
        client: WeakPtr<IAudioRenderClient>,
        buffer_size: u32,
    },
}

pub struct Device {
    client: WeakPtr<IAudioClient>,
    fence: Fence,
    device_stream: DeviceStream,
    callback: api::StreamCallback,
    properties: api::StreamProperties,
}

impl std::ops::Drop for Device {
    fn drop(&mut self) {
        unsafe {
            self.client.Release();
            self.fence.destory();
        }
    }
}

impl Device {
    unsafe fn acquire_buffers(&mut self, timeout_ms: u32) -> Result<api::StreamBuffers> {
        self.fence.wait(timeout_ms);

        match self.device_stream {
            DeviceStream::Input { client } => {
                let mut len = 0;
                client.GetNextPacketSize(&mut len);

                let mut data = ptr::null_mut();
                let mut num_frames = 0;
                let mut flags = 0;

                client.GetBuffer(
                    &mut data,
                    &mut num_frames,
                    &mut flags,
                    ptr::null_mut(),
                    ptr::null_mut(),
                );

                if flags != 0 {
                    dbg!(flags);
                }

                Ok(api::StreamBuffers {
                    frames: num_frames as _,
                    input: data as _,
                    output: ptr::null_mut(),
                })
            }
            DeviceStream::Output {
                client,
                buffer_size,
            } => {
                let mut data = ptr::null_mut();
                let mut padding = 0;

                self.client.GetCurrentPadding(&mut padding);

                let len = buffer_size - padding;
                client.GetBuffer(len, &mut data);
                Ok(api::StreamBuffers {
                    frames: len as _,
                    input: ptr::null(),
                    output: data as _,
                })
            }
        }
    }

    unsafe fn release_buffers(&mut self, num_frames: api::Frames) -> Result<()> {
        match self.device_stream {
            DeviceStream::Input { client } => {
                client.ReleaseBuffer(num_frames as _);
            }
            DeviceStream::Output { client, .. } => {
                client.ReleaseBuffer(num_frames as _, 0);
            }
        }
        Ok(())
    }
}

impl api::Device for Device {
    unsafe fn start(&self) {
        self.client.Start();
    }

    unsafe fn stop(&self) {
        self.client.Stop();
    }

    unsafe fn stream_properties(&self) -> api::StreamProperties {
        self.properties
    }

    unsafe fn submit_buffers(&mut self, timeout_ms: u32) -> Result<()> {
        let buffers = self.acquire_buffers(timeout_ms)?;
        (self.callback)(api::Stream {
            properties: self.properties,
            buffers,
        });
        self.release_buffers(buffers.frames)
    }
}