2 * omnplay.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 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>
40 #include "omplrclnt.h"
42 static gboolean
on_main_window_delete_event( GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
)
48 omnplay_instance_t
* omnplay_create(int argc
, char** argv
)
51 omnplay_instance_t
* app
;
53 /* prepare application instance */
54 app
= (omnplay_instance_t
*)malloc(sizeof(omnplay_instance_t
));
55 memset(app
, 0, sizeof(omnplay_instance_t
));
57 /* load parameters from command line */
58 if(!omnplay_opt(argc
, argv
, app
) && app
->players
.count
)
59 app
->window
= ui_omnplay(app
);
66 void omnplay_destroy(omnplay_instance_t
* app
)
71 static int find_index_of_playlist_item(omnplay_instance_t
* app
, int start
, int idx
)
75 if(app
->playlist
.item
[start
].omn_idx
== idx
)
78 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_END
)
87 static void omnplay_update_status(omnplay_player_t
* player
, OmPlrStatus
*prev
, OmPlrStatus
*curr
)
90 char tc_cur
[32], tc_rem
[32], state
[32], status
[32];
95 frames2tc(curr
->pos
- curr
->minPos
, 25.0, tc_cur
);
96 frames2tc(curr
->maxPos
- curr
->pos
, 25.0, tc_rem
);
97 strcpy(status
, "ONLINE");
98 clip
= curr
->currClipName
;
102 case omPlrStateStopped
: strcpy(state
, "STOPPED"); break;
103 case omPlrStateCuePlay
: strcpy(state
, "CUE_PLAY"); break;
104 case omPlrStatePlay
: strcpy(state
, "PLAY"); break;
105 case omPlrStateCueRecord
: strcpy(state
, "CUE_RECORD"); break;
106 case omPlrStateRecord
: strcpy(state
, "RECORD"); break;
115 strcpy(status
, "OFFLINE");
118 /* update status in status page */
120 gtk_label_set_text(GTK_LABEL (player
->label_tc_cur
), tc_cur
);
121 gtk_label_set_text(GTK_LABEL (player
->label_tc_rem
), tc_rem
);
122 gtk_label_set_text(GTK_LABEL (player
->label_state
), state
);
123 gtk_label_set_text(GTK_LABEL (player
->label_status
), status
);
124 gtk_label_set_text(GTK_LABEL (player
->label_clip
), clip
);
128 /* update remaining time */
130 pthread_mutex_lock(&player
->app
->playlist
.lock
);
131 pthread_mutex_lock(&player
->app
->players
.lock
);
132 if(curr
->state
== omPlrStatePlay
|| curr
->state
== omPlrStateCuePlay
)
134 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, curr
->currClipNum
);
137 frames2tc(curr
->currClipStartPos
+ curr
->currClipLen
- curr
->pos
, 25.0, tc_rem
);
138 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
140 if(curr
->currClipNum
!= prev
->currClipNum
&& 1 != prev
->numClips
)
143 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, prev
->currClipNum
);
145 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
151 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, curr
->currClipNum
);
153 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
154 idx
= find_index_of_playlist_item(player
->app
, player
->playlist_start
, prev
->currClipNum
);
156 omnplay_playlist_draw_item_rem(player
->app
, idx
, tc_rem
);
158 pthread_mutex_unlock(&player
->app
->players
.lock
);
159 pthread_mutex_unlock(&player
->app
->playlist
.lock
);
164 memcpy(prev
, curr
, sizeof(OmPlrStatus
));
167 static void* omnplay_thread_proc(void* data
)
170 OmPlrStatus st_curr
, st_prev
;
171 omnplay_player_t
* player
= (omnplay_player_t
*)data
;
174 pthread_mutex_lock(&player
->app
->players
.lock
);
175 r
= OmPlrOpen(player
->host
, player
->name
, (OmPlrHandle
*)&player
->handle
);
176 pthread_mutex_unlock(&player
->app
->players
.lock
);
179 fprintf(stderr
, "ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
180 player
->host
, player
->name
, r
);
185 /* setup to do not reconnect */
186 pthread_mutex_lock(&player
->app
->players
.lock
);
187 OmPlrSetRetryOpen((OmPlrHandle
)player
->handle
, 0);
188 pthread_mutex_unlock(&player
->app
->players
.lock
);
190 /* setup directory */
191 if(player
->app
->players
.path
[0])
193 pthread_mutex_lock(&player
->app
->players
.lock
);
194 r
= OmPlrClipSetDirectory((OmPlrHandle
)player
->handle
, player
->app
->players
.path
);
195 pthread_mutex_unlock(&player
->app
->players
.lock
);
199 fprintf(stderr
, "ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
200 player
->app
->players
.path
, r
);
202 pthread_mutex_lock(&player
->app
->players
.lock
);
203 OmPlrClose((OmPlrHandle
)player
->handle
);
204 pthread_mutex_unlock(&player
->app
->players
.lock
);
211 for(r
= 0 ; !player
->app
->f_exit
&& !r
;)
217 pthread_mutex_lock(&player
->app
->players
.lock
);
218 st_curr
.size
= sizeof(OmPlrStatus
);
219 r
= OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &st_curr
);
220 pthread_mutex_unlock(&player
->app
->players
.lock
);
223 fprintf(stderr
, "ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", r
);
225 if(memcmp(&st_curr
, &st_prev
, sizeof(OmPlrStatus
)))
226 omnplay_update_status(player
, &st_prev
, &st_curr
);
229 pthread_mutex_lock(&player
->app
->players
.lock
);
230 OmPlrClose((OmPlrHandle
)player
->handle
);
231 pthread_mutex_unlock(&player
->app
->players
.lock
);
236 void get_selected_items_playlist_proc(GtkTreeModel
*model
, GtkTreePath
*path
, GtkTreeIter
*iter
, gpointer data
)
238 int idx
, *list
= (int*)data
;
239 gtk_tree_model_get(model
, iter
, 7, &idx
, -1);
240 list
[list
[0] + 1] = idx
;
241 list
[0] = list
[0] + 1;
244 static int* get_selected_items_playlist(omnplay_instance_t
* app
)
247 GtkTreeSelection
*selection
;
249 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
));
252 list
= (int*)malloc(sizeof(int) * (MAX_PLAYLIST_ITEMS
+ 1));
253 memset(list
, 0, sizeof(int) * (MAX_PLAYLIST_ITEMS
+ 1));
255 gtk_tree_selection_selected_foreach(
257 get_selected_items_playlist_proc
,
270 static int get_first_selected_item_playlist(omnplay_instance_t
* app
)
273 int* list
= get_selected_items_playlist(app
);
280 static int get_playlist_block(omnplay_instance_t
* app
, int idx
, int* start_ptr
, int* stop_ptr
)
284 for(start
= idx
; start
>= 0; start
--)
285 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
)
288 for(stop
= idx
; stop
< app
->playlist
.count
; stop
++)
289 if(app
->playlist
.item
[stop
].type
& OMNPLAY_PLAYLIST_BLOCK_END
)
292 fprintf(stderr
, "get_playlist_block: range %d -> %d\n", start
, stop
);
294 /* check block range */
295 if(start
>= 0 && stop
< app
->playlist
.count
)
299 return (stop
- start
+ 1);
305 static omnplay_player_t
*get_player_at_pos(omnplay_instance_t
* app
, int pos
)
307 /* check player range */
308 if(app
->playlist
.item
[pos
].player
> -1 && app
->playlist
.item
[pos
].player
< app
->players
.count
)
309 return &app
->players
.item
[app
->playlist
.item
[pos
].player
];
314 static void omnplay_ctl(omnplay_instance_t
* app
, control_buttons_t button
)
317 int idx
, start
, stop
;
318 omnplay_player_t
*player
;
320 pthread_mutex_lock(&app
->playlist
.lock
);
322 idx
= get_first_selected_item_playlist(app
);
326 pthread_mutex_unlock(&app
->playlist
.lock
);
330 fprintf(stderr
, "cue: selected item is %d\n", idx
);
332 if(get_playlist_block(app
, idx
, &start
, &stop
) < 0)
334 pthread_mutex_unlock(&app
->playlist
.lock
);
338 fprintf(stderr
, "cue: range %d -> %d\n", start
, stop
);
340 player
= get_player_at_pos(app
, start
);
344 pthread_mutex_unlock(&app
->playlist
.lock
);
348 pthread_mutex_lock(&app
->players
.lock
);
350 if(BUTTON_PLAYER_STOP
== button
|| BUTTON_PLAYER_CUE
== button
)
353 OmPlrStop((OmPlrHandle
)player
->handle
);
355 /* detach previous clips */
356 // player->playlist_start = -1;
357 // player->playlist_count = -1;
358 OmPlrDetachAllClips((OmPlrHandle
)player
->handle
);
361 if(BUTTON_PLAYER_CUE
== button
)
365 /* Attach clips to timeline */
366 for(i
= start
, c
= 0, o
= 0; i
<= stop
; i
++)
371 clip
.maxMsTracks
= 0;
372 clip
.size
= sizeof(clip
);
373 r
= OmPlrClipGetInfo((OmPlrHandle
)player
->handle
, app
->playlist
.item
[i
].id
, &clip
);
379 fprintf(stderr
, "OmPlrClipGetInfo(%s): firstFrame=%d, lastFrame=%d\n",
380 app
->playlist
.item
[i
].id
, clip
.firstFrame
, clip
.lastFrame
);
382 /* should we fix playlist clip timings */
384 app
->playlist
.item
[i
].in
>= clip
.firstFrame
&&
385 app
->playlist
.item
[i
].in
+ app
->playlist
.item
[i
].dur
<= clip
.lastFrame
) ||
386 !app
->playlist
.item
[i
].dur
)
388 fprintf(stderr
, "cue: item [%s] will be updated [%d;%d]->[%d;%d]\n",
389 app
->playlist
.item
[i
].id
,
390 app
->playlist
.item
[i
].in
, app
->playlist
.item
[i
].dur
,
391 clip
.firstFrame
, clip
.lastFrame
- clip
.firstFrame
);
393 app
->playlist
.item
[i
].in
= clip
.firstFrame
;
394 app
->playlist
.item
[i
].dur
= clip
.lastFrame
- clip
.firstFrame
;
395 omnplay_playlist_draw_item(app
, i
);
398 r
= OmPlrAttach((OmPlrHandle
)player
->handle
,
399 app
->playlist
.item
[i
].id
,
400 app
->playlist
.item
[i
].in
,
401 app
->playlist
.item
[i
].in
+ app
->playlist
.item
[i
].dur
,
402 0, omPlrShiftModeAfter
, &l
);
407 fprintf(stderr
, "cue: failed with %d, %s\n", r
, OmPlrGetErrorString((OmPlrError
)r
));
408 app
->playlist
.item
[i
].omn_idx
= -1;
409 app
->playlist
.item
[i
].omn_offset
= -1;
413 app
->playlist
.item
[i
].omn_idx
= c
;
414 app
->playlist
.item
[i
].omn_offset
= o
;
416 /* save selected item offset */
420 o
+= app
->playlist
.item
[i
].dur
;
428 /* Set timeline min/max */
429 OmPlrSetMinPosMin((OmPlrHandle
)player
->handle
);
430 OmPlrSetMaxPosMax((OmPlrHandle
)player
->handle
);
432 /* Set timeline position */
434 hs
.size
= sizeof(OmPlrStatus
);
435 OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &hs
);
436 OmPlrSetPos((OmPlrHandle
)player
->handle
, hs
.minPos
+ p
);
439 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_LOOP
)
440 OmPlrLoop((OmPlrHandle
)player
->handle
, hs
.minPos
, hs
.maxPos
);
442 player
->playlist_start
= start
;
445 OmPlrCuePlay((OmPlrHandle
)player
->handle
, 0.0);
446 OmPlrPlay((OmPlrHandle
)player
->handle
, 0.0);
450 if(BUTTON_PLAYER_PLAY
== button
)
453 OmPlrPlay((OmPlrHandle
)player
->handle
, 1.0);
456 if(BUTTON_PLAYER_PAUSE
== button
)
458 OmPlrPlay((OmPlrHandle
)player
->handle
, 0.0);
460 pthread_mutex_unlock(&app
->players
.lock
);
462 pthread_mutex_unlock(&app
->playlist
.lock
);
465 static gboolean
omnplay_button_click(omnplay_instance_t
* app
, control_buttons_t button
)
469 case BUTTON_PLAYLIST_ITEM_ADD
:
470 case BUTTON_PLAYLIST_ITEM_DEL
:
471 case BUTTON_PLAYLIST_ITEM_EDIT
:
473 case BUTTON_PLAYLIST_LOAD
:
474 omnplay_playlist_load(app
);
476 case BUTTON_PLAYLIST_SAVE
:
477 omnplay_playlist_save(app
);
479 case BUTTON_PLAYLIST_BLOCK_SINGLE
:
480 case BUTTON_PLAYLIST_BLOCK_LOOP
:
482 case BUTTON_PLAYLIST_ITEM_UP
:
483 case BUTTON_PLAYLIST_ITEM_DOWN
:
485 case BUTTON_PLAYER_CUE
:
486 case BUTTON_PLAYER_PLAY
:
487 case BUTTON_PLAYER_PAUSE
:
488 case BUTTON_PLAYER_STOP
:
489 omnplay_ctl(app
, button
);
491 case BUTTON_LIBRARY_ADD
:
492 case BUTTON_LIBRARY_REFRESH
:
499 static gboolean
on_button_click(GtkWidget
*button
, gpointer user_data
)
502 omnplay_instance_t
* app
= (omnplay_instance_t
*)user_data
;
504 for(i
= 1; i
< BUTTON_LAST
; i
++)
505 if(app
->buttons
[i
] == button
)
506 return omnplay_button_click(app
, (control_buttons_t
)i
);
511 void omnplay_init(omnplay_instance_t
* app
)
514 pthread_mutexattr_t attr
;
516 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
518 gtk_signal_connect( GTK_OBJECT( app
->window
), "destroy",
519 GTK_SIGNAL_FUNC(on_main_window_delete_event
), app
);
522 pthread_mutex_init(&app
->players
.lock
, &attr
);
524 /* create a omneon status thread */
525 for(i
= 0; i
< app
->players
.count
; i
++)
526 pthread_create(&app
->players
.item
[i
].thread
, NULL
,
527 omnplay_thread_proc
, &app
->players
.item
[i
]);
530 pthread_mutex_init(&app
->playlist
.lock
, &attr
);
532 /* attach buttons click */
533 for(i
= 1; i
< BUTTON_LAST
; i
++)
534 gtk_signal_connect(GTK_OBJECT(app
->buttons
[i
]), "clicked",
535 GTK_SIGNAL_FUNC( on_button_click
), app
);
539 void omnplay_release(omnplay_instance_t
* app
)
546 for(i
= 0; i
< app
->players
.count
; i
++)
547 /* create a omneon status thread */
548 pthread_join(app
->players
.item
[i
].thread
, &r
);
551 pthread_mutex_destroy(&app
->players
.lock
);
554 pthread_mutex_destroy(&app
->playlist
.lock
);