summaryrefslogtreecommitdiffstats
path: root/plugins/pluginWinMF/plugin_win_mf_producer_audio.cxx
blob: 5745b244f6813b02c43bcb38806a87b971f634f5 (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
/* Copyright (C) 2013 Mamadou DIOP
* Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 DOUBANGO.
*/
#include "plugin_win_mf_config.h"
#include "internals/mf_utils.h"
#include "internals/mf_sample_grabber.h"
#include "internals/mf_devices.h"

#include "tinydav/audio/tdav_producer_audio.h"

#include "tsk_thread.h"
#include "tsk_debug.h"

static void* TSK_STDCALL RunSessionThread(void *pArg);

typedef struct plugin_win_mf_producer_audio_s {
    TDAV_DECLARE_PRODUCER_AUDIO;

    bool bStarted;
    tsk_thread_handle_t* ppTread[1];

    DeviceListAudio* pDeviceList;

    IMFMediaSession *pSession;
    IMFMediaSource *pSource;
    SampleGrabberCB *pCallback;
    IMFActivate *pSinkActivate;
    IMFTopology *pTopology;
    IMFMediaType *pType;
}
plugin_win_mf_producer_audio_t;

/* ============ Media Producer Interface ================= */
static int plugin_win_mf_producer_audio_set(tmedia_producer_t* self, const tmedia_param_t* param)
{
    plugin_win_mf_producer_audio_t* pSelf = (plugin_win_mf_producer_audio_t*)self;
    if(param->plugin_type == tmedia_ppt_producer) {
    }
    return tdav_producer_audio_set(TDAV_PRODUCER_AUDIO(pSelf), param);
}

static int plugin_win_mf_producer_audio_prepare(tmedia_producer_t* self, const tmedia_codec_t* codec)
{
    plugin_win_mf_producer_audio_t* pSelf = (plugin_win_mf_producer_audio_t*)self;

    if(!pSelf || !codec) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    TMEDIA_PRODUCER(pSelf)->audio.channels = TMEDIA_CODEC_CHANNELS_AUDIO_ENCODING(codec);
    TMEDIA_PRODUCER(pSelf)->audio.rate = TMEDIA_CODEC_RATE_ENCODING(codec);
    TMEDIA_PRODUCER(pSelf)->audio.ptime = TMEDIA_CODEC_PTIME_AUDIO_ENCODING(codec);

    TSK_DEBUG_INFO("MF audio producer: channels=%d, rate=%d, ptime=%d",
                   TMEDIA_PRODUCER(pSelf)->audio.channels,
                   TMEDIA_PRODUCER(pSelf)->audio.rate,
                   TMEDIA_PRODUCER(pSelf)->audio.ptime
                  );

    HRESULT hr = S_OK;

    // create device list object
    if(!pSelf->pDeviceList && !(pSelf->pDeviceList = new DeviceListAudio())) {
        TSK_DEBUG_ERROR("Failed to create device list");
        hr = E_OUTOFMEMORY;
        goto  bail;
    }
    // enumerate devices
    hr = pSelf->pDeviceList->EnumerateDevices();
    if(!SUCCEEDED(hr)) {
        goto bail;
    }

    // check if we have at least one MF video source connected to the PC
    if(pSelf->pDeviceList->Count() == 0) {
        TSK_DEBUG_WARN("No MF video source could be found...no video will be sent");
        // do not break the negotiation as one-way video connection is a valid use-case
    }
    else {
        IMFActivate* pActivate = NULL;
        // Get best MF audio source
        hr = pSelf->pDeviceList->GetDeviceBest(&pActivate);
        if(!SUCCEEDED(hr) || !pActivate) {
            TSK_DEBUG_ERROR("Failed to get best MF audio source");
            if(!pActivate) {
                hr = E_OUTOFMEMORY;
            }
            goto bail;
        }

        // Create the media source for the device.
        hr = pActivate->ActivateObject(
                 __uuidof(IMFMediaSource),
                 (void**)&pSelf->pSource
             );
        SafeRelease(&pActivate);
        if(!SUCCEEDED(hr)) {
            TSK_DEBUG_ERROR("ActivateObject(MF audio source) failed");
            goto bail;
        }

        // Create and configure the media type
        CHECK_HR(hr = MFCreateMediaType(&pSelf->pType));
        CHECK_HR(hr = pSelf->pType->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Audio));
        CHECK_HR(hr = pSelf->pType->SetGUID(MF_MT_SUBTYPE, MFAudioFormat_PCM));
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_AUDIO_NUM_CHANNELS, TMEDIA_PRODUCER(pSelf)->audio.channels));
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_AUDIO_SAMPLES_PER_SECOND, TMEDIA_PRODUCER(pSelf)->audio.rate));
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_AUDIO_BITS_PER_SAMPLE, TMEDIA_PRODUCER(pSelf)->audio.bits_per_sample));
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_ALL_SAMPLES_INDEPENDENT, TRUE)); // because uncompressed media type
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_FIXED_SIZE_SAMPLES, TRUE));
        UINT32 nBlockAlign = TMEDIA_PRODUCER(pSelf)->audio.channels * (TMEDIA_PRODUCER(pSelf)->audio.bits_per_sample >> 3);
        UINT32 nAvgBytesPerSec = (nBlockAlign * TMEDIA_PRODUCER(pSelf)->audio.rate);
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_AUDIO_BLOCK_ALIGNMENT, nBlockAlign));
        CHECK_HR(hr = pSelf->pType->SetUINT32(MF_MT_AUDIO_AVG_BYTES_PER_SECOND, nAvgBytesPerSec));

        // Create the sample grabber sink.
        CHECK_HR(hr = SampleGrabberCB::CreateInstance(TMEDIA_PRODUCER(pSelf), &pSelf->pCallback));
        CHECK_HR(hr = MFCreateSampleGrabberSinkActivate(pSelf->pType, pSelf->pCallback, &pSelf->pSinkActivate));

        // To run as fast as possible, set this attribute (requires Windows 7):
        CHECK_HR(hr = pSelf->pSinkActivate->SetUINT32(MF_SAMPLEGRABBERSINK_IGNORE_CLOCK, TRUE));

        // Create the Media Session.
        CHECK_HR(hr = MFCreateMediaSession(NULL, &pSelf->pSession));

        // Create the topology.
        CHECK_HR(hr = MFUtils::CreateTopology(pSelf->pSource, NULL/*NO ENCODER*/, pSelf->pSinkActivate, NULL/*Preview*/, pSelf->pType, &pSelf->pTopology));
    }

