### Build Advanced Examples Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Compile the advanced examples, which may require additional dependencies like ffmpeg. Ensure all prerequisites are installed. ```bash make advanced-examples ``` -------------------------------- ### Build Basic Examples Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Compile the basic examples provided with the SDK. Ensure dependencies are met before running. ```bash make examples ``` -------------------------------- ### Complete Setup with Multiple Observers Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Illustrates a comprehensive setup pattern integrating RtcConnectionObserver, LocalUserObserver, and AudioFrameObserver with VAD configuration for real-time audio processing. ```go // Connection observer conObserver := &agoraservice.RtcConnectionObserver{ OnConnected: func(con *agoraservice.RtcConnection, conInfo *agoraservice.RtcConnectionInfo, reason int) { fmt.Println("Connected!") }, OnUserJoined: func(con *agoraservice.RtcConnection, uid string) { fmt.Printf("User joined: %s\n", uid) con.GetLocalUser().SubscribeAudio(uid) }, } con.RegisterObserver(conObserver) // Local user observer localUserObserver := &agoraservice.LocalUserObserver{ OnAudioPublished: func(lu *agoraservice.LocalUser, state int, reason int) { if state == 0 { fmt.Println("Audio published!") } }, } con.RegisterLocalUserObserver(localUserObserver) // Audio frame observer with VAD vadCfg := &agoraservice.AudioVadConfigV2{ PreStartRecognizeCount: 16, StartRecognizeCount: 30, StopRecognizeCount: 50, } audioObserver := &agoraservice.AudioFrameObserver{ OnPlaybackAudioFrameBeforeMixing: func(lu *agoraservice.LocalUser, ch, uid string, frame *agoraservice.AudioFrame, vadState agoraservice.VadState, vadFrame *agoraservice.AudioFrame) bool { if vadState == agoraservice.VadStateStartSpeeking { fmt.Printf("User %s started speaking\n", uid) } return true }, } con.RegisterAudioFrameObserver(audioObserver, 1, vadCfg) // Connect con.Connect(token, channel, userId) ``` -------------------------------- ### Install RTM Dependencies Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Install the necessary dependencies for the RTM demo using the make command. ```bash make deps ``` -------------------------------- ### Clone and Install Agora Golang Server SDK Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Clone the repository, checkout to the desired release branch, and install the SDK. Ensure you are using a compatible Go version. ```bash git clone git@github.com:AgoraIO-Extensions/Agora-Golang-Server-SDK.git cd Agora-Golang-Server-SDK git checkout release/2.1.0 make install ``` -------------------------------- ### Install SDK on Linux Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Install SDK dependencies and build the project on a Linux system. Ensure the dynamic library path is set correctly before running the application. ```bash # Install SDK make deps # Build with SDK make build # Run with library path export LD_LIBRARY_PATH=./agora_sdk:$LD_LIBRARY_PATH ./your-app ``` -------------------------------- ### Download and Build Agora SDK Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Download the Agora SDK and build the server SDK. This is a prerequisite for running examples. ```bash make deps make build ``` -------------------------------- ### Run Advanced H.264 Example Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Execute the advanced H.264 example. Requires setting AGORA_APP_ID and AGORA_APP_CERTIFICATE environment variables. For Linux, set LD_LIBRARY_PATH; for macOS, set DYLD_LIBRARY_PATH. ```bash cd ./bin export AGORA_APP_ID=xxxx export AGORA_APP_CERTIFICATE=xxx export LD_LIBRARY_PATH=../agora_sdk # export DYLD_LIBRARY_PATH=../agora_sdk_mac ./send_h264 ``` -------------------------------- ### Run Basic PCM Example Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Execute the basic PCM (Pulse Code Modulation) example. Requires setting AGORA_APP_ID and AGORA_APP_CERTIFICATE environment variables. For Linux, set LD_LIBRARY_PATH; for macOS, set DYLD_LIBRARY_PATH. ```bash cd ./bin export AGORA_APP_ID=xxxx export AGORA_APP_CERTIFICATE=xxx export LD_LIBRARY_PATH=../agora_sdk # export DYLD_LIBRARY_PATH=../agora_sdk_mac ./send_recv_pcm ``` -------------------------------- ### Install Video Transcoding Dependencies on Linux Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Install necessary FFmpeg development libraries and pkg-config on Linux for video transcoding. This is an optional setup step. ```bash # Linux sudo apt-get install libavformat-dev libavcodec-dev libswscale-dev pkg-config ``` -------------------------------- ### Example: Registering AudioEncodedFrameObserver Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Demonstrates how to enable and register an AudioEncodedFrameObserver to receive and process encoded audio frames, which is more efficient for recording. ```go conCfg := &agoraservice.RtcConnectionConfig{ AudioEncodedFrameObserver: true, // Enable encoded audio } con := agoraservice.NewRtcConnection(conCfg, nil) observer := &agoraservice.AudioEncodedFrameObserver{ OnEncodedAudioFrameReceived: func(ch, uid string, frame *agoraservice.EncodedAudioFrame) bool { fmt.Printf("Encoded audio from user %s, codec: %d\n", uid, frame.EncodingType) recordEncodedAudio(frame) return true }, } con.RegisterAudioEncodedFrameObserver(observer) ``` -------------------------------- ### Install Video Transcoding Dependencies on macOS Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Install FFmpeg and pkg-config on macOS using Homebrew for video transcoding. This is an optional setup step. ```bash # macOS brew install ffmpeg pkg-config ``` -------------------------------- ### Example ColorSpace Configuration for AV1 Web Compatibility Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/types.md An example of setting ColorSpaceType for AV1 web compatibility, specifying full range, BT.709 matrix, and unspecified primaries/transfer. Used by ExternalVideoFrame.ColorSpace. ```go ColorSpace: agoraservice.ColorSpaceType{ RangeId: 1, // Full range MatrixId: 5, // BT.709 PrimariesId: 2, // Unspecified TransferId: 2, // Unspecified } ``` -------------------------------- ### Setup Music Streaming with Simulcast Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Configures a connection for music streaming with high-quality stereo audio and enables simulcast for dual streams. ```go publishCfg := &agoraservice.RtcConnectionPublishConfig{ PublishAudio: true, AudioProfile: agoraservice.AudioProfileMusicHighQualityStereo, PublishVideo: true, } con := agoraservice.NewRtcConnection(conCfg, publishCfg) // Setup simulcast for dual streams con.SetSimulcastStream(true, &agoraservice.SimulcastStreamConfig{ Width: 0, Height: 0, Bitrate: 0, Framerate: 0, }) ``` -------------------------------- ### Download Testing Data Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Download the necessary testing data for running the examples. This can be skipped if data is already present. ```bash curl -o test_data.zip https://download.agora.io/demo/test/server_sdk_test_data_202410252135.zip unzip test_data.zip ``` -------------------------------- ### Build with Video Transcoding Enabled Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Build the Go project with the `avcodec` tag enabled to include video transcoding capabilities. This requires FFmpeg development libraries to be installed. ```bash # Build go build -tags avcodec ``` -------------------------------- ### Connection Pool Lifecycle Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Initialize the service and create a connection pool at startup. Get connections from the pool for user logins and return them on logout. Release all connections and the service on process exit. Ideal for long-running services. ```go agoraservice.Initialize(cfg) pool := createConnectionPool(20) // Pre-create 20 connections // For each user login con := pool.Get() con.Connect(token, channel, userId) // For each user logout con.Disconnect() pool.Return(con) // On process exit pool.ReleaseAll() agoraservice.Release() ``` -------------------------------- ### Implement OnPlaybackAudioFrameBeforeMixing Callback Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Implement the OnPlaybackAudioFrameBeforeMixing callback to process individual remote user audio before it's mixed. This example demonstrates how to use the Voice Activity Detection (VAD) frame if available and react to different VAD states. Ensure VAD configuration is set when registering the observer. ```go OnPlaybackAudioFrameBeforeMixing: func(localUser *LocalUser, channelId string, userId string, frame *AudioFrame, vadState VadState, vadResultFrame *AudioFrame) bool ``` ```go observer := &agoraservice.AudioFrameObserver{ OnPlaybackAudioFrameBeforeMixing: func(lu *agoraservice.LocalUser, ch, uid string, frame *agoraservice.AudioFrame, vadState agoraservice.VadState, vadFrame *agoraservice.AudioFrame) bool { // Use VAD frame if available audioData := frame if vadFrame != nil { audioData = vadFrame } // Process based on VAD state switch vadState { case agoraservice.VadStateStartSpeeking: fmt.Printf("User %s started speaking\n", uid) // Start ASR/STT case agoraservice.VadStateSpeeking: fmt.Printf("User %s speaking\n", uid) // Continue ASR/STT case agoraservice.VadStateStopSpeeking: fmt.Printf("User %s stopped speaking\n", uid) // Finalize ASR/STT } // Save or process audio saveAudioFrame(audioData) return true }, } err := con.RegisterAudioFrameObserver(observer, 1, vadCfg) ``` -------------------------------- ### Handle Video Published Callback Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Callback triggered when video publishing starts. It receives the local user instance, state, and reason code. ```go OnVideoPublished: func(localUser *LocalUser, state int, reason int) ``` -------------------------------- ### Handle Audio Published Callback Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Callback triggered when audio publishing starts. Use this to check the publication state and log success or failure. ```go OnAudioPublished: func(localUser *LocalUser, state int, reason int) ``` ```go observer := &agoraservice.LocalUserObserver{ OnAudioPublished: func(lu *agoraservice.LocalUser, state int, reason int) { if state == 0 { fmt.Println("Audio publishing started") } else { fmt.Printf("Audio publish failed: %d\n", reason) } }, } con.RegisterLocalUserObserver(observer) ``` -------------------------------- ### Clone Agora Golang Server SDK Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Clone the Agora Golang Server SDK repository from GitHub to start using the RTM features. ```bash git clone https://github.com/AgoraIO-Extensions/Agora-Golang-Server-SDK.git cd Agora-Golang-Server-SDK ``` -------------------------------- ### Implement VideoEncodedFrameObserver Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Example of implementing the VideoEncodedFrameObserver to handle incoming encoded video frames. This is useful for recording or re-transmitting compressed video data. ```go observer := &agoraservice.VideoEncodedFrameObserver{ OnEncodedVideoFrameReceived: func(ch, uid string, frame *agoraservice.EncodedVideoFrame) bool { fmt.Printf("Encoded video from user %s, codec: %d, type: %d\n", uid, frame.CodecType, frame.FrameType) recordEncodedFrame(frame) return true }, } con.RegisterVideoEncodedFrameObserver(observer) ``` -------------------------------- ### Publish Local Video Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/02-rtc-connection.md Starts publishing video from the local user. Call this before pushing video data. It creates an internal video track if one doesn't exist, enabling others to subscribe. ```go err := conn.PublishVideo() ``` -------------------------------- ### Implement VideoFrameObserver Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Example of implementing the VideoFrameObserver to process incoming YUV video frames. Register this observer to receive frame data for analysis. ```go observer := &agoraservice.VideoFrameObserver{ OnFrame: func(ch, uid string, frame *agoraservice.VideoFrame) bool { fmt.Printf("Video frame from user %s: %dx%d\n", uid, frame.Width, frame.Height) // Process YUV frame return true }, } con.RegisterVideoFrameObserver(observer) ``` -------------------------------- ### Get Local User Instance Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/02-rtc-connection.md Retrieves the local user instance for controlling subscriptions and publishing. Use this to manage audio and video streams for the current connection. ```go localUser := conn.GetLocalUser() localUser.SubscribeAudio(remoteUserId) ``` -------------------------------- ### Configure H.264 720p Video Encoding Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of setting up a VideoEncoderConfiguration for H.264 codec with 720p resolution, 30fps, and specified bitrates. This configuration is suitable for standard video streaming. ```go cfg := &agoraservice.VideoEncoderConfiguration{ CodecType: agoraservice.VideoCodecTypeH264, Width: 1280, Height: 720, FrameRate: 30, Bitrate: 2500, // kbps MinBitrate: 1500, MaxBitrate: 3000, CodecProfile: agoraservice.CodecProfileTypeBaseline, } con.SetVideoEncoderConfiguration(cfg) ``` -------------------------------- ### Get and Set Global Agora Parameters Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Retrieve the global Agora parameter handler to set SDK-wide configuration parameters. Useful for private deployment settings. Call after service initialization. ```go param := agoraservice.GetAgoraParameter() param.SetParameters(`{"rtc.enable_nasa2": false}`) param.SetParameters(`{"rtc.force_local": true}`) ``` -------------------------------- ### Subscribe to Low Bitrate Video Stream Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of setting VideoSubscriptionOptions to subscribe to a low bitrate video stream. This is suitable for scenarios where bandwidth is limited and lower video quality is acceptable. ```go opts := &agoraservice.VideoSubscriptionOptions{ StreamType: agoraservice.VideoStreamLow, EncodedFrameOnly: false, } ``` -------------------------------- ### Initialize AI Server with Voice Processing Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Initializes the Agora service for an AI server scenario with audio processing enabled. Configures noise suppression and auto gain control for audio. Sets up an RTC connection and registers an audio frame observer to detect speech start using VAD. ```go // Initialize with audio processing cfg := agoraservice.NewAgoraServiceConfig() cfg.AppId = appId cfg.AudioScenario = agoraservice.AudioScenarioAiServer cfg.APMModel = 1 cfg.APMConfig = agoraservice.NewAPMConfig() cfg.APMConfig.ANS = true // Noise suppression cfg.APMConfig.AGC = true // Auto gain control agoraservice.Initialize(cfg) // Create connection conCfg := &agoraservice.RtcConnectionConfig{ AudioScenario: agoraservice.AudioScenarioAiServer, } con := agoraservice.NewRtcConnection(conCfg, nil) con.Connect(token, channel, userId) // Subscribe to audio with VAD vadCfg := &agoraservice.AudioVadConfigV2{ StartRecognizeCount: 30, StopRecognizeCount: 50, } audioObserver := &agoraservice.AudioFrameObserver{ OnPlaybackAudioFrameBeforeMixing: func(lu *agoraservice.LocalUser, ch, uid string, frame *agoraservice.AudioFrame, vadState agoraservice.VadState, vadFrame *agoraservice.AudioFrame) bool { if vadState == agoraservice.VadStateStartSpeeking { startASR(uid) } return true }, } con.RegisterAudioFrameObserver(audioObserver, 1, vadCfg) ``` -------------------------------- ### Initialize Agora Service with Configuration Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Demonstrates how to create a new AgoraServiceConfig, set essential parameters like AppId and AudioScenario, and then initialize the Agora service. ```go cfg := agoraservice.NewAgoraServiceConfig() cfg.AppId = "your-app-id" cfg.EnableAudioProcessor = true cfg.AudioScenario = agoraservice.AudioScenarioAiServer err := agoraservice.Initialize(cfg) ``` -------------------------------- ### Obtain LocalUser Instance Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/03-local-user.md Get a LocalUser instance from an RtcConnection. This is the starting point for managing local user functionalities. ```go con := agoraservice.NewRtcConnection(cfg, nil) localUser := con.GetLocalUser() ``` -------------------------------- ### Initialize Connection with Scenario and Profile Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md When creating a connection, specify the scenario and profile to configure audio and video behavior. This allows for tailored settings for different use cases, such as AI server interactions or default scenarios. ```go connection, err := agora.CreateConnection(AudioScenarioAiServer, AudioProfileDefault, nil) ``` -------------------------------- ### Standard Build with Make Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md For standard builds without transcoding features, use the 'make' command without specifying build tags. ```bash make build ``` ```bash make example ``` ```bash make advanced-examples ``` -------------------------------- ### Agora Go SDK Integration Workflow Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md This snippet outlines the typical lifecycle for integrating the Agora Go SDK, from service initialization to connection management and release. Steps 1-2 are for startup, 3-9 for connection loops, and 10 for process exit. ```go 1. svcCfg := agoraservice.NewAgoraServiceConfig() 2. agoraservice.Initialize(svcCfg) 3. con := agoraservice.NewRtcConnection(conCfg, publishConfig) 4. // Register observers: con.RegisterObserver(conHandler) con.RegisterAudioFrameObserver(...) con.RegisterLocalUserObserver(...) 5. con.Connect(token, channelName, userId) 6. con.PublishAudio() // or con.PublishVideo() 7. con.PushAudioPcmData(...) // or con.PushVideoFrame(...) 8. con.Disconnect() 9. con.Release() 10. agoraservice.Release() // Call on process exit ``` -------------------------------- ### Create Connections for Different Scenarios Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Demonstrates creating separate RTC connections for distinct audio scenarios like AI server and Chorus. ```go // Connection 1: AI server con1 := agoraservice.NewRtcConnection(&agoraservice.RtcConnectionConfig{ AudioScenario: agoraservice.AudioScenarioAiServer, }, nil) // Connection 2: Chorus con2 := agoraservice.NewRtcConnection(&agoraservice.RtcConnectionConfig{ AudioScenario: agoraservice.AudioScenarioChorus, }, nil) ``` -------------------------------- ### Configure Proxy Settings Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Enable proxy and set various related parameters. These calls are intended to be made after agoraService::Init(). ```go parameter.setBool("rtc.enable_proxy", true); s->setBool("rtc.force_local", true); s->setBool("rtc.local_ap_low_level", true); s->setBool("rtc.enable_nasa2", true); s->setParameters("{\"rtc.vos_list\":[\"10.62.0.95:4701\"]}"); s->setParameters("{\"rtc.local_ap_list\":[\"10.62.0.95\"]}"); ``` -------------------------------- ### Initialize Agora Service Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/00-START-HERE.md Initializes the Agora service with configuration. Ensure this is called before creating any connections. ```go // 1. Initialize service cfg := agoraservice.NewAgoraServiceConfig() cfg.AppId = "your-app-id" agoraservice.Initialize(cfg) // 2. Create connection con := agoraservice.NewRtcConnection(conCfg, publishCfg) // 3. Connect to channel con.Connect(token, "channel-name", "user-id") ``` -------------------------------- ### OnVideoPublished Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Triggered when video publishing starts. The state parameter indicates success (0) or failure, and the reason provides more details on the outcome. ```APIDOC ## OnVideoPublished ### Description Triggered when video publishing starts. ### Signature ```go OnVideoPublished(localUser *LocalUser, state int, reason int) ``` ### Parameters #### Path Parameters - **localUser** (*LocalUser) - Required - Local user instance - **state** (int) - Required - Publication state (0=success, other=error) - **reason** (int) - Required - Reason code ``` -------------------------------- ### Multiple Connections Lifecycle Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Initialize the service once, then create and manage individual connections for each user. Disconnect and release each connection as users log out. This is for multi-user applications. ```go agoraservice.Initialize(cfg) for userSession in userSessions { con := agoraservice.NewRtcConnection(conCfg, publishCfg) con.Connect(token, channel, userId) store(con) // Keep reference } // On disconnect con.Disconnect() con.Release() // On shutdown agoraservice.Release() ``` -------------------------------- ### OnAudioPublished Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Triggered when audio publishing starts. The state parameter indicates success (0) or failure, and the reason provides more details on the outcome. ```APIDOC ## OnAudioPublished ### Description Triggered when audio publishing starts. ### Signature ```go OnAudioPublished(localUser *LocalUser, state int, reason int) ``` ### Parameters #### Path Parameters - **localUser** (*LocalUser) - Required - Local user instance - **state** (int) - Required - Publication state (0=success, other=error) - **reason** (int) - Required - Reason code ### Request Example ```go observer := &agoraservice.LocalUserObserver{ OnAudioPublished: func(lu *agoraservice.LocalUser, state int, reason int) { if state == 0 { fmt.Println("Audio publishing started") } else { fmt.Printf("Audio publish failed: %d\n", reason) } }, } con.RegisterLocalUserObserver(observer) ``` ``` -------------------------------- ### Initialize Agora Service Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Initializes the Agora service with configuration. Ensure to call Release() when done. ```go import agoraservice "github.com/AgoraIO-Extensions/Agora-Golang-Server-SDK/v2/go_sdk/rtc" // Create and configure service cfg := agoraservice.NewAgoraServiceConfig() cfg.AppId = "your-app-id" cfg.AudioScenario = agoraservice.AudioScenarioAiServer err := agoraservice.Initialize(cfg) if err != 0 { log.Fatal("Initialize failed:", err) } defers agoraservice.Release() ``` -------------------------------- ### Build RTM Demo Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Build the RTM demo application using the provided script. ```bash ./scripts/rtmbuild.sh ``` -------------------------------- ### Initialize and Register AudioVadConfigV2 Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Initializes an AudioVadConfigV2 object with specific parameters and registers it with the Agora service. Ensure the observer and frame interval are correctly set. ```go vadCfg := &agoraservice.AudioVadConfigV2{ PreStartRecognizeCount: 16, StartRecognizeCount: 30, StopRecognizeCount: 50, ActivePercent: 0.7, InactivePercent: 0.5, StartVoiceProb: 70, StopVoiceProb: 70, StartRmsThreshold: -50, StopRmsThreshold: -50, } err := con.RegisterAudioFrameObserver(observer, 1, vadCfg) ``` -------------------------------- ### Build with AVCodec Tag for Transcoding Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md To use the PushVideoEncodedDataForTranscode API for video transcoding, you must enable the 'avcodec' build tag during compilation. Ensure FFmpeg is installed. ```bash go build -C /*/work/Agora-Golang-Server-SDK/go_sdk/rtc -tags avcodec ``` ```bash go build -C -tags avcodec ``` -------------------------------- ### Configure Private Deployment Parameters Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Set parameters for private deployment, including disabling NASA2, forcing local connection, and specifying local domain or AP list. These must be called after creating the connection but before Connect. ```go connectionCon := agoraservice.NewRtcConnection(&conCfg) localUser := con.GetLocalUser() // Test LAN params1 := `{"rtc.enable_nasa2": false}` params2 := `{"rtc.force_local": true}` params3 := `{"rtc.local_domain": "ap.1452738.agora.local"}` params4 := `{"rtc.local_ap_list": ["20.1.125.55"]}` // Set parameters agoraservice.GetAgoraParameter().SetParameters(params1) agoraservice.GetAgoraParameter().SetParameters(params2) agoraservice.GetAgoraParameter().SetParameters(params3) agoraservice.GetAgoraParameter().SetParameters(params4) ``` -------------------------------- ### Subscribe to Encoded Video Frames Only Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of setting VideoSubscriptionOptions to subscribe to only encoded frames for bandwidth optimization. This is useful when YUV conversion is not needed on the client side. ```go opts := &agoraservice.VideoSubscriptionOptions{ StreamType: agoraservice.VideoStreamHigh, EncodedFrameOnly: true, // Skip YUV conversion } ``` -------------------------------- ### Create Default AgoraServiceConfig Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Use this to create a new AgoraServiceConfig with default values before customizing it. ```go cfg := agoraservice.NewAgoraServiceConfig() cfg.AppId = "your-app-id" cfg.EnableAudioProcessor = true cfg.EnableVideo = false ``` -------------------------------- ### Configure Voice-Only with VAD Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Sets up a voice-only connection with Voice Activity Detection (VAD) enabled. Registers an audio frame observer to detect speech start. ```go vadCfg := &agoraservice.AudioVadConfigV2{ PreStartRecognizeCount: 16, StartRecognizeCount: 30, StopRecognizeCount: 50, } observer := &agoraservice.AudioFrameObserver{ OnPlaybackAudioFrameBeforeMixing: func(lu *agoraservice.LocalUser, ch, uid string, frame *agoraservice.AudioFrame, vadState agoraservice.VadState, vadFrame *agoraservice.AudioFrame) bool { if vadState == agoraservice.VadStateStartSpeeking { // Start speech detection } return true }, } con.RegisterAudioFrameObserver(observer, 1, vadCfg) ``` -------------------------------- ### Run RTM Demo Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Execute the RTM demo application. Requires providing App ID, Channel Name, and User ID as arguments. ```bash cd bin ./rtmdemo ``` -------------------------------- ### Subscribe to High Resolution Video Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of setting VideoSubscriptionOptions to subscribe to a high-resolution video stream without encoded frames only. This provides the full video data for rendering. ```go opts := &agoraservice.VideoSubscriptionOptions{ StreamType: agoraservice.VideoStreamHigh, EncodedFrameOnly: false, } localUser.SubscribeVideo(uid, opts) ``` -------------------------------- ### Set Callback Parameters for Dual-Channel Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Configure the SDK to use a 16k sample rate for dual-channel audio callbacks, as dual-channel VAD only supports this rate. ```go localUser.SetPlaybackAudioFrameBeforeMixingParameters(2, 16000) ``` -------------------------------- ### Set SDK Libraries and Credentials (Linux/macOS) Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Export environment variables to add SDK libraries to the system's library path and set your Agora App ID and Certificate. ```bash # Add SDK libraries to path export LD_LIBRARY_PATH=./agora_sdk:$LD_LIBRARY_PATH # Linux export DYLD_LIBRARY_PATH=./agora_sdk_mac:$DYLD_LIBRARY_PATH # macOS # Credentials export AGORA_APP_ID="your-app-id" export AGORA_APP_CERTIFICATE="your-certificate" # if required ``` -------------------------------- ### Enable Only Voice Activity Detection (VAD) Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of enabling only Voice Activity Detection (VAD) without applying other audio processing. This is useful when you only need to detect speech presence. ```go apmCfg := agoraservice.NewAPMConfig() apmCfg.VAD = true // Only VAD svcCfg := agoraservice.NewAgoraServiceConfig() svcCfg.APMModel = 1 svcCfg.APMConfig = apmCfg agoraservice.Initialize(svcCfg) ``` -------------------------------- ### Initialize StereoVad for Dual-Channel Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Initialize the StereoVad instance with configuration parameters before processing audio frames. This prepares the VAD for stereo input. ```go // VAD v1 for stereo vadConfigV1 := &agoraservice.AudioVadConfig{ StartRecognizeCount: 10, StopRecognizeCount: 6, PreStartRecognizeCount: 10, ActivePercent: 0.6, InactivePercent: 0.2, // Other parameters can be adjusted as needed or left as default } // Generate stereo VAD steroVadInst := agoraservice.NewSteroVad(vadConfigV1, vadConfigV1) ``` -------------------------------- ### Get Connection Information Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/02-rtc-connection.md Retrieves current connection state details, including channel ID, user ID, and connection status. Useful for monitoring and debugging connection states. ```go info := conn.GetConnectionInfo() fmt.Printf("Connected to channel: %s, User ID: %s, State: %d\n", info.ChannelId, info.LocalUserId, info.State) ``` -------------------------------- ### Service Initialization Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Initializes the Agora service with configuration and releases it when done. This is the first step before any other SDK operations. ```APIDOC ## Initialize Service ### Description Initializes the Agora service with the provided configuration. This must be called before any other SDK functions. ### Method `agoraservice.Initialize(cfg *agoraservice.AgoraServiceConfig)` ### Parameters #### Request Body - **cfg** (*agoraservice.AgoraServiceConfig*) - Required - Configuration for the Agora service. - **AppId** (string) - Required - Your Agora Application ID. - **AudioScenario** (agoraservice.AudioScenario) - Required - The audio scenario for the service. ### Response #### Success Response (0) Returns 0 on successful initialization. #### Error Response Returns a non-zero error code on failure. ## Release Service ### Description Releases all resources used by the Agora service. This should be called when the service is no longer needed, typically during application shutdown. ### Method `agoraservice.Release()` ### Parameters None ``` -------------------------------- ### Configure TTS with Speed Control Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of configuring external audio parameters for TTS, enabling incremental audio transmission with a specified send speed and initial fast transmission duration. This is part of the RtcConnectionPublishConfig. ```go publishCfg := &agoraservice.RtcConnectionPublishConfig{ PublishAudio: true, SendExternalAudioParameters: &agoraservice.SendExternalAudioParameters{ Enabled: true, SendSpeed: 2, // 1=slowest, 5=fastest SendMs: 100, // Fast send for first 100ms DeliverMuteDataForFakeAdm: false, }, } ``` -------------------------------- ### Initialize Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Initializes the Agora Service with the provided configuration. This function must be called once per process before any connections are established. ```APIDOC ## Initialize ### Description Initializes the Agora Service with the provided configuration. This function must be called once per process before creating any connections. ### Parameters #### Request Body - **cfg** (*AgoraServiceConfig) - Required - Service configuration containing AppId and other settings ### Returns - `0` on success - Non-zero error code on failure ### Notes - Must be called once per process before creating any connections - The service is a singleton; multiple calls to `Initialize` will reinitialize the service - On Linux, requires `LD_LIBRARY_PATH` to include the Agora SDK directory - On macOS, requires `DYLD_LIBRARY_PATH` for development ### Example ```go svcCfg := agoraservice.NewAgoraServiceConfig() svcCfg.AppId = appId svcCfg.EnableAudioProcessor = true svcCfg.ChannelProfile = agoraservice.ChannelProfileCommunication svcCfg.AudioScenario = agoraservice.AudioScenarioAiServer err := agoraservice.Initialize(svcCfg) if err != 0 { fmt.Printf("Initialize failed: %d\n", err) return } ``` ``` -------------------------------- ### Configure TTS Scenario with Incremental Sending Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Sets up publishing configuration for a TTS scenario, enabling incremental audio sending with specific parameters for speed and duration. ```go publishCfg := &agoraservice.RtcConnectionPublishConfig{ PublishAudio: true, Scenario: agoraservice.AudioScenarioAiServer, SendExternalAudioParameters: &agoraservice.SendExternalAudioParameters{ Enabled: true, SendSpeed: 2, // 1-5, recommended 2 SendMs: 0, // Fast transmission duration DeliverMuteDataForFakeAdm: false, }, } ``` -------------------------------- ### GetAgoraParameter Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Retrieves the global Agora parameter handler, allowing for the setting and getting of SDK parameters. This is useful for configuring low-level SDK settings like private deployment configurations and applies globally across all connections. ```APIDOC ## GetAgoraParameter ### Description Returns the global Agora parameter handler for setting and getting SDK parameters. ### Returns - `*AgoraParameter`: Pointer to `AgoraParameter` for SDK-wide configuration. ### Notes - Used for setting low-level SDK parameters like private deployment settings. - Can be called at any time after service initialization. - Parameters set here apply globally across all connections. ### Example ```go param := agoraservice.GetAgoraParameter() param.SetParameters(`{"rtc.enable_nasa2": false}`) param.SetParameters(`{"rtc.force_local": true}`) ``` ``` -------------------------------- ### Get Agora RTC Connection Session ID Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/02-rtc-connection.md Retrieves the unique session ID for the current RTC connection. This function should only be called after a successful connection and is useful for debugging and analytics. The session ID is unique per connection. ```go sid := conn.GetSid() fmt.Printf("Session ID: %s\n", sid) ``` -------------------------------- ### Initialize Agora Service Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Initializes the Agora Service. Must be called once per process before creating connections. Ensure LD_LIBRARY_PATH (Linux) or DYLD_LIBRARY_PATH (macOS) is set correctly. ```go svcCfg := agoraservice.NewAgoraServiceConfig() svcCfg.AppId = appId svcCfg.EnableAudioProcessor = true svcCfg.ChannelProfile = agoraservice.ChannelProfileCommunication svcCfg.AudioScenario = agoraservice.AudioScenarioAiServer err := agoraservice.Initialize(svcCfg) if err != 0 { fmt.Printf("Initialize failed: %d\n", err) return } ``` -------------------------------- ### Enable Specific APM Algorithms Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Example of enabling multiple audio processing algorithms including noise suppression, echo cancellation, auto gain control, and background human voice suppression. This configuration is applied when initializing the Agora service. ```go apmCfg := agoraservice.NewAPMConfig() apmCfg.ANS = true // Noise suppression apmCfg.AEC = true // Echo cancellation apmCfg.AGC = true // Auto gain control apmCfg.BGHVS = true // Background suppression svcCfg := agoraservice.NewAgoraServiceConfig() svcCfg.AppId = appId svcCfg.APMModel = 1 // Enable APM svcCfg.APMConfig = apmCfg agoraservice.Initialize(svcCfg) ``` -------------------------------- ### Configure Dual-Channel Encoding Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md Set the audio scenario to game streaming and enable stereo encoding mode for dual-channel audio. This should be done during service initialization. ```go svcCfg := agoraservice.NewAgoraServiceConfig() svcCfg.AppId = appid // Change audio scenario svcCfg.AudioScenario = agoraservice.AudioScenarioGameStreaming svcCfg.EnableSteroEncodeMode = 1 agoraservice.Initialize(svcCfg) ``` -------------------------------- ### Common Default Configurations Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/audio-video-constants.md Provides default settings for audio scenarios, profiles, raw audio frame operation modes, video streams, pixel formats, codecs, client roles, channel profiles, and area codes. ```go // Audio defaults AudioScenarioDefault // Safe for most use cases AudioProfileDefault // 16kHz mono, reasonable bitrate RawAudioFrameOpModeReadOnly // Don't modify frames // Video defaults VideoStreamHigh // Full resolution VideoPixelI420 // Most compatible raw format VideoCodecTypeH264 // Most widely supported // Connection defaults ClientRoleBroadcaster // Can publish and receive ChannelProfileCommunication // Bidirectional communication AreaCodeGlob // Automatic region selection ``` -------------------------------- ### Run Application on macOS Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Ensure the SDK is present in the expected directory on macOS. Set the dynamic library path to include the SDK before running your application. ```bash # SDK already in agora_sdk_mac/ # Run with library path export DYLD_LIBRARY_PATH=./agora_sdk_mac:$DYLD_LIBRARY_PATH ./your-app ``` -------------------------------- ### Initialize RtcConnection with Publish Config Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Initializes a new RTC connection, providing both connection and publishing configurations. ```go publishCfg := &agoraservice.RtcConnectionPublishConfig{ PublishAudio: true, PublishVideo: false, AudioProfile: agoraservice.AudioProfileMusicHighQuality, } con := agoraservice.NewRtcConnection(conCfg, publishCfg) ``` -------------------------------- ### Implement OnPlaybackAudioFrame Callback Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Implement the OnPlaybackAudioFrame callback to receive the mixed audio from all remote users. This callback provides the combined audio stream before it's played out. Returning 'false' will drop the frame. ```go OnPlaybackAudioFrame: func(localUser *LocalUser, channelId string, frame *AudioFrame) bool ``` -------------------------------- ### Build with Make and AVCodec Tag Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/README.md When using the 'make' utility, specify the TAGS=avcodec argument to build with video transcoding capabilities. ```bash make build TAGS=avcodec ``` ```bash make example TAGS=avcodec ``` ```bash make advanced-examples TAGS=avcodec ``` -------------------------------- ### Agora Service and Connection Pool Pattern Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Demonstrates the typical workflow for initializing the Agora service, managing connections from a pool for user logins/logouts, and releasing resources upon process exit. This pattern is suitable for multi-user scenarios in Docker or long-running services. ```go // Startup agoraservice.Initialize(svcCfg) // For each user login conn := connectionPool.GetConnection() conn.Connect(token, channelName, userId) // For each user logout conn.Disconnect() connectionPool.ReturnConnection(conn) // Shutdown agoraservice.Release() ``` -------------------------------- ### Initialize AI Server with Voice Processing Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Configures the Agora service for AI server scenarios with audio processing enabled. Sets up a real-time connection for publishing audio. ```go svcCfg := agoraservice.NewAgoraServiceConfig() svcCfg.AppId = appId svcCfg.AudioScenario = agoraservice.AudioScenarioAiServer svcCfg.EnableAudioProcessor = true svcCfg.APMModel = 1 svcCfg.APMConfig = agoraservice.NewAPMConfig() svcCfg.APMConfig.ANS = true svcCfg.APMConfig.AGC = true svcCfg.APMConfig.BGHVS = true agoraservice.Initialize(svcCfg) conCfg := &agoraservice.RtcConnectionConfig{ AudioScenario: agoraservice.AudioScenarioAiServer, } publishCfg := &agoraservice.RtcConnectionPublishConfig{ PublishAudio: true, Scenario: agoraservice.AudioScenarioAiServer, } con := agoraservice.NewRtcConnection(conCfg, publishCfg) con.Connect(token, channel, userId) ``` -------------------------------- ### Initialize RtcConnection with Config Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/configuration.md Initializes a new RTC connection using RtcConnectionConfig. The second parameter is for publish configuration, which can be nil. ```go conCfg := &agoraservice.RtcConnectionConfig{ AudioScenario: agoraservice.AudioScenarioAiServer, ChannelProfile: agoraservice.ChannelProfileCommunication, } con := agoraservice.NewRtcConnection(conCfg, nil) ``` -------------------------------- ### Configure Audio Volume Indication Parameters Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/03-local-user.md Configures parameters for audio volume indication callbacks. This enables the OnAudioVolumeIndication callback and allows customization of callback frequency, smoothing, and VAD reporting. ```go err := localUser.SetAudioVolumeIndicationParameters(100, 3, true) ``` -------------------------------- ### AgoraServiceConfig Structure Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Defines the service-level configuration for initializing the Agora Go SDK. Use this to control audio/video enablement, core settings, logging, and advanced features. ```go type AgoraServiceConfig struct { // Audio/Video Enablement EnableAudioProcessor bool EnableAudioDevice bool EnableVideo bool // Core Settings AppId string AreaCode AreaCode ChannelProfile ChannelProfile AudioScenario AudioScenario UseStringUid bool // Logging LogPath string LogSize int LogLevel int // Advanced Settings DomainLimit int ShouldCallbackWhenMuted int EnableSteroEncodeMode int ConfigDir string DataDir string // Audio Processing APMModel int APMConfig *APMConfig IdleMode bool } ``` -------------------------------- ### Create Connection Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/README.md Creates and configures an RTC connection, registers an observer, and connects to an Agora channel. ```APIDOC ## Create RTC Connection ### Description Creates a new Real-Time Communication (RTC) connection with specified configuration and registers an observer to handle connection events. ### Method `agoraservice.NewRtcConnection(conCfg *agoraservice.RtcConnectionConfig, publishCfg *agoraservice.RtcConnectionPublishConfig) *agoraservice.RtcConnection` ### Parameters #### Request Body - **conCfg** (*agoraservice.RtcConnectionConfig*) - Required - Configuration for the RTC connection. - **AudioScenario** (agoraservice.AudioScenario) - Required - The audio scenario for this connection. - **publishCfg** (*agoraservice.RtcConnectionPublishConfig*) - Required - Configuration for publishing streams. - **PublishAudio** (bool) - Required - Whether to publish audio. ### Response Returns a pointer to the newly created `RtcConnection` object. ## Register Connection Observer ### Description Registers an observer to receive callbacks for RTC connection events, such as connection success or failure. ### Method `con.RegisterObserver(observer *agoraservice.RtcConnectionObserver)` ### Parameters #### Request Body - **observer** (*agoraservice.RtcConnectionObserver*) - Required - An object containing callback functions for connection events. - **OnConnected** (func(*agoraservice.RtcConnection, *agoraservice.RtcConnectionInfo, int)) - Callback for when the connection is established. ### Response None ## Connect to Channel ### Description Connects the RTC connection to a specified Agora channel using a token and user ID. ### Method `con.Connect(token string, channelName string, userId string)` ### Parameters #### Request Body - **token** (string) - Required - The authentication token for the channel. - **channelName** (string) - Required - The name of the channel to connect to. - **userId** (string) - Required - The unique identifier for the user. ### Response None ## Release Connection ### Description Releases all resources associated with the RTC connection. This should be called when the connection is no longer needed. ``` -------------------------------- ### Load Extension Provider Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/01-agora-service.md Loads an extension provider from a specified path. Use this when you need to integrate custom or third-party audio/video processing extensions. Ensure the path points to a valid shared library. Note that macOS returns ERR_NOT_SUPPORTED. ```go path := "/path/to/extension_provider.so" err := agoraservice.LoadExtensionProvider(path, true) if err != 0 { fmt.Printf("Failed to load extension: %d\n", err) } ``` -------------------------------- ### Update Local User Audio Scenario Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/03-local-user.md Updates the audio scenario for the local user. This can be called anytime after connection creation and should align with the audio track's scenario for consistent behavior. ```go err := localUser.SetAudioScenario(agoraservice.AudioScenarioAiServer) ``` -------------------------------- ### Handle Successful Connection Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/callbacks.md Implement the OnConnected callback to log connection details. Register the observer with the connection instance. ```go observer := &agoraservice.RtcConnectionObserver{ OnConnected: func(con *agoraservice.RtcConnection, conInfo *agoraservice.RtcConnectionInfo, reason int) { fmt.Printf("Connected to channel %s, local user: %s\n", conInfo.ChannelId, conInfo.LocalUserId) }, } con.RegisterObserver(observer) ``` -------------------------------- ### Publish Audio Source: https://github.com/agoraio-extensions/agora-golang-server-sdk/blob/main/_autodocs/02-rtc-connection.md Publishes audio from the local user. Call this before pushing audio data. It creates an internal audio track if one doesn't exist and allows others to subscribe. ```go func (conn *RtcConnection) PublishAudio() int ``` ```go err := conn.PublishAudio() if err != 0 { fmt.Printf("PublishAudio failed: %d\n", err) } ```