1 /**************************************************************************
2 **
3 ** sngrep - SIP Messages flow viewer
4 **
5 ** Copyright (C) 2013-2018 Ivan Alonso (Kaian)
6 ** Copyright (C) 2013-2018 Irontec SL. All rights reserved.
7 **
8 ** This program is free software: you can redistribute it and/or modify
9 ** it under the terms of the GNU General Public License as published by
10 ** the Free Software Foundation, either version 3 of the License, or
11 ** (at your option) any later version.
12 **
13 ** This program is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ** GNU General Public License for more details.
17 **
18 ** You should have received a copy of the GNU General Public License
19 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
20 **
21 ****************************************************************************/
22 /**
23 * @file media.c
24 * @author Ivan Alonso [aka Kaian] <kaian@irontec.com>
25 *
26 * @brief Source of functions defined in media.h
27 */
28
29 #include "config.h"
30 #include <string.h>
31 #include <stdlib.h>
32 #include "media.h"
33 #include "rtp.h"
34 #include "util.h"
35
36 sdp_media_t *
media_create(struct sip_msg * msg)37 media_create(struct sip_msg *msg)
38 {
39 sdp_media_t *media;;
40
41 // Allocate memory for this media structure
42 if (!(media = sng_malloc(sizeof(sdp_media_t))))
43 return NULL;
44
45 // Initialize all fields
46 media->msg = msg;
47 media->formats = vector_create(0, 1);
48 vector_set_destroyer(media->formats, vector_generic_destroyer);
49 return media;
50 }
51
52 void
media_destroyer(void * item)53 media_destroyer(void *item)
54 {
55 sdp_media_t *media = (sdp_media_t *) item;
56 if (!item)
57 return;
58 vector_destroy(media->formats);
59 sng_free(media);
60 }
61
62 void
media_set_type(sdp_media_t * media,const char * type)63 media_set_type(sdp_media_t *media, const char *type)
64 {
65 strncpy(media->type, type, MEDIATYPELEN - 1);
66 }
67
68 void
media_set_address(sdp_media_t * media,address_t addr)69 media_set_address(sdp_media_t *media, address_t addr)
70 {
71 media->address = addr;
72 }
73
74 void
media_set_prefered_format(sdp_media_t * media,uint32_t code)75 media_set_prefered_format(sdp_media_t *media, uint32_t code)
76 {
77 media->fmtcode = code;
78 }
79
80 void
media_add_format(sdp_media_t * media,uint32_t code,const char * format)81 media_add_format(sdp_media_t *media, uint32_t code, const char *format)
82 {
83 sdp_media_fmt_t *fmt;
84
85 if (!(fmt = sng_malloc(sizeof(sdp_media_fmt_t))))
86 return;
87
88 fmt->id = code;
89 strcpy(fmt->format, format);
90 vector_append(media->formats, fmt);
91 }
92
93 const char *
media_get_format(sdp_media_t * media,uint32_t code)94 media_get_format(sdp_media_t *media, uint32_t code)
95 {
96 sdp_media_fmt_t *format;
97 vector_iter_t iter;
98 iter = vector_iterator(media->formats);
99 while ((format = vector_iterator_next(&iter))) {
100 if (format->id == code)
101 return format->format;
102 }
103
104 return "Unassigned";
105 }
106
107 const char *
media_get_prefered_format(sdp_media_t * media)108 media_get_prefered_format(sdp_media_t *media)
109 {
110 const char *format;
111
112 // Check if format is standard
113 if ((format = rtp_get_standard_format(media->fmtcode))) {
114 return format;
115 }
116 // Try to get format form SDP payload
117 return media_get_format(media, media->fmtcode);
118 }
119
120 int
media_get_format_code(sdp_media_t * media)121 media_get_format_code(sdp_media_t *media)
122 {
123 return media->fmtcode;
124 }
125
126
127
128