bail:
    return SUCCEEDED(hr) ? 0 : -1;
}

static int plugin_win_mf_producer_audio_start(tmedia_producer_t* self)
{
    plugin_win_mf_producer_audio_t* pSelf = (plugin_win_mf_producer_audio_t*)self;

    if(!pSelf) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    if(pSelf->bStarted) {
        TSK_DEBUG_INFO("MF audio producer already started");
        return 0;
    }

    HRESULT hr = S_OK;

    // Run the media session.
    CHECK_HR(hr = MFUtils::RunSession(pSelf->pSession, pSelf->pTopology));

    // Start asynchronous watcher thread
    pSelf->bStarted = true;
    int ret = tsk_thread_create(&pSelf->ppTread[0], RunSessionThread, pSelf);
    if(ret != 0) {
        TSK_DEBUG_ERROR("Failed to create thread");
        hr = E_FAIL;
        pSelf->bStarted = false;
        if(pSelf->ppTread[0]) {
            tsk_thread_join(&pSelf->ppTread[0]);
        }
        MFUtils::ShutdownSession(pSelf->pSession, pSelf->pSource);
        goto bail;
    }

bail:
    return SUCCEEDED(hr) ? 0 : -1;
}

static int plugin_win_mf_producer_audio_pause(tmedia_producer_t* self)
{
    plugin_win_mf_producer_audio_t* pSelf = (plugin_win_mf_producer_audio_t*)self;

    if(!pSelf) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    HRESULT hr = MFUtils::PauseSession(pSelf->pSession);

    return SUCCEEDED(hr) ? 0 : -1;
}

