srs_core_amf0.cpp
5.1 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
The MIT License (MIT)
Copyright (c) 2013 winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_core_amf0.hpp>
#include <srs_core_log.hpp>
#include <srs_core_stream.hpp>
// AMF0 marker
#define RTMP_AMF0_Number 0x00
#define RTMP_AMF0_Boolean 0x01
#define RTMP_AMF0_String 0x02
#define RTMP_AMF0_Object 0x03
#define RTMP_AMF0_MovieClip 0x04 // reserved, not supported
#define RTMP_AMF0_Null 0x05
#define RTMP_AMF0_Undefined 0x06
#define RTMP_AMF0_Reference 0x07
#define RTMP_AMF0_EcmaArray 0x08
#define RTMP_AMF0_ObjectEnd 0x09
#define RTMP_AMF0_StrictArray 0x0A
#define RTMP_AMF0_Date 0x0B
#define RTMP_AMF0_LongString 0x0C
#define RTMP_AMF0_UnSupported 0x0D
#define RTMP_AMF0_RecordSet 0x0E // reserved, not supported
#define RTMP_AMF0_XmlDocument 0x0F
#define RTMP_AMF0_TypedObject 0x10
// AVM+ object is the AMF3 object.
#define RTMP_AMF0_AVMplusObject 0x11
// origin array whos data takes the same form as LengthValueBytes
#define RTMP_AMF0_OriginStrictArray 0x20
std::string srs_amf0_read_utf8(SrsStream* stream)
{
std::string str;
// len
if (!stream->require(2)) {
srs_warn("amf0 read string length failed");
return str;
}
int16_t len = stream->read_2bytes();
srs_verbose("amf0 read string length success. len=%d", len);
// empty string
if (len <= 0) {
srs_verbose("amf0 read empty string.");
return str;
}
// data
if (!stream->require(len)) {
srs_warn("amf0 read string data failed");
return str;
}
str = stream->read_string(len);
// support utf8-1 only
// 1.3.1 Strings and UTF-8
// UTF8-1 = %x00-7F
for (int i = 0; i < len; i++) {
char ch = *(str.data() + i);
if ((ch & 0x80) != 0) {
srs_warn("only support utf8-1, 0x00-0x7F, actual is %#x", (int)ch);
return "";
}
}
srs_verbose("amf0 read string data success. str=%s", str.c_str());
return str;
}
std::string srs_amf0_read_string(SrsStream* stream)
{
// marker
if (!stream->require(1)) {
srs_warn("amf0 read string marker failed");
return "";
}
char marker = stream->read_char();
if (marker != RTMP_AMF0_String) {
srs_warn("amf0 check string marker failed. marker=%#x, required=%#x", marker, RTMP_AMF0_String);
return "";
}
srs_verbose("amf0 read string marker success");
return srs_amf0_read_utf8(stream);
}
double srs_amf0_read_number(SrsStream* stream)
{
double value = 0;
// marker
if (!stream->require(1)) {
srs_warn("amf0 read number marker failed");
return value;
}
char marker = stream->read_char();
if (marker != RTMP_AMF0_Number) {
srs_warn("amf0 check number marker failed. marker=%#x, required=%#x", marker, RTMP_AMF0_Number);
return value;
}
srs_verbose("amf0 read number marker success");
// value
if (!stream->require(8)) {
srs_warn("amf0 read number value failed");
return value;
}
int64_t temp = stream->read_8bytes();
memcpy(&value, &temp, 8);
srs_verbose("amf0 read number value success. value=%.2f", value);
return value;
}
SrsAmf0Object* srs_amf0_read_object(SrsStream* stream)
{
SrsAmf0Object* value = NULL;
return value;
}
SrsAmf0Any::SrsAmf0Any()
{
marker = RTMP_AMF0_Null;
}
SrsAmf0Any::~SrsAmf0Any()
{
}
bool SrsAmf0Any::is_string()
{
return marker == RTMP_AMF0_String;
}
bool SrsAmf0Any::is_number()
{
return marker == RTMP_AMF0_Number;
}
bool SrsAmf0Any::is_object()
{
return marker == RTMP_AMF0_Object;
}
SrsAmf0String::SrsAmf0String()
{
marker = RTMP_AMF0_String;
}
SrsAmf0String::~SrsAmf0String()
{
}
SrsAmf0Number::SrsAmf0Number()
{
marker = RTMP_AMF0_Number;
value = 0;
}
SrsAmf0Number::~SrsAmf0Number()
{
}
SrsAmf0ObjectEOF::SrsAmf0ObjectEOF()
{
utf8_empty = 0x00;
object_end_marker = RTMP_AMF0_ObjectEnd;
}
SrsAmf0ObjectEOF::~SrsAmf0ObjectEOF()
{
}
SrsAmf0Object::SrsAmf0Object()
{
marker = RTMP_AMF0_Object;
}
SrsAmf0Object::~SrsAmf0Object()
{
std::map<std::string, SrsAmf0Any*>::iterator it;
for (it = properties.begin(); it != properties.end(); ++it) {
SrsAmf0Any* any = it->second;
delete any;
}
properties.clear();
}