d7468b651fe9eccaa60d70b381ac5d7d3d70597b
[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 #include "playlist.h"
42
43 static char* status_to_text(mvcp_status status)
44 {
45 switch(status->status)
46 {
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;
55 }
56 return "unknown";
57 }
58
59 typedef struct player_handle_desc
60 {
61 mvcp_parser parser;
62 mvcp command;
63 mvcp status;
64 } player_handle_t;
65
66 static void player_update_status(player_t* player, mvcp_status_t *status_prev , mvcp_status_t *status_curr,
67 int *playlist_start_prev)
68 {
69 int idx;
70 char tc_cur[32], tc_rem[32], *state, status[32], *clip;
71
72 if(status_curr->status == unit_disconnected)
73 {
74 tc_cur[0] = 0;
75 tc_rem[0] = 0;
76 clip = "";
77 state = "";
78 strcpy(status, "OFFLINE");
79 }
80 else
81 {
82 int p = status_curr->start + (status_curr->position - status_curr->in);
83
84 frames2tc(p, 25.0, tc_cur);
85 frames2tc(status_curr->dur - p, 25.0, tc_rem);
86 clip = strrchr(status_curr->clip, '/');
87 if(clip)
88 clip++;
89 else
90 clip = status_curr->clip;
91 state = status_to_text(status_curr);
92 strcpy(status, "ONLINE");
93 };
94
95 ui_update_player(player, tc_cur, tc_rem, state, status, clip);
96
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)
101 {
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))
105 {
106 tc_rem[0] = 0;
107 idx = playlist_item_index /* find_index_of_playlist_item */(player->app, *playlist_start_prev, status_prev->clip_index);
108 if(idx >= 0)
109 ui_playlist_draw_item_rem(player->app, idx, tc_rem);
110 };
111
112 /* update current item */
113 idx = playlist_item_index /* find_index_of_playlist_item */(player->app, player->playlist_start, status_curr->clip_index);
114 if(idx >= 0)
115 {
116 /* reset value */
117 tc_rem[0] = 0;
118
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);
122
123 /* setup that value */
124 ui_playlist_draw_item_rem(player->app, idx, tc_rem);
125 };
126 };
127 pthread_mutex_unlock(&player->app->players.lock);
128 pthread_mutex_unlock(&player->app->playlist.lock);
129 };
130
131 static void* player_thread_proc(void* data)
132 {
133 int r, f;
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);
139
140 g_warning("player_thread_proc: started (player=%d, unit=%d)\n", player->idx, player->unit);
141
142 // memset(&st_curr, 0, sizeof(OmPlrStatus));
143 // memset(&st_prev, 0, sizeof(OmPlrStatus));
144
145 /* endless reconnect loop */
146 for(; !player->app->f_exit;)
147 {
148 /* connect */
149 if(mvcp_connect(handle->command) == mvcp_ok)
150 {
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);
153 sleep(1);
154 continue;
155 };
156
157 g_warning("player_thread_proc: connected to server %s (player=%d, unit=%d)\n",
158 player->app->players.host, player->idx, player->unit);
159
160 /* status request loop */
161 for(r = 0, f = 0; !player->app->f_exit && !r;)
162 {
163 int c;
164
165 /* wait for any event from notifier */
166 mvcp_notifier_wait(notifier, &status_curr);
167
168 /* get status for our unit */
169 if(player->unit != status_curr.unit)
170 mvcp_notifier_get(notifier, &status_curr, player->unit);
171
172 /* notify about exit from loop and reconnect */
173 if(status_curr.status == unit_disconnected)
174 {
175 r = 1;
176 continue;
177 };
178
179 /* do we need to update status */
180 c = memcmp(&status_curr, &status_prev, sizeof(mvcp_status_t));
181 if(!c && f)
182 continue;
183
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;
188 f = 1;
189 };
190 };
191
192 pthread_mutex_lock(&player->app->players.lock);
193 // OmPlrClose((OmPlrHandle)player->handle);
194 pthread_mutex_unlock(&player->app->players.lock);
195
196 return NULL;
197 };
198
199
200 void player_run(instance_t* app, int idx)
201 {
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);
209 };
210
211 void player_stop(instance_t* app, int idx)
212 {
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);
218 free(handle);
219 };