summaryrefslogtreecommitdiffstats
path: root/audio/out/ao_wasapi.h
blob: 7e7cc6cd064af59c475b52360d8399c0f06d442f (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
/*
 * This file is part of mpv.
 *
 * Original author: Jonathan Yong <10walls@gmail.com>
 *
 * mpv 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 2 of the License, or
 * (at your option) any later version.
 *
 * mpv 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 mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef MP_AO_WASAPI_H_
#define MP_AO_WASAPI_H_

#define COBJMACROS 1
#define _WIN32_WINNT 0x600

#include <audioclient.h>
#include <audiopolicy.h>
#include <mmdeviceapi.h>
#include <avrt.h>

#include "osdep/atomics.h"

typedef struct change_notify {
    IMMNotificationClient client; /* this must be first in the structure! */
    LPWSTR monitored; /* Monitored device */
    struct ao *ao;
} change_notify;

HRESULT wasapi_change_init(struct ao* ao);
void wasapi_change_uninit(struct ao* ao);

#define EXIT_ON_ERROR(hres)  \
              do { if (FAILED(hres)) { goto exit_label; } } while(0)
#define SAFE_RELEASE(unk, release) \
              do { if ((unk) != NULL) { release; (unk) = NULL; } } while(0)

typedef struct wasapi_state {
    struct mp_log *log;
    HANDLE threadLoop;

    /* Init phase */
    HRESULT init_ret;
    HANDLE init_done;
    int share_mode;

    HANDLE hUninit;

    /* volume control */
    DWORD vol_hw_support, status;
    float audio_volume;
    float previous_volume;
    float initial_volume;

    /* Buffers */
    size_t buffer_block_size; /* Size of each block in bytes */
    REFERENCE_TIME
        minRequestedDuration; /* minimum wasapi buffer block size, in 100-nanosecond units */
    REFERENCE_TIME
        defaultRequestedDuration; /* default wasapi default block size, in 100-nanosecond units */
    UINT32 bufferFrameCount; /* wasapi buffer block size, number of frames, frame size at format.nBlockAlign */

    /* WASAPI handles, owned by other thread */
    IMMDevice *pDevice;
    IAudioClient *pAudioClient;
    IAudioRenderClient *pRenderClient;
    ISimpleAudioVolume *pAudioVolume;
    IAudioEndpointVolume *pEndpointVolume;
    IAudioSessionControl *pSessionControl;
    IMMDeviceEnumerator *pEnumerator;

    HANDLE hFeed; /* wasapi event */
    HANDLE hForceFeed; /* forces writing a buffer (e.g. before audio_resume) */
    HANDLE hFeedDone; /* set only after a hForceFeed */
    HANDLE hTask; /* AV thread */
    DWORD taskIndex; /* AV task ID */
    WAVEFORMATEXTENSIBLE format;

    /* WASAPI proxy handles, for Single-Threaded Apartment communication.
       One is needed for each object that's accessed by a different thread. */
    IAudioClient *pAudioClientProxy;
    ISimpleAudioVolume *pAudioVolumeProxy;
    IAudioEndpointVolume *pEndpointVolumeProxy;
    IAudioSessionControl *pSessionControlProxy;

    /* Streams used to marshal the proxy objects. The thread owning the actual objects
       needs to marshal proxy objects into these streams, and the thread that wants the
       proxies unmarshals them from here. */
    IStream *sAudioClient;
    IStream *sAudioVolume;
    IStream *sEndpointVolume;
    IStream *sSessionControl;

    /* WASAPI internal clock information, for estimating delay */
    IAudioClock *pAudioClock;
    UINT64 clock_frequency; /* scale for the "samples" returned by the clock */
    atomic_ullong sample_count; /* the amount of samples per channel written to a GetBuffer buffer */
    LARGE_INTEGER qpc_frequency; /* frequency of windows' high resolution timer */

    int opt_exclusive;
    int opt_list;
    char *opt_device;

    /* We still need to support XP, don't use these functions directly, blob owned by main thread */
    struct {
        HMODULE hAvrt;
        HANDLE (WINAPI *pAvSetMmThreadCharacteristicsW)(LPCWSTR, LPDWORD);
        WINBOOL (WINAPI *pAvRevertMmThreadCharacteristics)(HANDLE);
    } VistaBlob;

    change_notify change;
} wasapi_state;

#endif