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>
43 static char* status_to_text(mvcp_status status
)
45 switch(status
->status
)
47 case unit_offline
: return "offline"; break;
48 case unit_undefined
: return "undefined"; break;
49 case unit_not_loaded
: return "unloaded"; break;
50 case unit_stopped
: return "stopped"; break;
51 case unit_playing
: return "playing"; break;
52 case unit_paused
: return "paused"; break;
53 case unit_disconnected
: return "disconnect"; break;
54 case unit_unknown
: return "unknown"; break;
59 typedef struct player_handle_desc
66 static void player_update_status(player_t
* player
, mvcp_status_t
*status_prev
, mvcp_status_t
*status_curr
,
67 int *playlist_start_prev
)
70 char tc_cur
[32], tc_rem
[32], *state
, status
[32], *clip
;
72 if(status_curr
->status
== unit_disconnected
)
78 strcpy(status
, "OFFLINE");
82 int p
= status_curr
->start
+ (status_curr
->position
- status_curr
->in
);
84 frames2tc(p
, 25.0, tc_cur
);
85 frames2tc(status_curr
->dur
- p
, 25.0, tc_rem
);
86 clip
= strrchr(status_curr
->clip
, '/');
90 clip
= status_curr
->clip
;
91 state
= status_to_text(status_curr
);
92 strcpy(status
, "ONLINE");
95 ui_update_player(player
, tc_cur
, tc_rem
, state
, status
, clip
);
97 /* update remaining time of playlist's item */
98 pthread_mutex_lock(&player
->app
->playlist
.lock
);
99 pthread_mutex_lock(&player
->app
->players
.lock
);
100 if(player
->playlist_length
)
102 /* clear remain on "previous" item */
103 if((status_curr
->clip_index
!= status_prev
->clip_index
&& 1 != player
->playlist_length
) ||
104 (*playlist_start_prev
!= player
->playlist_start
))
107 idx
= playlist_item_index
/* find_index_of_playlist_item */(player
->app
, *playlist_start_prev
, status_prev
->clip_index
);
109 ui_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
112 /* update current item */
113 idx
= playlist_item_index
/* find_index_of_playlist_item */(player
->app
, player
->playlist_start
, status_curr
->clip_index
);
119 /* for play and cue calc new value */
120 if(status_curr
->status
== unit_stopped
|| status_curr
->status
== unit_playing
|| status_curr
->status
== unit_paused
)
121 frames2tc(status_curr
->out
- status_curr
->position
, 25.0, tc_rem
);
123 /* setup that value */
124 ui_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
127 pthread_mutex_unlock(&player
->app
->players
.lock
);
128 pthread_mutex_unlock(&player
->app
->playlist
.lock
);
131 static void* player_thread_proc(void* data
)
134 int playlist_start_prev
= 0;
135 mvcp_status_t status_curr
, status_prev
;
136 player_t
* player
= (player_t
*)data
;
137 player_handle_t
* handle
= player
->handle
;
138 mvcp_notifier notifier
= mvcp_get_notifier(handle
->status
);
140 g_warning("player_thread_proc: started (player=%d, unit=%d)\n", player
->idx
, player
->unit
);
142 // memset(&st_curr, 0, sizeof(OmPlrStatus));
143 // memset(&st_prev, 0, sizeof(OmPlrStatus));
145 /* endless reconnect loop */
146 for(; !player
->app
->f_exit
;)
149 if(mvcp_connect(handle
->command
) == mvcp_ok
)
151 g_warning("player_thread_proc: failed to connect to server %s (player=%d, unit=%d)\n",
152 player
->app
->players
.host
, player
->idx
, player
->unit
);
157 g_warning("player_thread_proc: connected to server %s (player=%d, unit=%d)\n",
158 player
->app
->players
.host
, player
->idx
, player
->unit
);
160 /* status request loop */
161 for(r
= 0, f
= 0; !player
->app
->f_exit
&& !r
;)
165 /* wait for any event from notifier */
166 mvcp_notifier_wait(notifier
, &status_curr
);
168 /* get status for our unit */
169 if(player
->unit
!= status_curr
.unit
)
170 mvcp_notifier_get(notifier
, &status_curr
, player
->unit
);
172 /* notify about exit from loop and reconnect */
173 if(status_curr
.status
== unit_disconnected
)
179 /* do we need to update status */
180 c
= memcmp(&status_curr
, &status_prev
, sizeof(mvcp_status_t
));
184 /* call update func */
185 player_update_status(player
, &status_prev
, &status_curr
, &playlist_start_prev
);
186 playlist_start_prev
= player
->playlist_start
;
187 status_prev
= status_curr
;
192 pthread_mutex_lock(&player
->app
->players
.lock
);
193 // OmPlrClose((OmPlrHandle)player->handle);
194 pthread_mutex_unlock(&player
->app
->players
.lock
);
200 void player_run(instance_t
* app
, int idx
)
202 player_handle_t
* handle
= malloc(sizeof(player_handle_t
));
203 handle
->parser
= mvcp_parser_init_remote(app
->players
.host
, app
->players
.port
);
204 handle
->status
= mvcp_init(handle
->parser
);
205 handle
->command
= mvcp_init(handle
->parser
);
206 app
->players
.item
[idx
].handle
= handle
;
207 app
->players
.item
[idx
].thread
= g_thread_create(
208 player_thread_proc
, &app
->players
.item
[idx
], TRUE
, NULL
);
211 void player_stop(instance_t
* app
, int idx
)
213 player_handle_t
* handle
= app
->players
.item
[idx
].handle
;
214 g_thread_join(app
->players
.item
[idx
].thread
);
215 mvcp_close(handle
->status
);
216 mvcp_close(handle
->command
);
217 mvcp_parser_close(handle
->parser
);