static int plugin_win_mf_producer_audio_stop(tmedia_producer_t* self)
{
    plugin_win_mf_producer_audio_t* pSelf = (plugin_win_mf_producer_audio_t*)self;

    if(!pSelf) {
        TSK_DEBUG_ERROR("Invalid parameter");
        return -1;
    }

    HRESULT hr = S_OK;

    // for the thread
    pSelf->bStarted = false;
    hr = MFUtils::ShutdownSession(pSelf->pSession, NULL); // stop session to wakeup the asynchronous thread
    if(pSelf->ppTread[0]) {
        tsk_thread_join(&pSelf->ppTread[0]);
    }
    hr = MFUtils::ShutdownSession(NULL, pSelf->pSource); // stop source to release the camera

    return 0;
}


//
//	WaveAPI producer object definition
//
/* constructor */
static tsk_object_t* plugin_win_mf_producer_audio_ctor(tsk_object_t * self, va_list * app)
{
    MFUtils::Startup();

    plugin_win_mf_producer_audio_t *pSelf = (plugin_win_mf_producer_audio_t*)self;
    if(pSelf) {
        /* init base */
        tdav_producer_audio_init(TDAV_PRODUCER_AUDIO(pSelf));
        /* init self */

    }
    return self;
}
/* destructor */
static tsk_object_t* plugin_win_mf_producer_audio_dtor(tsk_object_t * self)
{
    plugin_win_mf_producer_audio_t *pSelf = (plugin_win_mf_producer_audio_t *)self;
    if(pSelf) {
        /* stop */
        if(pSelf->bStarted) {
            plugin_win_mf_producer_audio_stop(TMEDIA_PRODUCER(pSelf));
        }

        /* deinit base */
        tdav_producer_audio_deinit(TDAV_PRODUCER_AUDIO(pSelf));
        /* deinit self */
        if(pSelf->pDeviceList) {
            delete pSelf->pDeviceList, pSelf->pDeviceList = NULL;
        }
        if(pSelf->pSource) {
            pSelf->pSource->Shutdown();
        }
        if(pSelf->pSession) {
            pSelf->pSession->Shutdown();
        }

        SafeRelease(&pSelf->pSession);
        SafeRelease(&pSelf->pSource);
        SafeRelease(&pSelf->pCallback);
        SafeRelease(&pSelf->pSinkActivate);
        SafeRelease(&pSelf->pTopology);
        SafeRelease(&pSelf->pType);
    }

    return self;
}
/* object definition */
static const tsk_object_def_t plugin_win_mf_producer_audio_def_s = {
    sizeof(plugin_win_mf_producer_audio_t),
    plugin_win_mf_producer_audio_ctor,
    plugin_win_mf_producer_audio_dtor,
    tdav_producer_audio_cmp,
};
/* plugin definition*/
static const tmedia_producer_plugin_def_t plugin_win_mf_producer_audio_plugin_def_s = {
    &plugin_win_mf_producer_audio_def_s,

    tmedia_audio,
    "Media Foundation audio producer",

    plugin_win_mf_producer_audio_set,
    plugin_win_mf_producer_audio_prepare,
    plugin_win_mf_producer_audio_start,
    plugin_win_mf_producer_audio_pause,
    plugin_win_mf_producer_audio_stop
};
const tmedia_producer_plugin_def_t *plugin_win_mf_producer_audio_plugin_def_t = &plugin_win_mf_producer_audio_plugin_def_s;


// Run session async thread
static void* TSK_STDCALL RunSessionThread(void *pArg)
{
    plugin_win_mf_producer_audio_t *pSelf = (plugin_win_mf_producer_audio_t *)pArg;
    HRESULT hrStatus = S_OK;
    HRESULT hr = S_OK;
    IMFMediaEvent *pEvent = NULL;
    MediaEventType met;

    TSK_DEBUG_INFO("RunSessionThread (audio) - ENTER");

    while(pSelf->bStarted) {
        CHECK_HR(hr = pSelf->pSession->GetEvent(0, &pEvent));
        CHECK_HR(hr = pEvent->GetStatus(&hrStatus));
        CHECK_HR(hr = pEvent->GetType(&met));

        if (FAILED(hrStatus)) {
            TSK_DEBUG_ERROR("Session error: 0x%x (event id: %d)\n", hrStatus, met);
            hr = hrStatus;
            goto bail;
        }
        if (met == MESessionEnded) {
            break;
        }
        SafeRelease(&pEvent);
    }

bail:
    TSK_DEBUG_INFO("RunSessionThread (audio) - EXIT");

    return NULL;
}
OpenPOWER on IntegriCloud