2014-01-13 158 views
17

我正在開發一個聊天應用程序並使用它完成。現在我想要實現視頻聊天。 經過很多研究,我決定選擇「WebRTC」庫。本地服務器的ApprtcDemo適用於瀏覽器,但Android本機不適用於瀏覽器

我做了什麼?

1)能夠在本地服務器上運行AppRtcDemo並且在瀏覽器之間正常工作。

參考:http://www.webrtc.org/reference/getting-started

2)能夠構建Android AppRtcDemo.But當我運行它說: 「跨起源不支持」。

經過研究,我發現在webrtc的討論中,要解決這個問題,我需要設置自己的回合服務器。

3)所以我安裝了webrtc推薦的最新rfc5766TurnServer。我成功運行了轉向服務器。

參考:http://code.google.com/p/rfc5766-turn-server/

我以下更改ApprtcDemo(網頁)和(安卓)我打開服務器一起工作

1)apprtc.py

替換:

turn_url = 'https://computeengineondemand.appspot.com/' 
turn_url = turn_url + 'turn?' + 'username=' + user + '&key=4080218913' 

隨着指向我的服務器:

turn_url = 'http://192.168.5.85:3478/?service=turn&username=biraj' 

2)的index.html

替換:

var pcConfig = {{ pc_config|safe }}; 

隨着:

var pcConfig = {"iceServers": [{"url": "stun:stun.l.google.com:19302"},   {"url":"turn:[email protected]:3479", "credential":"0x5b04123c3eec4cf0be64ab909bb2ff5b"}]}; 

的Android

1)AppRTCDemoActivity.java

替換:

roomInput.setText("https://apprtc.appspot.com/?r="); 

與本地apprtc服務器:

roomInput.setText("http://192.168.5.86:8080/?r="); 

2)AppRTCClient。的java

private PeerConnection.IceServer requestTurnServer(String url){}功能

替換:

connection.addRequestProperty("origin", "https://apprtc.appspot.com"); 

隨着:

connection.addRequestProperty("origin", "http://192.168.5.86:8080"); 

3)/assets/channel.html

替換:

<script src="https://apprtc.appspot.com/_ah/channel/jsapi"></script> 

有了:

<script src="http://192.168.5.86:8080/_ah/channel/jsapi"></script> 

現在我的問題是,爲什麼這是瀏覽器之間而不是Android的AppRtcDemo和瀏覽器之間的工作。

當我在執行上述操作後在android上運行AppRtcDemo時,本地攝像頭預覽在右上角開始,消息提示「等待ICEcandidates」,則不會發生任何事情。

在此先感謝。

感謝所有支持我的問題。經過與ApprtcDemo長時間的騎行之後,我獲得了成功並且工作正常。我發佈瞭解決方案。

找到「GAEChannelClient.java」java文件。

並做如下更改。

/* 
* libjingle 
* Copyright 2013, Google Inc. 
* 
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met: 
* 
* 1. Redistributions of source code must retain the above copyright notice, 
*  this list of conditions and the following disclaimer. 
* 2. Redistributions in binary form must reproduce the above copyright notice, 
*  this list of conditions and the following disclaimer in the documentation 
*  and/or other materials provided with the distribution. 
* 3. The name of the author may not be used to endorse or promote products 
*  derived from this software without specific prior written permission. 
* 
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
*/ 

package org.appspot.apprtc; 

import java.io.InputStream; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.util.Log; 
import android.webkit.ConsoleMessage; 
import android.webkit.JavascriptInterface; 
import android.webkit.WebChromeClient; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

/** 
* Java-land version of Google AppEngine's JavaScript Channel API: 
* https://developers.google.com/appengine/docs/python/channel/javascript 
* 
* Requires a hosted HTML page that opens the desired channel and dispatches JS 
* on{Open,Message,Close,Error}() events to a global object named 
* "androidMessageHandler". 
*/ 
public class GAEChannelClient { 
    private static final String TAG = "GAEChannelClient"; 
    private WebView webView; 
    private final ProxyingMessageHandler proxyingMessageHandler; 

