集成了IMSdroid 视频通话后,发现双人视频显示为横屏,很纳闷,仔细查看代码,发现做了横竖屏切换的重力感应监听,就是可以发送任意的角度视频。如下:
if (mIsVideoCall) {
mListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orient) {
try {
if ((orient > 345 || orient < 15)
|| (orient > 75 && orient < 105)
|| (orient > 165 && orient < 195)
|| (orient > 255 && orient < 285)) {
int rotation = mAVSession.compensCamRotation(true);
if (rotation != mLastRotation) {
Log.d(ScreenAV.TAG,
"Received Screen Orientation Change setRotation["
+ String.valueOf(rotation)
+ "]");
applyCamRotation(rotation);
if (mSendDeviceInfo && mAVSession != null) {
final android.content.res.Configuration conf = getResources()
.getConfiguration();
if (conf.orientation != mLastOrientation) {
mLastOrientation = conf.orientation;
switch (mLastOrientation) {
case android.content.res.Configuration.ORIENTATION_LANDSCAPE:
mAVSession
.sendInfo(
"orientation:landscape\r\nlang:fr-FR\r\n",
NgnContentType.DOUBANGO_DEVICE_INFO);
break;
case android.content.res.Configuration.ORIENTATION_PORTRAIT:
mAVSession
.sendInfo(
"orientation:portrait\r\nlang:fr-FR\r\n",
NgnContentType.DOUBANGO_DEVICE_INFO);
break;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
if (!mListener.canDetectOrientation()) {
Log.w(TAG, "canDetectOrientation() is equal to false");
}
}
通过分析,双方接收到的视频显示为横屏,应该是对方发送过来的就为横屏,但是我手机设置成竖屏显示了,不可能会进入到横竖屏切换的监听器内部,通过查看视频预览产生类org.doubango.ngn.media.NgnProxyVideoProducer,观察到里面有个方法:
public int compensCamRotation(boolean preview){
if(mVideoProducer != null){
return mVideoProducer.compensCamRotation(preview);
}
return 0;
}
public int camRotation(boolean preview){
if(mVideoProducer != null){
return mVideoProducer.getNativeCameraHardRotation(preview);
}
return 0;
}
显而易见,应该就是这里处理没错!再深入代码查看:
public int compensCamRotation(boolean preview){
final int cameraHardRotation = getNativeCameraHardRotation(preview);
final android.content.res.Configuration conf = NgnApplication.getContext().getResources().getConfiguration();
if(conf.orientation == android.content.res.Configuration.ORIENTATION_LANDSCAPE){
return 0;
}
if (NgnApplication.getSDKVersion() >= 9) {
if (preview) {
return cameraHardRotation;
}
else{
switch (cameraHardRotation) {
case 0:
case 180:
default:
return 0;
case 90:
case 270:
return 90;
}
}
}
else {
int terminalRotation = getTerminalRotation();
int rotation = 0;
rotation = (terminalRotation-cameraHardRotation) % 360;
return rotation;
}
}
所以,我这边在进入Incall状态后,通过调用:
applyCamRotation(mAVSession.compensCamRotation(true));// 保持视频竖屏(其实用平板的话是会自动横屏显示的)