Meet Media API:C++ 參考用戶端快速入門
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面說明如何使用 C++ 參考用戶端實作設定及執行範例。如要瞭解 TypeScript 用戶端,請參閱 TypeScript 參考用戶端快速入門。
必要條件
如要執行本快速入門導覽課程,請先完成下列必要條件:
啟用 Meet REST API
使用 Google API 前,請先在 Google Cloud 專案中啟用這些 API。您可以在單一 Google Cloud 專案中啟用一或多個 API。
Google Cloud 控制台
在 Google Cloud 控制台中啟用 Meet REST API。
啟用 API
確認您要在正確的 Cloud 專案中啟用 Meet REST API,然後按一下「下一步」。
確認要啟用 Meet REST API,然後按一下「啟用」。
gcloud CLI
如有必要,請將目前的 Cloud 專案設為您建立的專案:
gcloud config set project PROJECT_ID
將 PROJECT_ID 替換為您建立的 Cloud 專案的專案 ID。
啟用 Meet REST API:
gcloud services enable meet.googleapis.com
建構 C++ 用戶端
C++ 實作項目是使用 Bazel 建構。不過,C++ WebRTC 程式庫 (libwebrtc) 沒有可用的 Bazel 建構作業,因此您必須先按照 WebRTC 文件中的說明建構該程式庫。
以下是 WebRTC 文件中說明的簡短版本:
$ cd ~
$ mkdir src
$ cd src
$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git
$ export PATH=~/src/depot_tools:$PATH
$ mkdir webrtc-checkout
$ cd webrtc-checkout
$ fetch --nohooks webrtc
$ cd src
$ # Latest known version to work with our builds
$ git checkout b00c469cad3f8c926fcf81ded90b90b6e1e62b9c
$ cd ..
$ gclient sync
$ mv src webrtc
$ cd webrtc
$ ./build/install-build-deps.sh
$ gn gen out/Default --args='is_debug=false use_custom_libcxx=false rtc_include_tests=false rtc_build_examples=false dcheck_always_on=true rtc_use_x11=false use_rtti=true'
$ ninja -C out/Default
注意:由於工具鍊和 abseil 更新,我們目前已知與建構作業相容的最新版本是提交 b00c469cad3f8c926fcf81ded90b90b6e1e62b9c
。目錄位置日後可能會有變動。這組指令現在可以運作,但如果基礎工具有所變更,請參閱提供的連結。如果您要建構的目標是 x64 以外的 Debian 或 Ubuntu Linux 變種版本,可能需要進行不同的必要設定。
建構 libwebrtc 後,請更新 WORKSPACE
檔案,指向 webrtc-checkout
目錄。更新該檔案頂端的 webrtc_path
路徑:
webrtc_path = "/usr/local/myuser/webrtc-checkout/"
使用 Bazel 建構 C++ 用戶端:
$ bazel build //cpp/...
注意:請使用 Bazel 7.4.1。如果安裝了較新版本的 Bazel,可以使用 bazel-7.4.1 build/run/test ...
執行
(選用) 執行測試:
$ bazel test //cpp/...
產生 OAuth 權杖
如要連線至 Meet Media API,應用程式必須使用 OAuth 產生存取權杖。如要進一步瞭解如何使用 OAuth 存取 Google API,請參閱「使用 OAuth 2.0 存取 Google API」。
您可以使用 OAuth 2.0 Playground 產生權杖。使用遊樂場時,請務必遵守下列事項:
- 使用雲端專案中的用戶端 ID 和密鑰憑證。
- 要求正確的範圍。
- 登入 Google 帳戶並接受存取權。
完成後,按一下 Exchange authorization code for tokens
按鈕,然後複製產生的存取權杖。
發起會議
使用產生 OAuth 權杖時所用的使用者帳戶發起會議。複製會議代碼。您現在可以執行範例了。
範例應用程式
GitHub 存放區提供範例,可從會議接收媒體和參與者中繼資料。
這些樣本會收集指定時間長度的資料 (預設為 3 秒),並將收集到的資料寫入檔案。
音訊檔案為 PCM16 格式。影片檔案為 YUV420p 格式。您可以使用 FFmpeg 等程式庫播放這些檔案。
由於會議期間的影片解析度可能會變更,因此範例會在影片檔名中加入解析度。
參與者中繼資料檔案為使用者可讀的文字檔。
單一使用者媒體範例是基本應用程式,主要用於收集音訊和影片。樣本不會判斷音訊和視訊資料是由哪位參與者建立。因此,如果會議有多位參與者,使用這個範例可能會導致輸出內容損毀。
如要執行單一使用者媒體範例,請執行:
Linux
$ bazel run cpp/samples:single_user_media_sample -- \
--meeting_space_id MEETING_SPACE_ID \
--oauth_token OAUTH_TOKEN
根據預設,單一音訊檔案會儲存至 /tmp/test_output_audio.pcm
。
由於視訊串流可能會在會議期間變更解析度,因此系統可能會建立多個視訊檔案。影片檔案名稱會包含遞增計數器和該檔案的解析度。舉例來說,如果影片串流解析度從 320x180 變更為 240x135,然後再變回 320x180,系統會建立下列影片檔案:
/tmp/test_output_video_0_320x180.pcm
/tmp/test_output_video_1_240x135.pcm
/tmp/test_output_video_2_320x180.pcm
(選用) 使用 FFmpeg 在 Linux 和 Mac 上播放輸出檔案
您可以使用 FFmpeg 播放建立的音訊和影片檔案。範例指令:
Linux 和 Mac
# Audio
$ ffplay -f s16le -ar 48k -af aformat=channel_layouts=mono \
/tmp/test_output_audio.pcm
# Video
#
# `video_size` must match the resolution in the video filename (320x180 in
# this example).
$ ffplay -f rawvideo -pixel_format yuv420p -video_size 320x180 \
/tmp/test_output_video_0_320x180.yuv
選項
執行範例時,您可以指定下列選項:
選項 |
說明 |
--output_file_prefix PREFIX |
指定輸出檔案的前置字串。預設值為 /tmp_test_output_ 。 |
--collection_duration DURATION |
指定媒體收集時間長度。預設值為 30s 。 |
--join_timeout TIMEOUT |
指定應用程式加入會議的等待時間。預設值為 2m 。 |
--meet_api_url URL |
指定 Meet Media API 的網址。預設值為 https://meet.googleapis.com/v2alpha/ 。 |
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間:2025-08-29 (世界標準時間)。"],[],[],null,["# Meet Media API: C++ reference client quickstart\n\n| **Developer Preview:** Available as part of the [Google Workspace Developer Preview Program](https://developers.google.com/workspace/preview), which grants early access to certain features. \n|\n| **To use the Meet Media API to access real-time media from a conference, the Google Cloud project, OAuth principal, and all participants in the conference must be enrolled in the Developer Preview Program.**\n\nThis page explains how to set up and run a sample using the [C++ reference\nclient\nimplementation](https://github.com/googleworkspace/meet-media-api-samples). To\nlearn about the TypeScript client instead, see the [TypeScript reference client\nquickstart](/workspace/meet/media-api/guides/ts).\n\nPrerequisites\n-------------\n\nTo run this quickstart, you need the following prerequisites:\n\n- You've cloned the [GitHub\n repository](https://github.com/googleworkspace/meet-media-api-samples).\n- [Bazel 7.4.1](https://bazel.build/versions/7.4.0/install/ubuntu).\n- [A Google Cloud project](/workspace/guides/create-project) with Google Meet REST API enabled.\n- A Google Workspace account.\n\n### Enable the Meet REST API\n\nBefore using Google APIs, you need to turn them on in a Google Cloud project. You can turn on one or more APIs in a single Google Cloud project. \n\n### Google Cloud console\n\n1. In the Google Cloud console, enable the Meet REST API.\n\n [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=meet.googleapis.com)\n2. Confirm that you're enabling the Meet REST API in the correct\n Cloud project, then click **Next**.\n\n3. Confirm that you're enabling the Meet REST API, then click\n **Enable**.\n\n### gcloud CLI\n\n1. If necessary, set the current Cloud project to the one you\n created:\n\n gcloud config set project \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with the **Project ID** of the\n Cloud project you created.\n2. Enable the Meet REST API:\n\n gcloud services enable meet.googleapis.com\n\nBuild the C++ client\n--------------------\n\n1. The C++ implementation is built with [Bazel](https://bazel.build/). However,\n the C++ WebRTC library (*libwebrtc* ) doesn't have a working Bazel build, so\n you must build that library first by following the instructions at [WebRTC\n docs](https://webrtc.github.io/webrtc-org/native-code/development/).\n\n The following is an abbreviated version of what's explained in the\n [WebRTC docs](https://webrtc.github.io/webrtc-org/native-code/development/): \n\n $ cd ~\n $ mkdir src\n $ cd src\n $ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git\n $ export PATH=~/src/depot_tools:$PATH\n $ mkdir webrtc-checkout\n $ cd webrtc-checkout\n $ fetch --nohooks webrtc\n $ cd src\n $ # Latest known version to work with our builds\n $ git checkout b00c469cad3f8c926fcf81ded90b90b6e1e62b9c\n $ cd ..\n $ gclient sync\n $ mv src webrtc\n $ cd webrtc\n $ ./build/install-build-deps.sh\n $ gn gen out/Default --args='is_debug=false use_custom_libcxx=false rtc_include_tests=false rtc_build_examples=false dcheck_always_on=true rtc_use_x11=false use_rtti=true'\n $ ninja -C out/Default\n\n \u003e **Note** : Commit `b00c469cad3f8c926fcf81ded90b90b6e1e62b9c` is the\n \u003e latest known version to work with our builds due to toolchain and abseil\n \u003e updates. This might change in the future. This set of commands works now,\n \u003e but the provided link should be referred to in case the underlying tooling\n \u003e changes. If you're building for a non-x64 Debian or Ubuntu Linux variant,\n \u003e your prerequisite setup might be different.\n2. After building *libwebrtc* , update your `WORKSPACE` file to point to your\n `webrtc-checkout` directory. Update the `webrtc_path` path near the top of\n that file:\n\n webrtc_path = \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-s2\"\u003e\"/usr/local/myuser/webrtc-checkout/\"\u003c/span\u003e \u003c/var\u003e\n\n3. Use Bazel to build the C++ client:\n\n $ bazel build //cpp/...\n\n\u003e **Note** : You should be using Bazel 7.4.1. If you have newer versions of\n\u003e Bazel installed, you can run it by using `bazel-7.4.1 build/run/test ...`\n\n1. Optionally, run the tests:\n\n $ bazel test //cpp/...\n\nGenerate OAuth tokens\n---------------------\n\nTo connect to the Meet Media API, your app must use OAuth to generate\naccess tokens. To learn more about accessing Google APIs with OAuth, see [*Using\nOAuth 2.0 to Access Google APIs*](/identity/protocols/oauth2).\n\nYou can use the [OAuth 2.0\nPlayground](https://developers.google.com/oauthplayground) to generate tokens.\nWhen using the playground, make sure to:\n\n- Use your client ID and secret credentials from your cloud project.\n- Request the correct [scopes](/workspace/meet/media-api/guides/get-started#scopes).\n- Sign in to a Google Account and accept access.\n\nOnce complete, click the `Exchange authorization code for tokens` button and\ncopy the generated access token.\n\nStart a meeting\n---------------\n\n[Start a meeting](https://meet.google.com/new) using the same user account that\nyou used to generate the OAuth token. Copy the meeting code. You're now ready to\nrun the samples.\n\nSample apps\n-----------\n\nThe [GitHub\nrepository](https://github.com/googleworkspace/meet-media-api-samples) offers\nsamples for receiving media and participant metadata from a meeting.\n\nThese samples collect data for a specified amount of time (default is 3 seconds)\nand write the collected data to files.\n\nAudio files are in PCM16 format. Video files are in YUV420p format. These files\ncan be played by using a library such as FFmpeg.\n\nBecause video resolutions might change during a meeting, samples include the\nresolution in video file names.\n\nParticipant metadata files will be human-readable text files.\n\n### Single User Media Sample\n\nThe single user media sample is a basic app that focuses on collecting audio and\nvideo. The sample doesn't determine which participant created the audio and\nvideo data. Therefore, using this sample in a meeting with more than one\nparticipant might result in corrupted output.\n\nTo run the single user media sample, run: \n\n### Linux\n\n $ bazel run cpp/samples:single_user_media_sample -- \\\n --meeting_space_id \u003cvar translate=\"no\"\u003eMEETING_SPACE_ID\u003c/var\u003e \\\n --oauth_token \u003cvar translate=\"no\"\u003eOAUTH_TOKEN\u003c/var\u003e\n\nBy default, a single audio file is saved to `/tmp/test_output_audio.pcm`.\n\nBecause video streams might change resolutions during a meeting, multiple video\nfiles might be created. Video file names will include an incrementing counter\nand the resolution for that file. For example, if the video stream resolution\nchanged from 320x180 to 240x135 and then back to 320x180, the following video\nfiles would be created:\n\n- `/tmp/test_output_video_0_320x180.pcm`\n- `/tmp/test_output_video_1_240x135.pcm`\n- `/tmp/test_output_video_2_320x180.pcm`\n\n### (Optional) Use FFmpeg to Play Output Files on Linux and Mac\n\n[FFmpeg](https://www.ffmpeg.org/) can be used to play created audio and video\nfiles. Example commands: \n\n### Linux \\& Mac\n\n # Audio\n $ ffplay -f s16le -ar 48k -af aformat=channel_layouts=mono \\\n /tmp/test_output_audio.pcm\n\n # Video\n #\n # `video_size` must match the resolution in the video filename (320x180 in\n # this example).\n $ ffplay -f rawvideo -pixel_format yuv420p -video_size 320x180 \\\n /tmp/test_output_video_0_320x180.yuv\n\n### Options\n\nYou can specify these options when running the samples:\n\n| Option | Description |\n|------------------------------------------------------------|-------------------------------------------------------------------------------------------------|\n| `--output_file_prefix `\u003cvar translate=\"no\"\u003ePREFIX\u003c/var\u003e | Specify the prefix for output files. Defaults to `/tmp_test_output_`. |\n| `--collection_duration `\u003cvar translate=\"no\"\u003eDURATION\u003c/var\u003e | Specify how long to collect media. Defaults to `30s`. |\n| `--join_timeout `\u003cvar translate=\"no\"\u003eTIMEOUT\u003c/var\u003e | Specify how long to wait for the app to join the conference. Defaults to `2m`. |\n| `--meet_api_url `\u003cvar translate=\"no\"\u003eURL\u003c/var\u003e | Specify the URL for the Meet Media API API. Defaults to `https://meet.googleapis.com/v2alpha/`. |\n\nRelated topics\n--------------\n\n- [Troubleshoot and fix Meet Media API errors](/workspace/meet/media-api/guides/troubleshoot)\n\n- [Meet Media API data channels reference](/workspace/meet/media-api/reference/dc/media_api)"]]