backup
[lcr.git] / h323_chan.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 //                                                                           //
3 // PBX4Linux                                                                 //
4 //                                                                           //
5 //---------------------------------------------------------------------------//
6 // Copyright: Andreas Eversberg                                              //
7 //                                                                           //
8 // H323_chan class                                                           //
9 //                                                                           //
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include "main.h"
16
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20
21 //
22 // constructor
23 //
24 H323_chan::H323_chan(const PString &token, BOOL isEncoding) : PIndirectChannel()
25 {
26         d_token = token;
27 //      d_isEncoding = isEncoding;
28
29         PDEBUG(DEBUG_H323, "H323 channel  constuctor of channel (%scoding)\n", (isEncoding)?"en":"de");
30
31         transfering = FALSE;
32 }
33
34
35 //
36 // destructor
37 //
38 H323_chan::~H323_chan(void)
39 {
40
41         PDEBUG(DEBUG_H323, "H323 channel  destuctor\n");
42 }
43
44
45 //
46 // Closes the
47 //
48 BOOL H323_chan::Close(void)
49 {
50         PDEBUG(DEBUG_H323, "H323 channel  Close\n");
51
52         return TRUE;
53 }
54
55
56 //
57 // IsOpen
58 //
59 BOOL H323_chan::IsOpen(void) const
60 {
61         PDEBUG(DEBUG_H323, "H323 channel  IsOpen\n");
62
63         return TRUE;
64 }
65
66
67 //
68 // Read
69 //
70 BOOL H323_chan::Read(void *buf, PINDEX len)
71 {
72         int nr_words;
73         class H323Port *port;
74         const unsigned char *token_string = d_token;
75         PTime Now;
76         PTimeInterval diff;
77
78         nr_words = len/2;
79
80 //      cout << "H323 channel  Read " << nr_words << " words" << endl;
81         mutex_h323.Wait();
82
83         if (!(port = (class H323Port *)find_port_with_token((char *)token_string)))
84         {
85                 PERROR("H323 channel  Read() cannot find port with token %s\n", token_string);
86                 mutex_h323.Signal();
87                 lastReadCount = 0;
88                 return FALSE;
89         }
90
91         nr_words = port->read_audio((unsigned char *)buf, nr_words, 0);
92
93         mutex_h323.Signal();
94
95         // delay
96         if (!transfering)
97         {
98                 PDEBUG(DEBUG_H323, "H323 channel  Read(%s) sending to h323 the first time\n", token_string);
99                 start = Now;
100                 transfering = TRUE;
101                 elapsed = 0;
102         }
103         diff = Now-start;
104         elapsed += nr_words*125;
105         if (elapsed > (diff.GetMilliSeconds()*1000))
106                 usleep(elapsed - (diff.GetMilliSeconds()*1000));
107
108         lastReadCount = 2 * nr_words;
109
110         return TRUE;
111 }
112
113
114 //
115 // Write
116 //
117 BOOL H323_chan::Write(const void *buf, PINDEX len)
118 {
119         int nr_words;
120         class H323Port *port;
121         const unsigned char *token_string = d_token;
122         PTime Now;
123         PTimeInterval diff;
124         unsigned char *data_temp;
125         unsigned long length_temp;
126         struct message *message;
127
128         nr_words = len / 2;
129
130 //      cout << "H323 channel  Write " << nr_words << " words" << endl;
131         mutex_h323.Wait();
132
133         if (!(port = (class H323Port *)find_port_with_token((char *)token_string)))
134         {
135                 PERROR("H323 channel  Write() cannot find port with token %s\n", token_string);
136                 mutex_h323.Signal();
137                 lastReadCount = 0;
138                 return FALSE;
139         }
140
141         // send data message
142         length_temp = len;
143         data_temp = (unsigned char *)buf;
144         while(length_temp)
145         {
146                 message = message_create(port->p_serial, ACTIVE_EPOINT(port->p_epointlist), PORT_TO_EPOINT, MESSAGE_DATA);
147                 message->param.data.len = (length_temp>sizeof(message->param.data.data))?sizeof(message->param.data.data):length_temp;
148                 memcpy(message->param.data.data, data_temp, message->param.data.len);
149                 message->param.data.compressed = 0;
150 /*              { // testin with law data
151                         int i=0;
152                         while (i<message->param.data.len)
153                         {
154                                 ((unsigned char *)message->param.data.data)[i] = audio_s16_to_law[((signed short*)data_temp)[i] & 0xffff];
155                                 i++;
156                         }
157                 }
158                 message->param.data.len = message->param.data.len/2;
159                 message->param.data.compressed = 1;
160 */
161                 message->param.data.port_type = port->p_type; 
162                 message->param.data.port_id = port->p_serial;
163                 message_put(message);
164                 if (length_temp <= sizeof(message->param.data.data))
165                         break;
166                 data_temp += sizeof(message->param.data.data);
167                 length_temp -= sizeof(message->param.data.data);
168         }
169
170         mutex_h323.Signal();
171
172         // delay
173         if (!transfering)
174         {
175                 PDEBUG(DEBUG_H323, "H323 channel  Write(%s) receiving from h323 the first time\n", token_string);
176                 start = Now;
177                 transfering = TRUE;
178                 elapsed = 0;
179         }
180         diff = Now-start;
181         elapsed += nr_words*125;
182         if (elapsed > (diff.GetMilliSeconds()*1000))
183                 usleep(elapsed - (diff.GetMilliSeconds()*1000));
184
185         lastWriteCount = 2 * nr_words;
186
187         return TRUE;
188 }
189