“@CalledByNative("...")”是什么意思?

3

如何使用@CalledByNative("...")?我需要从webrtc库中获得回调。

如果您了解它上的PeerConnection类:

PeerConnection.java

这是一个旧版本的PeerConnection,但现在几乎相同

我调用了函数addStream,但无法从中获取回调。

  • 请解释一下calledbynative的工作原理!

PeerConnection.java

/*
 *  Copyright 2013 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
package org.webrtc;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
 * Java-land version of the PeerConnection APIs; wraps the C++ API
 * http://www.webrtc.org/reference/native-apis, which in turn is inspired by the
 * JS APIs: http://dev.w3.org/2011/webrtc/editor/webrtc.html and
 * http://www.w3.org/TR/mediacapture-streams/
 */
public class PeerConnection {
  static {
    System.loadLibrary("jingle_peerconnection_so");
  }
  /** Tracks PeerConnectionInterface::IceGatheringState */
  public enum IceGatheringState { NEW, GATHERING, COMPLETE }
  /** Tracks PeerConnectionInterface::IceConnectionState */
  public enum IceConnectionState {
    NEW,
    CHECKING,
    CONNECTED,
    COMPLETED,
    FAILED,
    DISCONNECTED,
    CLOSED
  }
  /** Tracks PeerConnectionInterface::SignalingState */
  public enum SignalingState {
    STABLE,
    HAVE_LOCAL_OFFER,
    HAVE_LOCAL_PRANSWER,
    HAVE_REMOTE_OFFER,
    HAVE_REMOTE_PRANSWER,
    CLOSED
  }
  /** Java version of PeerConnectionObserver. */
  public static interface Observer {
    /** Triggered when the SignalingState changes. */
    public void onSignalingChange(SignalingState newState);
    /** Triggered when the IceConnectionState changes. */
    public void onIceConnectionChange(IceConnectionState newState);
    /** Triggered when the ICE connection receiving status changes. */
    public void onIceConnectionReceivingChange(boolean receiving);
    /** Triggered when the IceGatheringState changes. */
    public void onIceGatheringChange(IceGatheringState newState);
    /** Triggered when a new ICE candidate has been found. */
    public void onIceCandidate(IceCandidate candidate);
    /** Triggered when some ICE candidates have been removed. */
    public void onIceCandidatesRemoved(IceCandidate[] candidates);
    /** Triggered when media is received on a new stream from remote peer. */
    public void onAddStream(MediaStream stream);
    /** Triggered when a remote peer close a stream. */
    public void onRemoveStream(MediaStream stream);
    /** Triggered when a remote peer opens a DataChannel. */
    public void onDataChannel(DataChannel dataChannel);
    /** Triggered when renegotiation is necessary. */
    public void onRenegotiationNeeded();
  }
  /** Java version of PeerConnectionInterface.IceServer. */
  public static class IceServer {
    public final String uri;
    public final String username;
    public final String password;
    /** Convenience constructor for STUN servers. */
    public IceServer(String uri) {
      this(uri, "", "");
    }
    public IceServer(String uri, String username, String password) {
      this.uri = uri;
      this.username = username;
      this.password = password;
    }
    public String toString() {
      return uri + "[" + username + ":" + password + "]";
    }
  }
  /** Java version of PeerConnectionInterface.IceTransportsType */
  public enum IceTransportsType { NONE, RELAY, NOHOST, ALL }
  /** Java version of PeerConnectionInterface.BundlePolicy */
  public enum BundlePolicy { BALANCED, MAXBUNDLE, MAXCOMPAT }
  /** Java version of PeerConnectionInterface.RtcpMuxPolicy */
  public enum RtcpMuxPolicy { NEGOTIATE, REQUIRE }
  /** Java version of PeerConnectionInterface.TcpCandidatePolicy */
  public enum TcpCandidatePolicy { ENABLED, DISABLED }
  /** Java version of PeerConnectionInterface.CandidateNetworkPolicy */
  public enum CandidateNetworkPolicy { ALL, LOW_COST }
  /** Java version of rtc::KeyType */
  public enum KeyType { RSA, ECDSA }
  /** Java version of PeerConnectionInterface.ContinualGatheringPolicy */
  public enum ContinualGatheringPolicy { GATHER_ONCE, GATHER_CONTINUALLY }
  /** Java version of PeerConnectionInterface.RTCConfiguration */
  public static class RTCConfiguration {
    public IceTransportsType iceTransportsType;
    public List<IceServer> iceServers;
    public BundlePolicy bundlePolicy;
    public RtcpMuxPolicy rtcpMuxPolicy;
    public TcpCandidatePolicy tcpCandidatePolicy;
    public CandidateNetworkPolicy candidateNetworkPolicy;
    public int audioJitterBufferMaxPackets;
    public boolean audioJitterBufferFastAccelerate;
    public int iceConnectionReceivingTimeout;
    public int iceBackupCandidatePairPingInterval;
    public KeyType keyType;
    public ContinualGatheringPolicy continualGatheringPolicy;
    public int iceCandidatePoolSize;
    public boolean pruneTurnPorts;
    public boolean presumeWritableWhenFullyRelayed;
    public RTCConfiguration(List<IceServer> iceServers) {
      iceTransportsType = IceTransportsType.ALL;
      bundlePolicy = BundlePolicy.BALANCED;
      rtcpMuxPolicy = RtcpMuxPolicy.NEGOTIATE;
      tcpCandidatePolicy = TcpCandidatePolicy.ENABLED;
      candidateNetworkPolicy = candidateNetworkPolicy.ALL;
      this.iceServers = iceServers;
      audioJitterBufferMaxPackets = 50;
      audioJitterBufferFastAccelerate = false;
      iceConnectionReceivingTimeout = -1;
      iceBackupCandidatePairPingInterval = -1;
      keyType = KeyType.ECDSA;
      continualGatheringPolicy = ContinualGatheringPolicy.GATHER_ONCE;
      iceCandidatePoolSize = 0;
      pruneTurnPorts = false;
      presumeWritableWhenFullyRelayed = false;
    }
  };
  private final List<MediaStream> localStreams;
  private final long nativePeerConnection;
  private final long nativeObserver;
  private List<RtpSender> senders;
  private List<RtpReceiver> receivers;
  PeerConnection(long nativePeerConnection, long nativeObserver) {
    this.nativePeerConnection = nativePeerConnection;
    this.nativeObserver = nativeObserver;
    localStreams = new LinkedList<MediaStream>();
    senders = new LinkedList<RtpSender>();
    receivers = new LinkedList<RtpReceiver>();
  }
  // JsepInterface.
  public native SessionDescription getLocalDescription();
  public native SessionDescription getRemoteDescription();
  public native DataChannel createDataChannel(String label, DataChannel.Init init);
  public native void createOffer(SdpObserver observer, MediaConstraints constraints);
  public native void createAnswer(SdpObserver observer, MediaConstraints constraints);
  public native void setLocalDescription(SdpObserver observer, SessionDescription sdp);
  public native void setRemoteDescription(SdpObserver observer, SessionDescription sdp);
  public native boolean setConfiguration(RTCConfiguration config);
  public boolean addIceCandidate(IceCandidate candidate) {
    return nativeAddIceCandidate(candidate.sdpMid, candidate.sdpMLineIndex, candidate.sdp);
  }
  public boolean removeIceCandidates(final IceCandidate[] candidates) {
    return nativeRemoveIceCandidates(candidates);
  }
  public boolean addStream(MediaStream stream) {
    boolean ret = nativeAddLocalStream(stream.nativeStream);
    if (!ret) {
      return false;
    }
    localStreams.add(stream);
    return true;
  }
  public void removeStream(MediaStream stream) {
    nativeRemoveLocalStream(stream.nativeStream);
    localStreams.remove(stream);
  }
  public RtpSender createSender(String kind, String stream_id) {
    RtpSender new_sender = nativeCreateSender(kind, stream_id);
    if (new_sender != null) {
      senders.add(new_sender);
    }
    return new_sender;
  }
  // Note that calling getSenders will dispose of the senders previously
  // returned (and same goes for getReceivers).
  public List<RtpSender> getSenders() {
    for (RtpSender sender : senders) {
      sender.dispose();
    }
    senders = nativeGetSenders();
    return Collections.unmodifiableList(senders);
  }
  public List<RtpReceiver> getReceivers() {
    for (RtpReceiver receiver : receivers) {
      receiver.dispose();
    }
    receivers = nativeGetReceivers();
    return Collections.unmodifiableList(receivers);
  }
  public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
    return nativeGetStats(observer, (track == null) ? 0 : track.nativeTrack);
  }
  // Starts recording an RTC event log. Ownership of the file is transfered to
  // the native code. If an RTC event log is already being recorded, it will be
  // stopped and a new one will start using the provided file. Logging will
  // continue until the stopRtcEventLog function is called. The max_size_bytes
  // argument is ignored, it is added for future use.
  public boolean startRtcEventLog(int file_descriptor, int max_size_bytes) {
    return nativeStartRtcEventLog(file_descriptor, max_size_bytes);
  }
  // Stops recording an RTC event log. If no RTC event log is currently being
  // recorded, this call will have no effect.
  public void stopRtcEventLog() {
    nativeStopRtcEventLog();
  }
  // TODO(fischman): add support for DTMF-related methods once that API
  // stabilizes.
  public native SignalingState signalingState();
  public native IceConnectionState iceConnectionState();
  public native IceGatheringState iceGatheringState();
  public native void close();
  public void dispose() {
    close();
    for (MediaStream stream : localStreams) {
      nativeRemoveLocalStream(stream.nativeStream);
      stream.dispose();
    }
    localStreams.clear();
    for (RtpSender sender : senders) {
      sender.dispose();
    }
    senders.clear();
    for (RtpReceiver receiver : receivers) {
      receiver.dispose();
    }
    receivers.clear();
    freePeerConnection(nativePeerConnection);
    freeObserver(nativeObserver);
  }
  private static native void freePeerConnection(long nativePeerConnection);
  private static native void freeObserver(long nativeObserver);
  private native boolean nativeAddIceCandidate(
      String sdpMid, int sdpMLineIndex, String iceCandidateSdp);
  private native boolean nativeRemoveIceCandidates(final IceCandidate[] candidates);
  private native boolean nativeAddLocalStream(long nativeStream);
  private native void nativeRemoveLocalStream(long nativeStream);
  private native boolean nativeGetStats(StatsObserver observer, long nativeTrack);
  private native RtpSender nativeCreateSender(String kind, String stream_id);
  private native List<RtpSender> nativeGetSenders();
  private native List<RtpReceiver> nativeGetReceivers();
  private native boolean nativeStartRtcEventLog(int file_descriptor, int max_size_bytes);
  private native void nativeStopRtcEventLog();
}
2个回答

