[Mediaserver]mini_Mediaserver_1-코드설명/code description-amf0.cpp(1-8)

👉🏻 아래는 amf0.cpp 파일이 함수에 대한 설명입니다.
Below is a description of the functions in the amf0.cpp file.

👉🏻 코드 설명 / Code Explanation

✔️ void Amf0::parse_object(const std::vector& buf, size_t& pos) { }

— RTMP 패킷 안에 들어있는 AMF0 Object를 읽어서 버퍼 포인터를 다음 필드로 옮기는 용도입니다.
It is used to read an AMF0 object contained within an RTMP packet and advance the buffer pointer to the next field.

— 예시데이터(버퍼 페이로드)
Example data (buffer payload)

03 | 00 03 'a' 'p' 'p' | 02 00 04 'l' 'i' 'v' 'e' | 00 05 'f' 'p' 'a' 'd' | 01 00 | 00 00 09
pos 0 : 03 // AMF0 Object 시작 / AMF0 Object Start
pos 1-2: 00 03 // key 길이 3 / Key length 3
pos 3-5: 61 70 70 // 'a' 'p' 'p'
pos 6 : 02 // value 타입 = string / value type = string
pos 7-8: 00 04 // string 길이 4 / String length 4
pos 9-12: 6c 69 76 65 // 'l' 'i' 'v' 'e'
pos13-14: 00 05 // key 길이 5 / Key length 5
pos15-19: 66 70 61 64 // 'f' 'p' 'a' 'd'
pos20 : 01 // value 타입 = boolean / value type = boolean
pos21 : 00 // false
pos22-23: 00 00 // key 길이 0 / key length 0
pos24 : 09 // object end

— pos 값은 0인 경우로 합니다.
Assume the value of ‘pos’ is 0.

— 0x03으로시작하는지 확인
Check if it starts with 0x03.

if (pos >= buf.size() || buf[pos]!= 0x03) return;

— 0x03위치 다음으로 포인터이동,1,pos기본값 = 0
0x03 Move pointer to position; default pos = 0.

pos++;

— while (3 < 25) 인경우 입니다.
This is the case where while (3 < 25) applies.

while (pos + 2 < buf.size()) { ...} 

— buf[pos]를 상위바이트로 이동하고 하위는 8비트( 1바이트 로 채웁니다.(1바이트 사용시 16진수는 두자리 사용합니다.)
Move buf[pos] to the high byte and fill the low byte with 8 bits (1 byte). (Two hexadecimal digits are used for a single byte.)

— pos=1 : buf[1] = 0x00,pos=2 : buf[2] = 0x03,len = (0x00 << 8) | 0x03 = 0x0000 | 0x0003 = 3

pos=1 : buf[1] = 0x00
pos=2 : buf[2] = 0x03
len = (0x00 << 8) | 0x03 = 0x0000 | 0x0003 = 3

— 여기서 pos=3 이 됩니다.
Here, pos becomes 3.

pos += 2;

— if( 3 < 25 && buf[3] == 0x09)이며 ,0x09는 버퍼끝을 의미합니다.
If 3 < 25 && buf[3] == 0x09 is true, then 0x09 signifies the end of the buffer.

 if (pos < buf.size() && buf[pos] == 0x09) { pos++; break; }

— if(3 + 3) > 25 return;이 됩니다.
It becomes if(3 + 3) > 25 return;.

if (pos + len > buf.size()) return;

— vector에서 지정한 구간만큼 잘라서 std::string으로 복사하는 생성자입니다.
This is a constructor that extracts a specified range from a vector and copies it into an std::string.

— string key(3,6)이되고 ,6앞까지만 사용하므로 6은 포함되지 않습니다.
The string key is (3, 6); since it is used only up to the position before 6, the value 6 is not included.

— 그러면 버퍼에서 app이란 글자를 추출하게됩니다.
Then, the string “app” is extracted from the buffer.

std::string key(buf.begin() + pos, buf.begin() + pos + len);

— pos가 3이고 len이니까 결국 pos는 6이 됩니다.
Since pos is 3 and we add len, pos ultimately becomes 6.

pos += len;

— skip_value(buf, 6) 이 실행됩니다.
skip_value(buf, 6) is executed.

skip_value(buf, pos);

✔️ static void skip_value(const std::vector& buf, size_t& pos) { … }

— parse_object에서 pos초기 값이 0인 경우 여기는 6이 됩니다.
If the initial value of pos in parse_object is 0, it becomes 6 here.

— pos 0 → 03,pos 6 → 02,pos 19 → 01 이 세곳외에는 데이터를 읽는 부분이 없습니다.
There are no data-reading points other than these three locations: pos 0 → 03, pos 6 → 02, and pos 19 → 01.

— 아래의 case에서도 나머지는 적용 되지 않습니다.
The rest do not apply in the case below either.

    if (pos >= buf.size()) return;
    uint8_t t = buf[pos];

— 숫자인 경우를 의미하며 현재는 해당하는 부분이 없습니다.(pos가 00를 읽을 일이 없음)
This refers to the case where the value is a number; currently, there is no corresponding instance (as pos never reads 00).

case 0x00: pos += 9; break;

— case 0x01: boolean<-pos+=2->인건 그냥 AMF0 스펙에 따라 정해진것 입니다.
The case 0x01: boolean<-pos+=2-> part is simply determined by the AMF0 specification.

— buf[20] 인경우 pos는 22가되며 값은 00가 됩니다.
If buf[20] is used, pos becomes 22 and the value becomes 00.

case 0x01: pos += 2; break;

— buf[6], pos=6

case 0x02: { size_t tmp=pos; Amf0::parse_string(buf, tmp); pos=tmp; break; }

— pos = 0 인경우
When pos is 0

— Amf0::parse_object(buf, 6); 값이 대입됩니다.
The value Amf0::parse_object(buf, 6) is assigned.

— 위의 함수내에서 std::string str(buf.begin() + pos, buf.begin() + pos + len); 이 부분으로 문자열을 추출합니다.
Within the function above, the string is extracted using this part: std::string str(buf.begin() + pos, buf.begin() + pos + len);.

— std::string str(9,13); 이렇게 값이 대입됩니다.
The value is assigned like this: std::string str(9,13);.

— 실제로추출되는 문자열은 live입니다.
The string actually extracted is “live”.

case 0x03: { Amf0::parse_object(buf, pos); break; }

— null,undefinded인 경우 입니다.
This applies to cases where the value is null or undefined.

 case 0x05: case 0x06: pos += 1; break; 

— ECMA array 부분으로 버퍼에 값이 없습니다. 추후에 추가될 기능입니다.
There are no values ​​in the buffer for the ECMA array section; this is a feature to be added later.

while (pos + 2 < buf.size()) {
      uint16_t len = (buf[pos] << 8) | buf[pos+1];
      pos += 2;
      if (len == 0 && pos < buf.size() && buf[pos] == 0x09) { pos++; break; }
         pos += len;
         skip_value(buf, pos);
}

Leave a Reply