summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/maemo/pcm-gstreamer.c
blob: 33fa6d343f40d20196e0e1e5cb25aef17d9f24e7 (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
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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 by Thomas Jarosch
 *
 * This program 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.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/


#include "autoconf.h"

#include <stdbool.h>
#include "config.h"
#include "debug.h"
#include "sound.h"
#include "audiohw.h"
#include "system.h"
#include "settings.h"

#include "playback.h"
#include "kernel.h"

#include <pthread.h>
#include <SDL.h>
#include <glib.h>
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
#include <linux/types.h>

/* Maemo5: N900 specific libplayback support */
#include <libplayback/playback.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>
#include "maemo-thread.h"

#ifdef HAVE_RECORDING
#include "audiohw.h"
#ifdef HAVE_SPDIF_IN
#include "spdif.h"
#endif
#endif

#include "pcm.h"
#include "pcm-internal.h"
#include "pcm_sampr.h"

/*#define LOGF_ENABLE*/
#include "logf.h"

#ifdef DEBUG
#include <stdio.h>
extern bool debug_audio;
#endif

/* Declarations for libplayblack */
pb_playback_t *playback = NULL;
void playback_state_req_handler(pb_playback_t *pb,
                                                      enum pb_state_e req_state,
                                                      pb_req_t *ext_req,
                                                      void *data);
void playback_state_req_callback(pb_playback_t *pb,
            enum pb_state_e granted_state,
            const char *reason,
            pb_req_t *req,
            void *data);
bool playback_granted = false;

/* Gstreamer related vars */
GstCaps *gst_audio_caps = NULL;
GstElement *gst_pipeline = NULL;
GstElement *gst_appsrc = NULL;
GstElement *gst_volume = NULL;
GstElement *gst_pulsesink = NULL;
GstBus *gst_bus = NULL;
static int bus_watch_id = 0;
GMainLoop *pcm_loop = NULL;

static const void* pcm_data = NULL;
static size_t pcm_data_size = 0;

static int audio_locked = 0;
static pthread_mutex_t audio_lock_mutex = PTHREAD_MUTEX_INITIALIZER;
static int inside_feed_data = 0;

/*
 * mutex lock/unlock wrappers neatness' sake
 */
static inline void lock_audio(void)
{
    pthread_mutex_lock(&audio_lock_mutex);
}

static inline void unlock_audio(void)
{
    pthread_mutex_unlock(&audio_lock_mutex);
}

void pcm_play_lock(void)
{
    if (++audio_locked == 1)
        lock_audio();
}

void pcm_play_unlock(void)
{
    if (--audio_locked == 0)
        unlock_audio();
}

void pcm_dma_apply_settings(void)
{
}

void pcm_play_dma_start(const void *addr, size_t size)
{
    pcm_data = addr;
    pcm_data_size = size;

    if (playback_granted)
    {
        /* Start playing now */
        if (!inside_feed_data)
            gst_element_set_state (GST_ELEMENT(gst_pipeline), GST_STATE_PLAYING);
        else
            DEBUGF("ERROR: dma_start called while inside feed_data\n");
    } else
    {
        /* N900: Request change to playing state */
        pb_playback_req_state   (playback,
                                                PB_STATE_PLAY,
                                                playback_state_req_callback,
                                                NULL);
    }
}

void pcm_play_dma_stop(void)
{
    if (inside_feed_data)
        g_signal_emit_by_name (gst_appsrc, "end-of-stream", NULL);
    else
        gst_element_set_state (GST_ELEMENT(gst_pipeline), GST_STATE_NULL);
}

static void feed_data(GstElement * appsrc, guint size_hint, void *unused)
{
    (void)size_hint;
    (void)unused;

    lock_audio();

    /* Make sure we don't trigger a gst_element_set_state() call
       from inside gstreamer's stream thread as it will deadlock */
    inside_feed_data = 1;

    if (pcm_play_dma_complete_callback(PCM_DMAST_OK, &pcm_data, &pcm_data_size))
    {
        GstBuffer *buffer = gst_buffer_new ();
        GstFlowReturn ret;

        GST_BUFFER_DATA (buffer) = (__u8 *)pcm_data;
        GST_BUFFER_SIZE (buffer) = pcm_data_size;

        g_signal_emit_by_name (appsrc, "push-buffer", buffer, &ret);
        gst_buffer_unref (buffer);

        if (ret != 0)
            DEBUGF("push-buffer error result: %d\n", ret);

        pcm_play_dma_status_callback(PCM_DMAST_STARTED);
    } else
    {
        DEBUGF("feed_data: No Data.\n");
        g_signal_emit_by_name (appsrc, "end-of-stream", NULL);
    }

    inside_feed_data = 0;

    unlock_audio();
}

static gboolean
gst_bus_message (GstBus * bus, GstMessage * message, void *unused)
{
    (void)bus;
    (void)unused;

    DEBUGF("    [gst] got BUS message %s\n",
        gst_message_type_get_name (GST_MESSAGE_TYPE (message)));

    switch (GST_MESSAGE_TYPE (message)) {
        case GST_MESSAGE_ERROR:
        {
        GError *err;
        gchar *debug;
        gst_message_parse_error (message, &err, &debug);

        DEBUGF("[gst] Received error: Src: %s, msg: %s\n", GST_MESSAGE_SRC_NAME(message), err->message);

        g_error_free (err);
        g_free (debug);
        }

        g_main_loop_quit (pcm_loop);
        break;
        case GST_MESSAGE_EOS:
        gst_element_set_state (GST_ELEMENT(gst_pipeline), GST_STATE_NULL);
        break;
    case GST_MESSAGE_STATE_CHANGED:
    {
        GstState old_state, new_state;

        gst_message_parse_state_changed (message, &old_state, &new_state, NULL);
        DEBUGF("[gst] Element %s changed state from %s to %s.\n",
            GST_MESSAGE_SRC_NAME(message),
            gst_element_state_get_name (old_state),
            gst_element_state_get_name (new_state));
        break;
        }
        default:
        break;
    }

    return TRUE;
}

void maemo_configure_appsrc(void)
{
    /* Block push-buffer until there is enough room */
    g_object_set (G_OBJECT(gst_appsrc), "block", TRUE, NULL);

    g_object_set(G_OBJECT(gst_appsrc), "format", GST_FORMAT_BYTES, NULL);

    gst_audio_caps = gst_caps_new_simple("audio/x-raw-int", "width", G_TYPE_INT, (gint)16, "depth", G_TYPE_INT, (gint)16, "channels" ,G_TYPE_INT, (gint)2,
                                "signed",G_TYPE_BOOLEAN,1,
                                "rate",G_TYPE_INT,44100,"endianness",G_TYPE_INT,(gint)1234,NULL);

    g_object_set (G_OBJECT(gst_appsrc), "caps", gst_audio_caps, NULL);

    gst_app_src_set_stream_type(GST_APP_SRC(gst_appsrc),
        GST_APP_STREAM_TYPE_STREAM);

    /* configure the appsrc, we will push data into the appsrc from the
    * mainloop. */
    g_signal_connect (gst_appsrc, "need-data", G_CALLBACK (feed_data), NULL);
}

/* Init libplayback: Grant access rights to
   play audio while the phone is in silent mode */
void maemo_init_libplayback(void)
{
    DBusConnection *session_bus_raw = (DBusConnection*)osso_get_dbus_connection(maemo_osso_ctx);

    playback = pb_playback_new_2(session_bus_raw,
                                               PB_CLASS_MEDIA,
                                               PB_FLAG_AUDIO,
                                               PB_STATE_STOP,
                                               playback_state_req_handler,
                                               NULL);

    pb_playback_set_stream(playback, "Playback Stream");
}

/**
 * Gets called by the policy framework if an important
 * event arrives: Incoming calls etc.
 */
void maemo_tell_rockbox_to_stop_audio(void)
{
    sim_enter_irq_handler();
    queue_broadcast(SYS_CALL_INCOMING, 0);
    sim_exit_irq_handler();

    osso_system_note_infoprint(maemo_osso_ctx, "Stopping rockbox playback", NULL);
}

void playback_state_req_handler(pb_playback_t *pb,
                                                      enum pb_state_e req_state,
                                                      pb_req_t *ext_req,
                                                      void *data)
{
    (void)pb;
    (void)ext_req;
    (void)data;

    DEBUGF("External state change request: state: %s, data: %p\n",
            pb_state_to_string(req_state), data);

    if (req_state == PB_STATE_STOP && playback_granted)
    {
        DEBUGF("Stopping playback, might be an incoming call\n");

        playback_granted = false;
        maemo_tell_rockbox_to_stop_audio();
    }
}

/**
 * Callback for our own state change request.
 */
void playback_state_req_callback(pb_playback_t *pb, enum pb_state_e granted_state, const char *reason, pb_req_t *req, void *data)
{
    (void)data;
    (void)reason;

    DEBUGF("State request callback: granted_state: %s, reason: %s\n",
                pb_state_to_string(granted_state), reason);

    /* We are allowed to play audio */
    if (granted_state == PB_STATE_PLAY)
    {
        DEBUGF("Playback granted. Start playing...\n");
        playback_granted = true;
        if (!inside_feed_data)
            gst_element_set_state (GST_ELEMENT(gst_pipeline), GST_STATE_PLAYING);
    } else
    {
        DEBUGF("Can't start playing. Throwing away play request\n");

        playback_granted = false;
        maemo_tell_rockbox_to_stop_audio();
    }

    pb_playback_req_completed(pb, req);
}

void pcm_play_dma_init(void)
{
    maemo_init_libplayback();

    GMainContext *ctx = g_main_loop_get_context(maemo_main_loop);
    pcm_loop = g_main_loop_new (ctx, true);

    gst_init (NULL, NULL);

    gst_pipeline = gst_pipeline_new ("rockbox");

    gst_appsrc = gst_element_factory_make ("appsrc", NULL);
    gst_volume = gst_element_factory_make ("volume", NULL);
    gst_pulsesink = gst_element_factory_make ("pulsesink", NULL);

    /* Connect elements */
    gst_bin_add_many (GST_BIN (gst_pipeline),
                        gst_appsrc, gst_volume, gst_pulsesink, NULL);
    gst_element_link_many (gst_appsrc, gst_volume, gst_pulsesink, NULL);

    /* Connect to gstreamer bus of the pipeline */
    gst_bus = gst_pipeline_get_bus (GST_PIPELINE (gst_pipeline));
    bus_watch_id = gst_bus_add_watch (gst_bus, (GstBusFunc) gst_bus_message, NULL);

    maemo_configure_appsrc();
}

void pcm_shutdown_gstreamer(void)
{
    /* Try to stop playing */
    gst_element_set_state (GST_ELEMENT(gst_pipeline), GST_STATE_NULL);

    /* Make sure we are really stopped. This should return almost instantly,
       so we wait up to ten seconds and just continue otherwise */
    gst_element_get_state (GST_ELEMENT(gst_pipeline), NULL, NULL, GST_SECOND * 10);

    g_source_remove (bus_watch_id);
    g_object_unref(gst_bus);
    gst_bus = NULL;

    gst_object_unref (gst_pipeline);
    gst_pipeline = NULL;

    /* Shutdown libplayback and gstreamer */
    pb_playback_destroy (playback);
    gst_deinit();

    g_main_loop_quit(pcm_loop);
    g_main_loop_unref (pcm_loop);

    pthread_mutex_destroy(&audio_lock_mutex);
}

void pcm_play_dma_postinit(void)
{
}

void pcm_set_mixer_volume(int volume)
{
    /* gstreamer volume range is from 0.00 to 1.00
     * input is -990..0 */
    gdouble gst_vol = 1.0f - (gdouble)volume / -990.0f;
    g_object_set (G_OBJECT(gst_volume), "volume", gst_vol, NULL);
}


#ifdef HAVE_RECORDING
void pcm_rec_lock(void)
{
}

void pcm_rec_unlock(void)
{
}

void pcm_rec_dma_init(void)
{
}

void pcm_rec_dma_close(void)
{
}

void pcm_rec_dma_start(void *start, size_t size)
{
    (void)start;
    (void)size;
}

void pcm_rec_dma_stop(void)
{
}

const void * pcm_rec_dma_get_peak_buffer(void)
{
    return NULL;
}

void audiohw_set_recvol(int left, int right, int type)
{
    (void)left;
    (void)right;
    (void)type;
}

#ifdef HAVE_SPDIF_IN
unsigned long spdif_measure_frequency(void)
{
    return 0;
}
#endif

#endif /* HAVE_RECORDING */