blob: 004adf0d639b27dd0368d5ffe20ef82bf264bfd1
1 | /** |
2 | * \file adec-message.c |
3 | * \brief Audio Dec Message Handlers |
4 | * \version 1.0.0 |
5 | * \date 2011-03-08 |
6 | */ |
7 | /* Copyright (C) 2007-2011, Amlogic Inc. |
8 | * All right reserved |
9 | * |
10 | */ |
11 | #include <stdio.h> |
12 | #include <string.h> |
13 | #include <unistd.h> |
14 | #include <audio-dec.h> |
15 | #include <amthreadpool.h> |
16 | |
17 | /** |
18 | * \brief message is empty or not |
19 | * \param pool pointer to message pool |
20 | * \return TREU when empty, otherwise FALSE |
21 | */ |
22 | adec_bool_t message_pool_empty(aml_audio_dec_t *audec) |
23 | { |
24 | message_pool_t *pool = &audec->message_pool; |
25 | |
26 | if (pool->message_num == 0) { |
27 | return TRUE; |
28 | } else { |
29 | return FALSE; |
30 | } |
31 | } |
32 | |
33 | /** |
34 | * \brief init the adec message pool |
35 | * \param audec pointer to audec |
36 | * \return 0 on success |
37 | */ |
38 | int adec_message_pool_init(aml_audio_dec_t *audec) |
39 | { |
40 | message_pool_t *pool; |
41 | |
42 | pool = &audec->message_pool; |
43 | pool->message_in_index = 0; |
44 | pool->message_out_index = 0; |
45 | pool->message_num = 0; |
46 | memset(pool->message_lise, 0, sizeof(pool->message_lise)); |
47 | pthread_mutex_init(&pool->msg_mutex, NULL); |
48 | |
49 | return 0; |
50 | } |
51 | |
52 | /** |
53 | * \brief alloc new message |
54 | * \return pointer to the new message |
55 | */ |
56 | adec_cmd_t *adec_message_alloc(void) |
57 | { |
58 | adec_cmd_t *cmd; |
59 | |
60 | cmd = (adec_cmd_t *)malloc(sizeof(adec_cmd_t)); |
61 | if (cmd) { |
62 | memset(cmd, 0, sizeof(adec_cmd_t)); |
63 | } |
64 | |
65 | return cmd; |
66 | } |
67 | |
68 | /** |
69 | * \brief free the message |
70 | * \param cmd pointer to message |
71 | * \return 0 on success |
72 | */ |
73 | int adec_message_free(adec_cmd_t *cmd) |
74 | { |
75 | if (cmd) { |
76 | free(cmd); |
77 | } |
78 | |
79 | cmd = NULL; |
80 | |
81 | return 0; |
82 | } |
83 | |
84 | /** |
85 | * \brief send a message |
86 | * \param audec pointer to audec |
87 | * \param cmd pointer to message |
88 | * \return 0 on success otherwise -1 |
89 | */ |
90 | int adec_send_message(aml_audio_dec_t *audec, adec_cmd_t *cmd) |
91 | { |
92 | |
93 | int ret = -1; |
94 | message_pool_t *pool; |
95 | |
96 | pool = &audec->message_pool; |
97 | |
98 | int retry_count = 0; |
99 | adec_thread_wakeup(audec); |
100 | while (pool->message_num > MESSAGE_NUM_MAX / 2) { |
101 | usleep(1000 * 10); |
102 | if (retry_count++ > (pool->message_num - MESSAGE_NUM_MAX / 2) * 10) { |
103 | break; |
104 | } |
105 | } |
106 | |
107 | pthread_mutex_lock(&pool->msg_mutex); |
108 | if (pool->message_num < MESSAGE_NUM_MAX) { |
109 | pool->message_lise[pool->message_in_index] = cmd; |
110 | pool->message_in_index = (pool->message_in_index + 1) % MESSAGE_NUM_MAX; |
111 | pool->message_num += 1; |
112 | ret = 0; |
113 | } else { |
114 | /* message pool is full */ |
115 | adec_print("message pool is full! delete the oldest message!"); |
116 | adec_cmd_t *oldestcmd; |
117 | oldestcmd = pool->message_lise[pool->message_in_index]; |
118 | free(oldestcmd); |
119 | pool->message_out_index = (pool->message_out_index + 1) % MESSAGE_NUM_MAX; |
120 | pool->message_lise[pool->message_in_index] = cmd; |
121 | pool->message_in_index = (pool->message_in_index + 1) % MESSAGE_NUM_MAX; |
122 | ret = 0; |
123 | } |
124 | amthreadpool_thread_wake(audec->thread_pid); |
125 | pthread_mutex_unlock(&pool->msg_mutex); |
126 | |
127 | return ret; |
128 | } |
129 | |
130 | /** |
131 | * \brief get a message |
132 | * \param audec pointer to audec |
133 | * \return pointer to message otherwise NULL if an error occurred |
134 | */ |
135 | adec_cmd_t *adec_get_message(aml_audio_dec_t *audec) |
136 | { |
137 | |
138 | message_pool_t *pool; |
139 | adec_cmd_t *cmd = NULL; |
140 | |
141 | pool = &audec->message_pool; |
142 | if (!pool) { |
143 | adec_print("message pool is null! get message failed!"); |
144 | return NULL; |
145 | } |
146 | |
147 | pthread_mutex_lock(&pool->msg_mutex); |
148 | if (pool->message_num > 0) { |
149 | cmd = pool->message_lise[pool->message_out_index]; |
150 | pool->message_out_index = (pool->message_out_index + 1) % MESSAGE_NUM_MAX; |
151 | pool->message_num -= 1; |
152 | } |
153 | pthread_mutex_unlock(&pool->msg_mutex); |
154 | |
155 | return cmd; |
156 | } |
157 |