-
Notifications
You must be signed in to change notification settings - Fork 0
/
gstCCNxSegmenter.cc
184 lines (149 loc) · 5.31 KB
/
gstCCNxSegmenter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* Copyright (C) 2012 Jiwen Cai <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <assert.h>
#include "gstCCNxUtils.h"
#include "gstCCNxSegmenter.h"
GstCCNxPacketHeader* gst_ccnx_packet_header_unpack (
const struct ccn_charbuf*);
GstCCNxSegmentHeader* gst_ccnx_segment_header_unpack (
const struct ccn_charbuf * header_buf);
/* convertor bettwen GstBuffer and CCNx segment */
GstBuffer* gst_ccnx_segmenter_to_buffer (
const struct ccn_charbuf * segment, gint32& offset);
GstCCNxPacketHeader *
gst_ccnx_packet_header_unpack (const struct ccn_charbuf * header_buf)
{
GstCCNxPacketHeader * header =
(GstCCNxPacketHeader *) malloc (sizeof(GstCCNxPacketHeader));
gst_ccnx_unpack_be_guint (&header->mLeft, &header_buf->buf[0], 1);
gst_ccnx_unpack_be_guint (&header->mOffset, &header_buf->buf[1], 2);
gst_ccnx_unpack_be_guint (&header->mCount, &header_buf->buf[3], 1);
return header;
}
GstCCNxSegmentHeader *
gst_ccnx_segment_header_unpack (const struct ccn_charbuf * header_buf)
{
GstCCNxSegmentHeader * header =
(GstCCNxSegmentHeader *) malloc (sizeof(GstCCNxSegmentHeader));
gst_ccnx_unpack_be_guint (&header->mSize, &header_buf->buf[0], 4);
gst_ccnx_unpack_be_guint (&header->mTimestamp, &header_buf->buf[4], 8);
gst_ccnx_unpack_be_guint (&header->mDuration, &header_buf->buf[12], 8);
return header;
}
GstBuffer*
gst_ccnx_segmenter_to_buffer(const struct ccn_charbuf* segment, gint32& offset)
{
struct ccn_charbuf * segment_hdr_buffer = NULL;
GstCCNxSegmentHeader * segment_hdr = NULL;
GstBuffer * ret = NULL;
if (segment->length - offset < GST_CCNX_SEGMENT_HDR_LEN)
return NULL;
segment_hdr_buffer = ccn_charbuf_create ();
ccn_charbuf_append (
segment_hdr_buffer, segment->buf + offset, GST_CCNX_SEGMENT_HDR_LEN);
segment_hdr = gst_ccnx_segment_header_unpack (segment_hdr_buffer);
guint32 start = offset + GST_CCNX_SEGMENT_HDR_LEN;
guint32 end = start + segment_hdr->mSize;
if (end > segment->length)
goto ret_state;
ret = gst_buffer_new ();
gst_buffer_set_data (ret, segment->buf + start, segment_hdr->mSize);
ret->timestamp = segment_hdr->mTimestamp;
ret->duration = segment_hdr->mDuration;
offset = end;
ret_state:
/* clean up memory */
ccn_charbuf_destroy (&segment_hdr_buffer);
if (segment_hdr)
free (segment_hdr);
return ret;
}
/* public methods */
GstCCNxSegmenter *
gst_ccnx_segmenter_create (
GstCCNxDepacketizer * depkt, gst_ccnx_seg_callback func, guint32 max_size)
{
GstCCNxSegmenter * obj =
(GstCCNxSegmenter*) malloc (sizeof(GstCCNxSegmenter));
obj->mDepkt = depkt;
obj->mCallback = func;
obj->mMaxSize = max_size - GST_CCNX_PACKET_HDR_LEN;
obj->mPktContent = ccn_charbuf_create();
obj->mPktElements = 0;
obj->mPktElementOff = 0;
obj->mPktLost = FALSE;
return obj;
}
void
gst_ccnx_segmenter_destroy (GstCCNxSegmenter ** obj)
{
GstCCNxSegmenter * seg = *obj;
if (seg != NULL) {
/* mDepkt is just a back reference, we are not going to free it */
seg->mDepkt = NULL;
/* destroy dynamic allocated structs */
ccn_charbuf_destroy (&seg->mPktContent);
/* destroy the obj itself */
free (seg);
*obj = NULL;
}
}
void gst_ccnx_segmenter_pkt_lost (GstCCNxSegmenter* obj)
{
obj->mPktLost = TRUE;
if (obj->mPktContent == NULL)
obj->mPktContent = ccn_charbuf_create();
else
ccn_charbuf_reset(obj->mPktContent);
obj->mPktElements = 0;
}
void gst_ccnx_segmenter_process_pkt (
GstCCNxSegmenter* obj, const struct ccn_charbuf* pkt_buffer)
{
struct ccn_charbuf * hdr_buffer = NULL;
GstCCNxPacketHeader * pkt_header = NULL;
GstBuffer * buf = NULL;
hdr_buffer = ccn_charbuf_create ();
ccn_charbuf_append (hdr_buffer, pkt_buffer->buf, GST_CCNX_PACKET_HDR_LEN);
pkt_header = gst_ccnx_packet_header_unpack (hdr_buffer);
if ( !obj->mPktLost || obj->mPktContent->length > 0)
pkt_header->mOffset = 0;
pkt_header->mOffset += GST_CCNX_PACKET_HDR_LEN;
ccn_charbuf_append (obj->mPktContent,
pkt_buffer->buf + pkt_header->mOffset,
pkt_buffer->length - pkt_header->mOffset);
obj->mPktElements += pkt_header->mCount;
gint32 off = 0;
while (obj->mPktElements > 0) {
buf = gst_ccnx_segmenter_to_buffer (obj->mPktContent, off);
if (buf == NULL)
break;
if (obj->mPktLost) {
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
obj->mPktLost = FALSE;
}
obj->mCallback (obj->mDepkt, buf);
obj->mPktElements -= 1;
}
assert ((pkt_header->mLeft > 0 && obj->mPktElements == 1) ||
obj->mPktElements == 0);
/* clean up memory */
ccn_charbuf_destroy (&hdr_buffer);
free (pkt_header);
}