5 * Copyright 2006, Alejandro Vaquero <alejandrovaquero@yahoo.com>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1999 Gerald Combs
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.
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.
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.
27 * Here is a summary on how this works:
28 * - The VoipCalls will call add_rtp_packet() every time there is an RTP
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
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
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)
58 #ifdef HAVE_LIBPORTAUDIO
60 #include <epan/stats_tree.h>
61 #include <epan/addr_resolv.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"
72 #include "graph_analysis.h"
73 #include "voip_calls_dlg.h"
74 #include "voip_calls.h"
75 #include "gtkglobals.h"
78 #include <epan/dissectors/packet-rtp.h>
80 #include "rtp_player.h"
81 #include "codecs/G711a/G711adecode.h"
82 #include "codecs/G711u/G711udecode.h"
86 #define min(a,b) (((a)<(b))?(a):(b))
89 #define max(a,b) (((a)>(b))?(a):(b))
92 /*define this symbol to compile with G729 and G723 codecs*/
93 /*#define HAVE_G729_G723 1*/
96 #include "codecs/G729/G729decode.h"
97 #include "codecs/G723/G723decode.h"
98 #endif /* HAVE_G729_G723 */
100 static gboolean initialized = FALSE;
102 voip_calls_tapinfo_t *voip_calls = NULL;
104 /* Hash table with all the RTP streams */
105 static GHashTable* rtp_streams_hash = NULL;
107 /* List with all the RTP streams (this is used to decode them as it is sorted)*/
108 static GList* rtp_streams_list = NULL;
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;
123 static guint32 total_packets;
124 static guint32 total_frames;
125 static guint32 progbar_count;
127 static int new_jitter_buff;
129 /* a hash table with the RTP streams to play per audio channel */
130 static GHashTable *rtp_channels_hash = NULL;
132 /* Port Audio staff */
133 #define SAMPLE_RATE (8000)
134 #define NUM_CHANNELS (2)
136 #define PA_SAMPLE_TYPE paInt16
137 typedef gint16 SAMPLE;
138 #define SAMPLE_SILENCE (0)
139 #define FRAMES_PER_BUFFER (512)
141 typedef struct _sample_t {
147 #define S_DROP_BY_JITT 1
148 #define S_WRONG_SEQ 2
150 /* Display channels constants */
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
159 PortAudioStream *pa_stream;
160 #else /* PORTAUDIO_API_1 */
162 #endif /* PORTAUDIO_API_1 */
164 /* TODO: The RTP Player it is only supported for GTK >=2 */
165 #if GTK_MAJOR_VERSION >= 2
167 /* defines a RTP stream */
168 typedef struct _rtp_stream_info {
174 guint32 first_frame_number; /* first RTP frame for the stream */
175 double start_time; /* RTP stream start time in ms */
178 GList* rtp_packets_list; /* List of RTP packets in the stream */
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 */
191 guint32 drop_by_jitter_buff;
193 guint32 max_frame_index;
195 GtkWidget *separator;
196 GtkWidget *scroll_window;
197 GtkWidget *draw_area;
199 GtkAdjustment *h_scrollbar_adjustment;
200 GdkPixbuf* cursor_pixbuf;
202 PaTimestamp cursor_prev;
203 #else /* PORTAUDIO_API_1 */
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 */
210 } rtp_channel_info_t;
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;
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];
225 guint32 max_frame_index;
229 gint32 pause_duration;
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;
238 /* The two RTP channles to play */
239 static rtp_play_channles_t *rtp_channels = NULL;
242 /****************************************************************************/
244 rtp_key_destroy(gpointer key)
250 /****************************************************************************/
252 rtp_channel_value_destroy(gpointer rci_arg)
254 rtp_channel_info_t *rci = rci_arg;
256 g_array_free(rci->samples, TRUE);
261 /****************************************************************************/
263 rtp_stream_value_destroy(gpointer rsi_arg)
265 rtp_stream_info_t *rsi = rsi_arg;
266 GList* rtp_packets_list;
269 rtp_packets_list = g_list_first(rsi->rtp_packets_list);
270 while (rtp_packets_list)
272 rp = rtp_packets_list->data;
275 g_free(rp->payload_data);
279 rtp_packets_list = g_list_next(rtp_packets_list);
285 /****************************************************************************/
287 set_sensitive_check_bt(gchar *key _U_ , rtp_channel_info_t *rci, guint *stop _U_ )
289 gtk_widget_set_sensitive(rci->check_bt, !(*stop));
292 /****************************************************************************/
294 bt_state(gboolean decode, gboolean play, gboolean pause, gboolean stop)
296 gboolean new_jitter_value = FALSE;
297 gboolean false_val = FALSE;
299 gtk_widget_set_sensitive(bt_decode, decode);
300 gtk_widget_set_sensitive(jitter_spinner, decode);
302 if (new_jitter_buff != (int) gtk_spin_button_get_value((GtkSpinButton * )jitter_spinner)) {
303 new_jitter_value = TRUE;
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);
310 gtk_widget_set_sensitive(bt_play, FALSE);
313 if (!new_jitter_value) {
314 gtk_widget_set_sensitive(bt_pause, pause);
315 gtk_widget_set_sensitive(bt_stop, stop);
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);
321 gtk_widget_set_sensitive(bt_pause, FALSE);
322 gtk_widget_set_sensitive(bt_stop, FALSE);
324 if (rtp_channels_hash)
325 g_hash_table_foreach( rtp_channels_hash, (GHFunc)set_sensitive_check_bt, &false_val);
329 /****************************************************************************/
331 add_rtp_packet(const struct _rtp_info *rtp_info, packet_info *pinfo)
333 rtp_stream_info_t *stream_info = NULL;
334 rtp_packet_t *new_rtp_packet;
335 GString *key_str = NULL;
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);
341 /* Create a hash key to lookup in the RTP streams hash table
342 * uses: src_ip:src_port dst_ip:dst_port ssrc
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 );
349 /* lookup for this rtp packet in the stream hash table*/
350 stream_info = g_hash_table_lookup( rtp_streams_hash, key_str->str);
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;
367 g_hash_table_insert(rtp_streams_hash, g_strdup(key_str->str), stream_info);
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);
373 /* increment the number of packets in this stream, this is used for the progress bar and statistics*/
374 stream_info->num_packets++;
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));
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);
387 new_rtp_packet->payload_data = NULL;
390 stream_info->rtp_packets_list = g_list_append(stream_info->rtp_packets_list, new_rtp_packet);
392 g_string_free(key_str, TRUE);
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.
400 mark_rtp_stream_to_play(gchar *key _U_ , rtp_stream_info_t *rsi, gpointer ptr _U_)
403 graph_analysis_item_t *graph_item;
404 GList* voip_calls_list;
405 voip_calls_info_t *tmp_voip_call;
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
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);
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)
423 tmp_voip_call = voip_calls_list->data;
424 if ( (tmp_voip_call->call_num == rsi->call_num) && (tmp_voip_call->selected == TRUE) ) {
426 total_packets += rsi->num_packets;
429 voip_calls_list = g_list_next(voip_calls_list);
433 graph_list = g_list_next(graph_list);
438 /****************************************************************************/
439 /* Decode a RTP packet
440 * Return the number of decoded bytes
443 decode_rtp_packet(rtp_packet_t *rp, rtp_channel_info_t *rci, SAMPLE **out_buff)
445 unsigned int payload_type;
446 SAMPLE *tmp_buff = NULL;
447 int decoded_bytes = 0;
449 if ((rp->payload_data == NULL) || (rp->info->info_payload_len == 0) ) {
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);
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);
465 #ifdef HAVE_G729_G723
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);
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 */
480 decodeG723(rp->payload_data, rp->info->info_payload_len,
481 tmp_buff, &decoded_bytes);
483 #endif /* HAVE_G729_G723 */
486 *out_buff = tmp_buff;
487 return decoded_bytes;
490 /****************************************************************************/
492 update_progress_bar(gfloat percentage)
495 gtk_progress_bar_update(GTK_PROGRESS_BAR(progress_bar), percentage);
497 /* Force gtk to redraw the window before starting decoding the packet */
498 while (gtk_events_pending())
499 gtk_main_iteration();
502 /****************************************************************************/
503 /* Decode the RTP streams and add them to the RTP channels struct
506 decode_rtp_stream(rtp_stream_info_t *rsi, gpointer ptr _U_)
508 GString *key_str = NULL;
509 rtp_channel_info_t *rci;
510 gboolean first = TRUE;
511 GList* rtp_packets_list;
516 double rtp_time_prev;
518 double arrive_time_prev;
520 double start_rtp_time;
524 double total_time_prev;
525 gint32 silence_frames;
532 int decoded_bytes_prev;
534 SAMPLE *out_buff = NULL;
538 guint32 start_timestamp;
540 guint32 progbar_nextstep;
545 silence.status = S_NORMAL;
547 /* skip it if we are not going to play it */
548 if (rsi->play == FALSE) {
552 /* get the static jitter buffer from the spinner gui */
553 jitter_buff = (int) gtk_spin_button_get_value((GtkSpinButton * )jitter_spinner);
555 /* Create a hash key to lookup in the RTP channels hash
556 * uses: src_ip:src_port dst_ip:dst_port call_num
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 );
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);
568 /* lookup for this stream in the channel hash table */
569 rci = g_hash_table_lookup( rtp_channels_hash, key_str->str);
571 /* ..if it is not in the hash, create an entry */
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;
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;
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);
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);
600 rci->num_packets += rsi->num_packets;
603 /* decode the RTP stream */
607 decoded_bytes_prev = 0;
609 arrive_time = start_time = 0;
610 arrive_time_prev = 0;
621 /* we update the progress bar 100 times */
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;
631 rtp_packets_list = g_list_first(rsi->rtp_packets_list);
632 while (rtp_packets_list)
635 if (progbar_count >= progbar_nextstep) {
636 g_assert(total_packets > 0);
638 progbar_val = (gfloat) progbar_count / total_packets;
640 update_progress_bar(progbar_val);
642 progbar_nextstep += progbar_quantum;
646 rp = rtp_packets_list->data;
648 start_timestamp = rp->info->info_timestamp; /* defined start_timestmp to avoid overflow in timestamp. TODO: handle the timestamp correctly */
650 rtp_time_prev = start_rtp_time;
652 seq = rp->info->info_seq_num - 1;
655 decoded_bytes = decode_rtp_packet(rp, rci, &out_buff);
656 if (decoded_bytes == 0) {
657 seq = rp->info->info_seq_num;
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;
663 if (rp->info->info_seq_num != seq+1){
665 status = S_WRONG_SEQ;
667 seq = rp->info->info_seq_num;
669 diff = arrive_time - rtp_time;
671 delay = diff - prev_diff;
673 if (delay<0) delay = -delay;
675 if (diff<0) diff = -diff;
677 total_time = (double)rp->arrive_offset/1000;
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);
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;
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");
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);
699 /* only mark the fisrt in the silence that has the previos problem (S_DROP_BY_JITT or S_WRONG_SEQ ) */
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 */
706 start_time = (double)rp->arrive_offset/1000;
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);
716 /* only mark the fisrt in the silence that has the previos problem (S_DROP_BY_JITT or S_WRONG_SEQ ) */
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);
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;
736 rtp_packets_list = g_list_next (rtp_packets_list);
739 rci->max_frame_index = rci->samples->len;
740 rci->end_time = rci->start_time + ((double)rci->samples->len/SAMPLE_RATE)*1000;
742 g_string_free(key_str, TRUE);
745 /****************************************************************************/
747 h_scrollbar_changed(GtkWidget *widget _U_, gpointer user_data)
749 rtp_channel_info_t *rci = (rtp_channel_info_t *)user_data;
750 rci->cursor_catch = TRUE;
754 static gboolean draw_cursors(gpointer data);
756 /****************************************************************************/
763 /* we should never be here if we are already in STOP */
764 if(rtp_channels->stop){
768 rtp_channels->stop = TRUE;
769 /* force a draw_cursor to stop it */
772 err = Pa_StopStream(pa_stream);
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);
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);
792 pa_stream = NULL; /* to catch errors better */
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;
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;
808 /* set the sensitive state of the buttons (decode, play, pause, stop) */
809 bt_state(TRUE, TRUE, FALSE, FALSE);
813 /****************************************************************************/
814 /* Draw a cursor in a channel graph
817 draw_channel_cursor(rtp_channel_info_t *rci, guint32 start_index)
821 #else /* PORTAUDIO_API_1 */
823 #endif /* 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 */
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) ) {
841 /* If only this channel finished, then return */
842 if (index > rci->max_frame_index) {
846 /* draw the previous saved pixbuf line */
847 if (rci->cursor_pixbuf && (rci->cursor_prev>=0)) {
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);
851 gdk_draw_drawable(rci->draw_area->window,
852 rci->draw_area->style->fg_gc[GTK_WIDGET_STATE(rci->draw_area)],
854 (int) (rci->cursor_prev/MULT), 0,
855 (int) (rci->cursor_prev/MULT), 0,
856 1, rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
858 g_object_unref(rci->cursor_pixbuf);
859 rci->cursor_pixbuf = NULL;
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);
865 gdk_draw_line(rci->pixmap, rci->draw_area->style->black_gc,
869 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
871 gdk_draw_drawable(rci->draw_area->window,
872 rci->draw_area->style->fg_gc[GTK_WIDGET_STATE(rci->draw_area)],
874 (int) (index/MULT), 0,
875 (int) (index/MULT), 0,
876 1, rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
879 /* Disconnect the scroll bar "value" signal to not be called */
880 SIGNAL_DISCONNECT_BY_FUNC(rci->h_scrollbar_adjustment, h_scrollbar_changed, rci);
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);
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;
898 rci->h_scrollbar_adjustment->value = index/MULT - rci->h_scrollbar_adjustment->page_size/2;
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);
913 /* Connect back the "value" scroll signal */
914 SIGNAL_CONNECT(rci->h_scrollbar_adjustment, "value_changed", h_scrollbar_changed, rci);
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;
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;
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;
933 rci->h_scrollbar_adjustment->value = index/MULT - rci->h_scrollbar_adjustment->page_size/2;
936 /* gtk_adjustment_value_changed(rci->h_scrollbar_adjustment);
938 rci->cursor_prev = index;
941 /****************************************************************************/
942 /* Move and draw the cursor in the graph
945 draw_cursors(gpointer data)
947 if (!rtp_channels) return FALSE;
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]);
953 if ((rtp_channels->stop) || (rtp_channels->pause)) return FALSE;
958 /****************************************************************************/
960 init_rtp_channels_vals(void)
962 rtp_play_channles_t *rpci = rtp_channels;
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;
978 /* if the two channels are to be played, then we need to sync both based on the start/end time of each one */
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));
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);
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);
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;
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;
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().
1009 static int paCallback( void *inputBuffer, void *outputBuffer,
1010 unsigned long framesPerBuffer,
1011 PaTimestamp outTime, void *userData)
1013 #else /* PORTAUDIO_API_1 */
1014 static int paCallback( void *inputBuffer, void *outputBuffer,
1015 unsigned long framesPerBuffer,
1016 const PaStreamCallbackTimeInfo* outTime,
1017 PaStreamCallbackFlags statusFlags,
1020 /* (void) statusFlags;*/
1021 #endif /* PORTAUDIO_API_1 */
1022 rtp_play_channles_t *rpci = (rtp_play_channles_t*)userData;
1023 SAMPLE *wptr = (SAMPLE*)outputBuffer;
1027 unsigned int framesLeft;
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++ ) {
1036 rtp_channels->pause_duration += framesPerBuffer;
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 */
1047 /* set the values if this is the first time */
1048 if (rpci->max_frame_index == 0) {
1049 init_rtp_channels_vals();
1053 framesLeft = rpci->max_frame_index - rpci->frame_index;
1055 (void) inputBuffer; /* Prevent unused variable warnings. */
1058 if( framesLeft < framesPerBuffer )
1060 framesToPlay = framesLeft;
1065 framesToPlay = framesPerBuffer;
1069 for( i=0; i<(unsigned int)framesToPlay; i++ )
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;
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;
1085 for( ; i<framesPerBuffer; i++ )
1090 rpci->frame_index += framesToPlay;
1095 /****************************************************************************/
1097 on_bt_check_clicked(GtkButton *button _U_, gpointer user_data _U_)
1099 rtp_channel_info_t *rci = user_data;
1101 if (rci->selected) {
1102 if (rtp_channels->rci[0] == rci) {
1103 rtp_channels->rci[0] = NULL;
1104 rtp_channels->channel = 0;
1106 rtp_channels->rci[1] = NULL;
1107 rtp_channels->channel = 1;
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;
1119 rtp_channels->rci[rtp_channels->channel] = rci;
1120 rtp_channels->channel = !(rtp_channels->channel);
1123 rci->selected = !(rci->selected);
1125 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1126 bt_state(TRUE, TRUE, FALSE, FALSE);
1129 /****************************************************************************/
1130 static void channel_draw(rtp_channel_info_t* rci)
1136 PangoLayout *small_layout;
1137 guint32 label_width, label_height;
1138 char label_string[MAX_TIME_LABEL];
1140 guint32 progbar_nextstep;
1141 int progbar_quantum;
1146 GdkColor red_color = {0, 65535, 0, 0};
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],
1154 rci->draw_area->allocation.width,
1155 rci->draw_area->allocation.height);
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"));
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;
1163 gdk_draw_line(rci->pixmap, rci->draw_area->style->black_gc,
1165 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL,
1166 rci->draw_area->allocation.width,
1167 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL);
1169 imax = min(rci->draw_area->allocation.width,(gint)(rci->samples->len/MULT));
1171 /* we update the progress bar 100 times */
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;
1179 red_gc = gdk_gc_new(rci->draw_area->window);
1180 gdk_gc_set_rgb_fg_color(red_gc, &red_color);
1182 for (i=0; i< imax; i++) {
1188 if (progbar_count >= progbar_nextstep) {
1189 g_assert(total_frames > 0);
1191 progbar_val = (gfloat) i / imax;
1193 update_progress_bar(progbar_val);
1195 progbar_nextstep += progbar_quantum;
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;
1205 if (status == S_DROP_BY_JITT) {
1208 gc = rci->draw_area->style->black_gc;
1211 gdk_draw_line(rci->pixmap, gc,
1213 (gint)(( (0x7FFF+min) * (rci->draw_area->allocation.height-HEIGHT_TIME_LABEL))/0xFFFF),
1215 (gint)(( (0x7FFF+max) * (rci->draw_area->allocation.height-HEIGHT_TIME_LABEL))/0xFFFF));
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,
1221 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL,
1223 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL+4);
1225 g_snprintf(label_string, MAX_TIME_LABEL, "%.0f", floor(rci->start_time/1000) + i*MULT/SAMPLE_RATE);
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,
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,
1238 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL,
1240 rci->draw_area->allocation.height-HEIGHT_TIME_LABEL+2);
1249 /****************************************************************************/
1250 static gint expose_event_channels(GtkWidget *widget, GdkEventExpose *event)
1252 rtp_channel_info_t *rci;
1254 rci=(rtp_channel_info_t *)OBJECT_GET_DATA(widget, "rtp_channel_info_t");
1259 if (GDK_IS_DRAWABLE(widget->window))
1260 gdk_draw_drawable(widget->window,
1261 widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
1263 event->area.x, event->area.y,
1264 event->area.x, event->area.y,
1265 event->area.width, event->area.height);
1270 /****************************************************************************/
1272 configure_event_channels(GtkWidget *widget, GdkEventConfigure *event _U_)
1274 rtp_channel_info_t *rci;
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
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}
1295 rci=(rtp_channel_info_t *)OBJECT_GET_DATA(widget, "rtp_channel_info_t");
1301 g_object_unref(rci->pixmap);
1305 rci->pixmap = gdk_pixmap_new(widget->window,
1306 widget->allocation.width,
1307 widget->allocation.height,
1310 if ( GDK_IS_DRAWABLE(rci->pixmap) )
1311 gdk_draw_rectangle(rci->pixmap,
1312 widget->style->white_gc,
1315 widget->allocation.width,
1316 widget->allocation.height);
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]);
1329 /****************************************************************************/
1331 button_press_event_channel(GtkWidget *widget, GdkEventButton *event _U_)
1333 rtp_channel_info_t *rci;
1337 rci=(rtp_channel_info_t *)OBJECT_GET_DATA(widget, "rtp_channel_info_t");
1342 if (!rci->selected) {
1344 /* only select a new channels if we are in STOP */
1345 if (!rtp_channels->stop) return 0;
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;
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);
1361 rtp_channels->rci[rtp_channels->channel] = rci;
1362 rtp_channels->channel = !(rtp_channels->channel);
1363 rci->selected = TRUE;
1365 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1366 bt_state(TRUE, TRUE, FALSE, FALSE);
1369 if (rci == rtp_channels->rci[0]) {
1375 rci->frame_index = (unsigned int) (event->x * MULT);
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;
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();
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;
1392 rtp_channels->rci[!this_channel]->frame_index = rtp_channels->frame_index - rtp_channels->start_index[!this_channel];
1395 init_rtp_channels_vals();
1398 rtp_channels->out_diff_time = 0;
1400 rci->cursor_catch = TRUE;
1402 /* redraw the cusrsor */
1408 /****************************************************************************/
1410 add_channel_to_window(gchar *key _U_ , rtp_channel_info_t *rci, guint *counter _U_ )
1412 GString *label = NULL;
1413 GtkWidget *viewport;
1416 /* create the channel draw area */
1417 rci->draw_area=gtk_drawing_area_new();
1419 rci->scroll_window=gtk_scrolled_window_new(NULL, NULL);
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));
1425 gtk_widget_set_size_request(rci->draw_area, (gint)(rci->samples->len/MULT), CHANNEL_HEIGHT);
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);
1437 gtk_box_pack_start(GTK_BOX (channels_vb), rci->scroll_window, FALSE, FALSE, 0);
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);
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);
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);
1456 /* Create the Separator if it is not the last one */
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);
1463 SIGNAL_CONNECT(rci->check_bt, "clicked", on_bt_check_clicked, rci);
1465 g_string_free(label, TRUE);
1468 /****************************************************************************/
1470 count_channel_frames(gchar *key _U_ , rtp_channel_info_t *rci, gpointer ptr _U_ )
1472 total_frames += rci->samples->len;
1475 /****************************************************************************/
1482 /* we should never be here if we are in PLAY and !PAUSE */
1483 if(!rtp_channels->stop && !rtp_channels->pause){
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);
1493 /* if not PAUSE, then start to PLAY */
1496 err = Pa_OpenStream(
1498 paNoDevice, /* default input device */
1500 PA_SAMPLE_TYPE, /* 16 bit Integer input */
1502 Pa_GetDefaultOutputDeviceID(),
1503 NUM_CHANNELS, /* Stereo output */
1504 PA_SAMPLE_TYPE, /* 16 bit Integer output */
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 */
1512 #else /* PORTAUDIO_API_1 */
1513 err = Pa_OpenDefaultStream(
1522 #endif /* PORTAUDIO_API_1 */
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);
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);
1542 #if !PORTAUDIO_API_1
1543 rtp_channels->pa_start_time = Pa_GetStreamTime(pa_stream);
1544 #endif /* PORTAUDIO_API_1 */
1546 rtp_channels->stop = FALSE;
1548 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1549 bt_state(FALSE, FALSE, TRUE, TRUE);
1552 /* Draw the cursor in the graph */
1553 g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, MULT*1000/SAMPLE_RATE, draw_cursors, NULL, NULL);
1557 /****************************************************************************/
1559 pause_channels(void)
1561 rtp_channels->pause = !(rtp_channels->pause);
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);
1569 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1570 bt_state(FALSE, TRUE, FALSE, TRUE);
1573 /****************************************************************************/
1575 reset_rtp_channels(void)
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;
1592 /****************************************************************************/
1594 remove_channel_to_window(gchar *key _U_ , rtp_channel_info_t *rci, gpointer ptr _U_ )
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);
1601 gtk_widget_destroy(rci->separator);
1604 /****************************************************************************/
1606 reset_channels(void)
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);
1614 /* destroy the rtp channels hash table */
1615 g_hash_table_destroy(rtp_channels_hash);
1616 rtp_channels_hash = NULL;
1620 reset_rtp_channels();
1624 /****************************************************************************/
1626 reset_rtp_player(void)
1628 /* Destroy the rtp channels */
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;
1637 /* destroy the rtp streams list */
1638 if (rtp_streams_list) {
1639 g_list_free (rtp_streams_list);
1640 rtp_streams_list = NULL;
1645 /****************************************************************************/
1647 decode_streams(void)
1649 guint statusbar_context;
1652 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1653 bt_state(FALSE, FALSE, FALSE, FALSE);
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...");
1664 gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(info_bar), FALSE);
1666 /* reset the number of packet to be decoded, this is used for the progress bar */
1668 /* reset the Progress Bar count */
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);
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);
1678 /* reset the number of frames to be displayed, this is used for the progress bar */
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);
1684 /* reset the Progress Bar count again for the progress of creating the channels view */
1686 gtk_statusbar_pop((GtkStatusbar *) info_bar, statusbar_context);
1687 gtk_statusbar_push((GtkStatusbar *) info_bar, statusbar_context, " Creating channels view...");
1689 /* Display the RTP channels in the window */
1691 if (rtp_channels_hash)
1692 g_hash_table_foreach( rtp_channels_hash, (GHFunc)add_channel_to_window, &counter);
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));
1698 gtk_widget_show_all(main_scrolled_window);
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);
1704 /* blank the status label */
1705 gtk_statusbar_pop((GtkStatusbar *) info_bar, statusbar_context);
1707 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1708 bt_state(TRUE, FALSE, FALSE, FALSE);
1710 /* get the static jitter buffer from the spinner gui */
1711 new_jitter_buff = (int) gtk_spin_button_get_value((GtkSpinButton * )jitter_spinner);
1715 /****************************************************************************/
1717 on_bt_decode_clicked(GtkButton *button _U_, gpointer user_data _U_)
1722 /****************************************************************************/
1724 on_bt_play_clicked(GtkButton *button _U_, gpointer user_data _U_)
1729 /****************************************************************************/
1731 on_bt_pause_clicked(GtkButton *button _U_, gpointer user_data _U_)
1736 /****************************************************************************/
1738 on_bt_stop_clicked(GtkButton *button _U_, gpointer user_data _U_)
1743 /****************************************************************************/
1745 rtp_player_on_destroy(GtkObject *object _U_, gpointer user_data _U_)
1747 /* Stop the channels if necesary */
1748 if(rtp_channels && (!rtp_channels->stop)){
1752 /* Destroy the rtp channels */
1755 g_free(rtp_channels);
1756 rtp_channels = NULL;
1758 initialized = FALSE;
1760 gtk_widget_destroy(rtp_player_dlg_w);
1761 main_scrolled_window = NULL;
1762 rtp_player_dlg_w = NULL;
1765 /****************************************************************************/
1767 jitter_spinner_value_changed (GtkSpinButton *spinner, gpointer user_data _U_)
1769 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1770 bt_state(TRUE, TRUE, FALSE, FALSE);
1773 /****************************************************************************/
1775 rtp_player_dlg_create(void)
1778 GtkWidget *hbuttonbox;
1779 GtkWidget *h_jitter_buttons_box;
1780 GtkWidget *bt_close;
1781 GtkAdjustment *jitter_spinner_adj;
1784 GtkTooltips *tooltips = gtk_tooltips_new();
1786 rtp_player_dlg_w=gtk_window_new(GTK_WINDOW_TOPLEVEL);
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);
1791 gtk_window_set_default_size(GTK_WINDOW(rtp_player_dlg_w), 400, 50);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
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);
1851 SIGNAL_CONNECT(bt_close, "clicked", rtp_player_on_destroy, NULL);
1852 SIGNAL_CONNECT(rtp_player_dlg_w, "destroy", rtp_player_on_destroy, NULL);
1855 hbuttonbox = gtk_hbutton_box_new ();
1857 /* Filter/status hbox */
1858 stat_hbox = gtk_hbox_new(FALSE, 1);
1859 gtk_container_border_width(GTK_CONTAINER(stat_hbox), 0);
1862 info_bar = gtk_statusbar_new();
1863 gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(info_bar), TRUE);
1865 gtk_box_pack_start(GTK_BOX(stat_hbox), info_bar, TRUE, TRUE, 0);
1867 /* statusbar hbox */
1868 gtk_box_pack_start(GTK_BOX(main_vb), stat_hbox, FALSE, TRUE, 0);
1870 /* set the sensitive state of the buttons (decode, play, pause, stop) */
1871 bt_state(TRUE, FALSE, FALSE, FALSE);
1873 gtk_widget_show_all(rtp_player_dlg_w);
1875 /* Force gtk to redraw the window before starting decoding the packet */
1876 while (g_main_context_iteration(NULL, FALSE));
1879 /****************************************************************************/
1881 rtp_player_init(voip_calls_tapinfo_t *voip_calls_tap)
1886 if (initialized) return;
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;
1901 new_jitter_buff = -1;
1903 #ifdef HAVE_G729_G723
1904 /* Initialize the G729 and G723 decoders */
1907 #endif /* HAVE_G729_G723 */
1909 if (!rtp_channels) {
1910 rtp_channels = g_malloc(sizeof(rtp_play_channles_t));
1913 reset_rtp_channels();
1915 /* create the dialog window */
1916 rtp_player_dlg_create();
1920 #endif /* GTK_MAJOR_VERSION >= 2 */
1922 #endif /* HAVE_LIBPORTAUDIO */