RDK Documentation (Open Sourced RDK Components)
music_id.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 _MUSIC_ID_H_
20 #define _MUSIC_ID_H_
21 #include "audio_capture_manager.h"
22 #include "audio_converter.h"
23 #include "socket_adaptor.h"
24 #include <iostream>
25 #include <list>
26 #include <map>
27 #include <fstream>
28 #include <string>
29 #include <thread>
30 
31 /**
32  * @addtogroup AUDIO_CAPTURE_MANAGER_API
33  * @{
34  */
35 
36 typedef void (*request_complete_callback_t)(void *data, std::string &file, int result);
38 {
39  public:
40  typedef enum
41  {
42  FILE_OUTPUT = 0,
43  SOCKET_OUTPUT
44  } preferred_delivery_method_t;
45 
46  private:
47  typedef int request_id_t;
48  typedef struct
49  {
50  request_id_t id;
51  std::string filename;
52  unsigned int length;
53  unsigned int time_remaining;
54  request_complete_callback_t callback;
55  void * callback_data;
56  }request_t;
57 
58  std::list <audio_buffer *> m_queue;
59  std::list <request_t*> m_requests;
60  std::list <audio_converter_memory_sink *> m_outbox;
61  std::thread m_worker_thread;
62  bool m_worker_thread_alive;
63  unsigned int m_total_size;
64  unsigned int m_precapture_duration_seconds;
65  unsigned int m_precapture_size_bytes;
66  unsigned int m_queue_upper_limit_bytes;
67  unsigned int m_request_counter;
68  bool m_enable_wav_header_output;
69  audiocapturemgr::audio_properties_t m_output_properties;
70  bool m_convert_output;
71  preferred_delivery_method_t m_delivery_method;
72  socket_adaptor * m_sock_adaptor;
73  const std::string m_sock_path;
74 
75  void trim_queue();
76  int write_default_file_header(std::ofstream &file);
77  int update_file_header_size(std::ofstream &file, unsigned int data_size);
78  int grab_last_n_seconds(const std::string &filename, unsigned int seconds);
79  int grab_last_n_seconds(unsigned int seconds); //For socket mode output
80  void compute_queue_size();
81 
82  public:
83  music_id_client(q_mgr * manager, preferred_delivery_method_t mode);
84  ~music_id_client();
85  virtual int data_callback(audio_buffer *buf);
86 
87  /**
88  * @brief This API writes the precaptured sample to a file for file mode and in socket mode, sample is written to the unix
89  * socket connection established to the client.
90  *
91  * @param[in] filename File name, where precaptured output is written to in the case of file mode.
92  *
93  * @return Return 0 on success, appropiate error code otherwise.
94  */
95  int grab_precaptured_sample(const std::string &filename = nullptr);
96 
97  /**
98  * @brief This API requests for new sample.
99  *
100  * @param[in] seconds length of the sample.
101  * @param[in] filename Output file name.
102  * @param[in] cb Callback function.
103  * @param[in] cb_data Callback data.
104  *
105  * @return Return 0 on success, appropiate error code otherwise.
106  */
107  request_id_t grab_fresh_sample(unsigned int seconds, const std::string &filename = nullptr, request_complete_callback_t cb = nullptr, void * cb_data = nullptr);
108 
109  /**
110  * @brief Invokes an API for setting the audio specific properties of the audio capture client.
111  *
112  * @param[in] properties Properties to set.
113  *
114  * @return Return 0 on success, appropiate error code otherwise.
115  */
117 
118  /**
119  * @brief Invokes an API for getting the audio specific properties of the audio capture client.
120  *
121  * @param[out] properties Returns the audio properties like frequency,format of the device.
122  */
124 
125  /**
126  * @brief This API is used to set the precapture duration.
127  *
128  * It is in seconds.
129  *
130  * @param[in] seconds Time in seconds.
131  *
132  * @return Return 0 on success, appropiate error code otherwise.
133  */
134  int set_precapture_duration(unsigned int seconds);
135 
136  /**
137  * @brief This function manages a queue of requests for music id samples.
138  */
139  void worker_thread();
140 
141  /**
142  * @brief This API returns maximum precaptured length.
143  *
144  * @return Returns maximum precaptured length in seconds.
145  */
146  unsigned int get_max_supported_duration();
147 
148  /**
149  * @brief This API is to enable/disable WAV header.
150  *
151  * @param[in] isEnabled Boolean value indicates enabled/disabled.
152  *
153  * @return Return 0 on success, appropiate error code otherwise.
154  */
155  unsigned int enable_wav_header(bool isEnabled);
156 
157  /**
158  * @brief This API enables audio downsampling without anti-aliasing.
159  *
160  * @param[in] isEnabled Boolean value indicates enabled/disabled.
161  */
162  void enable_output_conversion(bool isEnabled) { m_convert_output = isEnabled; }
163 
164  /**
165  * @brief This API writes the captured clip data to the socket.
166  *
167  * Also it terminates the current connection.
168  */
169  void send_clip_via_socket();
170  const std::string & get_sock_path() { return m_sock_path; }
171 };
172 
173 #endif //_MUSIC_ID_H_
174 
175 /**
176  * @}
177  */
178 
179 
audio_capture_client
Definition: audio_capture_manager.h:219
music_id_client::grab_fresh_sample
request_id_t grab_fresh_sample(unsigned int seconds, const std::string &filename=nullptr, request_complete_callback_t cb=nullptr, void *cb_data=nullptr)
This API requests for new sample.
Definition: music_id.cpp:285
music_id_client
Definition: music_id.h:37
music_id_client::set_precapture_duration
int set_precapture_duration(unsigned int seconds)
This API is used to set the precapture duration.
Definition: music_id.cpp:100
audio_buffer
Definition: audio_buffer.h:25
audiocapturemgr::audio_properties_t
Definition: audio_capture_manager.h:56
music_id_client::grab_precaptured_sample
int grab_precaptured_sample(const std::string &filename=nullptr)
This API writes the precaptured sample to a file for file mode and in socket mode,...
Definition: music_id.cpp:185
music_id_client::enable_output_conversion
void enable_output_conversion(bool isEnabled)
This API enables audio downsampling without anti-aliasing.
Definition: music_id.h:162
music_id_client::get_audio_properties
virtual void get_audio_properties(audiocapturemgr::audio_properties_t &properties)
Invokes an API for getting the audio specific properties of the audio capture client.
Definition: music_id.cpp:127
socket_adaptor
Definition: socket_adaptor.h:33
music_id_client::enable_wav_header
unsigned int enable_wav_header(bool isEnabled)
This API is to enable/disable WAV header.
Definition: music_id.cpp:496
music_id_client::send_clip_via_socket
void send_clip_via_socket()
This API writes the captured clip data to the socket.
Definition: music_id.cpp:160
q_mgr
Definition: audio_capture_manager.h:70
music_id_client::get_max_supported_duration
unsigned int get_max_supported_duration()
This API returns maximum precaptured length.
Definition: music_id.cpp:490
music_id_client::worker_thread
void worker_thread()
This function manages a queue of requests for music id samples.
Definition: music_id.cpp:317
music_id_client::set_audio_properties
virtual int set_audio_properties(audiocapturemgr::audio_properties_t &properties)
Invokes an API for setting the audio specific properties of the audio capture client.
Definition: music_id.cpp:115
music_id_client::request_t
Definition: music_id.h:48