5

@CallByNative是Google WebRtc开发团队使用的注释,用于指定WebRtc的Java代码和Native(C/C++)栈之间的映射关系。

正如WebRtc Chromium Git的官方表述:

@CalledByNative由JNI生成器使用,以创建必要的JNI绑定并将此方法暴露给本地代码。

现在如果有一个问题,即WebRtc Java代码如何与WebRtc Native代码相互链接?

为此,我建议您查看WebRtc的Native代码将帮助您最多。您将从本地栈内部的注释中学到更多信息和相关内容。另一方面,您还应该具备以下基本知识:

  • Java Native Interface (JNI)的工作原理
  • WebRtc Java和Native Stack的相互关系

要详细了解Java Native Interface的直觉,您可以访问以下参考资料:

这里我举一个PeerConnection.java的例子。假设我们在PeerConnection.java类中有一个方法,例如:

@CalledByNative("IceGatheringState")
static IceGatheringState fromNativeIndex(int nativeIndex) {
  return values()[nativeIndex];
}

现在,这个方法被映射到 src/sdk/android/src/jni/pc/peer_connection.cc 类中,例如:
static ScopedJavaLocalRef<jobject> JNI_PeerConnection_IceGatheringState(
    JNIEnv* env,
    const JavaParamRef<jobject>& j_pc) {
  return Java_IceGatheringState_fromNativeIndex(
      env, ExtractNativePC(env, j_pc)->ice_gathering_state());
}

