視訊App如何使用無線傳輸服務獲得更好的播放體驗
技術標籤:android
1 前言
華為公司經過多年的發展,在網路連線,固網,有線,無線等部分積累了深厚的經驗。無線傳輸服務就是將這些經驗轉化為能力體現在手機側,更好的展現華為手機的優勢。
網路最重要的就是穩定,快速,低時延。視訊App重要的也是保證使用者更好的觀看體驗,對網路的要求也就更高。
本文將以視訊播放App為例,介紹基本的應用場景和實現方式,以幫助使用者快速瞭解如何使用無線傳輸服務的相關功能。
2 名詞介紹
在下面的介紹中可能會用到一些網路側常用的引數,含義如下:
QoE:Quality of Experience,使用者體驗質量。使用者對業務使用的最終感受。在這裡主要指體現網路情況的抖動,時延等指標。
WiFi切片:對裝置上不同的服務設定不同優先順序,並按優先順序進行相關資料分組。
3 業務背景介紹
現在各大運營商都開通了針對各種視訊軟體(如騰訊視訊,優酷視訊,抖音等)的大流量套餐,因此很多人都會在上班,旅遊的過程中通過它們觀看視訊。而觀看視訊過程中最擔心的就是網路不穩定造成的卡頓,解析度降低等情況,嚴重影響觀賞體驗。如果手機能夠最大限度保障視訊的流暢度,並且及時選擇更優的網路,就能夠讓使用者有更好的觀賞體驗。如果能提前知道網路的變化,豈不是能更好的應對可能的問題嘛。
4 關鍵流程
4.1 整合無線傳輸服務
整合無線傳輸服務的基本操作可以參考如下文件進行操作:
https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides-V5/dev-process-0000001051069902-V5?ha_source=hms1
4.2 如何保障視訊質量
視訊軟體的訴求是什麼樣的?
首先,網路速度越快越好,速度越快,視訊質量越高。
網路要保持穩定,保證持續的觀影效果。
當網路狀態有波動時,可以通過快取功能對網路進行平滑。
當網路不允許時,降低解析度優先保障連續性。
下面我們看一下,無線傳輸服務如何從各個角度提升視訊軟體的效果。
4.2.1 網路優選
首先確認一點,無線傳輸服務並不能對您的網路進行加速。但是它可以在網路狀態不穩定,不足以支撐應用要求時,將網路切換到可以滿足要求的網路中(如果存在)。
比如使用者當前使用WIFI網路,當遠離WIFI區域時,網路質量逐漸下降。但是如果使用者WIFI沒有中斷,系統會繼續使用該網路,導致視訊播放出現異常。
如果可以分析當前網路狀態,並且及時切換到蜂窩網路,則可以更好的進行觀影。
無線傳輸服務提供了基於QoE的網路優選功能。通過主動反饋的方式,向App提供網路狀態的回撥。App可以通過當前的QoE狀態進行相應操作(切換網路,增加快取等)。
- 註冊服務
public class NetworkPredictActivity extends AppCompatActivity {
private NetworkQoeClient networkQoeClient;
private IQoeService qoeService;
private ServiceConnection mSrcConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
qoeService = IQoeService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
qoeService = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
networkQoeClient = WirelessClient.getNetworkQoeClient(NetworkPredictActivity.this);
if (networkQoeClient != null) {
networkQoeClient.getNetworkQoeServiceIntent()
.addOnSuccessListener(new OnSuccessListener<WirelessResult>() {
@Override
public void onSuccess(WirelessResult wirelessResult) {
Intent intent = wirelessResult.getIntent();
if (intent == null) {
return;
}
NetworkPredictActivity.this.bindService(intent, mSrcConn, Context.BIND_AUTO_CREATE);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof ApiException) {
ApiException ex = (ApiException) e;
int errCode = ex.getStatusCode();
}
}
});
}
}
}
- 註冊回撥
註冊回撥後,系統會通過callback函式返回網路資訊。反饋頻率為每秒一次。
public class NetworkQoeActivity extends AppCompatActivity {
private int[] channelIndex = new int[4];
private int[] uLRtt = new int[4];
private int[] dLRtt = new int[4];
private int[] uLBandwidth = new int[4];
private int[] dLBandwidth = new int[4];
private int[] uLRate = new int[4];
private int[] dLRate = new int[4];
private int[] netQoeLevel = new int[4];
private int[] uLPkgLossRate = new int[4];
private IQoeService qoeService;
private IQoeCallBack callBack = new IQoeCallBack.Stub() {
@Override
public void callBack(int type, Bundle qoeInfo) throws RemoteException {
if (qoeInfo == null || type != 0) {
return;
}
int channelNum = 0;
if (qoeInfo.containsKey("channelNum")) {
channelNum = qoeInfo.getInt("channelNum");
}
String channelQoe = String.valueOf(channelNum);
for (int i = 0; i < channelNum; i++) {
uLRtt[i] = qoeInfo.getInt("uLRtt" + i);
dLRtt[i] = qoeInfo.getInt("dLRtt" + i);
uLBandwidth[i] = qoeInfo.getInt("uLBandwidth" + i);
dLBandwidth[i] = qoeInfo.getInt("dLBandwidth" + i);
uLRate[i] = qoeInfo.getInt("uLRate" + i);
dLRate[i] = qoeInfo.getInt("dLRate" + i);
netQoeLevel[i] = qoeInfo.getInt("netQoeLevel" + i);
uLPkgLossRate[i] = qoeInfo.getInt("uLPkgLossRate" + i);
channelIndex[i] = qoeInfo.getInt("channelIndex" + i);
channelQoe += "," + channelIndex[i] + "," + uLRtt[i] + "," + dLRtt[i] + "," + uLBandwidth[i] + ","
+ dLBandwidth[i] + "," + uLRate[i] + "," + dLRate[i] + "," + netQoeLevel[i] + ","
+ uLPkgLossRate[i];
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
if (qoeService != null) {
try {
int ret = qoeService.registerNetQoeCallBack("com.huawei.hms.wirelessdemo",callBack);
} catch (RemoteException ex) {
}
}
}
}
- 主動獲取狀態
App可以主動獲取網路狀態(需要首先註冊回撥)。
Bundle qoeInfo = qoeService.queryRealTimeQoe("com.huawei.hms.wirelessdemo");
if (qoeInfo == null) {
return;
}
- 上報網路狀態
當App發現網路狀態異常時,可以主動採取降低解析度的方式,保障視訊順利播放。也可以向無線傳輸服務上報網路狀態,它將分析當前網路狀態,切換到更優的網路中。
data.putInt("eventId", 1);
data.putString("netReason", "2");
data.putString("pkgName", "com.huawei.hms.wirelessdemo");
data.putInt("direction", 1);
data.putString("version", "5.0.1.300");
long currentTime = System.currentTimeMillis();
String timeNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
data.putString("errorTime", timeNow);
if (qoeService != null) {
try {
int ret = qoeService.reportAppQuality(data);
} catch (RemoteException ex) {
}
}
4.2.2 網路狀態預測
對於視訊App來說,一個很大的應用場景就是上下班。在這種情況下,使用者的路徑相同,而網路的變化趨勢通常也是很類似。針對這種場景,無線傳輸服務提供了基於學習的預測能力,根據使用者的位置變化,主動預知網路變化趨勢,以便App提前做出應對(比如提升視訊快取時長)。
- 註冊弱訊號接收廣播
無線傳輸服務提供基於廣播的弱訊號預測。
final IntentFilter filter = new IntentFilter();
filter.addAction(NETWORK_PREDICTION_ACTION);
registerReceiver(receiver, filter);
- 接收弱訊號廣播
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int enteringTime = intent.getIntExtra("enteringTime", 0);
int leavingTime = intent.getIntExtra("leavingTime", 0);
int type =intent.getIntExtra("type",0);
}
};
當Type為1時,在enteringTime後將進入弱訊號區間,在進入弱訊號區間後,將在leavingTime後離開弱訊號區間。
Type為0時表示取消前一次預測。
訊號預測的時間範圍為30s到60s。
4.2.3 高優先順序WIFI發包
當在頻寬有限的WIFI環境下,有多個不同App爭搶頻寬時,有更高優先順序的App將可以更好的保障使用效果。
無線傳輸服務提供了Wifi增強功能,可以保障高優先順序上行發包(如果使用華為路由器,可實現下行高優先順序)。
- 繫結服務
WifiEnhanceClient wifiEnhanceClient = WirelessClient.getWifiEnhanceClient(WifiEnhanceActivity.this);
wifiEnhanceClient.getWifiEnhanceServiceIntent()
.addOnSuccessListener(new OnSuccessListener<WirelessResult>() {
@Override
public void onSuccess(WirelessResult wirelessResult) {
Intent intent = wirelessResult.getIntent();
if (intent == null) {
return;
}
WifiEnhanceActivity.this.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception exception) {
if (exception instanceof ApiException) {
ApiException ex = (ApiException) exception;
int errCode = ex.getStatusCode();
}
}
});
- 設定Wi-Fi高優先順序發包
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mWifiEnhanceService != null) {
try {
mWifiEnhanceService.setHighPriority("com.huawei.hms.wirlesstestdemo", 6, 1);
} catch (RemoteException exception) {
// 這裡可以新增列印
}
}
}
對視訊應用來說下行頻寬更加重要,因此該功能在華為WIFI的情況下更加有效。
綜合以上三種場景,可以讓視訊應用在千變萬化的網路情況下,給使用者最好的視訊使用感受。
5 其他
無線傳輸服務中的功能使用到了很多硬體能力,因此部分功能需要有硬體的支援。
欲瞭解更多詳情,請參閱:
華為開發者聯盟官網:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides-V5/introduction-0000001050749815-V5#ZH-CN_TOPIC_0000001050749815__section10966112614216?ha_source=hms1
參與開發者討論請到Reddit社群:https://www.reddit.com/r/HuaweiDevelopers/
下載demo和示例程式碼請到Github:https://github.com/HMS-Core
解決整合問題請到Stack Overflow:https://stackoverflow.com/questions/tagged/huawei-mobile-services?tab=Newest
原文連結:https://developer.huawei.com/consumer/cn/forum/topic/0202442899177200495?fid=18
原作者:胡椒