{"id":5731,"date":"2026-05-22T11:00:00","date_gmt":"2026-05-22T02:00:00","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=5731"},"modified":"2026-05-23T11:56:22","modified_gmt":"2026-05-23T02:56:22","slug":"mediaserver-mini_mediaserver_1-code-description-protocol-1-3","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/05\/22\/mediaserver-mini_mediaserver_1-code-description-protocol-1-3\/","title":{"rendered":"[Mediaserver]mini_Mediaserver_1-\ucf54\ub4dc\uc124\uba85\/code description-fmt,cs_id(1-3)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb void RtmpSession::read_chunk_header() {} \ud568\uc218\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.<br>This is a description of the void RtmpSession::read_chunk_header() {} function.<\/p>\n\n\n\n<p>\u2714\ufe0f \uc5ec\uae30\uc11c\ub294 \uc18c\ucf13\uc5d0\uc11c 1\ubc14\uc774\ud2b8(8\ube44\ud2b8)\ub97c \uc77d\uc5b4\uc640\uc11c fmt\uc640 cs_id\ub97c \ucd94\ucd9c\ud569\ub2c8\ub2e4.<br>Here, we read 1 byte (8 bits) from the socket to extract fmt and cs_id.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>fmt \uac12\/value<\/td><td>MessageHeader \ud06c\uae30<\/td><td>\uc758\ubbf8 \/ meaning<\/td><\/tr><tr><td>0<\/td><td>11Byte<\/td><td>\ud480 \ud5e4\ub354\/Full Header<\/td><\/tr><tr><td>1<\/td><td>7Byte<\/td><td>stream_id \uc0dd\ub7b5 \/ Stream_id omitted<\/td><\/tr><tr><td>2<\/td><td>3Byte<\/td><td>only timestamp<\/td><\/tr><tr><td>3<\/td><td>0Byte<\/td><td>\ud5e4\ub354 \uc5c6\uc74c<br>no header<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u2714\ufe0f \uc18c\ucf13\uc5d0\uc11c1 \ubc14\uc774\ud2b8 \uc77d\uae30<br>Read 1 byte from socket<\/p>\n\n\n\n<p>&#8212; 1\ubc14\uc774\ud2b8\ub294 8\ube44\ud2b8\uc785\ub2c8\ub2e4.(7,6,5,4,3,2,1,0)<br>1 byte is 8 bits. (7,6,5,4,3,2,1,0)<\/p>\n\n\n\n<p>&#8212; 8\ube44\ud2b8\uc758 \uac12\uc740 \uc124\uba85\uc744 \uc704\ud574 0x43 = 01000011 = 67 \uc774 \uac12\uc73c\ub85c \ud569\ub2c8\ub2e4.<br>For the purpose of explanation, the 8-bit value is set to 0x43 = 01000011 = 67.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>socket_.async_read_some(asio::buffer(&amp;chunk_basic_header_, 1)<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ubcc0\uc218 \uc0dd\ub7b5  \/ Variable omitted<\/p>\n\n\n\n<p>&#8212; C++\ubb38\ubc95\uc0c1 \uc0ac\uc6a9\ub418\uc9c0 \uc54a\ub294 \ubcc0\uc218\uba85 \uc0dd\ub7b5 \uac00\ub2a5\ud574\uc11c \uc0dd\ub7b5\ub418\uc5c8\uc2b5\ub2c8\ub2e4.<br>Variable names not used in C++ syntax can be omitted, so they have been omitted.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \ubcc0\uc218 \uc0dd\ub7b5\ub41c \ucf54\ub4dc\n# Code with omitted variables\n&#91;this, self](std::error_code ec, std::size_t) {...}\n\n# \ubcc0\uc218 \uc0dd\ub7b5 \uc774\uc804\n# Before variable omission\n&#91;this, self](std::error_code ec, std::size_t <strong>bytes_transferred<\/strong>) { ... }<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f fmt\uac12 \ucd94\ucd9c,\uc624\ub978\ucabd\uc73c\ub85c 6\ube44\ud2b8 \ubc00\uae30<br>Extract fmt value, shift right by 6 bits<\/p>\n\n\n\n<p>&#8212; fmt\uac12\uc740 0,1,2,3\ubc16\uc5d0 \uc5c6\uae30 \ub54c\ubb38\uc5d0 0,1\ube44\ud2b8\uc704\uce58\uc758 \uac12\ub9cc \uc0ac\uc6a9\ud569\ub2c8\ub2e4.<br>Since fmt values \u200b\u200bare only 0, 1, 2, and 3, only the values \u200b\u200bat bit positions 0 and 1 are used.<\/p>\n\n\n\n<p>&#8212; \uadf8\ub798\uc11c 6\ube44\ud2b8 \ubc00\uc5b4\uc11c 7\ubc88\ube44\ud2b8\uac00 1\ubc88\ube44\ud2b8\uc5d0 \uc624\ub3c4\ub85d \ube44\ud2b8\ub97c \ub0b4\ub9bd\ub2c8\ub2e4.<br>So, we shift by 6 bits to lower the bit so that the 7th bit becomes the 1st bit.<\/p>\n\n\n\n<p>&#8212; \ube44\ud2b8 \uc774\ub3d9\ud6c4\uc5d0<br>After moving the beat<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uint8_t fmt = (chunk_basic_header_ &gt;&gt; 6) &amp; 0x03;<\/code><\/pre>\n\n\n\n<p>&#8212; \uc624\ub978\ucabd\uc73c\ub85c 6\ube44\ud2b8 \ubc00\uae30\ub780?(\uc608:)<br>What is shifting 6 bits to the right? (e.g.)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\ube44\ud2b8\ubc88\ud638\/bit bumber: \n         &#91;7 6]&#91; 5 4 3 2 1 0]\n         &#91;fmt]&#91;   cs_id   ]\n          2-bit   6-bit<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>0x43 = 01000011 = 67\n\n\uc6d0\ubcf8\/original value: \n0 1 0 0 0 0 1 1\n\n>> 6 \uc801\uc6a9\/apply\n1. \uc624\ub978\ucabd\uc73c\ub85c 1\uce78\/1 spaces to the right: 00100001\n2. \uc624\ub978\ucabd\uc73c\ub85c 2\uce78\/2 spaces to the right: 00010000\n3. \uc624\ub978\ucabd\uc73c\ub85c 3\uce78\/3 spaces to the right: 00001000\n4. \uc624\ub978\ucabd\uc73c\ub85c 4\uce78\/4 spaces to the right: 00000100\n5. \uc624\ub978\ucabd\uc73c\ub85c 5\uce78\/5 spaces to the right: 00000010\n6. \uc624\ub978\ucabd\uc73c\ub85c 6\uce78\/6 spaces to the right: 00000001<\/code><\/pre>\n\n\n\n<p>&#8212; <code>&amp; 0x03<\/code>&nbsp;\uc774 6\ube44\ud2b8 0\uc73c\ub85c \ub36e\uc5b4 \uc50c\uc6b0\uae30<br>&amp; Overwrite 0x03 with 6-bit 0<\/p>\n\n\n\n<p>&#8211;AND\uc5f0\uc0b0\uc740 \ub458\ub2e4 1\uc778 \uacbd\uc6b0\ub9cc 1\uc774\ub428(&amp;)<br>The AND operation returns 1 only when both are 1 (&amp;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  01000001 \u2190 &gt;&gt; 6 \ube44\ud2b8\uc774\ub3d9\ud55c \uacb0\uacfc \/ 6 Bit shift result\n&amp; 00000011 \u2190 0x03 \ub9c8\uc2a4\ud06c \/ 0x03 mask\n\n  00000001 \u2190 \ud558\uc704 2\ube44\ud2b8\ub9cc \uc0b4\uc544\ub0a8\uc74c \/ Only the lower 2 bits survive<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f cs_id \ucd94\ucd9c \/ Extract cs_id<\/p>\n\n\n\n<p>&#8212; <code>&amp; 0x3F<\/code>\ub294&nbsp;\ud558\uc704 6\ube44\ud2b8\ub9cc \ubf51\uc544\uc11c&nbsp;<code>cs_id<\/code>&nbsp;\ub9cc\ub4dc\ub294 \uc785\ub2c8\ub2e4.<br>&amp; 0x3F is used to create cs_id by extracting only the lower 6 bits.<\/p>\n\n\n\n<p>&#8212; fmt\uac00 7,6\ubc88 \ube44\ud2b8\ub85c \ub9cc\ub4e4\uc5c8\uace0 \ub098\uba38\uc9c0 0~5\ube44\ud2b8\ub85c AND \uc5f0\uc0b0\ud558\uba74 10\uc9c4\uc218\ub85c 3\ub9cc \ub0a8\uc2b5\ub2c8\ub2e4.<br>fmt is created using bits 7 and 6, and performing an AND operation with the remaining bits 0 through 5 leaves only 3 in decimal.<\/p>\n\n\n\n<p>&#8212; \uadf8\ub798\uc11c \ub530\ub85c \ube44\ud2b8\uc774\ub3d9\uc740 \ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br>Therefore, we do not perform separate bit shifting.<\/p>\n\n\n\n<p>&#8211;AND\uc5f0\uc0b0\uc740 \ub458\ub2e4 1\uc778 \uacbd\uc6b0\ub9cc 1\uc774 \ub429\ub2c8\ub2e4.(&amp;)<br>The AND operation returns 1 only when both are 1. (&amp;)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  01000011 \u2190 chunk_basic_header_ = 0x43 = 67\n&amp; 00111111 \u2190 0x3F \ub9c8\uc2a4\ud06c \/ 0x3F mask\n\n  00000011 \u2190 \uacb0\uacfc \/ result = 3<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \uadf8\ub798\uc11c <code>fmt = 1<\/code>&nbsp;= &#8220;\ud5e4\ub354 7\ubc14\uc774\ud2b8&#8221;,<code>cs_id = 3<\/code>&nbsp;= &#8220;3\ubc88 \ucc44\ub110&#8221; \uc774 \ub429\ub2c8\ub2e4.<br>So, fmt = 1 = &#8220;7 bytes of header&#8221;, cs_id = 3 = &#8220;channel 3&#8221;.<\/p>\n\n\n\n<p>\u2714\ufe0f cs_id\ub294 2<sup>6<\/sup> = 64 (0~63)\uac1c\uc758 \ucc44\ub110\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.(cs_id = 0~63)<br>cs_id is 26 = 64 (0~63) channels can be used. (cs_id = 0~63)<\/p>\n\n\n\n<p>\u2714\ufe0f \ub9cc\uc57d cs_id == 0\uc774\uba74 1\ubc14\uc774\ud2b8\ub97c \ub354 \uc77d\uc5b4\uc11c \ucc44\ub110\uc744 (64+255=)319\uac1c \ub354 \ud655\uc7a5\ud569\ub2c8\ub2e4.<br>If cs_id == 0, read 1 more byte to expand the channel to (64+255=)319 more.<\/p>\n\n\n\n<p>&#8212; 1byte = 8bit = 255\uae4c\uc9c0 \uc0ac\uc6a9\uac00\ub2a5<br>1 byte = 8 bits = up to 255 can be used<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            if (cs_id == 0) {\n                uint8_t ext;\n                asio::async_read(socket_, asio::buffer(&amp;ext, 1),\n                    &#91;this, self, fmt, ext](std::error_code ec, std::size_t) {\n                        if (ec) return;\n                        read_message_header(fmt, 64 + ext);\n                    });\n            } <\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f cs_id == 1\uc774\uba74 2\ubc14\uc774\ud2b8\ub97c \uc77d\uc5b4\uc11c buf[2]\ubc30\uc5f4\uc5d0 \uac01\uac01 \uc800\uc7a5\ud569\ub2c8\ub2e4.<br>If cs_id == 1, read 2 bytes and store them in the buf[2] array.<\/p>\n\n\n\n<p>\u2714\ufe0f \ucd5c\ub300: 64 + 255 + (255&lt;&lt;8) = 64 + 255 + 65280 = 65599 \uac1c \uae4c\uc9c0\uc758 \ucc44\ub110\uc744 \uc0ac\uc6a9\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Max: 64 + 255 + (255&lt;&lt;8) = 64 + 255 + 65280 = 65599 channels can be used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            else if (cs_id == 1) {\n                uint8_t buf&#91;2];\n                asio::async_read(socket_, asio::buffer(buf, 2),\n                    &#91;this, self, fmt, buf](std::error_code ec, std::size_t) {\n                        if (ec) return;\n                        read_message_header(fmt, 64 + buf&#91;0] + (buf&#91;1] &lt;&lt; 8));\n                    });\n            } <\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f 64 + buf[0] + (buf[1] &lt;&lt; 8) \uacc4\uc0b0<\/p>\n\n\n\n<p>&#8212; \uc18c\ucf13\uc73c\ub85c&nbsp;<code>0x10 0x01<\/code>&nbsp;2\ubc14\uc774\ud2b8 \uac12\uc774 \ub0a0\uc544\uc624\uba74<br>If a 2-byte value 0x10 0x01 is sent through the socket<\/p>\n\n\n\n<p>&#8212; buf[0] = 0x10 = 00000001 = 1 ,buf[1] = 0x01 = 00010000 = 16 <\/p>\n\n\n\n<p>&#8212; \ub9ac\ud2c0\uc5d4\ub514\uc548\uc778\uacbd\uc6b0 \uc0c1\uc704 \ubc14\uc774\ud2b8\ub97c \uc67c\ucabd\uc73c\ub85c 8\ube44\ud2b8 \ubc00\uc5b4\uc11c 256(=<code>100000000<\/code>&nbsp;)\uc744 \ub9cc\ub4e6<br>In the case of Little Endian, the upper byte is shifted left by 8 bits to create 256 (= 100000000).<\/p>\n\n\n\n<p>&#8212; \uc608\uc2dc \/ example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>buf&#91;1]=0x01: 0x01 &lt;&lt; 8 = 0x0100 = 256 ,\nbuf&#91;0]=0x10: 0x10 = 16\n\ud569\uacc4\/total: 256 + 16 = 272\n\ucd5c\uc885\/final cs_id = 64 + 272 = 336<\/code><\/pre>\n\n\n\n<p>&#8212; \ucd5c\uc18c\uac12\/minimum value :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>buf&#91;0] = 0x00 = 0, \nbuf&#91;1] = 0x00 = 0 ,\ncs_id = 64 + 0 + (0 &lt;&lt; 8) = 64 + 0 + 0 = 64<\/code><\/pre>\n\n\n\n<p>&#8212; \ucd5c\ub300\uac12\/maximum value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>buf&#91;0] = 0xFF = 255, \nbuf&#91;1] = 0xFF = 255, \nbuf&#91;1] &lt;&lt; 8 = 0xFF &lt;&lt; 8 = 0xFF00 = 65280,\ncs_id = 64 + 255 + 65280 = 65599<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ud568\uc218 \uc804\uccb4 \ucf54\ub4dc \/ full function code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void RtmpSession::read_chunk_header() {\n    auto self(shared_from_this());\n    \/\/ 1\ubc14\uc774\ud2b8\ub9cc \uc77d\uae30 \/ Read only 1 byte\n    socket_.async_read_some(asio::buffer(&amp;chunk_basic_header_, 1),\n        &#91;this, self](std::error_code ec, std::size_t) {\n            if (ec) {\n                std::cout &lt;&lt; \"&#91;Info] Connection closed: \" &lt;&lt; ec.message() &lt;&lt; \"\\n\";\n                return;\n            }\n            uint8_t fmt = (chunk_basic_header_ &gt;&gt; 6) &amp; 0x03;\n            uint32_t cs_id = chunk_basic_header_ &amp; 0x3F;\n            if (cs_id == 0) {\n                uint8_t ext;\n                asio::async_read(socket_, asio::buffer(&amp;ext, 1),\n                    &#91;this, self, fmt, ext](std::error_code ec, std::size_t) {\n                        if (ec) return;\n                        read_message_header(fmt, 64 + ext);\n                    });\n            } else if (cs_id == 1) {\n                uint8_t buf&#91;2];\n                asio::async_read(socket_, asio::buffer(buf, 2),\n                    &#91;this, self, fmt, buf](std::error_code ec, std::size_t) {\n                        if (ec) return;\n                        read_message_header(fmt, 64 + buf&#91;0] + (buf&#91;1] &lt;&lt; 8));\n                    });\n            } else {\n                read_message_header(fmt, cs_id);\n            }\n        });\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb void RtmpSession::read_chunk_header() {} \ud568\uc218\uc5d0 \ub300\ud55c \uc124\uba85\uc785\ub2c8\ub2e4.This is a description of the void RtmpSession::read_chunk_header() {} function. \u2714\ufe0f \uc5ec\uae30\uc11c\ub294 \uc18c\ucf13\uc5d0\uc11c 1\ubc14\uc774\ud2b8(8\ube44\ud2b8)\ub97c \uc77d\uc5b4\uc640\uc11c fmt\uc640 cs_id\ub97c \ucd94\ucd9c\ud569\ub2c8\ub2e4.Here, we read 1 byte (8 bits) from the socket to extract fmt and cs_id. fmt \uac12\/value MessageHeader \ud06c\uae30 \uc758\ubbf8 \/ meaning 0 11Byte \ud480 \ud5e4\ub354\/Full Header 1 7Byte stream_id \uc0dd\ub7b5 \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,1],"tags":[],"class_list":["post-5731","post","type-post","status-publish","format-standard","hentry","category-cpp","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5731","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=5731"}],"version-history":[{"count":28,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5731\/revisions"}],"predecessor-version":[{"id":5760,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/5731\/revisions\/5760"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=5731"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=5731"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=5731"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}