如果你同时查看Java和Native Class的PeerConnection,使用相同的技巧,你一定会发现这种类型的其他联系。那么,@CallByNative在这里做了什么呢?
如果你看一下,在加载库之后,@CallByNative基本上做了以下两件事:
- 告诉该方法属于哪个内部类 - 将此Java方法暴露给Native Code - 用Java和Native Code进行JNI绑定
这些类型的注释由Google开发人员提供,以使在使用预构建库或自己编译的库(如libwebrtc.aar)时,与JNI易于理解并且更容易操作。

3
我最近对这个主题进行了一些调查,现在想分享我的研究结果。我发现这可能与Java中的@Override方法和"super"关键字有关。我猜想当我们尝试从MainActivity.java类调用位于PeerConnection.java文件中的覆盖方法onIceCandidateonAddStream时,我们会写成:
public class MainActivity extends AppCompatActivity implements View.OnClickListener, SignallingClient.SignalingInterface {
...
private void createPeerConnection() {
...
            @Override
            public void onIceCandidate(IceCandidate iceCandidate) {
                super.onIceCandidate(iceCandidate);
                onIceCandidateReceived(iceCandidate);
            }

            @Override
            public void onAddStream(MediaStream mediaStream) {
                showToast("Received Remote stream");
                super.onAddStream(mediaStream);
                gotRemoteStream(mediaStream);
            }
...
}
...
}