    /** 
    * Callback interface for messages delivered on the Google AppEngine 
    * channel. 
    * 
    * Methods are guaranteed to be invoked on the UI thread of |activity| 
    * passed to GAEChannelClient's constructor. 
    */ 
    public interface MessageHandler { 
     public void onOpen(); 

     public void onMessage(String data); 

     public void onClose(); 

     public void onError(int code, String description); 
    } 

    /** Asynchronously open an AppEngine channel. */ 
    @SuppressLint("SetJavaScriptEnabled") 
    public GAEChannelClient(Activity activity, String token, MessageHandler handler) { 
     webView = new WebView(activity); 

     webView.getSettings().setJavaScriptEnabled(true); 
     webView.getSettings().setAllowFileAccessFromFileURLs(true); // Maybe you 
                    // don't 
                    // need this 
                    // rule 
     webView.getSettings().setAllowUniversalAccessFromFileURLs(true); 

     webView.setWebChromeClient(new WebChromeClient() { // Purely for 
                  // debugging. 
      public boolean onConsoleMessage(ConsoleMessage msg) { 
       Log.d(TAG, "console: " + msg.message() + " at " + msg.sourceId() + ":" + msg.lineNumber()); 
       return false; 
      } 
     }); 
     webView.setWebViewClient(new WebViewClient() { // Purely for debugging. 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Log.e(TAG, "JS error: " + errorCode + " in " + failingUrl + ", desc: " + description); 
      } 

      @Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
       System.out.println("HI"); 
       return super.shouldOverrideUrlLoading(view, url); 
      } 
     }); 

     proxyingMessageHandler = new ProxyingMessageHandler(activity, handler, token); 
     webView.addJavascriptInterface(proxyingMessageHandler, "androidMessageHandler"); 
//  webView.loadUrl("file:///android_asset/channel.html"); 
     try { 
      InputStream is = activity.getAssets().open("channel.html"); 
      StringBuilder builder = new StringBuilder(); 
      byte[] buffer = new byte[1024]; 
      while (is.read(buffer) != -1) { 
       builder.append(new String(buffer)); 
      } 
      is.close(); 
      String str = builder.toString(); 
      webView.loadDataWithBaseURL("http://192.168.5.86:8080", str, "text/html", "utf-8", null); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

    /** Close the connection to the AppEngine channel. */ 
    public void close() { 
     if (webView == null) { 
      return; 
     } 
     proxyingMessageHandler.disconnect(); 
     webView.removeJavascriptInterface("androidMessageHandler"); 
     webView.loadUrl("about:blank"); 
     webView = null; 
    } 

    // Helper class for proxying callbacks from the Java<->JS interaction 
    // (private, background) thread to the Activity's UI thread. 
    private static class ProxyingMessageHandler { 
     private final Activity activity; 
     private final MessageHandler handler; 
     private final boolean[] disconnected = { false }; 
     private final String token; 

     public ProxyingMessageHandler(Activity activity, MessageHandler handler, String token) { 
      this.activity = activity; 
      this.handler = handler; 
      this.token = token; 
     } 

     public void disconnect() { 
      disconnected[0] = true; 
     } 

     private boolean disconnected() { 
      return disconnected[0]; 
     } 

     @JavascriptInterface 
     public String getToken() { 
      return token; 
     } 

     @JavascriptInterface 
     public void onOpen() { 

      System.out.println("GAEClient : Open"); 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        if (!disconnected()) { 
         handler.onOpen(); 
        } 
       } 
      }); 
     } 

     @JavascriptInterface 
     public void onMessage(final String data) { 
      System.out.println("GAEClient : Message : " +data); 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        if (!disconnected()) { 
         handler.onMessage(data); 
        } 
       } 
      }); 
     } 

     @JavascriptInterface 
     public void onClose() { 
      System.out.println("GAEClient : Close"); 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        if (!disconnected()) { 
         handler.onClose(); 
        } 
       } 
      }); 
     } 

     @JavascriptInterface 
     public void onError(final int code, final String description) { 
      System.out.println("GAEClient : Erroe : " + description); 
      activity.runOnUiThread(new Runnable() { 
       public void run() { 
        if (!disconnected()) { 
         handler.onError(code, description); 
        } 
       } 
      }); 
     } 
    } 
} 

