OSDN Git Service

2ddafbcc5c144e4f6f324662c4f98d0a3ace95d7
[peercast-im/PeerCastIM.git] / PeerCast.root / PeerCast_1 / core / common / mms.cpp
1 // ------------------------------------------------
2 // File : mms.cpp
3 // Date: 28-may-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 #include "channel.h"
20 #include "mms.h"
21 #include "asf.h"
22 #ifdef _DEBUG
23 #include "chkMemoryLeak.h"
24 #define DEBUG_NEW new(__FILE__, __LINE__)
25 #define new DEBUG_NEW
26 #endif
27
28 // ------------------------------------------
29 ASFInfo parseASFHeader(Stream &in);
30
31
32 // ------------------------------------------
33 void MMSStream::readEnd(Stream &,Channel *)
34 {
35 }
36
37 // ------------------------------------------
38 void MMSStream::readHeader(Stream &,Channel *)
39 {
40 }
41 // ------------------------------------------
42 int MMSStream::readPacket(Stream &in,Channel *ch)
43 {
44         {
45                 ASFChunk chunk;
46
47                 chunk.read(in);
48
49                 switch (chunk.type)
50                 {
51                         case 0x4824:            // asf header
52                         {
53                                 MemoryStream mem(ch->headPack.data,sizeof(ch->headPack.data));
54
55                                 chunk.write(mem);
56
57
58
59
60                                 MemoryStream asfm(chunk.data,chunk.dataLen);
61                                 ASFObject asfHead;
62                                 asfHead.readHead(asfm);
63
64                                 ASFInfo asf = parseASFHeader(asfm);
65                                 LOG_DEBUG("ASF Info: pnum=%d, psize=%d, br=%d",asf.numPackets,asf.packetSize,asf.bitrate);
66                                 for(int i=0; i<ASFInfo::MAX_STREAMS; i++)
67                                 {
68                                         ASFStream *s = &asf.streams[i];
69                                         if (s->id)
70                                                 LOG_DEBUG("ASF Stream %d : %s, br=%d",s->id,s->getTypeName(),s->bitrate);
71                                 }
72
73                                 ch->info.bitrate = asf.bitrate/1000;
74
75                                 ch->headPack.type = ChanPacket::T_HEAD;
76                                 ch->headPack.len = mem.pos;
77                                 ch->headPack.pos = ch->streamPos;
78                                 ch->newPacket(ch->headPack);
79
80                                 ch->streamPos += ch->headPack.len;
81
82                                 break;
83                         }
84                         case 0x4424:            // asf data
85                         {
86
87                                 ChanPacket pack;
88
89                                 MemoryStream mem(pack.data,sizeof(pack.data));
90
91                                 chunk.write(mem);
92
93                                 pack.type = ChanPacket::T_DATA;
94                                 pack.len = mem.pos;
95                                 pack.pos = ch->streamPos;
96
97                                 ch->newPacket(pack);
98                                 ch->streamPos += pack.len;
99
100                                 break;
101                         }
102                         default:
103                                 throw StreamException("Unknown ASF chunk");
104
105                 }
106
107         }
108         return 0;
109 }
110
111
112 // -----------------------------------
113 ASFInfo parseASFHeader(Stream &in)
114 {
115         ASFInfo asf;
116
117         try
118         {
119                 int numHeaders = in.readLong();
120
121                 in.readChar();
122                 in.readChar();
123
124                 LOG_CHANNEL("ASF Headers: %d",numHeaders);
125                 for(int i=0; i<numHeaders; i++)
126                 {
127
128                         ASFObject obj;
129
130                         unsigned int l = obj.readHead(in);
131                         obj.readData(in,l);
132
133
134                         MemoryStream data(obj.data,obj.lenLo);
135
136
137                         switch (obj.type)
138                         {
139                                 case ASFObject::T_FILE_PROP:
140                                 {
141                                         data.skip(32);
142
143                                         unsigned int dpLo = data.readLong();
144                                         unsigned int dpHi = data.readLong();
145
146                                         data.skip(24);
147
148                                         data.readLong();
149                                         //data.writeLong(1);    // flags = broadcast, not seekable
150
151                                         int min = data.readLong();
152                                         int max = data.readLong();
153                                         int br = data.readLong();
154
155                                         if (min != max)
156                                                 throw StreamException("ASF packetsizes (min/max) must match");
157
158                                         asf.packetSize = max;
159                                         asf.bitrate = br;
160                                         asf.numPackets = dpLo;
161                                         break;
162                                 }
163                                 case ASFObject::T_STREAM_BITRATE:
164                                 {
165                                         int cnt = data.readShort();
166                                         for(int i=0; i<cnt; i++)
167                                         {
168                                                 unsigned int id = data.readShort();
169                                                 int bitrate = data.readLong();
170                                                 if (id < ASFInfo::MAX_STREAMS)
171                                                         asf.streams[id].bitrate = bitrate;
172                                         }
173                                         break;
174                                 }
175                                 case ASFObject::T_STREAM_PROP:
176                                 {
177                                         ASFStream s;
178                                         s.read(data);
179                                         asf.streams[s.id].id = s.id;
180                                         asf.streams[s.id].type = s.type;
181                                         break;
182                                 }
183                         }
184
185                 }
186         }catch(StreamException &e)
187         {
188                 LOG_ERROR("ASF: %s",e.msg);
189         }
190
191         return asf;
192 }
193
194