minimal status monitoring added
[melted_gui] / src / player.c
1 /*
2 * player.h -- GTK+ 2 melted gui
3 * Copyright (C) 2012 Maksym Veremeyenko <verem@m1stereo.tv>
4 *
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.
9 *
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.
14 *
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.
18 */
19 #ifndef _GNU_SOURCE
20 #define _GNU_SOURCE
21 #endif
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <pthread.h>
34
35 #include <mvcp/mvcp.h>
36 #include <mvcp/mvcp_remote.h>
37
38 #include "player.h"
39 #include "ui.h"
40 #include "timecode.h"
41
42 static char* status_to_text(mvcp_status status)
43 {
44 switch(status->status)
45 {
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;
54 }
55 return "unknown";
56 }
57
58 typedef struct player_handle_desc
59 {
60 mvcp_parser parser;
61 mvcp conn;
62 } player_handle_t;
63
64 static void player_update_status(player_t* player, mvcp_status_t *status_prev , mvcp_status_t *status_curr,
65 int *playlist_start_prev)
66 {
67 int idx;
68 char tc_cur[32], tc_rem[32], *state, status[32], *clip;
69
70 if(status_curr->status == unit_disconnected)
71 {
72 tc_cur[0] = 0;
73 tc_rem[0] = 0;
74 clip = "";
75 state = "";
76 strcpy(status, "OFFLINE");
77 }
78 else
79 {
80 int p = status_curr->start + (status_curr->position - status_curr->in);
81
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");
87 };
88
89 ui_update_player(player, tc_cur, tc_rem, state, status, clip);
90
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)
95 {
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))
99 {
100 tc_rem[0] = 0;
101 idx = playlist_item_index /* find_index_of_playlist_item */(player->app, *playlist_start_prev, status_prev->clip_index);
102 if(idx >= 0)
103 ui_playlist_draw_item_rem(player->app, idx, tc_rem);
104 };
105
106 /* update current item */
107 idx = playlist_item_index /* find_index_of_playlist_item */(player->app, player->playlist_start, status_curr->clip_index);
108 if(idx >= 0)
109 {
110 /* reset value */
111 tc_rem[0] = 0;
112
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);
116
117 /* setup that value */
118 ui_playlist_draw_item_rem(player->app, idx, tc_rem);
119 };
120 };
121 pthread_mutex_unlock(&player->app->players.lock);
122 pthread_mutex_unlock(&player->app->playlist.lock);
123 };
124
125 static void* player_thread_proc(void* data)
126 {
127 int r, f;
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);
133
134 g_warning("player_thread_proc: started\n");
135
136 // memset(&st_curr, 0, sizeof(OmPlrStatus));
137 // memset(&st_prev, 0, sizeof(OmPlrStatus));
138
139 /* endless reconnect loop */
140 for(; !player->app->f_exit;)
141 {
142 /* connect */
143 pthread_mutex_lock(&player->app->players.lock);
144 if(mvcp_connect(handle->conn) == mvcp_ok)
145 {
146 g_warning("player_thread_proc: failed to connect to server\n");
147 sleep(1);
148 continue;
149 };
150
151 /* status request loop */
152 for(r = 0, f = 0; !player->app->f_exit && !r;)
153 {
154 /* wait for any event from notifier */
155 mvcp_notifier_wait(notifier, &status_curr);
156
157 /* get status for our unit */
158 if(player->unit != status_curr.unit)
159 mvcp_notifier_get(notifier, &status_curr, player->unit);
160
161 /* notify about exit from loop and reconnect */
162 if(status_curr.status == unit_disconnected)
163 r = 1;
164
165 /* do we need to update status */
166 if(!memcmp(&status_curr, &status_prev, sizeof(mvcp_status_t)) || !f)
167 {
168 };
169
170 f = 1;
171 player_update_status(player, &status_prev , &status_curr, &playlist_start_prev);
172 playlist_start_prev = player->playlist_start;
173 status_prev = status_curr;
174 };
175 };
176
177 pthread_mutex_lock(&player->app->players.lock);
178 // OmPlrClose((OmPlrHandle)player->handle);
179 pthread_mutex_unlock(&player->app->players.lock);
180
181 return NULL;
182 };
183
184
185 void player_run(instance_t* app, int idx)
186 {
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);
193 };
194
195 void player_stop(instance_t* app, int idx)
196 {
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);
201 free(handle);
202 };