RDK Documentation (Open Sourced RDK Components)
socket_adaptor.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #ifndef _socket_adaptor_H_
20 #define _socket_adaptor_H_
21 #include <fstream>
22 #include <string>
23 #include <thread>
24 #include <mutex>
25 
26 /**
27  * @addtogroup AUDIO_CAPTURE_MANAGER_API
28  * @{
29  */
30 
31 typedef void (*socket_adaptor_cb_t)(void * data);
32 
34 {
35  public:
36  typedef enum
37  {
38  EXIT = 0,
39  NEW_CALLBACK,
40  CODE_MAX
41  } control_code_t;
42 
43  private:
44  std::string m_path;
45  int m_listen_fd;
46  int m_write_fd;
47  int m_control_pipe[2];
48  unsigned int m_num_connections;
49  std::thread m_thread;
50  std::mutex m_mutex;
51  socket_adaptor_cb_t m_callback;
52  void * m_callback_data;
53 
54  void process_new_connection();
55  void process_control_message(control_code_t message);
56  void stop_listening();
57  void lock();
58  void unlock();
59  void worker_thread();
60 
61  public:
63  ~socket_adaptor();
64 
65  /**
66  * @brief This function makes the audiocapturemgr listen for incoming unix domain connections to the given path.
67  *
68  * @param[in] path binding path.
69  *
70  * @return Returns 0 on success, appropiate error code otherwise.
71  */
72  int start_listening(const std::string &path);
73 
74  /**
75  * @brief This api returns the data path.
76  *
77  * It is the path of unix domain server that ip-out clients must connect to in order to receive real-time audio data.
78  *
79  * @return Returns the data path in string.
80  */
81  std::string& get_path();
82 
83  /**
84  * @brief This api invokes unix write() to write data to the socket.
85  *
86  * @param[in] buffer Data buffer.
87  * @param[in] size Size of the buffer
88  *
89  * @return Returns 0 on success, appropiate errorcode otherwise.
90  */
91  int write_data(const char * buffer, const unsigned int size);
92 
93  /**
94  * @brief This api invokes close() to terminate the current connection.
95  */
96  void terminate_current_connection(); //handy to simulate EOS at the other end.
97 
98  /**
99  * @brief This api returns the number of active connections.
100  *
101  * @return Returns the number of active connections.
102  */
103  unsigned int get_active_connections();
104  void register_data_ready_callback(socket_adaptor_cb_t cb, void *data);
105 };
106 #endif //_socket_adaptor_H_
107 
108 /**
109  * @}
110  */
111 
socket_adaptor::get_active_connections
unsigned int get_active_connections()
This api returns the number of active connections.
Definition: socket_adaptor.cpp:319
socket_adaptor::terminate_current_connection
void terminate_current_connection()
This api invokes close() to terminate the current connection.
Definition: socket_adaptor.cpp:277
socket_adaptor::get_path
std::string & get_path()
This api returns the data path.
Definition: socket_adaptor.cpp:88
socket_adaptor
Definition: socket_adaptor.h:33
socket_adaptor::start_listening
int start_listening(const std::string &path)
This function makes the audiocapturemgr listen for incoming unix domain connections to the given path...
Definition: socket_adaptor.cpp:93
socket_adaptor::write_data
int write_data(const char *buffer, const unsigned int size)
This api invokes unix write() to write data to the socket.
Definition: socket_adaptor.cpp:64