OSDN Git Service

Change a directory tree
[peercast-im/PeerCastIM.git] / core / common / asf.h
1 // ------------------------------------------------
2 // File : asf.h
3 // Date: 10-apr-2003
4 // Author: giles
5 //
6 // (c) 2002-3 peercast.org
7 // ------------------------------------------------
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 // ------------------------------------------------
18
19 #ifndef _ASF_H
20 #define _ASF_H
21
22
23 #include "stream.h"
24
25 // -----------------------------------
26 class MSID
27 {
28 public:
29
30         void read(Stream &in)
31         {
32                 data1 = in.readLong();
33                 data2 = in.readShort();
34                 data3 = in.readShort();
35                 in.read(data4,8);
36         }
37
38         void write(Stream &out)
39         {
40                 out.writeLong(data1);
41                 out.writeShort(data2);
42                 out.writeShort(data3);
43                 out.write(data4,8);
44         }
45         
46         
47         void toString(String &s)
48         {
49                 sprintf(s.data,"%X-%X-%X-%02X%02X%02X%02X%02X%02X%02X%02X",
50                         data1,data2,data3,
51                         data4[0],data4[1],data4[2],data4[3],
52                         data4[4],data4[5],data4[6],data4[7]);
53         }
54
55     int operator==(const MSID& msid) const{return !memcmp(this, &msid, sizeof(MSID));}
56
57         unsigned int data1;
58         unsigned short data2,data3;
59         unsigned char data4[8];
60
61 };
62
63
64
65
66 // -----------------------------------
67 const MSID headObjID=
68         {0x75B22630, 0x668E, 0x11CF, 0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C};
69 const MSID dataObjID=
70         {0x75B22636, 0x668E, 0x11CF, 0xA6,0xD9,0x00,0xAA,0x00,0x62,0xCE,0x6C};
71 const MSID filePropObjID=
72         {0x8CABDCA1, 0xA947, 0x11CF, 0x8E,0xE4,0x00,0xC0,0x0C,0x20,0x53,0x65};
73 const MSID streamPropObjID=
74         {0xB7DC0791, 0xA9B7, 0x11CF, 0x8E,0xE6,0x00,0xC0,0x0C,0x20,0x53,0x65};
75
76 const MSID audioStreamObjID=
77         {0xF8699E40, 0x5B4D, 0x11CF, 0xA8,0xFD,0x00,0x80,0x5F,0x5C,0x44,0x2B};
78 const MSID videoStreamObjID=
79         {0xBC19EFC0, 0x5B4D, 0x11CF, 0xA8,0xFD,0x00,0x80,0x5F,0x5C,0x44,0x2B};
80
81 const MSID streamBitrateObjID=
82         {0x7BF875CE, 0x468D, 0x11D1, 0x8D,0x82,0x00,0x60,0x97,0xC9,0xA2,0xB2};
83
84
85 // -----------------------------------
86 class ASFObject
87 {
88 public:
89
90         enum TYPE
91         {
92                 T_UNKNOWN,
93                 T_HEAD_OBJECT,
94                 T_DATA_OBJECT,
95                 T_FILE_PROP,
96                 T_STREAM_PROP,
97                 T_STREAM_BITRATE
98         };
99
100         int getTotalLen()
101         {
102                 return 24+dataLen;
103         }
104
105         unsigned int readHead(Stream &in)
106         {
107                 id.read(in);
108
109                 lenLo = in.readLong();
110                 lenHi = in.readLong();
111
112                 type = T_UNKNOWN;
113                 if (id == headObjID)
114                         type = T_HEAD_OBJECT;
115                 else if (id == dataObjID)
116                         type = T_DATA_OBJECT;
117                 else if (id == filePropObjID)
118                         type = T_FILE_PROP;
119                 else if (id == streamPropObjID)
120                         type = T_STREAM_PROP;
121                 else if (id == streamBitrateObjID)
122                         type = T_STREAM_BITRATE;
123
124                 String str;
125                 id.toString(str);
126                 LOG_DEBUG("ASF: %s (%s)= %d : %d\n",str.data,getTypeName(),lenLo,lenHi);
127
128
129                 dataLen = 0;
130
131                 return lenLo-24;
132         }
133
134         void readData(Stream &in,int len)
135         {
136                 dataLen = len;
137
138                 if ((dataLen > sizeof(data)) || (lenHi)) 
139                         throw StreamException("ASF object too big");
140
141                 in.read(data,dataLen);
142         }
143
144
145         void write(Stream &out)
146         {
147                 id.write(out);
148                 out.writeLong(lenLo);
149                 out.writeLong(lenHi);
150                 if (dataLen)
151                         out.write(data,dataLen);
152         }
153
154         const char *getTypeName()
155         {
156                 switch(type)
157                 {
158                         case T_HEAD_OBJECT:
159                                 return "ASF_Header_Object";
160                         case T_DATA_OBJECT:
161                                 return "ASF_Data_Object";
162                         case T_FILE_PROP:
163                                 return "ASF_File_Properties_Object";
164                         case T_STREAM_PROP:
165                                 return "ASF_Stream_Properties_Object";
166                         case T_STREAM_BITRATE:
167                                 return "ASF_Stream_Bitrate_Properties_Object";
168                         default:
169                                 return "Unknown_Object";
170                 }
171         }
172
173         char data[8192];
174         MSID    id;
175         unsigned int lenLo,lenHi,dataLen;
176         TYPE type;
177 };
178 // -----------------------------------
179 class ASFStream
180 {
181 public:
182         enum TYPE
183         {
184                 T_UNKNOWN,
185                 T_AUDIO,
186                 T_VIDEO
187         };
188
189         void read(Stream &in)
190         {
191                 MSID sid;
192                 sid.read(in);
193
194                 if (sid == videoStreamObjID)
195                         type = T_VIDEO;
196                 else if (sid == audioStreamObjID)
197                         type = T_AUDIO;
198                 else 
199                         type = T_UNKNOWN;
200
201                 in.skip(32);
202                 id = in.readShort()&0x7f;
203         }
204
205
206         const char *getTypeName()
207         {
208                 switch(type)
209                 {
210                         case T_VIDEO:
211                                 return "Video";
212                         case T_AUDIO:
213                                 return "Audio";
214                 }
215                 return "Unknown";
216         }
217
218         void reset()
219         {
220                 id = 0;
221                 bitrate = 0;
222                 type = T_UNKNOWN;
223         }
224
225         unsigned int id;
226         int bitrate;
227         TYPE type;
228 };
229
230 // -----------------------------------
231 class ASFInfo
232 {
233 public:
234         enum
235         {
236                 MAX_STREAMS = 128
237         };
238
239         ASFInfo()
240         {
241                 numPackets = 0;
242                 packetSize = 0;
243                 flags = 0;
244                 bitrate=0;
245                 for(int i=0; i<MAX_STREAMS; i++)
246                         streams[i].reset();
247         }
248
249         unsigned int packetSize,numPackets,flags,bitrate;
250
251         ASFStream streams[MAX_STREAMS];
252 };
253
254 // -----------------------------------
255 class ASFChunk
256 {
257 public:
258
259         void read(Stream &in)
260         {
261                 type = in.readShort();
262                 len = in.readShort();
263                 seq = in.readLong();
264                 v1 = in.readShort();
265                 v2 = in.readShort();
266
267                 dataLen = len-8;
268                 if (dataLen > sizeof(data)) 
269                         throw StreamException("ASF chunk too big");
270                 in.read(data,dataLen);
271         }
272
273         void write(Stream &out)
274         {
275                 out.writeShort(type);
276                 out.writeShort(len);
277                 out.writeLong(seq);
278                 out.writeShort(v1);
279                 out.writeShort(v2);
280                 out.write(data,dataLen);
281         }
282
283         unsigned int seq,dataLen;
284         unsigned short type,len,v1,v2;
285         unsigned char data[8192];
286 };
287
288
289 #endif
290