Channel.html資產的文件夾

<html> 
    <head> 
    <script src="http://192.168.5.86:8080/_ah/channel/jsapi"></script> 
    </head> 
    <!-- 
    Helper HTML that redirects Google AppEngine's Channel API to a JS object named 
    |androidMessageHandler|, which is expected to be injected into the WebView 
    rendering this page by an Android app's class such as AppRTCClient. 
    --> 
    <body onbeforeunload="closeSocket()" onload="openSocket()"> 
    <script type="text/javascript"> 
     var token = androidMessageHandler.getToken(); 
     if (!token) 
     throw "Missing/malformed token parameter: [" + token + "]"; 

     var channel = null; 
     var socket = null; 

     function openSocket() { 
     channel = new goog.appengine.Channel(token); 
     socket = channel.open({ 
      'onopen': function() { androidMessageHandler.onOpen(); }, 
      'onmessage': function(msg) { androidMessageHandler.onMessage(msg.data); }, 
      'onclose': function() { androidMessageHandler.onClose(); }, 
      'onerror': function(err) { androidMessageHandler.onError(err.code, err.description); } 
     }); 
     } 

     function closeSocket() { 
     socket.close(); 
     } 
    </script> 
    </body> 
</html> 
+0

您會在android和web通信期間提供日誌嗎? –

+0

@Biraj Zalavadia:像你花了1個月,我已經失去了3個月仍然無法工作,你提到你的修改爲你工作,但我確實喜歡你顯示哪些從來沒有爲我工作。你能否也請加入我的鏈接:http://stackoverflow.com/questions/23949237/webrtc-apprtcdemo-with-local-server-does-not-work-with-android-native-to-pc-br – YumYumYum

+0

@胡皓問道:「你是否在本地服務器創建了你的本地服務器?它是否與[http://apprtc.appspot.com]一樣?」 – drs

回答

6

可悲的是,我不知道,如果你做了這些事情:

  1. 使用相同的眩暈並打開服務器上的每個應用程序(個人電腦或手機)。
  2. 你是否甚至在應用程序之間發送ICE候選人(我認爲你是這樣做的,但只是爲了驗證)。
  3. 你確定STUN/TURN網址是錯誤的,因爲我不能相信這些事情是關於跨越原點的(他們不應該這樣做,因爲你只是從客戶端連接到服務器,交叉原點主要是在網頁上使用,從外部來源加載數據,你不能從XHR那裏做到這一點)。我真的認爲它與https://apprtc.appspot.com/_ah/channel/jsapi有關,因爲這是一個很好的跨越原點的例子。

如果您打開在移動設備上的chrome瀏覽器中工作的網頁,該怎麼辦?那它做了什麼? (請注意,您可以將您的手機連接到您的電腦上,以獲得Chrome的完整開發人員工具。Chrome可在您的Android設備上運行,但您可以在PC上看到Devtools)。

如果你能爲我提供這些答案,我可能會幫助你。嘗試恢復所有這些更改,並只使用谷歌的TURN服務器,但只能使https://apprtc.appspot.com/_ah/channel/jsapi文件在本地。

編輯:I see you found your answer。你願意分享嗎?

+0

我在html文件中添加了javascript,而不是通過網絡加載。但仍然沒有工作。你能否請幫助,沒有人分享誰使它工作的細節。 – YumYumYum

+1

@YumYumYum什麼不工作?我看到OP已經找到答案,但我不知道是什麼。 – MarijnS95

+0

@ MarjinS95:實際上,OP在這裏和其他網址上報告的內容完全相同,並且花費了1個多月的時間,而且它不像OP所說的那樣工作。我的問題是在AppRTCDemo中,當我執行連接本地設置時,它在加入房間後沒有做任何事情。如果你想我應該打開一個新的問題與此相關,並請你鏈接?我認爲你是唯一瞭解這個問題的人,其他人大多都很困惑。 – YumYumYum

相關問題