When creating a thread, waiting for the thread function to start
[platform/core/multimedia/libmm-radio.git] / src / include / mm_radio_priv.h
1 /*
2  * mm_radio_priv.h
3  *
4  * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifndef __MM_Radio_INTERNAL_H__
21 #define __MM_Radio_INTERNAL_H__
22
23 /*===========================================================================================
24   INCLUDE FILES
25 ========================================================================================== */
26 #include <stdio.h>
27 #include <stdbool.h>
28 #include <unistd.h>
29 #include <malloc.h>
30 #include <pthread.h>
31 #include <signal.h>
32
33 #include <mm_types.h>
34 #include <mm_message.h>
35 #include <media/mm_resource_manager.h>
36
37 #include "mm_radio.h"
38 #include "mm_radio_utils.h"
39 #include <linux/videodev2.h>
40
41 #include <gst/gst.h>
42 #include <gst/gstbuffer.h>
43
44 #include <glib.h>
45
46 #ifdef __cplusplus
47         extern "C" {
48 #endif
49
50 /*===========================================================================================
51   GLOBAL DEFINITIONS AND DECLARATIONS FOR MODULE
52 ========================================================================================== */
53
54 /*---------------------------------------------------------------------------
55     GLOBAL #defines:
56 ---------------------------------------------------------------------------*/
57 #define SAMPLEDELAY                                     15000
58
59 /* si470x dependent define */
60 #define SYSCONFIG1                                      4               /* System Configuration 1 */
61 #define SYSCONFIG1_RDS                          0x1000  /* bits 12..12: RDS Enable */
62 #define SYSCONFIG1_RDS_OFFSET           12              /* bits 12..12: RDS Enable Offset */
63
64 #define SYSCONFIG2                                      5               /* System Configuration 2 */
65 #define SYSCONFIG2_SEEKTH                       0xff00  /* bits 15..08: RSSI Seek Threshold */
66 #define SYSCONFIG2_SEEKTH_OFFSET        8               /* bits 15..08: RSSI Seek Threshold Offset */
67
68 #define SYSCONFIG3                                      6               /* System Configuration 3 */
69 #define SYSCONFIG3_SKSNR                        0x00f0  /* bits 07..04: Seek SNR Threshold */
70 #define SYSCONFIG3_SKCNT                        0x000f  /* bits 03..00: Seek FM Impulse Detection Threshold */
71 #define SYSCONFIG3_SKSNR_OFFSET 4               /* bits 07..04: Seek SNR Threshold Offset */
72 #define SYSCONFIG3_SKCNT_OFFSET 0               /* bits 03..00: Seek FM Impulse Detection Threshold Offset */
73
74 #define DEFAULT_CHIP_MODEL                      "radio-si470x"
75
76 /*---------------------------------------------------------------------------
77     GLOBAL CONSTANT DEFINITIONS:
78 ---------------------------------------------------------------------------*/
79 typedef enum {
80         MMRADIO_COMMAND_CREATE = 0,
81         MMRADIO_COMMAND_DESTROY,
82         MMRADIO_COMMAND_REALIZE,
83         MMRADIO_COMMAND_UNREALIZE,
84         MMRADIO_COMMAND_START,
85         MMRADIO_COMMAND_STOP,
86         MMRADIO_COMMAND_START_SCAN,
87         MMRADIO_COMMAND_STOP_SCAN,
88         MMRADIO_COMMAND_SET_FREQ,
89         MMRADIO_COMMAND_GET_FREQ,
90         MMRADIO_COMMAND_MUTE,
91         MMRADIO_COMMAND_UNMUTE,
92         MMRADIO_COMMAND_SEEK,
93         MMRADIO_COMMAND_SET_REGION,
94         MMRADIO_COMMAND_GET_REGION,
95         MMRADIO_COMMAND_SET_VOLUME,
96         MMRADIO_COMMAND_GET_VOLUME,
97         MMRADIO_COMMAND_NUM
98 } MMRadioCommand;
99
100 /* max and mix frequency types, KHz */
101 typedef enum {
102         MM_RADIO_FREQ_NONE                              = 0,
103         /* min band types */
104         MM_RADIO_FREQ_MIN_76100_KHZ             = 76100,
105         MM_RADIO_FREQ_MIN_87500_KHZ             = 87500,
106         MM_RADIO_FREQ_MIN_88100_KHZ             = 88100,
107         /* max band types */
108         MM_RADIO_FREQ_MAX_89900_KHZ             = 89900,
109         MM_RADIO_FREQ_MAX_108000_KHZ    = 108000,
110 } MMRadioFreqTypes;
111
112 /* de-emphasis types  */
113 typedef enum {
114         MM_RADIO_DEEMPHASIS_NONE = 0,
115         MM_RADIO_DEEMPHASIS_50_US,
116         MM_RADIO_DEEMPHASIS_75_US,
117 } MMRadioDeemphasis;
118
119 /* radio region settings */
120 typedef struct {
121         MMRadioRegionType country;
122         MMRadioDeemphasis deemphasis;   // unit :  us
123         MMRadioFreqTypes band_min;              // <- freq. range, unit : KHz
124         MMRadioFreqTypes band_max;              // ->
125         int channel_spacing;                            // TBD
126 } MMRadioRegion_t;
127
128 typedef struct {
129         pthread_t thread;
130         pthread_mutex_t mutex;
131         pthread_cond_t cond;
132         int thread_id;
133         bool is_running;
134         bool stop;
135         bool thread_exit;
136 } MMRadioThread_t;
137
138 typedef enum {
139         MM_RADIO_MSG_DESTROY = 0,
140         MM_RADIO_MSG_SCAN_INFO,
141         MM_RADIO_MSG_SCAN_STOPPED,
142         MM_RADIO_MSG_SCAN_FINISHED,
143         MM_RADIO_MSG_SEEK_FINISHED,
144         MM_RADIO_MSG_STATE_INTERRUPTED,
145         MM_RADIO_MSG_NUM
146 } MMRadioMsgTypes;
147
148 typedef struct {
149         bool destroy;
150         MMRadioMsgTypes msg_type;
151         int data;
152 } mm_radio_msg_t;
153
154 typedef enum {
155         MM_RADIO_THREAD_MSG = 0,
156         MM_RADIO_THREAD_SEEK,
157         MM_RADIO_THREAD_SCAN,
158         MM_RADIO_THREAD_NUM
159 } MMRadioThreadTypes;
160
161 /*---------------------------------------------------------------------------
162     GLOBAL DATA TYPE DEFINITIONS:
163 ---------------------------------------------------------------------------*/
164 #define USE_GST_PIPELINE
165
166 #ifdef USE_GST_PIPELINE
167 typedef struct _mm_radio_gstreamer_s {
168         GMainLoop *loop;
169         GstElement *pipeline;
170         GstElement *audiosrc;
171         GstElement *queue2;
172         GstElement *volume;
173         GstElement *audiosink;
174         GstBuffer *output_buffer;
175 } mm_radio_gstreamer_s;
176 #endif
177
178 typedef struct {
179         /* radio state */
180         int current_state;
181         int old_state;
182         int pending_state;
183
184         int cmd;
185
186         /* command lock */
187         pthread_mutex_t cmd_lock;
188
189         /* radio attributes */
190         MMHandleType* attrs;
191
192         MMRadioThread_t thread[MM_RADIO_THREAD_NUM];
193
194         /* message callback */
195         GAsyncQueue *msg_queue;
196         MMMessageCallback msg_cb;
197         void* msg_cb_param;
198
199         /* radio device fd */
200         int radio_fd;
201
202         /* device control */
203         struct v4l2_capability vc;
204         struct v4l2_tuner vt;
205         struct v4l2_control vctrl;
206         struct v4l2_frequency vf;
207
208         /* hw debug */
209         struct v4l2_dbg_register reg;
210
211         /* seek */
212         int prev_seek_freq;
213         MMRadioSeekDirectionType seek_direction;
214
215         int freq;
216 #ifdef USE_GST_PIPELINE
217         mm_radio_gstreamer_s* pGstreamer_s;
218 #endif
219         mm_resource_manager_h resource_manager;
220         mm_resource_manager_res_h radio_resource;
221         int interrupted_by_resource_conflict;
222
223         unsigned int subs_id;
224         float local_volume;
225
226         /* region settings */
227         MMRadioRegion_t region_setting;
228 } mm_radio_t;
229
230 /*===========================================================================================
231   GLOBAL FUNCTION PROTOTYPES
232 ========================================================================================== */
233 int _mmradio_create_radio(mm_radio_t *radio);
234 int _mmradio_destroy(mm_radio_t *radio);
235 int _mmradio_realize(mm_radio_t *radio);
236 int _mmradio_unrealize(mm_radio_t *radio);
237 int _mmradio_set_message_callback(mm_radio_t *radio, MMMessageCallback callback, void *user_param);
238 int _mmradio_get_state(mm_radio_t *radio, int *pState);
239 int _mmradio_set_frequency(mm_radio_t *radio, int freq);
240 int _mmradio_get_frequency(mm_radio_t *radio, int *pFreq);
241 int _mmradio_mute(mm_radio_t *radio);
242 int _mmradio_unmute(mm_radio_t *radio);
243 int _mmradio_start(mm_radio_t *radio);
244 int _mmradio_stop(mm_radio_t *radio);
245 int _mmradio_seek(mm_radio_t *radio, MMRadioSeekDirectionType direction);
246 int _mmradio_start_scan(mm_radio_t *radio);
247 int _mmradio_stop_scan(mm_radio_t *radio);
248 int _mm_radio_get_signal_strength(mm_radio_t *radio, int *strength);
249 #ifdef USE_GST_PIPELINE
250 int _mmradio_realize_pipeline(mm_radio_t *radio);
251 int _mmradio_start_pipeline(mm_radio_t *radio);
252 int _mmradio_stop_pipeline(mm_radio_t *radio);
253 int _mmradio_destroy_pipeline(mm_radio_t *radio);
254 #endif
255 int _mmradio_apply_region(mm_radio_t *radio, MMRadioRegionType region, bool update);
256 int _mmradio_get_region_type(mm_radio_t *radio, MMRadioRegionType *type);
257 int _mmradio_get_region_frequency_range(mm_radio_t *radio, unsigned int *min_freq, unsigned int *max_freq);
258 int _mmradio_get_channel_spacing(mm_radio_t *radio, unsigned int *ch_spacing);
259 int _mmradio_set_volume(mm_radio_t *radio, float volume);
260 int _mmradio_get_volume(mm_radio_t *radio, float *pVolume);
261
262 #ifdef __cplusplus
263         }
264 #endif
265
266 #endif  /* __MM_Radio_INTERNAL_H__ */