2 * Copyright (C) 2005-2012, Jon Gettler
3 * http://www.mvpmc.org/
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <cmyth_local.h>
27 cmyth_commbreaklist_destroy(cmyth_commbreaklist_t cbl)
31 cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
35 for (i = 0; i < cbl->commbreak_count; ++i) {
36 if (cbl->commbreak_list[i]) {
37 ref_release(cbl->commbreak_list[i]);
39 cbl->commbreak_list[i] = NULL;
41 if (cbl->commbreak_list) {
42 free(cbl->commbreak_list);
47 cmyth_commbreaklist_create(void)
49 cmyth_commbreaklist_t ret;
51 cmyth_dbg(CMYTH_DBG_DEBUG, "%s\n", __FUNCTION__);
52 ret = ref_alloc(sizeof(*ret));
56 ref_set_destroy(ret, (ref_destroy_t)cmyth_commbreaklist_destroy);
58 ret->commbreak_list = NULL;
59 ret->commbreak_count = 0;
64 cmyth_commbreak_destroy(cmyth_commbreak_t b)
66 cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
68 cmyth_dbg(CMYTH_DBG_DEBUG, "%s }!a\n", __FUNCTION__);
71 cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
75 cmyth_commbreak_create(void)
77 cmyth_commbreak_t ret = ref_alloc(sizeof(*ret));
79 cmyth_dbg(CMYTH_DBG_DEBUG, "%s {\n", __FUNCTION__);
81 cmyth_dbg(CMYTH_DBG_DEBUG, "%s }!\n", __FUNCTION__);
84 ref_set_destroy(ret, (ref_destroy_t)cmyth_commbreak_destroy);
87 ret->start_offset = 0;
91 cmyth_dbg(CMYTH_DBG_DEBUG, "%s }\n", __FUNCTION__);
96 cmyth_get_commbreaklist(cmyth_conn_t conn, cmyth_proginfo_t prog)
98 unsigned int len = CMYTH_UTC_LEN + CMYTH_LONGLONG_LEN + 19;
104 cmyth_commbreaklist_t breaklist = cmyth_commbreaklist_create();
111 sprintf(buf,"%s %ld %i", "QUERY_COMMBREAK", prog->proginfo_chanId,
112 (int)cmyth_timestamp_to_unixtime(prog->proginfo_rec_start_ts));
113 pthread_mutex_lock(&mutex);
114 if ((err = cmyth_send_message(conn, buf)) < 0) {
115 cmyth_dbg(CMYTH_DBG_ERROR,
116 "%s: cmyth_send_message() failed (%d)\n",
121 count = cmyth_rcv_length(conn);
123 cmyth_dbg(CMYTH_DBG_ERROR,
124 "%s: cmyth_rcv_length() failed (%d)\n",
125 __FUNCTION__, count);
129 if ((r = cmyth_rcv_commbreaklist(conn, &err, breaklist, count)) < 0) {
130 cmyth_dbg(CMYTH_DBG_ERROR,
131 "%s: cmyth_rcv_string() failed (%d)\n",
137 pthread_mutex_unlock(&mutex);
141 cmyth_commbreaklist_t
142 cmyth_get_cutlist(cmyth_conn_t conn, cmyth_proginfo_t prog)
144 unsigned int len = CMYTH_UTC_LEN + CMYTH_LONGLONG_LEN + 17;
150 cmyth_commbreaklist_t breaklist = cmyth_commbreaklist_create();
157 sprintf(buf,"%s %ld %i", "QUERY_CUTLIST", prog->proginfo_chanId,
158 (int)cmyth_timestamp_to_unixtime(prog->proginfo_rec_start_ts));
160 if ((err = cmyth_send_message(conn, buf)) < 0) {
161 cmyth_dbg(CMYTH_DBG_ERROR,
162 "%s: cmyth_send_message() failed (%d)\n",
167 count = cmyth_rcv_length(conn);
169 cmyth_dbg(CMYTH_DBG_ERROR,
170 "%s: cmyth_rcv_length() failed (%d)\n",
171 __FUNCTION__, count);
175 if ((r = cmyth_rcv_commbreaklist(conn, &err, breaklist, count)) < 0) {
176 cmyth_dbg(CMYTH_DBG_ERROR,
177 "%s: cmyth_rcv_string() failed (%d)\n",
183 pthread_mutex_unlock(&mutex);
187 int cmyth_rcv_commbreaklist(cmyth_conn_t conn, int *err,
188 cmyth_commbreaklist_t breaklist, int count)
194 long long start = -1;
196 cmyth_commbreak_t commbreak;
198 unsigned short start_type;
209 consumed = cmyth_rcv_long(conn, err, &rows, count);
213 failed = "cmyth_rcv_long";
218 cmyth_dbg(CMYTH_DBG_DEBUG, "%s: no commercial breaks found.\n",
223 for (i = 0; i < rows; i++) {
224 consumed = cmyth_rcv_ushort(conn, err, &type, count);
228 failed = "cmyth_rcv_ushort";
232 consumed = cmyth_rcv_int64(conn, err, &mark, count);
236 failed = "cmyth_rcv_long long";
239 if (type == CMYTH_COMMBREAK_START || type == CMYTH_CUTLIST_START) {
242 } else if (type == CMYTH_COMMBREAK_END || type == CMYTH_CUTLIST_END) {
244 ((type == CMYTH_COMMBREAK_END && start_type == CMYTH_COMMBREAK_START)
245 || (type == CMYTH_CUTLIST_END && start_type == CMYTH_CUTLIST_START)))
247 commbreak = cmyth_commbreak_create();
248 commbreak->start_mark = start;
249 commbreak->end_mark = mark;
251 breaklist->commbreak_list = realloc(breaklist->commbreak_list,
252 (++breaklist->commbreak_count) * sizeof(cmyth_commbreak_t));
253 breaklist->commbreak_list[breaklist->commbreak_count - 1] = commbreak;
255 cmyth_dbg(CMYTH_DBG_WARN,
256 "%s: ignoring 'end' marker without a 'start' marker at %lld\n",
257 __FUNCTION__, type, mark);
260 cmyth_dbg(CMYTH_DBG_WARN,
261 "%s: type (%d) is not a COMMBREAK or CUTLIST\n",
267 * If the last entry is a start marker then it doesn't have an associated end marker. In this
268 * case we choose to simply ignore it. Another option is to put in a really large fake end marker
269 * but that may cause strange seek behaviour in a client application.
275 cmyth_dbg(CMYTH_DBG_ERROR, "%s: %s() failed (%d)\n",
276 __FUNCTION__, failed, *err);
280 cmyth_commbreaklist_t
281 cmyth_mysql_get_commbreaklist(cmyth_database_t db, cmyth_conn_t conn, cmyth_proginfo_t prog)
283 cmyth_commbreaklist_t breaklist = cmyth_commbreaklist_create();
284 char start_ts_dt[CMYTH_TIMESTAMP_LEN + 1];
287 cmyth_timestamp_to_display_string(start_ts_dt, prog->proginfo_rec_start_ts, 0);
288 pthread_mutex_lock(&mutex);
289 if ((r=cmyth_mysql_get_commbreak_list(db, prog->proginfo_chanId, start_ts_dt, breaklist, conn->conn_version)) < 0) {
290 cmyth_dbg(CMYTH_DBG_ERROR,
291 "%s: cmyth_mysql_get_commbreak_list() failed (%d)\n",
296 fprintf(stderr, "Found %li commercial breaks for current program.\n", breaklist->commbreak_count);
297 if (r != breaklist->commbreak_count) {
298 fprintf(stderr, "commbreak error. Setting number of commercial breaks to zero\n");
299 cmyth_dbg(CMYTH_DBG_ERROR, "%s - returned rows=%d commbreak_count=%li\n",__FUNCTION__, r,breaklist->commbreak_count);
300 breaklist->commbreak_count = 0;
303 pthread_mutex_unlock(&mutex);