code cleanup
[obnox/wireshark/wip.git] / gtk / rtp_player.c
1  /* rtp_player.c
2  *
3  * $Id$
4  *
5  *  Copyright 2006, Alejandro Vaquero <alejandrovaquero@yahoo.com>
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1999 Gerald Combs
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 /*
27  * Here is a summary on how this works:
28  *  - The VoipCalls will call add_rtp_packet() every time there is an RTP
29  *    packet
30  *  - add_rtp_packet() will add the RTP packet in a RTP stream struct, and
31  *    create the RTP stream if it is the  first RTP in the stream.
32  *  - Each new RTP stream will be added to a list of RTP stream, called
33  *    rtp_streams_list
34  *  - When the user clicks "Player" in the VoipCall dialogue,
35  *    rtp_player_init() is called.
36  *  - rtp_player_init() create the main dialog, and it calls:
37  *    + mark_rtp_stream_to_play() to mark the RTP streams that needs to be
38  *      displayed. These are the RTP stream that match the selected calls in
39  *      the VoipCall dlg.
40  *    + decode_rtp_stream() this will decode the RTP packets in each RTP
41  *      stream, and will also create  the RTP channles. An RTP channel is a
42  *      group of RTP stream that have in common the source and destination
43  *      IP and UPD ports. The RTP channels is what the user will listen in
44  *      one of the two Audio channles. 
45  *      The RTP channels are stored in the hash table rtp_channels_hash
46  *    + add_channel_to_window() will create and add the Audio graphic
47  *      representation in the main window
48  *  - When the user click the check box to listen one of the Audio channels,
49  *    the structure rtp_channels is filled  to play one or two RTP channels
50  *    (a max of two channels can be listened at a given moment)
51  */
52
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57
58 #ifdef HAVE_LIBPORTAUDIO
59
60 #include <epan/stats_tree.h>
61 #include <epan/addr_resolv.h>
62 #include <string.h>
63 #include <glib.h>
64 #include <gtk/gtk.h>
65 #include "globals.h"
66 #include "portaudio.h"
67 #include "simple_dialog.h"
68 #include "gui_utils.h"
69 #include "dlg_utils.h"
70 #include "compat_macros.h"
71
72 #include "graph_analysis.h"
73 #include "voip_calls_dlg.h"
74 #include "voip_calls.h"
75 #include "gtkglobals.h"
76
77
78 #include <epan/dissectors/packet-rtp.h>
79
80 #include "rtp_player.h"
81 #include "codecs/G711a/G711adecode.h"
82 #include "codecs/G711u/G711udecode.h"
83 #include <math.h>
84
85 #ifndef min
86 #define min(a,b) (((a)<(b))?(a):(b))
87 #endif
88 #ifndef max
89 #define max(a,b) (((a)>(b))?(a):(b))
90 #endif
91
92 /*define this symbol to compile with G729 and G723 codecs*/
93 /*#define HAVE_G729_G723 1*/
94
95 #ifdef HAVE_G729_G723
96 #include "codecs/G729/G729decode.h"
97 #include "codecs/G723/G723decode.h"
98 #endif /* HAVE_G729_G723 */
99
100 static gboolean initialized = FALSE;
101
102 voip_calls_tapinfo_t *voip_calls = NULL;
103
104 /* Hash table with all the RTP streams */
105 static GHashTable*  rtp_streams_hash = NULL;
106
107 /* List with all the RTP streams (this is used to decode them as it is sorted)*/
108 static GList*  rtp_streams_list = NULL;
109
110 /* the window */
111 static GtkWidget *rtp_player_dlg_w;
112 static GtkWidget *channels_vb;
113 static GtkWidget *main_scrolled_window = NULL;
114 static GtkWidget *jitter_spinner;
115 static GtkWidget *bt_decode;
116 static GtkWidget *bt_play;
117 static GtkWidget *bt_pause;
118 static GtkWidget *bt_stop;
119 static GtkWidget *progress_bar;
120 static GtkWidget *info_bar;
121 static GtkWidget *stat_hbox;
122
123 static guint32 total_packets;
124 static guint32 total_frames;
125 static guint32 progbar_count;
126
127 static int new_jitter_buff;
128
129 /* a hash table with the RTP streams to play per audio channel */
130 static GHashTable *rtp_channels_hash = NULL;
131
132 /* Port Audio staff */
133 #define SAMPLE_RATE  (8000)
134 #define NUM_CHANNELS    (2)
135
136 #define PA_SAMPLE_TYPE  paInt16
137 typedef gint16 SAMPLE;
138 #define SAMPLE_SILENCE  (0)
139 #define FRAMES_PER_BUFFER  (512)
140
141 typedef struct _sample_t {
142         SAMPLE val;
143         guint8 status;
144 } sample_t;
145
146 #define S_NORMAL 0
147 #define S_DROP_BY_JITT 1
148 #define S_WRONG_SEQ 2
149
150 /* Display channels constants */
151 #define MULT 80
152 #define CHANNEL_WIDTH 500
153 #define CHANNEL_HEIGHT 100
154 #define MAX_TIME_LABEL 10
155 #define HEIGHT_TIME_LABEL 18
156 #define MAX_NUM_COL_CONV 10
157
158 #if PORTAUDIO_API_1
159 PortAudioStream *pa_stream;
160 #else /* PORTAUDIO_API_1 */
161 PaStream *pa_stream;
162 #endif /* PORTAUDIO_API_1 */
163
164 /* TODO: The RTP Player it is only supported for GTK >=2 */
165 #if GTK_MAJOR_VERSION >= 2
166
167 /* defines a RTP stream */
168 typedef struct _rtp_stream_info {
169         address src_addr;
170         guint16 src_port;
171         address dest_addr;
172         guint16 dest_port;
173         guint32 ssrc;
174         guint32 first_frame_number; /* first RTP frame for the stream */
175         double start_time;                      /* RTP stream start time in ms */
176         gboolean play;
177         guint16 call_num;
178         GList*  rtp_packets_list; /* List of RTP packets in the stream */
179         guint32 num_packets;
180 } rtp_stream_info_t;
181
182
183 /* defines the RTP streams to be played in an audio channel */
184 typedef struct _rtp_channel_info {
185         double start_time;                      /* RTP stream start time in ms */
186         double end_time;                        /* RTP stream end time in ms */
187         GArray *samples;                        /* the array with decoded audio */
188         guint16 call_num;
189         gboolean selected;
190         guint32 frame_index;
191         guint32 drop_by_jitter_buff;
192         guint32 out_of_seq;
193         guint32 max_frame_index;
194         GtkWidget *check_bt;
195         GtkWidget *separator;
196         GtkWidget *scroll_window;
197         GtkWidget *draw_area;
198         GdkPixmap *pixmap;
199         GtkAdjustment *h_scrollbar_adjustment;
200         GdkPixbuf* cursor_pixbuf;
201 #if PORTAUDIO_API_1
202         PaTimestamp cursor_prev;
203 #else /* PORTAUDIO_API_1 */
204         PaTime cursor_prev;
205 #endif /* PORTAUDIO_API_1 */
206         GdkGC *bg_gc[MAX_NUM_COL_CONV+1];
207         gboolean cursor_catch;
208         rtp_stream_info_t *first_stream;        /* This is the first RTP stream in the channel */
209         guint32 num_packets;
210 } rtp_channel_info_t;
211
212 /* defines a RTP packet */
213 typedef struct _rtp_packet {
214         struct _rtp_info *info; /* the RTP dissected info */
215         double arrive_offset;   /* arrive offset time since the begining of the stream in ms */
216         guint8* payload_data;
217 } rtp_packet_t;
218
219 /* defines the two RTP channels to be played */
220 typedef struct _rtp_play_channles {
221         rtp_channel_info_t* rci[2]; /* Channels to be played */
222         guint32 start_index[2];
223         guint32 end_index[2];
224         int channel;
225         guint32 max_frame_index;
226         guint32 frame_index;
227         gboolean pause;
228         gboolean stop;
229         gint32 pause_duration;
230 #if PORTAUDIO_API_1
231         PaTimestamp out_diff_time;
232 #else /* PORTAUDIO_API_1 */
233         PaTime out_diff_time;
234         PaTime pa_start_time;
235 #endif /* PORTAUDIO_API_1 */
236 } rtp_play_channles_t;
237
238 /* The two RTP channles to play */
239 static rtp_play_channles_t *rtp_channels = NULL;
240
241
242 /****************************************************************************/
243 static void 
244 rtp_key_destroy(gpointer key)
245 {
246         g_free(key);
247         key = NULL;
248 }
249
250 /****************************************************************************/
251 static void 
252 rtp_channel_value_destroy(gpointer rci_arg)
253 {
254         rtp_channel_info_t *rci = rci_arg;
255
256         g_array_free(rci->samples, TRUE);
257         g_free(rci);
258         rci = NULL;
259 }
260
261 /****************************************************************************/
262 static void 
263 rtp_stream_value_destroy(gpointer rsi_arg)
264 {
265         rtp_stream_info_t *rsi = rsi_arg;
266         GList*  rtp_packets_list;
267         rtp_packet_t *rp;
268
269         rtp_packets_list = g_list_first(rsi->rtp_packets_list);
270         while (rtp_packets_list)
271         {
272                 rp = rtp_packets_list->data;
273
274                 g_free(rp->info);
275                 g_free(rp->payload_data);
276                 g_free(rp);
277                 rp = NULL;
278         
279                 rtp_packets_list = g_list_next(rtp_packets_list);
280         }
281         g_free(rsi);
282         rsi = NULL;
283 }
284
285 /****************************************************************************/
286 static void
287 set_sensitive_check_bt(gchar *key _U_ , rtp_channel_info_t *rci, guint *stop _U_ ) 
288 {
289         gtk_widget_set_sensitive(rci->check_bt, !(*stop));      
290 }
291
292 /****************************************************************************/
293 static void 
294 bt_state(gboolean decode, gboolean play, gboolean pause, gboolean stop)
295 {
296         gboolean new_jitter_value = FALSE;
297         gboolean false_val = FALSE;
298
299         gtk_widget_set_sensitive(bt_decode, decode);
300         gtk_widget_set_sensitive(jitter_spinner, decode);
301                 
302         if (new_jitter_buff != (int) gtk_spin_button_get_value((GtkSpinButton * )jitter_spinner)) {
303                 new_jitter_value = TRUE;
304         }
305
306         /* set the sensitive state of play only if there is a channel selected */
307         if ( play && (rtp_channels->rci[0] || rtp_channels->rci[1]) && !new_jitter_value) {             
308                 gtk_widget_set_sensitive(bt_play, TRUE);
309         } else {
310                 gtk_widget_set_sensitive(bt_play, FALSE);
311         }
312         
313         if (!new_jitter_value) {
314                 gtk_widget_set_sensitive(bt_pause, pause);
315                 gtk_widget_set_sensitive(bt_stop, stop);
316
317                 /* Set sensitive to the check buttons based on the STOP state */
318                 if (rtp_channels_hash)
319                         g_hash_table_foreach( rtp_channels_hash, (GHFunc)set_sensitive_check_bt, &stop);        
320         } else {
321                 gtk_widget_set_sensitive(bt_pause, FALSE);
322                 gtk_widget_set_sensitive(bt_stop, FALSE);
323
324                 if (rtp_channels_hash)
325                         g_hash_table_foreach( rtp_channels_hash, (GHFunc)set_sensitive_check_bt, &false_val);   
326         }
327 }
328
329 /****************************************************************************/
330 void 
331 add_rtp_packet(const struct _rtp_info *rtp_info, packet_info *pinfo)
332 {
333         rtp_stream_info_t *stream_info = NULL;
334         rtp_packet_t *new_rtp_packet;
335         GString *key_str = NULL;
336
337         /* create the the streams hash if it doen't exist */
338         if (!rtp_streams_hash)
339                 rtp_streams_hash = g_hash_table_new_full( g_str_hash, g_str_equal, rtp_key_destroy, rtp_stream_value_destroy);
340
341         /* Create a hash key to lookup in the RTP streams hash table
342          * uses: src_ip:src_port dst_ip:dst_port ssrc
343          */
344         key_str = g_string_new("");
345         g_string_printf(key_str, "%s:%d %s:%d %d", get_addr_name(&(pinfo->src)),
346                 pinfo->srcport, get_addr_name(&(pinfo->dst)),
347                 pinfo->destport, rtp_info->info_sync_src );
348
349         /* lookup for this rtp packet in the stream hash table*/
350         stream_info =  g_hash_table_lookup( rtp_streams_hash, key_str->str);
351
352         /* if it is not in the hash table, create a new stream */
353         if (stream_info==NULL) {
354                 stream_info = g_malloc(sizeof(rtp_stream_info_t));
355                 COPY_ADDRESS(&(stream_info->src_addr), &(pinfo->src));
356                 stream_info->src_port = pinfo->srcport;
357                 COPY_ADDRESS(&(stream_info->dest_addr), &(pinfo->dst));
358                 stream_info->dest_port = pinfo->destport;
359                 stream_info->ssrc = rtp_info->info_sync_src;
360                 stream_info->rtp_packets_list = NULL;
361                 stream_info->first_frame_number = pinfo->fd->num;
362                 stream_info->start_time = nstime_to_msec(&pinfo->fd->rel_ts);
363                 stream_info->call_num = 0;
364                 stream_info->play = FALSE;
365                 stream_info->num_packets = 0;
366
367                 g_hash_table_insert(rtp_streams_hash, g_strdup(key_str->str), stream_info);
368
369                 /* Add the element to the List too. The List is used to decode the packets because it is sordted */
370                 rtp_streams_list = g_list_append(rtp_streams_list, stream_info);
371         }
372
373         /* increment the number of packets in this stream, this is used for the progress bar and statistics*/
374         stream_info->num_packets++;
375
376         /* Add the RTP packet to the list */
377         new_rtp_packet = g_malloc(sizeof(rtp_packet_t));
378         new_rtp_packet->info = g_malloc(sizeof(struct _rtp_info));
379
380         memcpy(new_rtp_packet->info, rtp_info, sizeof(struct _rtp_info));
381         new_rtp_packet->arrive_offset = nstime_to_msec(&pinfo->fd->rel_ts) - stream_info->start_time;
382         /* copy the RTP payload to the rtp_packet to be decoded later */
383         if (rtp_info->info_payload_len) {
384                 new_rtp_packet->payload_data = g_malloc(rtp_info->info_payload_len);
385                 memcpy(new_rtp_packet->payload_data, &(rtp_info->info_data[rtp_info->info_payload_offset]), rtp_info->info_payload_len);
386         } else {
387                 new_rtp_packet->payload_data = NULL;
388         }
389
390         stream_info->rtp_packets_list = g_list_append(stream_info->rtp_packets_list, new_rtp_packet);
391
392         g_string_free(key_str, TRUE);
393 }
394
395 /****************************************************************************/
396 /* Mark the RTP stream to be played. Use the voip_calls graph to see if the 
397  * setup_frame is there and then if the associated voip_call is selected.
398  */
399 static void 
400 mark_rtp_stream_to_play(gchar *key _U_ , rtp_stream_info_t *rsi, gpointer ptr _U_)
401 {
402         GList*  graph_list;
403         graph_analysis_item_t *graph_item;
404         GList*  voip_calls_list;
405         voip_calls_info_t *tmp_voip_call;
406
407         /* Reset the "to be play" value because the user can close and reopen the RTP Player window
408          * and the streams are nor reset in that case
409          */
410         rsi->play = FALSE;
411
412         /* and associate the RTP stream with a call using the first RTP in the stream*/
413         graph_list = g_list_first(voip_calls->graph_analysis->list);
414         while (graph_list)
415         {
416                 graph_item = graph_list->data;
417                 if (rsi->first_frame_number == graph_item->frame_num) {
418                         rsi->call_num = graph_item->conv_num;
419                         /* if it is in the graph list, then check if the voip_call is selected */
420                         voip_calls_list = g_list_first(voip_calls->strinfo_list);
421                         while (voip_calls_list)
422                         {
423                                 tmp_voip_call = voip_calls_list->data;
424                                 if ( (tmp_voip_call->call_num == rsi->call_num) && (tmp_voip_call->selected == TRUE) ) {
425                                         rsi->play = TRUE;
426                                         total_packets += rsi->num_packets;
427                                         break;
428                                 }
429                                 voip_calls_list = g_list_next(voip_calls_list);
430                         }
431                         break;
432                 }
433                 graph_list = g_list_next(graph_list);
434         }
435 }
436
437
438 /****************************************************************************/
439 /* Decode a RTP packet 
440  * Return the number of decoded bytes
441  */
442 static int 
443 decode_rtp_packet(rtp_packet_t *rp, rtp_channel_info_t *rci, SAMPLE **out_buff)
444 {
445         unsigned int  payload_type;
446         SAMPLE *tmp_buff = NULL;
447         int decoded_bytes = 0;
448
449         if ((rp->payload_data == NULL) || (rp->info->info_payload_len == 0) ) {
450                 return 0;
451         }
452
453         payload_type = rp->info->info_payload_type;
454         switch (payload_type) {
455         case 0: /* G711 Ulaw */
456                 tmp_buff = malloc(sizeof(SAMPLE) * rp->info->info_payload_len * 1);
457                 decodeG711u(rp->payload_data, rp->info->info_payload_len,
458                           tmp_buff, &decoded_bytes);
459                 break; 
460         case 8: /* G711 Alaw */
461                 tmp_buff = malloc(sizeof(SAMPLE) * rp->info->info_payload_len * 1);
462                 decodeG711a(rp->payload_data, rp->info->info_payload_len,
463                           tmp_buff, &decoded_bytes);
464                 break; 
465 #ifdef HAVE_G729_G723
466         case 18:        /* G729 */
467                 tmp_buff = malloc(sizeof(SAMPLE) * rp->info->info_payload_len * 8); /* G729 8kbps => 64kbps/8kbps = 8  */
468                 decodeG729(rp->payload_data, rp->info->info_payload_len,
469                           tmp_buff, &decoded_bytes);
470                 break; 
471         case 4: /* G723 */
472
473                 if (rp->info->info_payload_len%24 == 0) /* G723 High 6.4kbps */
474                         tmp_buff = malloc(sizeof(SAMPLE) * rp->info->info_payload_len * 10); /* G723 High 64kbps/6.4kbps = 10  */       
475                 else if (rp->info->info_payload_len%20 == 0)    /* G723 Low 5.3kbps */
476                         tmp_buff = malloc(sizeof(SAMPLE) * rp->info->info_payload_len * 13); /* G723 High 64kbps/5.3kbps = 13  */       
477                 else {
478                         return 0;
479                 }
480                 decodeG723(rp->payload_data, rp->info->info_payload_len,
481                           tmp_buff, &decoded_bytes);
482                 break;
483 #endif /* HAVE_G729_G723 */
484         } 
485
486         *out_buff = tmp_buff;
487         return decoded_bytes;
488 }
489
490 /****************************************************************************/
491 static void
492 update_progress_bar(gfloat percentage)
493 {
494
495         gtk_progress_bar_update(GTK_PROGRESS_BAR(progress_bar), percentage);
496
497         /* Force gtk to redraw the window before starting decoding the packet */
498         while (gtk_events_pending())
499                 gtk_main_iteration();
500 }
501
502 /****************************************************************************/
503 /* Decode the RTP streams and add them to the RTP channels struct
504  */
505 static void 
506 decode_rtp_stream(rtp_stream_info_t *rsi, gpointer ptr _U_)
507 {
508         GString *key_str = NULL;
509         rtp_channel_info_t *rci;
510         gboolean first = TRUE;
511         GList*  rtp_packets_list;
512         rtp_packet_t *rp;
513
514         int i;
515         double rtp_time;
516         double rtp_time_prev;
517         double arrive_time;
518         double arrive_time_prev;
519         double start_time;
520         double start_rtp_time;
521         double diff;
522         double pack_period;
523         double total_time;
524         double total_time_prev;
525         gint32 silence_frames;
526         int seq;
527         double delay;
528         double prev_diff;
529         double mean_delay;
530         double variation;
531         int decoded_bytes;
532         int decoded_bytes_prev;
533         int jitter_buff;
534         SAMPLE *out_buff = NULL;
535         sample_t silence;
536         sample_t sample;
537         guint8 status;
538         guint32 start_timestamp; 
539
540         guint32 progbar_nextstep;
541         int progbar_quantum;
542         gfloat progbar_val;
543
544         silence.val = 0;
545         silence.status = S_NORMAL;
546
547         /* skip it if we are not going to play it */ 
548         if (rsi->play == FALSE) {
549                 return;
550         }
551
552         /* get the static jitter buffer from the spinner gui */
553         jitter_buff = (int) gtk_spin_button_get_value((GtkSpinButton * )jitter_spinner);
554
555         /* Create a hash key to lookup in the RTP channels hash
556          * uses: src_ip:src_port dst_ip:dst_port call_num
557          */
558         key_str = g_string_new("");
559         g_string_printf(key_str, "%s:%d %s:%d %d", get_addr_name(&(rsi->src_addr)),
560                 rsi->src_port, get_addr_name(&(rsi->dest_addr)),
561                 rsi->dest_port, rsi->call_num );
562
563         /* create the rtp_channels_hash table if it doesn't exist */
564         if (!rtp_channels_hash) {
565                 rtp_channels_hash = g_hash_table_new_full( g_str_hash, g_str_equal, rtp_key_destroy, rtp_channel_value_destroy);
566         }
567
568         /* lookup for this stream in the channel hash table */
569         rci =  g_hash_table_lookup( rtp_channels_hash, key_str->str);
570
571         /* ..if it is not in the hash, create an entry */
572         if (rci == NULL) {
573                 rci = malloc(sizeof(rtp_channel_info_t));
574                 rci->call_num = rsi->call_num;
575                 rci->start_time = rsi->start_time;
576                 rci->end_time = rsi->start_time;                
577                 rci->selected = FALSE;
578                 rci->frame_index = 0;
579                 rci->drop_by_jitter_buff = 0;
580                 rci->out_of_seq = 0;
581                 rci->max_frame_index = 0;
582                 rci->samples = g_array_new (FALSE, FALSE, sizeof(sample_t));
583                 rci->check_bt = NULL;
584                 rci->separator = NULL;
585                 rci->draw_area = NULL;
586                 rci->pixmap = NULL;
587                 rci->h_scrollbar_adjustment = NULL;
588                 rci->cursor_pixbuf = NULL;
589                 rci->cursor_prev = 0;
590                 rci->cursor_catch = FALSE;
591                 rci->first_stream = rsi;
592                 rci->num_packets = rsi->num_packets;
593                 g_hash_table_insert(rtp_channels_hash, g_strdup(key_str->str), rci);
594         } else {
595                 /* Add silence between the two streams if needed */
596                 silence_frames = (gint32)( ((rsi->start_time - rci->end_time)/1000)*SAMPLE_RATE );
597                 for (i = 0; i< silence_frames; i++) {
598                         g_array_append_val(rci->samples, silence);
599                 }
600                 rci->num_packets += rsi->num_packets;
601         }
602
603         /* decode the RTP stream */
604         first = TRUE;
605         rtp_time = 0;
606         decoded_bytes = 0;
607         decoded_bytes_prev = 0;
608         silence_frames = 0;
609         arrive_time = start_time = 0;
610         arrive_time_prev = 0;
611         pack_period = 0;
612         total_time = 0;
613         total_time_prev = 0;
614         seq = 0;
615         delay = 0;
616         prev_diff = 0;
617         mean_delay = 0;
618         variation = 0;
619         start_timestamp = 0;
620
621         /* we update the progress bar 100 times */
622
623         /* Update the progress bar when it gets to this value. */
624         progbar_nextstep = 0;
625         /* When we reach the value that triggers a progress bar update,
626            bump that value by this amount. */
627         progbar_quantum = total_packets/100;
628
629         status = S_NORMAL;
630
631         rtp_packets_list = g_list_first(rsi->rtp_packets_list);
632         while (rtp_packets_list)
633         {
634
635                 if (progbar_count >= progbar_nextstep) {
636                         g_assert(total_packets > 0);
637
638                         progbar_val = (gfloat) progbar_count / total_packets;
639
640                         update_progress_bar(progbar_val);
641
642                         progbar_nextstep += progbar_quantum;
643                 }
644                 
645
646                 rp = rtp_packets_list->data;
647                 if (first == TRUE) {
648                         start_timestamp = rp->info->info_timestamp; /* defined start_timestmp to avoid overflow in timestamp. TODO: handle the timestamp correctly */
649                         start_rtp_time = 0;
650                         rtp_time_prev = start_rtp_time;
651                         first = FALSE;
652                         seq = rp->info->info_seq_num - 1;
653                 }
654
655                 decoded_bytes = decode_rtp_packet(rp, rci, &out_buff);
656                 if (decoded_bytes == 0) {
657                         seq = rp->info->info_seq_num;
658                 }
659
660                 rtp_time = (double)(rp->info->info_timestamp-start_timestamp)/SAMPLE_RATE - start_rtp_time;
661                 arrive_time = (double)rp->arrive_offset/1000 - start_time;
662
663                 if (rp->info->info_seq_num != seq+1){
664                         rci->out_of_seq++;
665                         status = S_WRONG_SEQ;
666                 }
667                 seq = rp->info->info_seq_num;
668
669                 diff = arrive_time - rtp_time;
670
671                 delay = diff - prev_diff;
672                 prev_diff = diff;
673                 if (delay<0) delay = -delay;
674
675                 if (diff<0) diff = -diff;
676   
677                 total_time = (double)rp->arrive_offset/1000;
678                 
679                 printf("seq = %d arr = %f abs_diff = %f index = %d tim = %f ji=%d jb=%f\n",rp->info->info_seq_num, 
680                         total_time, diff, rci->samples->len, ((double)rci->samples->len/8000 - total_time)*1000, 0,
681                                 (mean_delay + 4*variation)*1000);
682                 fflush(stdout);
683
684                 /* if the jitter buffer was exceeded */ 
685                 if ( diff*1000 > jitter_buff ) {
686                         printf("Packet drop by jitter buffer exceeded\n");
687                         rci->drop_by_jitter_buff++;
688                         status = S_DROP_BY_JITT;
689
690                         /* if there was a silence period (more than two packetization period) resync the source */
691                         if ( (rtp_time - rtp_time_prev) > pack_period*2 ){
692                                 printf("Resync...\n");
693
694                                 silence_frames = (gint32)((arrive_time - arrive_time_prev)*SAMPLE_RATE - decoded_bytes_prev/2);
695                                 for (i = 0; i< silence_frames; i++) {
696                                         silence.status = status;
697                                         g_array_append_val(rci->samples, silence);
698
699                                         /* only mark the fisrt in the silence that has the previos problem (S_DROP_BY_JITT  or S_WRONG_SEQ ) */
700                                         status = S_NORMAL;
701                                 }
702
703                                 decoded_bytes_prev = 0;
704                                 start_timestamp = rp->info->info_timestamp; /* defined start_timestmp to avoid overflow in timestamp. TODO: handle the timestamp correctly */
705                                 start_rtp_time = 0;
706                                 start_time = (double)rp->arrive_offset/1000;
707                                 rtp_time_prev = 0;
708                         }
709                 } else {
710                         /* Add silence if it is necessary */
711                         silence_frames = (gint32)((rtp_time - rtp_time_prev)*SAMPLE_RATE - decoded_bytes_prev/2);
712                         for (i = 0; i< silence_frames; i++) {
713                                 silence.status = status;
714                                 g_array_append_val(rci->samples, silence);
715
716                                 /* only mark the fisrt in the silence that has the previos problem (S_DROP_BY_JITT  or S_WRONG_SEQ ) */
717                                 status = S_NORMAL;
718                         }
719
720                         status = S_NORMAL;
721
722                         /* Add the audio */
723                         for (i = 0; i< (decoded_bytes/2); i++) {
724                                 sample.val = out_buff[i];
725                                 sample.status = status;
726                                 g_array_append_val(rci->samples, sample);
727                         }
728         
729                         rtp_time_prev = rtp_time;
730                         pack_period = (double)(decoded_bytes/2)/SAMPLE_RATE;
731                         decoded_bytes_prev = decoded_bytes;
732                         arrive_time_prev = arrive_time;
733
734                 }
735
736                 rtp_packets_list = g_list_next (rtp_packets_list);
737                 progbar_count++;
738         }
739         rci->max_frame_index = rci->samples->len;
740         rci->end_time = rci->start_time + ((double)rci->samples->len/SAMPLE_RATE)*1000;
741
742         g_string_free(key_str, TRUE);
743 }
744
745 /****************************************************************************/
746 static gint 
747 h_scrollbar_changed(GtkWidget *widget _U_, gpointer user_data)
748 {
749         rtp_channel_info_t *rci = (rtp_channel_info_t *)user_data;
750         rci->cursor_catch = TRUE;
751         return TRUE;
752 }
753
754 static gboolean draw_cursors(gpointer data);
755
756 /****************************************************************************/
757 static void
758 stop_channels(void) 
759 {       
760         PaError err;
761         GtkWidget *dialog;
762
763         /* we should never be here if we are already in STOP */
764         if(rtp_channels->stop){
765                 exit(10);
766         }
767
768         rtp_channels->stop = TRUE;
769         /* force a draw_cursor to stop it */
770         draw_cursors(NULL);
771
772         err = Pa_StopStream(pa_stream);
773
774         if( err != paNoError ) {
775                 dialog = gtk_message_dialog_new ((GtkWindow *) rtp_player_dlg_w,
776                                                           GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
777                                                           "Can not Stop Stream in PortAudio Library.\n Error: %s", Pa_GetErrorText( err ));
778                 gtk_dialog_run (GTK_DIALOG (dialog));
779                 gtk_widget_destroy (dialog);
780                 return;
781         }
782
783         err = Pa_CloseStream(pa_stream);
784         if( err != paNoError ) {
785                 dialog = gtk_message_dialog_new ((GtkWindow *) rtp_player_dlg_w,
786                                                           GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
787                                                           "Can not Close Stream in PortAudio Library.\n Error: %s", Pa_GetErrorText( err ));
788                 gtk_dialog_run (GTK_DIALOG (dialog));
789                 gtk_widget_destroy (dialog);
790                 return;
791         }
792         pa_stream = NULL;       /* to catch errors better */
793
794         rtp_channels->start_index[0] = 0;
795         rtp_channels->start_index[1] = 0;
796         rtp_channels->end_index[0] = 0;
797         rtp_channels->end_index[1] = 0;
798         rtp_channels->max_frame_index = 0;
799         rtp_channels->frame_index = 0;
800         rtp_channels->pause = FALSE;
801         rtp_channels->pause_duration = 0;
802         rtp_channels->stop = TRUE;
803         rtp_channels->out_diff_time = 10000;
804
805         if (rtp_channels->rci[0]) rtp_channels->rci[0]->frame_index = 0;
806         if (rtp_channels->rci[1]) rtp_channels->rci[1]->frame_index = 0;
807
808         /* set the sensitive state of the buttons (decode, play, pause, stop) */
809         bt_state(TRUE, TRUE, FALSE, FALSE);
810
811 }
812
813 /****************************************************************************/
814 /* Draw a cursor in a channel graph 
815  */
816 static void 
817 draw_channel_cursor(rtp_channel_info_t *rci, guint32 start_index)
818 {
819 #if PORTAUDIO_API_1
820         PaTimestamp index;
821 #else /* PORTAUDIO_API_1 */
822         PaTime index;
823 #endif /* PORTAUDIO_API_1 */
824         int i;
825
826         if (!rci) return;
827
828 #if PORTAUDIO_API_1
829         index = Pa_StreamTime( pa_stream ) - rtp_channels->pause_duration - rtp_channels->out_diff_time - start_index;
830 #else  /* PORTAUDIO_API_1 */
831         index = ((guint32)(SAMPLE_RATE) * (Pa_GetStreamTime(pa_stream)-rtp_channels->pa_start_time))- rtp_channels->pause_duration - rtp_channels->out_diff_time - start_index;
832 #endif  /* PORTAUDIO_API_1 */
833
834
835         /* If we finished playing both channels, then stop them */
836         if ( (rtp_channels && (!rtp_channels->stop) && (!rtp_channels->pause)) && (index > rtp_channels->max_frame_index) ) {
837                 stop_channels();
838                 return;
839         }
840
841         /* If only this channel finished, then return */
842         if (index > rci->max_frame_index) {
843                 return;
844         }
845
846         /* draw the previous saved pixbuf line */
847         if (rci->cursor_pixbuf && (rci->cursor_prev>=0)) {
848
849                 gdk_draw_pixbuf(rci->pixmap, NULL, rci->cursor_pixbuf, 0, 0, (int) (rci->cursor_prev/MULT), 0, -1, -1, GDK_RGB_DITHER_NONE, 0 ,0);
850
851                 gdk_draw_drawable(rci->draw_area->window,
852                         rci->draw_area->style->fg_gc[GTK_WIDGET_STATE(rci->draw_area)],
853                         rci->pixmap,
854                         (int) (rci->cursor_prev/MULT), 0,
855                         (int) (rci->cursor_prev/MULT), 0,
856                         1, rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
857
858                 g_object_unref(rci->cursor_pixbuf);
859                 rci->cursor_pixbuf = NULL;
860         }
861
862         if (index>0 && (rci->cursor_prev>=0)) {
863                 rci->cursor_pixbuf = gdk_pixbuf_get_from_drawable(NULL, rci->pixmap, NULL, (int) (index/MULT), 0, 0, 0, 1, rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
864
865                 gdk_draw_line(rci->pixmap, rci->draw_area->style->black_gc,
866                         (int) (index/MULT),
867                         0,
868                         (int) (index/MULT),
869                         rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
870
871                 gdk_draw_drawable(rci->draw_area->window,
872                         rci->draw_area->style->fg_gc[GTK_WIDGET_STATE(rci->draw_area)], 
873                         rci->pixmap,
874                         (int) (index/MULT), 0,
875                         (int) (index/MULT), 0,
876                         1, rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
877         }
878
879         /* Disconnect the scroll bar "value" signal to not be called */
880         SIGNAL_DISCONNECT_BY_FUNC(rci->h_scrollbar_adjustment, h_scrollbar_changed, rci);
881
882         /* Move the horizontal scroll bar */
883 /*      if ( (rci->cursor_prev/MULT < (rci->h_scrollbar_adjustment->value+rci->h_scrollbar_adjustment->page_increment)) && 
884                 (index/MULT >= (rci->h_scrollbar_adjustment->value+rci->h_scrollbar_adjustment->page_increment)) ){             
885                 for (i=1; i<10; i++) {
886                         rci->h_scrollbar_adjustment->value += rci->h_scrollbar_adjustment->page_size/10;
887                         gtk_adjustment_value_changed(rci->h_scrollbar_adjustment);
888                 }
889
890         }
891  */
892         if (!rci->cursor_catch) {
893                 if (index/MULT < rci->h_scrollbar_adjustment->page_size/2) {
894                         rci->h_scrollbar_adjustment->value = rci->h_scrollbar_adjustment->lower;
895                 } else if (index/MULT > (rci->h_scrollbar_adjustment->upper - rci->h_scrollbar_adjustment->page_size/2)) {
896                         rci->h_scrollbar_adjustment->value = rci->h_scrollbar_adjustment->upper - rci->h_scrollbar_adjustment->page_size;
897                 } else {
898                         rci->h_scrollbar_adjustment->value = index/MULT - rci->h_scrollbar_adjustment->page_size/2;
899                 }
900
901                 gtk_adjustment_value_changed(rci->h_scrollbar_adjustment);
902         } else if ( (rci->cursor_prev/MULT < (rci->h_scrollbar_adjustment->value+rci->h_scrollbar_adjustment->page_increment)) && 
903                 (index/MULT >= (rci->h_scrollbar_adjustment->value+rci->h_scrollbar_adjustment->page_increment)) ){     
904                 rci->cursor_catch = FALSE;
905                 for (i=1; i<10; i++) {
906                         rci->h_scrollbar_adjustment->value = min(rci->h_scrollbar_adjustment->upper-rci->h_scrollbar_adjustment->page_size, rci->h_scrollbar_adjustment->value + (rci->h_scrollbar_adjustment->page_size/20));
907                         gtk_adjustment_value_changed(rci->h_scrollbar_adjustment);
908                 }
909
910         }
911
912
913         /* Connect back the "value" scroll signal */
914         SIGNAL_CONNECT(rci->h_scrollbar_adjustment, "value_changed", h_scrollbar_changed, rci);
915
916
917 /*      if (index/MULT < rci->h_scrollbar_adjustment->page_increment) {
918                 rci->h_scrollbar_adjustment->value = rci->h_scrollbar_adjustment->lower;
919         } else if (index/MULT > (rci->h_scrollbar_adjustment->upper - rci->h_scrollbar_adjustment->page_size + rci->h_scrollbar_adjustment->page_increment)) {
920                 rci->h_scrollbar_adjustment->value = rci->h_scrollbar_adjustment->upper - rci->h_scrollbar_adjustment->page_size;
921         } else {
922                 if ( (index/MULT < rci->h_scrollbar_adjustment->value) || (index/MULT > (rci->h_scrollbar_adjustment->value+rci->h_scrollbar_adjustment->page_increment)) ){
923                         rci->h_scrollbar_adjustment->value = index/MULT;
924                 }
925         }
926  */
927
928 /*      if (index/MULT < rci->h_scrollbar_adjustment->page_size/2) {
929                 rci->h_scrollbar_adjustment->value = rci->h_scrollbar_adjustment->lower;
930         } else if (index/MULT > (rci->h_scrollbar_adjustment->upper - rci->h_scrollbar_adjustment->page_size/2)) {
931                 rci->h_scrollbar_adjustment->value = rci->h_scrollbar_adjustment->upper - rci->h_scrollbar_adjustment->page_size;
932         } else {
933                 rci->h_scrollbar_adjustment->value = index/MULT - rci->h_scrollbar_adjustment->page_size/2;
934         }
935  */
936 /*      gtk_adjustment_value_changed(rci->h_scrollbar_adjustment);
937  */
938         rci->cursor_prev = index;
939 }
940
941 /****************************************************************************/
942 /* Move and draw the cursor in the graph 
943  */
944 static gboolean 
945 draw_cursors(gpointer data)
946 {
947         if (!rtp_channels) return FALSE;
948
949         /* Draw and move each of the two channels */
950         draw_channel_cursor(rtp_channels->rci[0], rtp_channels->start_index[0]);
951         draw_channel_cursor(rtp_channels->rci[1], rtp_channels->start_index[1]);
952
953         if ((rtp_channels->stop) || (rtp_channels->pause)) return FALSE;
954
955         return TRUE;
956 }
957
958 /****************************************************************************/
959 static void
960 init_rtp_channels_vals(void)
961 {
962         rtp_play_channles_t *rpci = rtp_channels; 
963         
964         /* if we only have one channel to play, we just use the info from that channel */
965         if (rpci->rci[0] == NULL) {
966                 rpci->max_frame_index = rpci->rci[1]->max_frame_index;
967                 rpci->start_index[0] = rpci->max_frame_index;
968                 rpci->start_index[1] = 0;
969                 rpci->end_index[0] = rpci->max_frame_index;
970                 rpci->end_index[1] = rpci->max_frame_index;
971         } else if (rpci->rci[1] == NULL) {
972                 rpci->max_frame_index = rpci->rci[0]->max_frame_index;
973                 rpci->start_index[1] = rpci->max_frame_index;
974                 rpci->start_index[0] = 0;
975                 rpci->end_index[0] = rpci->max_frame_index;
976                 rpci->end_index[1] = rpci->max_frame_index;
977
978         /* if the two channels are to be played, then we need to sync both based on the start/end time of each one */
979         } else {
980                 rpci->max_frame_index = (guint32)(SAMPLE_RATE/1000) * (guint32)(max(rpci->rci[0]->end_time, rpci->rci[1]->end_time) -
981                                                         (guint32)min(rpci->rci[0]->start_time, rpci->rci[1]->start_time));
982
983                 if (rpci->rci[0]->start_time < rpci->rci[1]->start_time) {
984                         rpci->start_index[0] = 0;
985                         rpci->start_index[1] = (guint32)(SAMPLE_RATE/1000) * (guint32)(rpci->rci[1]->start_time - rpci->rci[0]->start_time);
986                 } else {
987                         rpci->start_index[1] = 0;
988                         rpci->start_index[0] = (guint32)(SAMPLE_RATE/1000) * (guint32)(rpci->rci[0]->start_time - rpci->rci[1]->start_time);
989                 } 
990
991                 if (rpci->rci[0]->end_time < rpci->rci[1]->end_time) {
992                         rpci->end_index[0] = rpci->max_frame_index - ((guint32)(SAMPLE_RATE/1000) * (guint32)(rpci->rci[1]->end_time - rpci->rci[0]->end_time));
993                         rpci->end_index[1] = rpci->max_frame_index;
994                 } else {
995                         rpci->end_index[1] = rpci->max_frame_index - ((guint32)(SAMPLE_RATE/1000) * (guint32)(rpci->rci[0]->end_time - rpci->rci[1]->end_time));
996                         rpci->end_index[0] = rpci->max_frame_index;
997                 } 
998         }
999 }
1000
1001
1002 /****************************************************************************/
1003 /* This routine will be called by the PortAudio engine when audio is needed.
1004  * It may called at interrupt level on some machines so don't do anything
1005  * that could mess up the system like calling malloc() or free().
1006  */
1007 #if PORTAUDIO_API_1
1008
1009 static int paCallback(   void *inputBuffer, void *outputBuffer,
1010                              unsigned long framesPerBuffer,
1011                              PaTimestamp outTime, void *userData)
1012 {
1013 #else /* PORTAUDIO_API_1 */
1014 static int paCallback( void *inputBuffer, void *outputBuffer,
1015                              unsigned long framesPerBuffer,
1016                                                          const PaStreamCallbackTimeInfo* outTime,
1017                                                          PaStreamCallbackFlags statusFlags,
1018                              void *userData)
1019 {
1020 /*      (void) statusFlags;*/
1021 #endif /* PORTAUDIO_API_1 */
1022         rtp_play_channles_t *rpci = (rtp_play_channles_t*)userData;
1023         SAMPLE *wptr = (SAMPLE*)outputBuffer;
1024         sample_t sample;
1025         unsigned int i;
1026         int finished;
1027         unsigned int framesLeft;
1028         int framesToPlay;
1029
1030         /* if it is pasued, we keep the stream running but with silence only */
1031         if (rtp_channels->pause) {
1032                 for(i=0; i<framesPerBuffer; i++ ) {
1033                         *wptr++ = 0;
1034                         *wptr++ = 0;
1035                 }
1036                 rtp_channels->pause_duration += framesPerBuffer;
1037                 return 0;
1038         }
1039
1040 #if PORTAUDIO_API_1
1041         rpci->out_diff_time = outTime -  Pa_StreamTime(pa_stream) ;
1042 #else /* PORTAUDIO_API_1 */
1043         rpci->out_diff_time = (guint32)(SAMPLE_RATE) * (outTime->outputBufferDacTime - Pa_GetStreamTime(pa_stream)) ; 
1044 #endif /* PORTAUDIO_API_1 */
1045
1046
1047         /* set the values if this is the first time */
1048         if (rpci->max_frame_index == 0) {
1049                 init_rtp_channels_vals();
1050
1051         }
1052
1053         framesLeft = rpci->max_frame_index - rpci->frame_index;
1054
1055         (void) inputBuffer; /* Prevent unused variable warnings. */
1056         (void) outTime;
1057
1058         if( framesLeft < framesPerBuffer )
1059         {
1060                 framesToPlay = framesLeft;
1061                 finished = 1;
1062         }
1063         else
1064         {
1065                 framesToPlay = framesPerBuffer;
1066                 finished = 0;
1067         }
1068
1069         for( i=0; i<(unsigned int)framesToPlay; i++ )
1070         {
1071                 if (rpci->rci[0] && ( (rpci->frame_index >= rpci->start_index[0]) && (rpci->frame_index <= rpci->end_index[0]) )) {
1072                         sample = g_array_index(rpci->rci[0]->samples, sample_t, rpci->rci[0]->frame_index++);
1073                         *wptr++ = sample.val;
1074                 } else {
1075                         *wptr++ = 0;
1076                 }
1077
1078                 if (rpci->rci[1] && ( (rpci->frame_index >= rpci->start_index[1]) && (rpci->frame_index <= rpci->end_index[1]) )) {
1079                         sample = g_array_index(rpci->rci[1]->samples, sample_t, rpci->rci[1]->frame_index++);
1080                         *wptr++ = sample.val;
1081                 } else {
1082                         *wptr++ = 0;
1083                 }
1084         }
1085         for( ; i<framesPerBuffer; i++ )
1086         {
1087                 *wptr++ = 0;
1088                 *wptr++ = 0;
1089         }
1090         rpci->frame_index += framesToPlay;
1091
1092         return finished;
1093 }
1094
1095 /****************************************************************************/
1096 static void 
1097 on_bt_check_clicked(GtkButton *button _U_, gpointer user_data _U_)
1098 {
1099         rtp_channel_info_t *rci = user_data;
1100
1101         if (rci->selected) {
1102                 if (rtp_channels->rci[0] == rci) {
1103                         rtp_channels->rci[0] = NULL;
1104                         rtp_channels->channel = 0;
1105                 } else {
1106                         rtp_channels->rci[1] = NULL;
1107                         rtp_channels->channel = 1;
1108                 }
1109         } else {
1110                 /* if there are already both channels selected, unselect the old one */
1111                 if (rtp_channels->rci[rtp_channels->channel]) {
1112                         /* we disconnect the signal temporarly to avoid been called back */
1113                         SIGNAL_DISCONNECT_BY_FUNC(rtp_channels->rci[rtp_channels->channel]->check_bt, on_bt_check_clicked, rtp_channels->rci[rtp_channels->channel]);
1114                         gtk_toggle_button_set_active((GtkToggleButton *)rtp_channels->rci[rtp_channels->channel]->check_bt, FALSE);
1115                         SIGNAL_CONNECT(rtp_channels->rci[rtp_channels->channel]->check_bt, "clicked", on_bt_check_clicked, rtp_channels->rci[rtp_channels->channel]);
1116                         rtp_channels->rci[rtp_channels->channel]->selected = FALSE;
1117                 }
1118
1119                 rtp_channels->rci[rtp_channels->channel] = rci;
1120                 rtp_channels->channel = !(rtp_channels->channel);
1121         }
1122
1123         rci->selected = !(rci->selected);
1124
1125         /* set the sensitive state of the buttons (decode, play, pause, stop) */
1126         bt_state(TRUE, TRUE, FALSE, FALSE);
1127 }
1128
1129 /****************************************************************************/
1130 static void channel_draw(rtp_channel_info_t* rci)
1131 {
1132         int i, imax;
1133         int j;
1134         sample_t sample;
1135         SAMPLE min, max;
1136         PangoLayout  *small_layout;
1137         guint32 label_width, label_height;
1138         char label_string[MAX_TIME_LABEL];
1139         double offset;
1140         guint32 progbar_nextstep;
1141         int progbar_quantum;
1142         gfloat progbar_val;
1143         guint status;
1144         GdkGC *gc;
1145         GdkGC *red_gc;
1146         GdkColor red_color = {0, 65535, 0, 0};
1147         
1148         if (GDK_IS_DRAWABLE(rci->pixmap)) {
1149                 /* Clear out old plot */
1150                 gdk_draw_rectangle(rci->pixmap,
1151                         rci->bg_gc[1+rci->call_num%MAX_NUM_COL_CONV],
1152                         TRUE,
1153                         0, 0,
1154                         rci->draw_area->allocation.width,
1155                         rci->draw_area->allocation.height);
1156
1157                 small_layout = gtk_widget_create_pango_layout(rci->draw_area, NULL);
1158                 pango_layout_set_font_description(small_layout, pango_font_description_from_string("Helvetica,Sans,Bold 7"));
1159
1160                 /* calculated the pixel offset to display integer seconds */
1161                 offset = ((double)rci->start_time/1000 - floor((double)rci->start_time/1000))*SAMPLE_RATE/MULT;
1162
1163                 gdk_draw_line(rci->pixmap, rci->draw_area->style->black_gc,
1164                                 0,
1165                                 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL,
1166                                 rci->draw_area->allocation.width,
1167                                 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
1168
1169                 imax = min(rci->draw_area->allocation.width,(gint)(rci->samples->len/MULT));
1170
1171                 /* we update the progress bar 100 times */
1172
1173                 /* Update the progress bar when it gets to this value. */
1174                 progbar_nextstep = 0;
1175                 /* When we reach the value that triggers a progress bar update,
1176                    bump that value by this amount. */
1177                 progbar_quantum = imax/100;
1178
1179                 red_gc = gdk_gc_new(rci->draw_area->window);
1180                 gdk_gc_set_rgb_fg_color(red_gc, &red_color);
1181
1182                 for (i=0; i< imax; i++) {
1183                         sample.val = 0;
1184                         status = S_NORMAL;
1185                         max=(SAMPLE)0xFFFF;
1186                         min=(SAMPLE)0x7FFF;
1187
1188                         if (progbar_count >= progbar_nextstep) {
1189                                 g_assert(total_frames > 0);
1190
1191                                 progbar_val = (gfloat) i / imax;
1192
1193                                 update_progress_bar(progbar_val);
1194
1195                                 progbar_nextstep += progbar_quantum;
1196                         }
1197
1198                         for (j=0; j<MULT; j++) {
1199                                 sample = g_array_index(rci->samples, sample_t, i*MULT+j);
1200                                 max = max(max, sample.val);
1201                                 min = min(min, sample.val);
1202                                 if (sample.status == S_DROP_BY_JITT) status = S_DROP_BY_JITT;
1203                         }
1204
1205                         if (status == S_DROP_BY_JITT) {
1206                                 gc = red_gc;
1207                         } else {
1208                                 gc = rci->draw_area->style->black_gc;
1209                         }
1210
1211                         gdk_draw_line(rci->pixmap, gc,
1212                                 i,
1213                                 (gint)(( (0x7FFF+min) * (rci->draw_area->allocation.height-HEIGHT_TIME_LABEL))/0xFFFF),
1214                                 i,
1215                                 (gint)(( (0x7FFF+max) * (rci->draw_area->allocation.height-HEIGHT_TIME_LABEL))/0xFFFF));
1216
1217                         /*draw the time label and grid */
1218                         if ( !((i*MULT)%(SAMPLE_RATE)) ) {
1219                                 gdk_draw_line(rci->pixmap, rci->draw_area->style->black_gc,
1220                                         (int) (i - offset),
1221                                         rci->draw_area->allocation.height-HEIGHT_TIME_LABEL,
1222                                         (int) (i - offset),
1223                                         rci->draw_area->allocation.height-HEIGHT_TIME_LABEL+4);
1224
1225                                 g_snprintf(label_string, MAX_TIME_LABEL, "%.0f", floor(rci->start_time/1000) + i*MULT/SAMPLE_RATE);
1226
1227                                 pango_layout_set_text(small_layout, label_string, -1);
1228                                 pango_layout_get_pixel_size(small_layout, &label_width, &label_height);
1229                                 gdk_draw_layout(rci->pixmap,
1230                                         rci->draw_area->style->black_gc,
1231                                         (int) (i - offset - label_width/2),
1232                                         rci->draw_area->allocation.height - label_height,
1233                                         small_layout);
1234                         /* draw the 1/2 sec grid */
1235                         } else if ( !((i*MULT)%(SAMPLE_RATE/2)) ) {
1236                                 gdk_draw_line(rci->pixmap, rci->draw_area->style->black_gc,
1237                                         (int) (i - offset),
1238                                         rci->draw_area->allocation.height-HEIGHT_TIME_LABEL,
1239                                         (int) (i - offset),
1240                                         rci->draw_area->allocation.height-HEIGHT_TIME_LABEL+2);
1241
1242                         }
1243
1244                         progbar_count++;
1245                 }
1246         }
1247
1248 }
1249 /****************************************************************************/
1250 static gint expose_event_channels(GtkWidget *widget, GdkEventExpose *event)
1251 {
1252         rtp_channel_info_t *rci;
1253
1254         rci=(rtp_channel_info_t *)OBJECT_GET_DATA(widget, "rtp_channel_info_t");
1255         if(!rci){
1256                 exit(10);
1257         }
1258
1259         if (GDK_IS_DRAWABLE(widget->window))
1260                 gdk_draw_drawable(widget->window,
1261                         widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
1262                         rci->pixmap,
1263                         event->area.x, event->area.y,
1264                         event->area.x, event->area.y,
1265                         event->area.width, event->area.height);
1266
1267         return FALSE;
1268 }
1269
1270 /****************************************************************************/
1271 static gint 
1272 configure_event_channels(GtkWidget *widget, GdkEventConfigure *event _U_)
1273 {
1274         rtp_channel_info_t *rci;
1275         int i;
1276
1277         /* the first calor is blue to highlight the selected item 
1278          * the other collors are the same as in the Voip Graph analysys
1279          * to match the same calls 
1280          */
1281         static GdkColor col[MAX_NUM_COL_CONV+1] = {
1282                 {0,     0x00FF, 0x00FF, 0xFFFF},
1283                 {0,     0x33FF, 0xFFFF, 0x33FF},
1284                 {0,     0x00FF, 0xCCFF, 0xCCFF},
1285                 {0,     0x66FF, 0xFFFF, 0xFFFF},
1286                 {0,     0x99FF, 0x66FF, 0xFFFF},
1287                 {0,     0xFFFF, 0xFFFF, 0x33FF},
1288                 {0,     0xCCFF, 0x99FF, 0xFFFF},
1289                 {0,     0xCCFF, 0xFFFF, 0x33FF},
1290                 {0,     0xFFFF, 0xCCFF, 0xCCFF},
1291                 {0,     0xFFFF, 0x99FF, 0x66FF},
1292                 {0,     0xFFFF, 0xFFFF, 0x99FF}
1293         };
1294
1295         rci=(rtp_channel_info_t *)OBJECT_GET_DATA(widget, "rtp_channel_info_t");
1296         if(!rci){
1297                 exit(10);
1298         }
1299
1300         if(rci->pixmap){
1301                 g_object_unref(rci->pixmap);
1302                 rci->pixmap=NULL;
1303         }
1304
1305         rci->pixmap = gdk_pixmap_new(widget->window,
1306                                         widget->allocation.width,
1307                                         widget->allocation.height,
1308                                         -1);
1309
1310         if ( GDK_IS_DRAWABLE(rci->pixmap) )
1311                 gdk_draw_rectangle(rci->pixmap,
1312                         widget->style->white_gc,
1313                         TRUE,
1314                         0, 0,
1315                         widget->allocation.width,
1316                         widget->allocation.height);
1317
1318         /* create gcs for the background color of each channel */
1319         for (i=0; i<MAX_NUM_COL_CONV+1; i++){
1320                 rci->bg_gc[i]=gdk_gc_new(rci->pixmap);
1321                 gdk_gc_set_rgb_fg_color(rci->bg_gc[i], &col[i]);
1322         }
1323
1324         channel_draw(rci);
1325
1326         return TRUE;
1327 }
1328
1329 /****************************************************************************/
1330 static gint 
1331 button_press_event_channel(GtkWidget *widget, GdkEventButton *event _U_)
1332 {
1333         rtp_channel_info_t *rci;
1334         int this_channel;
1335         guint32 prev_index;
1336
1337         rci=(rtp_channel_info_t *)OBJECT_GET_DATA(widget, "rtp_channel_info_t");
1338         if(!rci){
1339                 exit(10);
1340         }
1341
1342         if (!rci->selected) {
1343
1344                 /* only select a new channels if we are in STOP */
1345                 if (!rtp_channels->stop) return 0;
1346
1347                 /* if there are already both channels selected, unselect the old one */
1348                 if (rtp_channels->rci[rtp_channels->channel]) {
1349                         /* we disconnect the signal temporarly to avoid been called back */
1350                         SIGNAL_DISCONNECT_BY_FUNC(rtp_channels->rci[rtp_channels->channel]->check_bt, on_bt_check_clicked, rtp_channels->rci[rtp_channels->channel]);
1351                         gtk_toggle_button_set_active((GtkToggleButton *) rtp_channels->rci[rtp_channels->channel]->check_bt, FALSE);
1352                         SIGNAL_CONNECT(rtp_channels->rci[rtp_channels->channel]->check_bt, "clicked", on_bt_check_clicked, rtp_channels->rci[rtp_channels->channel]);
1353                         rtp_channels->rci[rtp_channels->channel]->selected = FALSE;
1354                 }
1355
1356                 /* we disconnect the signal temporarly to avoid been called back */
1357                 SIGNAL_DISCONNECT_BY_FUNC(rci->check_bt, on_bt_check_clicked, rci);
1358                 gtk_toggle_button_set_active((GtkToggleButton *) rci->check_bt, TRUE);
1359                 SIGNAL_CONNECT(rci->check_bt, "clicked", on_bt_check_clicked, rci);
1360
1361                 rtp_channels->rci[rtp_channels->channel] = rci;
1362                 rtp_channels->channel = !(rtp_channels->channel);
1363                 rci->selected = TRUE;
1364
1365                 /* set the sensitive state of the buttons (decode, play, pause, stop) */                
1366                 bt_state(TRUE, TRUE, FALSE, FALSE);
1367         }
1368
1369         if (rci == rtp_channels->rci[0]) {
1370                 this_channel = 0;
1371         } else {
1372                 this_channel = 1;
1373         }
1374
1375         rci->frame_index = (unsigned int) (event->x * MULT);
1376         
1377         prev_index = rtp_channels->frame_index;
1378         rtp_channels->frame_index = rtp_channels->start_index[this_channel] + rci->frame_index;
1379         rtp_channels->pause_duration += prev_index - rtp_channels->frame_index;
1380
1381
1382
1383         /* change the index in the other channel if selected, according with the index position */
1384         if (rtp_channels->rci[!this_channel]) {
1385                 init_rtp_channels_vals();
1386
1387                 if (rtp_channels->frame_index < rtp_channels->start_index[!this_channel]) {
1388                         rtp_channels->rci[!this_channel]->frame_index = 0;
1389                 } else if (rtp_channels->frame_index > rtp_channels->end_index[!this_channel]) {
1390                         rtp_channels->rci[!this_channel]->frame_index = rtp_channels->rci[!this_channel]->max_frame_index;
1391                 } else {
1392                         rtp_channels->rci[!this_channel]->frame_index = rtp_channels->frame_index - rtp_channels->start_index[!this_channel];
1393                 }
1394         } else {
1395                 init_rtp_channels_vals();
1396         }
1397
1398         rtp_channels->out_diff_time = 0;
1399
1400         rci->cursor_catch = TRUE;
1401
1402         /* redraw the cusrsor */
1403         draw_cursors(NULL);
1404
1405         return TRUE;
1406 }
1407
1408 /****************************************************************************/
1409 static void
1410 add_channel_to_window(gchar *key _U_ , rtp_channel_info_t *rci, guint *counter _U_ )
1411 {
1412         GString *label = NULL;
1413         GtkWidget *viewport;
1414
1415
1416         /* create the channel draw area */
1417         rci->draw_area=gtk_drawing_area_new();
1418         
1419         rci->scroll_window=gtk_scrolled_window_new(NULL, NULL);
1420
1421         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (rci->scroll_window), GTK_POLICY_ALWAYS, GTK_POLICY_NEVER);
1422         rci->h_scrollbar_adjustment = gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(rci->scroll_window));
1423
1424         
1425         gtk_widget_set_size_request(rci->draw_area, (gint)(rci->samples->len/MULT), CHANNEL_HEIGHT);
1426
1427
1428         viewport = gtk_viewport_new(rci->h_scrollbar_adjustment, gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(rci->scroll_window)));
1429         gtk_container_add(GTK_CONTAINER(viewport), rci->draw_area);
1430         gtk_container_add(GTK_CONTAINER(rci->scroll_window), viewport);
1431         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1432         OBJECT_SET_DATA(rci->draw_area, "rtp_channel_info_t", rci);
1433         gtk_widget_add_events (rci->draw_area, GDK_BUTTON_PRESS_MASK);
1434         GTK_WIDGET_SET_FLAGS(rci->draw_area, GTK_CAN_FOCUS);
1435         gtk_widget_grab_focus(rci->draw_area);
1436
1437         gtk_box_pack_start(GTK_BOX (channels_vb), rci->scroll_window, FALSE, FALSE, 0);
1438
1439         /* signals needed to handle backing pixmap */
1440         SIGNAL_CONNECT(rci->draw_area, "expose_event", expose_event_channels, NULL);
1441         SIGNAL_CONNECT(rci->draw_area, "configure_event", configure_event_channels, rci);
1442         gtk_widget_add_events (rci->draw_area, GDK_BUTTON_PRESS_MASK);
1443         SIGNAL_CONNECT(rci->draw_area, "button_press_event", button_press_event_channel, rci);
1444         SIGNAL_CONNECT(rci->h_scrollbar_adjustment, "value_changed", h_scrollbar_changed, rci);
1445
1446
1447         label = g_string_new("");
1448         g_string_printf(label, "From %s:%d to %s:%d   Duration:%.2f   Drop by Jitter Buff:%d(%.1f%%)   Out of Seq: %d(%.1f%%)", get_addr_name(&(rci->first_stream->src_addr)), 
1449                 rci->first_stream->src_port, get_addr_name(&(rci->first_stream->dest_addr)), rci->first_stream->dest_port, 
1450                 (double)rci->samples->len/SAMPLE_RATE, rci->drop_by_jitter_buff, (double)rci->drop_by_jitter_buff * 100 / (double)rci->num_packets
1451                 , rci->out_of_seq, (double)rci->out_of_seq * 100 / (double)rci->num_packets);
1452
1453         rci->check_bt = gtk_check_button_new_with_label(label->str);
1454         gtk_box_pack_start(GTK_BOX (channels_vb), rci->check_bt, FALSE, FALSE, 1);
1455         
1456         /* Create the Separator if it is not the last one */
1457         (*counter)++;
1458         if (*counter < g_hash_table_size(rtp_channels_hash)) {
1459             rci->separator = gtk_hseparator_new();
1460                 gtk_box_pack_start(GTK_BOX (channels_vb), rci->separator, FALSE, FALSE, 5);
1461         }
1462
1463         SIGNAL_CONNECT(rci->check_bt, "clicked", on_bt_check_clicked, rci);
1464
1465         g_string_free(label, TRUE);
1466 }
1467
1468 /****************************************************************************/
1469 static void
1470 count_channel_frames(gchar *key _U_ , rtp_channel_info_t *rci, gpointer ptr _U_ ) 
1471 {
1472         total_frames += rci->samples->len;
1473 }
1474
1475 /****************************************************************************/
1476 static void
1477 play_channels(void) 
1478 {       
1479         PaError err;
1480         GtkWidget *dialog;
1481
1482         /* we should never be here if we are in PLAY and !PAUSE */
1483         if(!rtp_channels->stop && !rtp_channels->pause){
1484                 exit(10);
1485         }
1486
1487         /* if we are in PAUSE change the sate */
1488         if (rtp_channels->pause) {
1489                 rtp_channels->pause = FALSE;
1490                 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1491                 bt_state(FALSE, FALSE, TRUE, TRUE);
1492
1493         /* if not PAUSE, then start to PLAY */
1494         } else {
1495 #if PORTAUDIO_API_1
1496                 err = Pa_OpenStream(
1497                           &pa_stream,
1498                           paNoDevice,     /* default input device */
1499                           0,              /* no input */
1500                           PA_SAMPLE_TYPE, /* 16 bit Integer input */
1501                           NULL,
1502                           Pa_GetDefaultOutputDeviceID(),
1503                           NUM_CHANNELS,   /* Stereo output */
1504                           PA_SAMPLE_TYPE, /* 16 bit Integer output */
1505                           NULL,
1506                           SAMPLE_RATE,
1507                           FRAMES_PER_BUFFER,
1508                           0,              /* number of buffers, if zero then use default minimum */
1509                           paClipOff,      /* we won't output out of range samples so don't bother clipping them */
1510                           paCallback,
1511                           rtp_channels );
1512 #else /* PORTAUDIO_API_1 */
1513                 err = Pa_OpenDefaultStream( 
1514                                 &pa_stream,
1515                                 0,
1516                                 NUM_CHANNELS,
1517                                 PA_SAMPLE_TYPE,
1518                                 SAMPLE_RATE,
1519                                 FRAMES_PER_BUFFER,
1520                                 paCallback,
1521                                 rtp_channels );
1522 #endif /* PORTAUDIO_API_1 */
1523
1524                 if( err != paNoError ) {
1525                         dialog = gtk_message_dialog_new ((GtkWindow *) rtp_player_dlg_w,
1526                                                                   GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
1527                                                                   "Can not Open Stream in PortAudio Library.\n Error: %s", Pa_GetErrorText( err ));
1528                         gtk_dialog_run (GTK_DIALOG (dialog));
1529                         gtk_widget_destroy (dialog);
1530                         return;
1531                 }
1532
1533                 err = Pa_StartStream( pa_stream );
1534                 if( err != paNoError ) {
1535                         dialog = gtk_message_dialog_new ((GtkWindow *) rtp_player_dlg_w,
1536                                                                   GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
1537                                                                   "Can not Start Stream in PortAudio Library.\n Error: %s", Pa_GetErrorText( err ));
1538                         gtk_dialog_run (GTK_DIALOG (dialog));
1539                         gtk_widget_destroy (dialog);
1540                         return;
1541                 }
1542 #if !PORTAUDIO_API_1
1543                 rtp_channels->pa_start_time = Pa_GetStreamTime(pa_stream);
1544 #endif /* PORTAUDIO_API_1 */
1545
1546                 rtp_channels->stop = FALSE;
1547
1548                 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1549                 bt_state(FALSE, FALSE, TRUE, TRUE);
1550         }
1551
1552         /* Draw the cursor in the graph */
1553         g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, MULT*1000/SAMPLE_RATE, draw_cursors, NULL, NULL);
1554
1555 }
1556
1557 /****************************************************************************/
1558 static void
1559 pause_channels(void) 
1560 {       
1561         rtp_channels->pause = !(rtp_channels->pause);
1562
1563         /* reactivate the cusrosr display if no in pause */
1564         if (!rtp_channels->pause) {
1565                 /* Draw the cursor in the graph */
1566                 g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, MULT*1000/SAMPLE_RATE, draw_cursors, NULL, NULL);
1567         }
1568
1569         /* set the sensitive state of the buttons (decode, play, pause, stop) */        
1570         bt_state(FALSE, TRUE, FALSE, TRUE);
1571 }
1572
1573 /****************************************************************************/
1574 static void 
1575 reset_rtp_channels(void)
1576 {
1577         rtp_channels->channel = 0;
1578         rtp_channels->rci[0] = NULL;
1579         rtp_channels->rci[1] = NULL;
1580         rtp_channels->start_index[0] = 0;
1581         rtp_channels->start_index[1] = 0;
1582         rtp_channels->end_index[0] = 0;
1583         rtp_channels->end_index[1] = 0;
1584         rtp_channels->max_frame_index = 0;
1585         rtp_channels->frame_index = 0;
1586         rtp_channels->pause = FALSE;
1587         rtp_channels->pause_duration = 0;
1588         rtp_channels->stop = TRUE;
1589         rtp_channels->out_diff_time = 10000;
1590 }
1591
1592 /****************************************************************************/
1593 static void
1594 remove_channel_to_window(gchar *key _U_ , rtp_channel_info_t *rci, gpointer ptr _U_ ) 
1595 {
1596         g_object_unref(rci->pixmap);
1597         gtk_widget_destroy(rci->draw_area);
1598         gtk_widget_destroy(rci->scroll_window);
1599         gtk_widget_destroy(rci->check_bt);
1600         if (rci->separator)
1601                 gtk_widget_destroy(rci->separator);
1602 }
1603
1604 /****************************************************************************/
1605 static void
1606 reset_channels(void)
1607 {
1608
1609         if (rtp_channels_hash) {
1610                 /* Remove the channels from the main window if there are there */
1611                 g_hash_table_foreach( rtp_channels_hash, (GHFunc)remove_channel_to_window, NULL);
1612
1613
1614                 /* destroy the rtp channels hash table */
1615                 g_hash_table_destroy(rtp_channels_hash);
1616                 rtp_channels_hash = NULL;
1617         }
1618
1619         if (rtp_channels) {
1620                 reset_rtp_channels();
1621         }
1622 }
1623
1624 /****************************************************************************/
1625 void
1626 reset_rtp_player(void)
1627 {
1628         /* Destroy the rtp channels */
1629         reset_channels();
1630
1631         /* destroy the rtp streams hash table */
1632         if (rtp_streams_hash) {
1633                 g_hash_table_destroy(rtp_streams_hash);
1634                 rtp_streams_hash = NULL;
1635         }
1636
1637         /* destroy the rtp streams list */
1638         if (rtp_streams_list) {
1639                 g_list_free (rtp_streams_list);
1640                 rtp_streams_list = NULL;
1641         }
1642
1643 }
1644
1645 /****************************************************************************/
1646 static void
1647 decode_streams(void) 
1648 {       
1649         guint statusbar_context;
1650         guint counter;
1651
1652         /* set the sensitive state of the buttons (decode, play, pause, stop) */
1653         bt_state(FALSE, FALSE, FALSE, FALSE);
1654
1655         reset_channels();
1656
1657         progress_bar = gtk_progress_bar_new();
1658         WIDGET_SET_SIZE(progress_bar, 100, -1);
1659         gtk_box_pack_start(GTK_BOX (stat_hbox), progress_bar, FALSE, FALSE, 2);
1660         gtk_widget_show(progress_bar);
1661         statusbar_context = gtk_statusbar_get_context_id((GtkStatusbar *) info_bar, "main");
1662         gtk_statusbar_push((GtkStatusbar *) info_bar, statusbar_context, "  Decoding RTP packets...");
1663
1664         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(info_bar), FALSE);
1665
1666         /* reset the number of packet to be decoded, this is used for the progress bar */
1667         total_packets = 0;
1668         /* reset the Progress Bar count */
1669         progbar_count = 0;
1670
1671         /* Mark the RTP streams to be played using the selected VoipCalls*/
1672         if (rtp_streams_hash)
1673                 g_hash_table_foreach( rtp_streams_hash, (GHFunc)mark_rtp_stream_to_play, NULL);
1674
1675         /* Decode the RTP streams and add them to the RTP channels to be played */
1676         g_list_foreach( rtp_streams_list, (GFunc)decode_rtp_stream, NULL);
1677
1678         /* reset the number of frames to be displayed, this is used for the progress bar */
1679         total_frames = 0;
1680         /* Count the frames in all the RTP channels */
1681         if (rtp_channels_hash)
1682                 g_hash_table_foreach( rtp_channels_hash, (GHFunc)count_channel_frames, NULL);   
1683
1684         /* reset the Progress Bar count again for the progress of creating the channels view */
1685         progbar_count = 0;
1686         gtk_statusbar_pop((GtkStatusbar *) info_bar, statusbar_context);
1687         gtk_statusbar_push((GtkStatusbar *) info_bar, statusbar_context, "  Creating channels view...");
1688
1689         /* Display the RTP channels in the window */
1690         counter = 0;
1691         if (rtp_channels_hash)
1692                 g_hash_table_foreach( rtp_channels_hash, (GHFunc)add_channel_to_window, &counter);      
1693
1694         /* Resize the main scroll window to display no more than 5 channels, otherwise the scroll bar need to be used */
1695         WIDGET_SET_SIZE(main_scrolled_window, CHANNEL_WIDTH, 
1696                 min(counter, 5) * (CHANNEL_HEIGHT+60));
1697
1698         gtk_widget_show_all(main_scrolled_window);
1699
1700         gtk_widget_destroy(progress_bar);
1701         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(info_bar), TRUE);
1702         gtk_statusbar_pop((GtkStatusbar *) info_bar, statusbar_context);
1703
1704         /* blank the status label */
1705         gtk_statusbar_pop((GtkStatusbar *) info_bar, statusbar_context);
1706
1707         /* set the sensitive state of the buttons (decode, play, pause, stop) */
1708         bt_state(TRUE, FALSE, FALSE, FALSE);
1709
1710         /* get the static jitter buffer from the spinner gui */
1711         new_jitter_buff = (int) gtk_spin_button_get_value((GtkSpinButton * )jitter_spinner);
1712
1713 }
1714
1715 /****************************************************************************/
1716 static void 
1717 on_bt_decode_clicked(GtkButton *button _U_, gpointer user_data _U_)
1718 {
1719         decode_streams();
1720 }
1721
1722 /****************************************************************************/
1723 static void 
1724 on_bt_play_clicked(GtkButton *button _U_, gpointer user_data _U_)
1725 {
1726         play_channels();
1727 }
1728
1729 /****************************************************************************/
1730 static void 
1731 on_bt_pause_clicked(GtkButton *button _U_, gpointer user_data _U_)
1732 {
1733         pause_channels();
1734 }
1735
1736 /****************************************************************************/
1737 static void 
1738 on_bt_stop_clicked(GtkButton *button _U_, gpointer user_data _U_)
1739 {
1740         stop_channels();
1741 }
1742
1743 /****************************************************************************/
1744 static void
1745 rtp_player_on_destroy(GtkObject *object _U_, gpointer user_data _U_)
1746 {
1747         /* Stop the channels if necesary */
1748         if(rtp_channels && (!rtp_channels->stop)){
1749                 stop_channels();
1750         }
1751
1752         /* Destroy the rtp channels */
1753         reset_channels();
1754
1755         g_free(rtp_channels);
1756         rtp_channels = NULL;
1757
1758         initialized = FALSE;
1759
1760         gtk_widget_destroy(rtp_player_dlg_w);
1761         main_scrolled_window = NULL;
1762         rtp_player_dlg_w = NULL;
1763 }
1764
1765 /****************************************************************************/
1766 static void
1767 jitter_spinner_value_changed (GtkSpinButton *spinner, gpointer user_data _U_)
1768 {
1769         /* set the sensitive state of the buttons (decode, play, pause, stop) */
1770         bt_state(TRUE, TRUE, FALSE, FALSE);
1771 }
1772
1773 /****************************************************************************/
1774 static void
1775 rtp_player_dlg_create(void)
1776 {
1777         GtkWidget *main_vb;
1778         GtkWidget *hbuttonbox;
1779         GtkWidget *h_jitter_buttons_box;
1780         GtkWidget *bt_close;
1781         GtkAdjustment *jitter_spinner_adj;
1782         GtkWidget *label;
1783
1784         GtkTooltips *tooltips = gtk_tooltips_new();
1785
1786         rtp_player_dlg_w=gtk_window_new(GTK_WINDOW_TOPLEVEL);
1787         
1788         gtk_window_set_title(GTK_WINDOW(rtp_player_dlg_w), "Wireshark: RTP Player");
1789         gtk_window_set_position(GTK_WINDOW(rtp_player_dlg_w), GTK_WIN_POS_NONE);
1790
1791         gtk_window_set_default_size(GTK_WINDOW(rtp_player_dlg_w), 400, 50);
1792
1793         main_vb = gtk_vbox_new (FALSE, 0);
1794         gtk_container_add(GTK_CONTAINER(rtp_player_dlg_w), main_vb);
1795         gtk_container_set_border_width (GTK_CONTAINER (main_vb), 2);
1796         
1797         main_scrolled_window=gtk_scrolled_window_new(NULL, NULL);
1798         gtk_container_set_border_width (GTK_CONTAINER (main_scrolled_window), 4);
1799         WIDGET_SET_SIZE(main_scrolled_window, CHANNEL_WIDTH, 0);
1800
1801         gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (main_scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1802         gtk_container_add(GTK_CONTAINER(main_vb), main_scrolled_window);
1803
1804         channels_vb = gtk_vbox_new (FALSE, 0);
1805         gtk_container_set_border_width (GTK_CONTAINER (channels_vb), 2);
1806         gtk_scrolled_window_add_with_viewport((GtkScrolledWindow *) main_scrolled_window, channels_vb);
1807
1808         h_jitter_buttons_box = gtk_hbox_new (FALSE, 0);
1809         gtk_container_set_border_width (GTK_CONTAINER (h_jitter_buttons_box), 10);
1810         gtk_box_pack_start (GTK_BOX(main_vb), h_jitter_buttons_box, FALSE, FALSE, 0);
1811         label = gtk_label_new("Jitter buffer [ms] ");
1812         gtk_box_pack_start(GTK_BOX(h_jitter_buttons_box), label, FALSE, FALSE, 0);
1813         
1814         jitter_spinner_adj = (GtkAdjustment *) gtk_adjustment_new (50, 0, 500, 5, 10, 10);
1815         jitter_spinner = gtk_spin_button_new (jitter_spinner_adj, 5, 0);
1816         gtk_box_pack_start(GTK_BOX(h_jitter_buttons_box), jitter_spinner, FALSE, FALSE, 0);
1817         gtk_tooltips_set_tip (tooltips, jitter_spinner, "The simulated jitter buffer in [ms]", NULL);
1818         SIGNAL_CONNECT(GTK_OBJECT (jitter_spinner_adj), "value_changed", (GtkSignalFunc) jitter_spinner_value_changed, NULL);
1819
1820         /* button row */
1821         hbuttonbox = gtk_hbutton_box_new ();
1822         gtk_box_pack_start (GTK_BOX (h_jitter_buttons_box), hbuttonbox, TRUE, TRUE, 0);
1823         gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox), GTK_BUTTONBOX_SPREAD);
1824         gtk_button_box_set_spacing (GTK_BUTTON_BOX (hbuttonbox), 30);
1825
1826         bt_decode = gtk_button_new_with_label("Decode");
1827         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_decode);
1828         SIGNAL_CONNECT(bt_decode, "clicked", on_bt_decode_clicked, NULL);
1829         gtk_tooltips_set_tip (tooltips, bt_decode, "Decode the RTP stream(s)", NULL);
1830
1831         bt_play = gtk_button_new_with_label("Play");
1832         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_play);
1833         SIGNAL_CONNECT(bt_play, "clicked", on_bt_play_clicked, NULL);
1834         gtk_tooltips_set_tip (tooltips, bt_play, "Play the RTP channel(s)", NULL);
1835
1836         bt_pause = gtk_button_new_with_label("Pause");
1837         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_pause);
1838         SIGNAL_CONNECT(bt_pause, "clicked", on_bt_pause_clicked, NULL);
1839         gtk_tooltips_set_tip (tooltips, bt_pause, "Pause the RTP channel(s)", NULL);
1840
1841         bt_stop = gtk_button_new_with_label("Stop");
1842         gtk_container_add(GTK_CONTAINER(hbuttonbox), bt_stop);
1843         SIGNAL_CONNECT(bt_stop, "clicked", on_bt_stop_clicked, NULL);
1844         gtk_tooltips_set_tip (tooltips, bt_stop, "Stop the RTP channel(s)", NULL);
1845
1846         bt_close = BUTTON_NEW_FROM_STOCK(GTK_STOCK_CLOSE);
1847         gtk_container_add (GTK_CONTAINER (hbuttonbox), bt_close);
1848         GTK_WIDGET_SET_FLAGS(bt_close, GTK_CAN_DEFAULT);
1849         gtk_tooltips_set_tip (tooltips, bt_close, "Close this dialog", NULL);
1850
1851         SIGNAL_CONNECT(bt_close, "clicked", rtp_player_on_destroy, NULL);
1852         SIGNAL_CONNECT(rtp_player_dlg_w, "destroy", rtp_player_on_destroy, NULL);
1853
1854         /* button row */
1855         hbuttonbox = gtk_hbutton_box_new ();
1856
1857         /* Filter/status hbox */
1858         stat_hbox = gtk_hbox_new(FALSE, 1);
1859         gtk_container_border_width(GTK_CONTAINER(stat_hbox), 0);
1860
1861         /* statusbar */
1862         info_bar = gtk_statusbar_new();
1863         gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(info_bar), TRUE);
1864
1865         gtk_box_pack_start(GTK_BOX(stat_hbox), info_bar, TRUE, TRUE, 0);
1866
1867         /* statusbar hbox */
1868         gtk_box_pack_start(GTK_BOX(main_vb), stat_hbox, FALSE, TRUE, 0);
1869
1870         /* set the sensitive state of the buttons (decode, play, pause, stop) */
1871         bt_state(TRUE, FALSE, FALSE, FALSE);
1872         
1873         gtk_widget_show_all(rtp_player_dlg_w);
1874
1875         /* Force gtk to redraw the window before starting decoding the packet */
1876         while (g_main_context_iteration(NULL, FALSE));
1877 }
1878
1879 /****************************************************************************/
1880 void
1881 rtp_player_init(voip_calls_tapinfo_t *voip_calls_tap)
1882 {
1883         PaError err;
1884         GtkWidget *dialog;
1885
1886         if (initialized) return;
1887         initialized = TRUE;
1888
1889         voip_calls = voip_calls_tap;
1890         err = Pa_Initialize();
1891         if( err != paNoError ) {
1892                 dialog = gtk_message_dialog_new ((GtkWindow *) rtp_player_dlg_w,
1893                                   GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
1894                                   "Can not Initialize the PortAudio Library.\n Error: %s", Pa_GetErrorText( err ));
1895                 gtk_dialog_run (GTK_DIALOG (dialog));
1896                 gtk_widget_destroy (dialog);
1897                 initialized = FALSE;
1898                 return;
1899         }
1900
1901         new_jitter_buff = -1;
1902
1903 #ifdef HAVE_G729_G723
1904         /* Initialize the G729 and G723 decoders */
1905         initG723();
1906         initG729();
1907 #endif /* HAVE_G729_G723 */
1908
1909         if (!rtp_channels) {
1910                 rtp_channels = g_malloc(sizeof(rtp_play_channles_t));
1911         }
1912
1913         reset_rtp_channels();
1914
1915         /* create the dialog window */
1916         rtp_player_dlg_create();
1917         
1918 }
1919
1920 #endif /* GTK_MAJOR_VERSION >= 2 */
1921  
1922 #endif /* HAVE_LIBPORTAUDIO */