-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.c
117 lines (90 loc) · 2.63 KB
/
misc.c
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
/*
* Copyright (c) 2011 Paulo Alcantara <[email protected]>
*
* 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 program 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 this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111EXIT_FAILURE307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <gst/gst.h>
#include <glib.h>
#include "misc.h"
extern int verbose;
static GMainLoop *loop;
static gboolean bus_call(GstBus *bus, GstMessage *msg, void *data)
{
switch (GST_MESSAGE_TYPE(msg)) {
case GST_MESSAGE_EOS:
g_message("End of stream");
g_main_loop_quit(loop);
break;
case GST_MESSAGE_ERROR:
{
GError *err;
gst_message_parse_error(msg, &err, NULL);
g_error("%s", err->message);
g_error_free(err);
g_main_loop_quit(loop);
break;
}
}
return true;
}
/* Play a given sound file */
int misc_play_sound_file(const char *path, const char *file, const char *suffix)
{
char *_file = NULL;
size_t len;
GstBus *bus;
GstElement *pipeline;
char *uri = NULL;
/* Set up the pipeline */
len = strlen(path) + strlen(file);
len = path[strlen(path) - 1] == '/' ? len : len + 1;
len = suffix ? len + strlen(suffix) : len;
_file = (char *)malloc(len + 1);
if (!file) {
print_err("alloc char *");
return -ENOMEM;
}
strncpy(_file, path, len);
if (path[strlen(path) - 1] != '/')
strncat(_file, "/", len);
strncat(_file, file, len);
if (suffix)
strncat(_file, suffix, len);
loop = g_main_loop_new(NULL, FALSE);
pipeline = gst_element_factory_make("playbin", "player");
len = strlen(_file) + 7 + 1;
uri = (char *)malloc(len);
if (!uri) {
print_err("alloc char *");
return -ENOMEM;
}
strncpy(uri, "file://", len);
strncat(uri, _file, len);
print_info("%s", uri);
g_object_set(G_OBJECT(pipeline), "uri", uri, NULL);
bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
gst_bus_add_watch(bus, bus_call, NULL);
gst_object_unref(bus);
gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_PLAYING);
g_main_loop_run(loop);
gst_element_set_state(GST_ELEMENT(pipeline), GST_STATE_NULL);
gst_object_unref(GST_OBJECT(pipeline));
return 0;
}