-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGstPlayer.cpp
More file actions
139 lines (100 loc) · 2.5 KB
/
GstPlayer.cpp
File metadata and controls
139 lines (100 loc) · 2.5 KB
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
#include "GstPlayer.h"
void GstPlayer::init(int &argc, char ***argv)
{
gst_init (&argc, argv);
_loop = g_main_loop_new (NULL, FALSE);
_playElem = gst_element_factory_make ("playbin", "playElem");
addBus();
gst_element_set_state (_playElem, GST_STATE_READY);
}
void GstPlayer::play(const std::string& uri)
{
std::string gstUri = uri;
if (!gst_uri_is_valid (gstUri.c_str()))
{
gstUri = gst_filename_to_uri (gstUri.c_str(), NULL);
}
g_object_set (G_OBJECT (_playElem), "uri", uri.c_str(), NULL);
gst_element_set_state (_playElem, GST_STATE_PLAYING);
runLoop();
}
void GstPlayer::pause()
{
gst_element_set_state (_playElem, GST_STATE_PAUSED);
}
void GstPlayer::stop()
{
gst_element_set_state (_playElem, GST_STATE_NULL);
}
GstPlayer::~GstPlayer()
{
stop();
gst_object_unref (GST_OBJECT (_playElem));
}
void GstPlayer::runLoop()
{
if(!_isLooping)
{
_mainLoop = std::thread(g_main_loop_run, _loop);
_isLooping = true;
}
}
void GstPlayer::quit()
{
g_main_loop_quit(_loop);
_mainLoop.join();
_isLooping = true;
}
void GstPlayer::addBus()
{
GstBus *bus = gst_pipeline_get_bus (GST_PIPELINE (_playElem));
gst_bus_add_watch (bus, my_bus_callback, _loop);
gst_object_unref (bus);
}
double GstPlayer::getVolume()
{
double volume = 0;
g_object_get (G_OBJECT (_playElem), "volume", &volume, NULL);
return volume;
}
void GstPlayer::setVolume(double volume)
{
volume=std::max(std::min(volume, 1.0), 0.0);
g_object_set (G_OBJECT (_playElem), "volume", volume, NULL);
}
void GstPlayer::setMute(bool f)
{
if(f)
g_object_set (G_OBJECT (_playElem), "mute", TRUE, NULL);
else
g_object_set (G_OBJECT (_playElem), "mute", FALSE, NULL);
}
bool GstPlayer::getMute()
{
gboolean mute=TRUE;
g_object_get (G_OBJECT (_playElem), "mute", &mute, NULL);
return mute == TRUE;
}
gboolean GstPlayer::my_bus_callback (GstBus *bus, GstMessage *msg, gpointer data)
{
GMainLoop *loop = (GMainLoop *) data;
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_EOS:
g_print ("End of stream\n");
g_main_loop_quit (loop);
break;
case GST_MESSAGE_ERROR: {
gchar *debug;
GError *error;
gst_message_parse_error (msg, &error, &debug);
g_free (debug);
g_printerr ("Error: %s\n", error->message);
g_error_free (error);
g_main_loop_quit (loop);
break;
}
default:
break;
}
return TRUE;
}