~ubuntu-branches/ubuntu/precise/me-tv/precise-proposed

« back to all changes in this revision

Viewing changes to src/libdvben50221/en50221_app_teletext.c

  • Committer: Bazaar Package Importer
  • Author(s): Philipp Kern
  • Date: 2008-07-23 14:03:56 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080723140356-m6ze7fbkydes42c7
Tags: 0.5.33-3
Fix xine-lib ffmpeg dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    en50221 encoder An implementation for libdvb
3
 
    an implementation for the en50221 transport layer
4
 
 
5
 
    Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
6
 
    Copyright (C) 2005 Julian Scheel (julian at jusst dot de)
7
 
    Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
8
 
 
9
 
    This library is free software; you can redistribute it and/or modify
10
 
    it under the terms of the GNU Lesser General Public License as
11
 
    published by the Free Software Foundation; either version 2.1 of
12
 
    the License, or (at your option) any later version.
13
 
 
14
 
    This program is distributed in the hope that it will be useful,
15
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
    GNU Lesser General Public License for more details.
18
 
 
19
 
    You should have received a copy of the GNU Lesser General Public
20
 
    License along with this library; if not, write to the Free Software
21
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22
 
*/
23
 
 
24
 
#include <string.h>
25
 
#include <libdvbmisc/dvbmisc.h>
26
 
#include <pthread.h>
27
 
#include "en50221_app_teletext.h"
28
 
#include "en50221_app_tags.h"
29
 
#include "asn_1.h"
30
 
 
31
 
struct en50221_app_teletext {
32
 
        struct en50221_app_send_functions *funcs;
33
 
 
34
 
        en50221_app_teletext_callback callback;
35
 
        void *callback_arg;
36
 
 
37
 
        pthread_mutex_t lock;
38
 
};
39
 
 
40
 
static int en50221_app_teletext_parse_ebu(struct en50221_app_teletext *teletext,
41
 
                                          uint8_t slot_id,
42
 
                                          uint16_t session_number,
43
 
                                          uint8_t * data,
44
 
                                          uint32_t data_length);
45
 
 
46
 
 
47
 
 
48
 
struct en50221_app_teletext *
49
 
        en50221_app_teletext_create(struct en50221_app_send_functions *funcs)
50
 
{
51
 
        struct en50221_app_teletext *teletext = NULL;
52
 
 
53
 
        // create structure and set it up
54
 
        teletext = malloc(sizeof(struct en50221_app_teletext));
55
 
        if (teletext == NULL) {
56
 
                return NULL;
57
 
        }
58
 
        teletext->funcs = funcs;
59
 
        teletext->callback = NULL;
60
 
 
61
 
        pthread_mutex_init(&teletext->lock, NULL);
62
 
 
63
 
        // done
64
 
        return teletext;
65
 
}
66
 
 
67
 
void en50221_app_teletext_destroy(struct en50221_app_teletext *teletext)
68
 
{
69
 
        pthread_mutex_destroy(&teletext->lock);
70
 
        free(teletext);
71
 
}
72
 
 
73
 
void en50221_app_teletext_register_callback(struct en50221_app_teletext *teletext,
74
 
                                            en50221_app_teletext_callback callback, void *arg)
75
 
{
76
 
        pthread_mutex_lock(&teletext->lock);
77
 
        teletext->callback = callback;
78
 
        teletext->callback_arg = arg;
79
 
        pthread_mutex_unlock(&teletext->lock);
80
 
}
81
 
 
82
 
int en50221_app_teletext_message(struct en50221_app_teletext *teletext,
83
 
                                 uint8_t slot_id,
84
 
                                 uint16_t session_number,
85
 
                                 uint32_t resource_id,
86
 
                                 uint8_t * data, uint32_t data_length)
87
 
{
88
 
        (void) resource_id;
89
 
 
90
 
        // get the tag
91
 
        if (data_length < 3) {
92
 
                print(LOG_LEVEL, ERROR, 1, "Received short data\n");
93
 
                return -1;
94
 
        }
95
 
        uint32_t tag = (data[0] << 16) | (data[1] << 8) | data[2];
96
 
 
97
 
        switch (tag) {
98
 
        case TAG_TELETEXT_EBU:
99
 
                return en50221_app_teletext_parse_ebu(teletext, slot_id,
100
 
                                                      session_number,
101
 
                                                      data + 3,
102
 
                                                      data_length - 3);
103
 
        }
104
 
 
105
 
        print(LOG_LEVEL, ERROR, 1, "Received unexpected tag %x\n", tag);
106
 
        return -1;
107
 
}
108
 
 
109
 
 
110
 
static int en50221_app_teletext_parse_ebu(struct en50221_app_teletext *teletext,
111
 
                                          uint8_t slot_id,
112
 
                                          uint16_t session_number,
113
 
                                          uint8_t *data,
114
 
                                          uint32_t data_length)
115
 
{
116
 
        // first of all, decode the length field
117
 
        uint16_t asn_data_length;
118
 
        int length_field_len;
119
 
        if ((length_field_len = asn_1_decode(&asn_data_length, data, data_length)) < 0) {
120
 
                print(LOG_LEVEL, ERROR, 1, "ASN.1 decode error\n");
121
 
                return -1;
122
 
        }
123
 
 
124
 
        // check it
125
 
        if (asn_data_length > (data_length - length_field_len)) {
126
 
                print(LOG_LEVEL, ERROR, 1, "Received short data\n");
127
 
                return -1;
128
 
        }
129
 
        uint8_t *teletext_data = data + length_field_len;
130
 
 
131
 
        // tell the app
132
 
        pthread_mutex_lock(&teletext->lock);
133
 
        en50221_app_teletext_callback cb = teletext->callback;
134
 
        void *cb_arg = teletext->callback_arg;
135
 
        pthread_mutex_unlock(&teletext->lock);
136
 
        if (cb) {
137
 
                return cb(cb_arg, slot_id, session_number, teletext_data,
138
 
                          asn_data_length);
139
 
        }
140
 
        return 0;
141
 
}