此外,onAddStreamonIceCandidate都是重写方法。如果我们通过super关键字追踪这些方法,我们会进入以下文件CustomPeerConnectionObserver.java
class CustomPeerConnectionObserver implements PeerConnection.Observer {
...
    @Override
    public void onIceCandidate(IceCandidate iceCandidate) {
        Log.d(logTag, "onIceCandidate() called with: iceCandidate = [" + iceCandidate + "]");
    }
...
    @Override
    public void onAddStream(MediaStream mediaStream) {
        Log.d(logTag, "onAddStream() called with: mediaStream = [" + mediaStream + "]");
    }
...
}

现在我们看一下这个类,发现它“实现”了PeerConnection.Observer。如果我们继续深入调查,我们会发现我们被引导到文件PeerConnection.java

public class PeerConnection {
...
public interface Observer {
...
        @CalledByNative("Observer")
        void onIceCandidate(IceCandidate var1);
...
        @CalledByNative("Observer")
        void onAddStream(MediaStream var1);
...
 }
...
}

我们进入了被称为“Observer”的“接口”。到目前为止,我使用了GitHub中的例子并尝试理解应用程序如何工作。它是为Android设计的,但我认为我们使用Java,所以不应该有太大的区别,如果我错了请纠正我。
总之,我认为当发生特定事件和/或在作为@Override方法时提及/编写它时,它会自动调用。我还假设@CalledByNative标记可能会影响方法调用,例如addStream,同样在我上面提供的示例中:
  • onIceCandidate
  • onAddStream。
我希望至少能给你一些关于我对这种行为调查的参考点,你可以从中找到线索并进行进一步研究。我也期待着专家能回答这个好问题,即@CalledByNative如何工作。

这里有更多的细节可供参考(https://github.com/adobe/chromium/blob/master/base/android/java/org/chromium/base/CalledByNative.java)。`CalledByNative`被记录为“//此注释由JNI生成器用于创建必要的JNI绑定并将此方法公开给本地代码。” - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接