2 * player.h -- GTK+ 2 melted gui
3 * Copyright (C) 2012 Maksym Veremeyenko <verem@m1stereo.tv>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <gdk/gdkkeysyms.h>
35 #include <mvcp/mvcp.h>
36 #include <mvcp/mvcp_remote.h>
42 static char* status_to_text(mvcp_status status
)
44 switch(status
->status
)
46 case unit_offline
: return "offline"; break;
47 case unit_undefined
: return "undefined"; break;
48 case unit_not_loaded
: return "unloaded"; break;
49 case unit_stopped
: return "stopped"; break;
50 case unit_playing
: return "playing"; break;
51 case unit_paused
: return "paused"; break;
52 case unit_disconnected
: return "disconnect"; break;
53 case unit_unknown
: return "unknown"; break;
58 typedef struct player_handle_desc
64 static void player_update_status(player_t
* player
, mvcp_status_t
*status_prev
, mvcp_status_t
*status_curr
,
65 int *playlist_start_prev
)
68 char tc_cur
[32], tc_rem
[32], *state
, status
[32], *clip
;
70 if(status_curr
->status
== unit_disconnected
)
76 strcpy(status
, "OFFLINE");
80 int p
= status_curr
->start
+ (status_curr
->position
- status_curr
->in
);
82 frames2tc(p
, 25.0, tc_cur
);
83 frames2tc(status_curr
->dur
- p
, 25.0, tc_rem
);
84 clip
= status_curr
->clip
;
85 state
= status_to_text(status_curr
);
86 strcpy(status
, "ONLINE");
89 ui_update_player(player
, tc_cur
, tc_rem
, state
, status
, clip
);
91 /* update remaining time of playlist's item */
92 pthread_mutex_lock(&player
->app
->playlist
.lock
);
93 pthread_mutex_lock(&player
->app
->players
.lock
);
94 if(player
->playlist_length
)
96 /* clear remain on "previous" item */
97 if((status_curr
->clip_index
!= status_prev
->clip_index
&& 1 != player
->playlist_length
) ||
98 (*playlist_start_prev
!= player
->playlist_start
))
101 idx
= playlist_item_index
/* find_index_of_playlist_item */(player
->app
, *playlist_start_prev
, status_prev
->clip_index
);
103 ui_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
106 /* update current item */
107 idx
= playlist_item_index
/* find_index_of_playlist_item */(player
->app
, player
->playlist_start
, status_curr
->clip_index
);
113 /* for play and cue calc new value */
114 if(status_curr
->status
== unit_stopped
|| status_curr
->status
== unit_playing
|| status_curr
->status
== unit_paused
)
115 frames2tc(status_curr
->out
- status_curr
->position
, 25.0, tc_rem
);
117 /* setup that value */
118 ui_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
121 pthread_mutex_unlock(&player
->app
->players
.lock
);
122 pthread_mutex_unlock(&player
->app
->playlist
.lock
);
125 static void* player_thread_proc(void* data
)
128 int playlist_start_prev
= 0;
129 mvcp_status_t status_curr
, status_prev
;
130 player_t
* player
= (player_t
*)data
;
131 player_handle_t
* handle
= player
->handle
;
132 mvcp_notifier notifier
= mvcp_get_notifier(handle
->conn
);
134 g_warning("player_thread_proc: started\n");
136 // memset(&st_curr, 0, sizeof(OmPlrStatus));
137 // memset(&st_prev, 0, sizeof(OmPlrStatus));
139 /* endless reconnect loop */
140 for(; !player
->app
->f_exit
;)
143 pthread_mutex_lock(&player
->app
->players
.lock
);
144 if(mvcp_connect(handle
->conn
) == mvcp_ok
)
146 g_warning("player_thread_proc: failed to connect to server\n");
151 /* status request loop */
152 for(r
= 0, f
= 0; !player
->app
->f_exit
&& !r
;)
154 /* wait for any event from notifier */
155 mvcp_notifier_wait(notifier
, &status_curr
);
157 /* get status for our unit */
158 if(player
->unit
!= status_curr
.unit
)
159 mvcp_notifier_get(notifier
, &status_curr
, player
->unit
);
161 /* notify about exit from loop and reconnect */
162 if(status_curr
.status
== unit_disconnected
)
165 /* do we need to update status */
166 if(!memcmp(&status_curr
, &status_prev
, sizeof(mvcp_status_t
)) || !f
)
171 player_update_status(player
, &status_prev
, &status_curr
, &playlist_start_prev
);
172 playlist_start_prev
= player
->playlist_start
;
173 status_prev
= status_curr
;
177 pthread_mutex_lock(&player
->app
->players
.lock
);
178 // OmPlrClose((OmPlrHandle)player->handle);
179 pthread_mutex_unlock(&player
->app
->players
.lock
);
185 void player_run(instance_t
* app
, int idx
)
187 player_handle_t
* handle
= malloc(sizeof(player_handle_t
));
188 handle
->parser
= mvcp_parser_init_remote(app
->players
.host
, 5250);
189 handle
->conn
= mvcp_init(handle
->parser
);
190 app
->players
.item
[idx
].handle
= handle
;
191 app
->players
.item
[idx
].thread
= g_thread_create(
192 player_thread_proc
, &app
->players
.item
[idx
], TRUE
, NULL
);
195 void player_stop(instance_t
* app
, int idx
)
197 player_handle_t
* handle
= app
->players
.item
[idx
].handle
;
198 g_thread_join(app
->players
.item
[idx
].thread
);
199 mvcp_close(handle
->conn
);
200 mvcp_parser_close(handle
->parser
);