{"id":14192,"date":"2024-08-01T10:35:24","date_gmt":"2024-08-01T01:35:24","guid":{"rendered":"https:\/\/www.ecomottblog.com\/?p=14192"},"modified":"2024-08-01T11:12:09","modified_gmt":"2024-08-01T02:12:09","slug":"%ef%bc%8alivekit%ef%bc%8awebrtc%e3%81%a7%e3%83%aa%e3%82%a2%e3%83%ab%e3%82%bf%e3%82%a4%e3%83%a0%e5%8b%95%e7%94%bb%e9%85%8d%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.ecomottblog.com\/?p=14192","title":{"rendered":"\uff0aLiveKit\uff0aWebRTC\u3067\u30ea\u30a2\u30eb\u30bf\u30a4\u30e0\u52d5\u753b\u914d\u4fe1"},"content":{"rendered":"<p>G\u30ea\u30dd\u30fc\u30c8\u306e\u958b\u767a\u3092\u62c5\u5f53\u3057\u3066\u3044\u308b\u8d99\u3067\u3059\u3002<\/p>\n<p>\u524d\u56de\u306f\u7121\u6599\u3067\u4f7f\u3048\u308bliveKit\u3092\u69cb\u7bc9\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"3YLjnGFQa7\"><p><a href=\"https:\/\/www.ecomottblog.com\/?p=11305\">\uff0aLiveKit\uff0a\u7121\u6599\u3067\u30d3\u30c7\u30aa\u901a\u8a71\u304c\u3067\u304d\u308b\u30b5\u30fc\u30d0\u30fc\u3092\u69cb\u7bc9<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;\uff0aLiveKit\uff0a\u7121\u6599\u3067\u30d3\u30c7\u30aa\u901a\u8a71\u304c\u3067\u304d\u308b\u30b5\u30fc\u30d0\u30fc\u3092\u69cb\u7bc9&#8221; &#8212; ecomott blog\" src=\"https:\/\/www.ecomottblog.com\/?p=11305&#038;embed=true#?secret=xc5qmh6iGB#?secret=3YLjnGFQa7\" data-secret=\"3YLjnGFQa7\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p><span class=\"ui-provider a b c d e f g h i j k l m n o p q r s t u v w x y z ab ac ae af ag ah ai aj ak\" dir=\"ltr\">\u5f15\u304d\u7d9a\u304d\u4eca\u56de\u3001\u3053\u306e\u30b5\u30fc\u30d0\u30fc\u3092\u5229\u7528\u3057\u3066\u52d5\u753b\u914d\u4fe1\u3092\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\uff0a\u305f\u3060\u3057\u6620\u50cf\u3060\u3051<\/span><\/p>\n<p><!--more--><\/p>\n<h3>\u5229\u7528\u3059\u308bSDK<\/h3>\n<p>\u524d\u56de\u3054\u7d39\u4ecb\u3057\u305f\u3088\u3046\u306b\u3001LiveKit\u306b\u306f\u8c4a\u5bcc\u306aSDK\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001<\/p>\n<p>\u3055\u307e\u3056\u307e\u306a\u8a00\u8a9e\u3067\u306e\u5b9f\u88c5\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u52d5\u753b\u914d\u4fe1\u306e\u65b9\u6cd5\u3068\u3057\u3066\u3088\u304f\u4f7f\u308f\u308c\u308b\u306e\u306fFFmpeg\u3067\u3059\u304c\u3001<\/p>\n<p>\u4eca\u5f8c\u306f\u52d5\u753b\u3092\u30d5\u30ec\u30fc\u30e0\u3054\u3068\u306b\u51e6\u7406\u3057\u306a\u304c\u3089\u914d\u4fe1\u3059\u308b\u53ef\u80fd\u6027\u3082\u8003\u616e\u3057\u3001\u4eca\u56de\u306f<a href=\"https:\/\/github.com\/livekit\/python-sdks\">Python\u30e9\u30a4\u30d6\u30e9\u30ea<\/a>\u3092\u5229\u7528\u3057\u3066\u5b9f\u88c5\u3057\u307e\u3059\u3002<\/p>\n<p>\u52d5\u753b\u914d\u4fe1\u306e\u30b5\u30f3\u30d7\u30eb\u3068\u3057\u3066 <a href=\"https:\/\/github.com\/livekit\/python-sdks\/blob\/main\/examples\/publish_hue.py\"><code>publish_hue.py<\/code><\/a> \u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u305d\u308c\u3092\u30d9\u30fc\u30b9\u306b\u6539\u9020\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306e\u6982\u8981<\/h3>\n<p>\u30d3\u30c7\u30aa\u306e\u8272\u76f8\uff08Hue\uff09\u3092\u5468\u671f\u7684\u306b\u5909\u5316\u3055\u305b\u308b\u3053\u3068\u3067\u3001\u8272\u304c\u5909\u308f\u308b\u30d3\u30c7\u30aa\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u4f5c\u6210\u3057\u3066\u3001\u6307\u5b9a\u3057\u305f\u30eb\u30fc\u30e0\u306b\u914d\u4fe1\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true\">import asyncio\nimport colorsys\nimport logging\nimport os\nfrom signal import SIGINT, SIGTERM\n\nimport numpy as np\nfrom livekit import api, rtc\n\nWIDTH, HEIGHT = 1280, 720\n\n# ensure LIVEKIT_URL, LIVEKIT_API_KEY, and LIVEKIT_API_SECRET are set\n\nasync def main(room: rtc.Room):\ntoken = (\napi.AccessToken()\n.with_identity(\"python-publisher\")\n.with_name(\"Python Publisher\")\n.with_grants(\napi.VideoGrants(\nroom_join=True,\nroom=\"my-room\",\n)\n)\n.to_jwt()\n)\nurl = os.getenv(\"LIVEKIT_URL\")\nlogging.info(\"connecting to %s\", url)\ntry:\nawait room.connect(url, token)\nlogging.info(\"connected to room %s\", room.name)\nexcept rtc.ConnectError as e:\nlogging.error(\"failed to connect to the room: %s\", e)\nreturn\n\n# publish a track\nsource = rtc.VideoSource(WIDTH, HEIGHT)\ntrack = rtc.LocalVideoTrack.create_video_track(\"hue\", source)\noptions = rtc.TrackPublishOptions()\noptions.source = rtc.TrackSource.SOURCE_CAMERA\npublication = await room.local_participant.publish_track(track, options)\nlogging.info(\"published track %s\", publication.sid)\n\nasyncio.ensure_future(draw_color_cycle(source))\n\nasync def draw_color_cycle(source: rtc.VideoSource):\nargb_frame = bytearray(WIDTH * HEIGHT * 4)\narr = np.frombuffer(argb_frame, dtype=np.uint8)\n\nframerate = 1 \/ 30\nhue = 0.0\n\nwhile True:\nstart_time = asyncio.get_event_loop().time()\n\nrgb = colorsys.hsv_to_rgb(hue, 1.0, 1.0)\nrgb = [(x * 255) for x in rgb] # type: ignore\n\nargb_color = np.array(rgb + [255], dtype=np.uint8)\narr.flat[::4] = argb_color[0]\narr.flat[1::4] = argb_color[1]\narr.flat[2::4] = argb_color[2]\narr.flat[3::4] = argb_color[3]\n\nframe = rtc.VideoFrame(WIDTH, HEIGHT, rtc.VideoBufferType.RGBA, argb_frame)\nsource.capture_frame(frame)\nhue = (hue + framerate \/ 3) % 1.0\n\ncode_duration = asyncio.get_event_loop().time() - start_time\nawait asyncio.sleep(1 \/ 30 - code_duration)\n\nif __name__ == \"__main__\":\nlogging.basicConfig(\nlevel=logging.INFO,\nhandlers=[logging.FileHandler(\"publish_hue.log\"), logging.StreamHandler()],\n)\n\nloop = asyncio.get_event_loop()\nroom = rtc.Room(loop=loop)\n\nasync def cleanup():\nawait room.disconnect()\nloop.stop()\n\nasyncio.ensure_future(main(room))\nfor signal in [SIGINT, SIGTERM]:\nloop.add_signal_handler(signal, lambda: asyncio.ensure_future(cleanup()))\n\ntry:\nloop.run_forever()\nfinally:\nloop.close()<\/pre>\n<p><strong>main<\/strong>\u95a2\u6570\u3068<strong>draw_color_cycle<\/strong>\u95a2\u6570\u306b\u6ce8\u76ee\u3057\u305f\u3044\u3067\u3059\u3002<\/p>\n<p><strong>main<\/strong>\u95a2\u6570\u306e\u5f79\u5272<\/p>\n<ul>\n<li><strong>\u30c8\u30fc\u30af\u30f3\u306e\u751f\u6210\u3068\u63a5\u7d9a:<\/strong> LiveKit\u30b5\u30fc\u30d0\u30fc\u3078\u306e\u63a5\u7d9a\u306b\u5fc5\u8981\u306a\u30c8\u30fc\u30af\u30f3\u3092\u751f\u6210\u3057\u3001\u30b5\u30fc\u30d0\u30fc\u306b\u63a5\u7d9a\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30d3\u30c7\u30aa\u30c8\u30e9\u30c3\u30af\u306e\u516c\u958b:<\/strong> \u65b0\u3057\u3044\u30d3\u30c7\u30aa\u30bd\u30fc\u30b9\u3068\u30c8\u30e9\u30c3\u30af\u3092\u4f5c\u6210\u3057\u3001\u30eb\u30fc\u30e0\u306b\u516c\u958b\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30d3\u30c7\u30aa\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u66f4\u65b0:<\/strong> \u8272\u304c\u5909\u308f\u308b\u30d3\u30c7\u30aa\u30b9\u30c8\u30ea\u30fc\u30e0\u3092\u975e\u540c\u671f\u3067\u63cf\u753b\u3059\u308b\u30bf\u30b9\u30af\u3092\u958b\u59cb\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><strong>draw_color_cycle<\/strong>\u95a2\u6570\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6a5f\u80fd\u3057\u307e\u3059<\/p>\n<ul>\n<li><strong>\u30d3\u30c7\u30aa\u30d5\u30ec\u30fc\u30e0\u306e\u6e96\u5099:<\/strong> \u30d3\u30c7\u30aa\u30d5\u30ec\u30fc\u30e0\u3092\u683c\u7d0d\u3059\u308b\u30d0\u30c3\u30d5\u30a1\u3092\u6e96\u5099\u3057\u3001numpy\u914d\u5217\u3092\u4f7f\u3063\u3066\u30a2\u30af\u30bb\u30b9\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u8272\u306e\u5909\u5316:<\/strong> \u8272\u76f8\uff08hue\uff09\u3092\u5468\u671f\u7684\u306b\u5909\u5316\u3055\u305b\u3001HSV\u304b\u3089RGB\u306b\u5909\u63db\u3057\u3001\u30d3\u30c7\u30aa\u30d5\u30ec\u30fc\u30e0\u306b\u8a2d\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30d5\u30ec\u30fc\u30e0\u306e\u30ad\u30e3\u30d7\u30c1\u30e3:<\/strong> \u5909\u66f4\u3055\u308c\u305f\u30d3\u30c7\u30aa\u30d5\u30ec\u30fc\u30e0\u3092\u30d3\u30c7\u30aa\u30bd\u30fc\u30b9\u306b\u30ad\u30e3\u30d7\u30c1\u30e3\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30d5\u30ec\u30fc\u30e0\u30ec\u30fc\u30c8\u306e\u7ba1\u7406:<\/strong> \u30d5\u30ec\u30fc\u30e0\u30ec\u30fc\u30c8\u306b\u5408\u308f\u305b\u3066\u51e6\u7406\u3092\u8abf\u6574\u3057\u3001\u30eb\u30fc\u30d7\u3092\u7d99\u7d9a\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p>\u3053\u3053\u3067\u3084\u308a\u305f\u3044\u3053\u3068\u306f\u3001draw_color_cycle\u3092\u307e\u306d\u3066\u3001\u8272\u76f8\u306e\u4ee3\u308f\u308a\u306b\u52d5\u753b\u30d5\u30ec\u30fc\u30e0\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3059\u308b\u3053\u3068<\/p>\n<h3>\u30b3\u30fc\u30c9\u6539\u9020<\/h3>\n<ul>\n<li><span class=\"ui-provider a b c d e f g h i j k l m n o p q r s t u v w x y z ab ac ae af ag ah ai aj ak\" dir=\"ltr\">\u307e\u305a\u306f\u52d5\u753b\u306e\u30d5\u30ec\u30fc\u30e0\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3059\u308b\u3053\u3068\u304b\u3089<\/span><\/li>\n<\/ul>\n<p>Python\u306b\u99b4\u67d3\u307f\u306e\u3042\u308b\u65b9\u306a\u3089\u3088\u304f\u76ee\u306b\u3059\u308bOpenCV(cv2)\u3092\u4f7f\u3063\u3066\u30d5\u30ec\u30fc\u30e0\u62bd\u51fa\u3057\u307e\u3059\u3002<\/p>\n<p>\uff0a\u30c6\u30b9\u30c8\u7528\u52d5\u753b\u306f\u3053\u3061\u3089<\/p>\n<div>https:\/\/test-videos.co.uk\/vids\/bigbuckbunny\/mp4\/h264\/720\/Big_Buck_Bunny_720_10s_1MB.mp4\u3000\uff08\u30b3\u30fc\u30c7\u30a3\u30c3\u30af\uff1ah264\uff09<\/div>\n<div>&nbsp;<\/div>\n<div>\n<pre class=\"lang:python decode:true\"> video_capture = cv2.VideoCapture(INPUT)\n\n while True:\n    success, frame = video_capture.read()<\/pre>\n<\/div>\n<p>video_capture.read()\u304c\u6210\u529f\u3057\u305f\u5834\u5408\u3001frame\u306b\u52d5\u753b\u306e\u30d5\u30ec\u30fc\u30e0\u30c7\u30fc\u30bf\u304c\u4fdd\u5b58\u3055\u308c\u308b\u304c\u3001<\/p>\n<p>\u30d5\u30ec\u30fc\u30e0\u305d\u306e\u307e\u307e\u3067\u306f\u914d\u4fe1\u3067\u304d\u306a\u3044\u305f\u3081\u3001rtc.VideoFrame\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u5fc5\u8981\u3067\u3001<\/p>\n<p>\u307e\u305a\u306fframe\u306e\u5f62\u5f0f\u3092\u8abf\u3079\u307e\u3059\u3002<\/p>\n<pre class=\"lang:python decode:true\">num_channels = frame.shape[2] if len(frame.shape) == 3 else 1\n\n        # # Determine the frame type based on the number of channels\n        if num_channels == 1:\n            print(\"Grayscale frame\")\n        elif num_channels == 3:\n            print(\"RGB frame\")\n        elif num_channels == 4:\n            print(\"RGBA frame\")\n        elif num_channels == 2:\n            print(\"YUV frame\")\n        else:<\/pre>\n<p>\u7d50\u679cRGB frame\u3067\u3057\u305f\u3001\u30a2\u30eb\u30d5\u30a1\u30c1\u30e3\u30f3\u30cd\u30eb\u3092\u8ffd\u52a0\u3057\u3066\u30d5\u30ec\u30fc\u30e0\u5909\u63db<\/p>\n<pre class=\"lang:python decode:true \">alpha_channel = np.full((frame.shape[0], frame.shape[1]), 255, dtype=np.uint8)\n\nrgba_frame = cv2.merge((frame, alpha_channel))\n\nheight, width = frame.shape[:2]  # get frame size\n\nnew_frame = rtc.VideoFrame(width, height, rtc.VideoBufferType.BGRA, rgba_frame.tobytes())<\/pre>\n<p>\u3042\u3068\u306f\u3067\u304d\u305f<strong>new_frame<\/strong>\u3092\u30ad\u30e3\u30d7\u30c1\u30e3\u3059\u308b\u3060\u3051<\/p>\n<pre class=\"lang:python decode:true\">source.capture_frame(new_frame)<\/pre>\n<h3>\u52d5\u4f5c\u30c1\u30a7\u30c3\u30af<\/h3>\n<p><span class=\"ui-provider a b c d e f g h i j k l m n o p q r s t u v w x y z ab ac ae af ag ah ai aj ak\" dir=\"ltr\">\u524d\u56de\u69cb\u7bc9\u3057\u305fLiveKit\u30b5\u30fc\u30d0\u30fc\u306b\u914d\u4fe1\u3057\u3066\u307f\u307e\u3057\u305f\uff01<\/span><\/p>\n<div style=\"width: 1600px;\" class=\"wp-video\"><!--[if lt IE 9]><script>document.createElement('video');<\/script><![endif]-->\n<video class=\"wp-video-shortcode\" id=\"video-14192-1\" width=\"1600\" height=\"1012\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/www.ecomottblog.com\/wp-content\/uploads\/2024\/08\/20240801105744.mp4?_=1\" \/><a href=\"https:\/\/www.ecomottblog.com\/wp-content\/uploads\/2024\/08\/20240801105744.mp4\">https:\/\/www.ecomottblog.com\/wp-content\/uploads\/2024\/08\/20240801105744.mp4<\/a><\/video><\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u30a8\u30b3\u30e2\u30c3\u30c8\u3067\u306f\u3001WebRTC\u3092\u306f\u3058\u3081\u3068\u3059\u308b\u3055\u307e\u3056\u307e\u306a\u6280\u8853\u3092\u6d3b\u7528\u3057\u3001<\/p>\n<p>IoT\u306b\u95a2\u9023\u3057\u305f\u69d8\u3005\u306a\u6848\u4ef6\u306b\u53d6\u308a\u7d44\u3093\u3067\u3044\u307e\u3059\u3002<\/p>\n<p>\u79c1\u305f\u3061\u3068\u4e00\u7dd2\u306b\u30e2\u30ce\u3065\u304f\u308a\u306b\u6311\u6226\u3057\u3066\u3044\u305f\u3060\u3051\u308b\u65b9\u3092\u52df\u96c6\u4e2d\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<div style=\"margin-bottom: 32px;text-align: center\"><a style=\"border: solid 1px #1ABB9A;padding: 16px 72px\" href=\"https:\/\/www.ecomott.co.jp\/recruit\/?__hstc=42779642.76f9c1b6731348a1b4133ac7d2d6aa41.1714096199012.1722386207987.1722406485127.12&amp;__hssc=42779642.37.1722406485127&amp;__hsfp=1113258742\" target=\"_blank\" rel=\"noopener\">\u52df\u96c6\u8981\u9805<\/a><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>G\u30ea\u30dd\u30fc\u30c8\u306e\u958b\u767a\u3092\u62c5\u5f53\u3057\u3066\u3044\u308b\u8d99\u3067\u3059\u3002 \u524d\u56de\u306f\u7121\u6599\u3067\u4f7f\u3048\u308bliveKit\u3092\u69cb\u7bc9\u3092\u3057\u3066\u307f\u307e\u3057\u305f\u3002 \uff0aLiveKit\uff0a\u7121\u6599\u3067\u30d3\u30c7\u30aa\u901a\u8a71\u304c\u3067\u304d\u308b\u30b5\u30fc\u30d0\u30fc\u3092\u69cb\u7bc9 \u5f15\u304d\u7d9a\u304d\u4eca\u56de\u3001\u3053\u306e\u30b5\u30fc\u30d0\u30fc\u3092\u5229\u7528\u3057\u3066\u52d5\u753b\u914d\u4fe1\u3092\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\uff0a [&hellip;]<\/p>\n","protected":false},"author":106,"featured_media":11352,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[236,50,162],"_links":{"self":[{"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/posts\/14192"}],"collection":[{"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/users\/106"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14192"}],"version-history":[{"count":46,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/posts\/14192\/revisions"}],"predecessor-version":[{"id":14311,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/posts\/14192\/revisions\/14311"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=\/wp\/v2\/media\/11352"}],"wp:attachment":[{"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecomottblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}