Exemplo n.º 1
0
    public void onStreamMessageNative(string data)
    {
        //take uid and data

        string[] splitData = data.Split(new string[] { "*data*" }, StringSplitOptions.None);

        string uidString = splitData[0];

        //convert uid to uint uid
        uint uid = UInt32.Parse(uidString);

        string justData = splitData[1];

        NetworkCharacter character = getCharacterFromID(uid);

        character.ReceiveLipsyncMessage(justData);
    }
Exemplo n.º 2
0
    public void initAgoraEngine()
    {
        Debug.Log("init agora engine");

        mRtcEngine = IRtcEngine.GetEngine(appId);

        audioRawDataManager = AudioRawDataManager.GetInstance(mRtcEngine);
        audioRawDataManager.SetOnMixedAudioFrameCallback(OnMixedAudioFrameHandler);
        audioRawDataManager.SetOnPlaybackAudioFrameBeforeMixingCallback(OnPlaybackAudioFrameBeforeMixingHandler);
        audioRawDataManager.SetOnPlaybackAudioFrameCallback(OnPlaybackAudioFrameHandler);
        audioRawDataManager.SetOnRecordAudioFrameCallback(OnRecordAudioFrameHandler);

        // mRtcEngine.SetLogFilter(LOG_FILTER.INFO);

        // mRtcEngine.setLogFile("path_to_file_unity.log");

        //mRtcEngine.EnableDualStreamMode(true);
        mRtcEngine.SetClientRole(CLIENT_ROLE.BROADCASTER);

        //mRtcEngine.SetChannelProfile(CHANNEL_PROFILE.GAME_FREE_MODE);

        mDataStreamId = GameState.Instance.mRtcEngine.CreateDataStream(true, true);

        mRtcEngine.OnJoinChannelSuccess += (string channelName, uint uid, int elapsed) => {
            string joinSuccessMessage = string.Format(debugTag + "joinChannel callback uid: {0}, channel: {1}, version: {2}", uid, channelName, IRtcEngine.GetSdkVersion());
            //Debug.Log(joinSuccessMessage);

            Debug.Log("OnJoinChannelSuccess " + uid);

            UDID = uid;

            createCharacter(uid, true);

            GameState.Instance.PushState(EGameState.ChatRoomState);

            PopupManager.Instance.showLoadingPopUp(false);
        };

        //mRtcEngine.EnableVideo();
        //mRtcEngine.EnableVideoObserver();

        mRtcEngine.OnLeaveChannel += (RtcStats stats) => {
            string leaveChannelMessage = string.Format(debugTag + "onLeaveChannel callback duration {0}, tx: {1}, rx: {2}, tx kbps: {3}, rx kbps: {4}", stats.duration, stats.txBytes, stats.rxBytes, stats.txKBitRate, stats.rxKBitRate);
            //Debug.Log(leaveChannelMessage);



            //reloadEngine();
            audioRawDataManager.UnRegisteAudioRawDataObserver();
        };

        mRtcEngine.OnUserJoined += (uint uid, int elapsed) => {
            string userJoinedMessage = string.Format(debugTag + "onUserJoined callback uid {0} {1}", uid, elapsed);
            //Debug.Log(userJoinedMessage);

            Debug.Log("on user joined " + uid);

            //remote client
            //GameManager.Instance.createAvatar(uid, string.Empty);
            NetworkCharacter character = createCharacter(uid);

            if (lipSyncSolution == ELipSyncSolution.SalsaSolution)
            {
                int samplerate = 44100;

                clipTemp = AudioClip.Create("clipTemp", samplerate * 2, 1, samplerate, false);

                character.audioSource.clip = clipTemp;
                character.audioSource.loop = true;

                character.salsaComponent.audioSrc = character.audioSource;

                character.salsaComponent.SetAudioClip(character.audioSource.clip);



                character.audioSource.Play();



                //character.salsaComponent.Play();

                //character.audioSource.clip = clipTemp;


                //character.audioSource.Play();

                //character.salsaComponent.audioClip = clipTemp;
                character.salsaComponent.Play();
            }
        };

        mRtcEngine.OnUserOffline += (uint uid, USER_OFFLINE_REASON reason) => {
            string userOfflineMessage = string.Format(debugTag + "onUserOffline callback uid {0} {1}", uid, reason);
            //Debug.Log(userOfflineMessage);

            NetworkCharacter character = getCharacterFromID(uid);

            Destroy(character.gameObject);

            GameManager.Instance.UserLeaveChannel(uid);
        };

        mRtcEngine.OnVolumeIndication += (AudioVolumeInfo[] speakers, int speakerNumber, int totalVolume) => {
            if (speakerNumber == 0 || speakers == null)
            {
                //Debug.Log(string.Format(debugTag + "onVolumeIndication only local {0}", totalVolume));
            }

            for (int idx = 0; idx < speakerNumber; idx++)
            {
                string volumeIndicationMessage = string.Format(debugTag + "{0} onVolumeIndication {1} {2}", speakerNumber, speakers[idx].uid, speakers[idx].volume);
                //Debug.Log(volumeIndicationMessage);
            }
        };

        mRtcEngine.OnStreamMessageError += (uint userId, int streamId, int code, int missed, int cached) =>
        {
            Debug.Log("OnStreamMessageError " + code);
        };

        mRtcEngine.OnStreamMessage += (uint uid, int streamId, string data, int length) => {
            NetworkCharacter networkCharacter = getCharacterFromID(uid);

            Debug.Log("receive OnStreamMessage");

            networkCharacter.ReceiveLipsyncMessage(data);
        };

        mRtcEngine.OnUserMuted += (uint uid, bool muted) => {
            string userMutedMessage = string.Format(debugTag + "onUserMuted callback uid {0} {1}", uid, muted);
            //Debug.Log(userMutedMessage);
        };

        mRtcEngine.OnWarning += (int warn, string msg) => {
            string description    = IRtcEngine.GetErrorDescription(warn);
            string warningMessage = string.Format(debugTag + "onWarning callback {0} {1} {2}", warn, msg, description);
            //Debug.Log(warningMessage);
        };

        mRtcEngine.OnError += (int error, string msg) => {
            string description  = IRtcEngine.GetErrorDescription(error);
            string errorMessage = string.Format(debugTag + "onError callback {0} {1} {2}", error, msg, description);
            //Debug.Log(errorMessage);
        };

        mRtcEngine.OnRtcStats += (RtcStats stats) => {
            string rtcStatsMessage = string.Format(debugTag + "onRtcStats callback duration {0}, tx: {1}, rx: {2}, tx kbps: {3}, rx kbps: {4}, tx(a) kbps: {5}, rx(a) kbps: {6} users {7}",
                                                   stats.duration, stats.txBytes, stats.rxBytes, stats.txKBitRate, stats.rxKBitRate, stats.txAudioKBitRate, stats.rxAudioKBitRate, stats.users);
            //Debug.Log(rtcStatsMessage);

            int lengthOfMixingFile = mRtcEngine.GetAudioMixingDuration();
            int currentTs          = mRtcEngine.GetAudioMixingCurrentPosition();

            //string mixingMessage = string.Format(debugTag + "Mixing File Meta {0}, {1}", lengthOfMixingFile, currentTs);
            // Debug.Log(mixingMessage);
        };

        mRtcEngine.OnAudioRouteChanged += (AUDIO_ROUTE route) => {
            //string routeMessage = string.Format(debugTag + "onAudioRouteChanged {0}", route);
            //Debug.Log(routeMessage);
        };

        mRtcEngine.OnRequestToken += () => {
            string requestKeyMessage = string.Format(debugTag + "OnRequestToken");
            //Debug.Log(requestKeyMessage);
        };

        mRtcEngine.OnConnectionInterrupted += () => {
            string interruptedMessage = string.Format(debugTag + "OnConnectionInterrupted");
            //Debug.Log(interruptedMessage);
        };

        mRtcEngine.OnConnectionLost += () => {
            string lostMessage = string.Format(debugTag + "OnConnectionLost");
            //Debug.Log(lostMessage);
        };
    }