You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用中如何通过PJSIP获知远端停止视频通话?

Detecting When a Remote Peer Stops Video in PJSIP (iOS)

Great question! When building iOS apps with PJSIP, detecting when a remote peer stops sending video mid-call is a common requirement, and the library provides solid tools to handle this. Let’s break down the reliable approaches you can use:

1. Listen for the onCallMediaState Callback

This is the most straightforward method—PJSIP triggers this event whenever the media state of a call changes. When the remote stops sending video, the corresponding video media stream will transition from an active to inactive state.

In your iOS implementation (using the PJSIP Objective-C wrapper), you’ll implement the PJSUACallDelegate method like this:

- (void)onCallMediaState:(PJSUACall *)call {
    // Loop through all media streams attached to the call
    for (PJSUAMedia *media in call.mediaList) {
        // Target only video media streams
        if (media.type == PJMEDIA_TYPE_VIDEO) {
            // Check if the remote stream has become inactive
            if (media.stream.remoteInfo.status == PJSUA_MEDIA_STATUS_INACTIVE) {
                // Your custom logic here—update UI, notify user, etc.
                NSLog(@"Remote has stopped sending video!");
            }
        }
    }
}

Keep in mind: If the remote completely removes the video stream (not just pauses), the media entry might also be removed from the mediaList, so you can also check if the video media count drops to zero.

2. Handle onCallMediaUpdate Events

When the remote updates their session description (SDP)—for example, removing the video track—PJSIP fires the onCallMediaUpdate event. You can parse the updated call info to verify if video is still present.

Here’s how to implement this:

- (void)onCallMediaUpdate:(PJSUACall *)call {
    pjsua_call_info callInfo;
    pjsua_call_get_info(call.callId, &callInfo);
    
    // Iterate through each media entry in the call info
    for (int i = 0; i < callInfo.media_cnt; i++) {
        pjsua_call_media_info *mediaInfo = &callInfo.media[i];
        if (mediaInfo->type == PJMEDIA_TYPE_VIDEO) {
            // If the media status is no longer active, video has stopped
            if (mediaInfo->status != PJSUA_CALL_MEDIA_ACTIVE) {
                NSLog(@"Remote video stream has been stopped or removed");
            }
        }
    }
}

3. Proactively Check Remote Video State (On-Demand)

If you need to check the remote video state proactively (instead of waiting for events), use the pjsua_vid_channel_get_remote_state API. This is useful for periodic checks or when you need to confirm the state at a specific moment:

// Replace 0 with the correct video media index if you have multiple streams
pjsua_vid_channel *videoChannel = pjsua_call_get_vid_channel(call.callId, 0);
if (videoChannel != NULL) {
    pjsua_vid_remote_state remoteState;
    pjsua_vid_channel_get_remote_state(videoChannel, &remoteState);
    
    if (remoteState == PJSUA_VID_REMOTE_STATE_STOPPED) {
        NSLog(@"Remote video is currently inactive");
    }
}

Quick Notes to Avoid Headaches

  • Make sure your PJSIP build includes video support, and you’ve initialized the video subsystem correctly during app startup (setting codecs, renderers, etc.).
  • Network glitches can cause temporary video drops—add a small debounce delay (1-2 seconds) before triggering your "video stopped" logic to avoid false positives.
  • API names might vary slightly between PJSIP versions, so cross-check with the docs for your specific library version.

内容的提问来源于stack exchange,提问作者Erhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:18:15