RDK Documentation (Open Sourced RDK Components)
audio_capture_manager.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 _AUDIO_CAPTURE_MANAGER_H_
20 #define _AUDIO_CAPTURE_MANAGER_H_
21 #include <pthread.h>
22 #include <vector>
23 #include <semaphore.h>
24 #include <string>
25 #include <thread>
26 #include <condition_variable>
27 #include <mutex>
28 #include "audio_buffer.h"
29 #include "basic_types.h"
30 #include "rmf_error.h"
31 #include "media-utils/audioCapture/rmfAudioCapture.h"
32 
33 /**
34  * @defgroup AUDIO_CAPTURE_MANAGER audiocapturemgr
35  *
36  * - Audio capture manager presents the audio data to registered applications.
37  * - Supports creation of audio clips from currently streaming content.
38  * - Creates clips of audio from content currently viewing so that it can be used for song identification service.
39  * - The Data Capture Service Manager API is used to create audio clip and post/stream to Music ID server (specified URL.)
40  * - When Service Manager API is called to create audio clip, settop creates audio clip of specified duration.
41  * - Used in Bluetooth audio streaming
42  *
43  * @defgroup AUDIO_CAPTURE_MANAGER_API Audio Capture Manager Data Types and API(s)
44  * This file provides the data types and API(s) used by the audiocapturemgr.
45  * @ingroup AUDIO_CAPTURE_MANAGER
46  *
47  */
48 
49 /**
50  * @addtogroup AUDIO_CAPTURE_MANAGER_API
51  * @{
52  */
53 
54 namespace audiocapturemgr
55 {
56  typedef struct
57  {
58  racFormat format;
59  racFreq sampling_frequency;
60  size_t fifo_size;
61  size_t threshold;
62  unsigned int delay_compensation_ms;
64  void get_individual_audio_parameters(const audio_properties_t &audio_props, unsigned int &sampling_rate, unsigned int &bits_per_sample, unsigned int &num_channels);
65  unsigned int calculate_data_rate(const audio_properties_t &audio_props);
66  std::string get_suffix(unsigned int ticker);
67 }
68 
70 class q_mgr
71 {
72  private:
73  std::vector <audio_buffer *> *m_current_incoming_q;
74  std::vector <audio_buffer *> *m_current_outgoing_q;
75  std::vector <audio_capture_client *> m_clients;
76  audiocapturemgr::audio_properties_t m_audio_properties;
77  unsigned int m_bytes_per_second;
78  unsigned int m_inflow_byte_counter; // It's okay if this rolls over.
79  unsigned int m_num_clients;
80  pthread_mutex_t m_q_mutex;
81  pthread_mutex_t m_client_mutex;
82  sem_t m_sem;
83  pthread_t m_thread;
84  bool m_processing_thread_alive;
85  bool m_notify_new_data;
86  bool m_started;
87  RMF_AudioCaptureHandle m_device_handle;
88  unsigned int m_max_queue_size;
89 
90  std::thread m_data_monitor_thread;
91  std::mutex m_data_monitor_mutex;
92  std::condition_variable m_data_monitor_cv;
93  bool m_stop_data_monitor;
94 
95  private:
96  inline void lock(pthread_mutex_t &mutex);
97  inline void unlock(pthread_mutex_t &mutex);
98  inline void notify_data_ready();
99  void swap_queues(); //caller must lock before invoking this.
100  void flush_queue(std::vector <audio_buffer *> *q);
101  void flush_system();
102  void process_data();
103  void update_buffer_references();
104  void data_monitor();
105 
106  public:
107  q_mgr();
108  ~q_mgr();
109 
110  /**
111  * @brief This API is used to set the audio properties to the client device.
112  *
113  * Properties like format,sampling_frequency,fifo_size,threshold,delay_compensation_ms.
114  * Checks if audio playback is started, restarted the client device after settings are applied.
115  *
116  * @param[in] in_properties Structure which holds the audio properties.
117  *
118  * @return 0 on success, appropiate errorcode otherwise.
119  */
121 
122  /**
123  * @brief This API returns the current audio properties of the device.
124  *
125  * @param[out] out_properties Structure which holds the audio properties.
126  */
128 
129  /**
130  * @brief This function will return default RMF_AudioCapture_Settings settings.
131  *
132  * Once AudioCaptureStart gets called with RMF_AudioCapture_Status argument this
133  * should still continue to return the default capture settings.
134  *
135  * @param[out] out_properties Structure which holds the audio properties.
136  *
137  * @return Device Settings error code
138  * @retval dsERR_NONE Indicates dsGetAudioPort API was successfully called using iarmbus call.
139  * @retval dsERR_GENERAL Indicates error due to general failure.
140  */
142 
143  /**
144  * @brief Returns data rate in bytes per second.
145  *
146  * The data rate is a term to denote the transmission speed, or the number of bytes per second transferred.
147  * Datarate is calculated as bits_per_sample * sampling_rate * num_channels / 8;
148  *
149  * @returns Number of bytes per second transferred.
150  */
151  unsigned int get_data_rate();
152 
153  /**
154  * @brief This API creates new audio buffer and pushes the data to the queue.
155  *
156  * @param[in] buf Data to be inserted into the queue.
157  * @param[in] size size of the buffer.
158  *
159  */
160  void add_data(unsigned char *buf, unsigned int size);
161 
162  /**
163  * @brief This API processes the audio buffers available in the queue.
164  *
165  */
166  void data_processor_thread();
167 
168  /**
169  * @brief This API registers the client.
170  *
171  * Client is the consumer of audio data.
172  *
173  * @param[in] client Client info for registering.
174  *
175  * @return 0 on success, appropiate errorcode otherwise.
176  */
178 
179  /**
180  * @brief Removes the client.
181  *
182  * @param[in] client Client info for registering.
183  *
184  * @return 0 on success, appropiate errorcode otherwise.
185  */
187 
188  /**
189  * @brief This function will start the Audio capture.
190  *
191  * If Settings is not null, reconfigure the settings with the provided capture settings and starts the audio capture .
192  * If it is NULL, start with the default capture settings.
193  *
194  * @return Returns 0 on success, appropiate error code otherwise.
195  */
196  int start();
197 
198  /**
199  * @brief This function will stop the audio capture.
200  *
201  * Start can be called again after a Stop, as long as Close has not been called.
202  *
203  * @return Returns 0 on success, appropiate error code otherwise.
204  */
205  int stop();
206 
207  /**
208  * @brief This function invokes an API for adding data to the audio buffer.
209  *
210  * @param[in] context data callback context
211  * @param[in] buf Data to be inserted into the queue.
212  * @param[in] size size of the buffer.
213  *
214  * @return Returns RMF_SUCCESS on success, appropiate error code otherwise.
215  */
216  static rmf_Error data_callback(void *context, void *buf, unsigned int size);
217 };
218 
220 {
221 
222  private:
223  unsigned int m_priority;
224  pthread_mutex_t m_mutex;
225 
226  protected:
227  q_mgr * m_manager;
228  void release_buffer(audio_buffer *ptr);
229  void lock();
230  void unlock();
231 
232  public:
233  virtual int data_callback(audio_buffer *buf);
234  audio_capture_client(q_mgr * manager);
235  virtual ~audio_capture_client();
236  unsigned int get_priority() {return m_priority;}
237  void set_manager(q_mgr *manager);
238  virtual int set_audio_properties(audiocapturemgr::audio_properties_t &properties);
239  virtual void get_audio_properties(audiocapturemgr::audio_properties_t &properties);
240  void get_default_audio_properties(audiocapturemgr::audio_properties_t &properties);
241  virtual void notify_event(audio_capture_events_t event){}
242  virtual int start();
243  virtual int stop();
244 };
245 
246 /**
247  * @}
248  */
249 
250 #endif // _AUDIO_CAPTURE_MANAGER_h_
q_mgr::set_audio_properties
int set_audio_properties(audiocapturemgr::audio_properties_t &in_properties)
This API is used to set the audio properties to the client device.
Definition: audio_capture_manager.cpp:223
audio_capture_client
Definition: audio_capture_manager.h:219
q_mgr::add_data
void add_data(unsigned char *buf, unsigned int size)
This API creates new audio buffer and pushes the data to the queue.
Definition: audio_capture_manager.cpp:290
q_mgr::get_data_rate
unsigned int get_data_rate()
Returns data rate in bytes per second.
Definition: audio_capture_manager.cpp:285
q_mgr::get_audio_properties
void get_audio_properties(audiocapturemgr::audio_properties_t &out_properties)
This API returns the current audio properties of the device.
Definition: audio_capture_manager.cpp:270
q_mgr::data_callback
static rmf_Error data_callback(void *context, void *buf, unsigned int size)
This function invokes an API for adding data to the audio buffer.
Definition: audio_capture_manager.cpp:43
audio_buffer
Definition: audio_buffer.h:25
q_mgr::data_processor_thread
void data_processor_thread()
This API processes the audio buffers available in the queue.
Definition: audio_capture_manager.cpp:306
audiocapturemgr::audio_properties_t
Definition: audio_capture_manager.h:56
q_mgr::get_default_audio_properties
void get_default_audio_properties(audiocapturemgr::audio_properties_t &out_properties)
This function will return default RMF_AudioCapture_Settings settings.
Definition: audio_capture_manager.cpp:275
q_mgr::register_client
int register_client(audio_capture_client *client)
This API registers the client.
Definition: audio_capture_manager.cpp:390
q_mgr::start
int start()
This function will start the Audio capture.
Definition: audio_capture_manager.cpp:482
q_mgr::stop
int stop()
This function will stop the audio capture.
Definition: audio_capture_manager.cpp:515
q_mgr
Definition: audio_capture_manager.h:70
q_mgr::unregister_client
int unregister_client(audio_capture_client *client)
Removes the client.
Definition: audio_capture_manager.cpp:411