|
7#
楼主 |
发表于 2014-5-22 09:51:46
|
只看该作者
activity 代码如下- package net.nci.demo.activity;
- import java.util.Date;
- import java.util.Timer;
- import java.util.TimerTask;
- import net.nci.core.utils.LogUtil;
- import net.nci.demo.R;
- import net.nci.demo.service.ConfigEntity;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.Context;
- import android.content.SharedPreferences;
- import android.graphics.PixelFormat;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.Surface;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- import com.bairuitech.anychat.AnyChatBaseEvent;
- import com.bairuitech.anychat.AnyChatCoreSDK;
- import com.bairuitech.anychat.AnyChatDefine;
- public class LiveVideoActivity extends Activity implements AnyChatBaseEvent {
-
- private static final String TAG = LogUtil.makeLogTag(LiveVideoActivity.class);
- private SurfaceView videoView;
- ProgressBar volumeProgressBar;
- private ImageView mCameraSwitchImage; // 前后摄像头切换按钮
- public AnyChatCoreSDK anychat;
- int userID = -1;
- int otherVideoIndex = -1; // 视频显示序号(Java视频显示驱动需要)
- private ConfigEntity configEntity;
- private boolean bVideoOpened = false; // 视频是否已打开
- private boolean bSuccessEnterRoom = false; // 是否已成功进入房间
- private boolean bOnPaused = false;
- private Timer mTimer = new Timer(true);
- private TimerTask mTimerTask;
- private Handler handler = null;
- private Button quitBtn;
- private boolean bShowQuitBtn = true; // 是否已显示退出按钮
- private Date mLastShowQuitBtnTime = new Date(); // 开始显示退出按钮的时间
- @SuppressLint("HandlerLeak")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- InitialSDK();
- InitialLayout();
- mTimerTask = new TimerTask() {
- public void run() {
- if (handler == null)
- return;
- Message mesasge = new Message();
- handler.sendMessage(mesasge);
- }
- };
- mTimer.schedule(mTimerTask, 1000, 100);
- handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- CheckVideoStatus();
- // 刷新音量条
- if (LiveVideoActivity.this.bSuccessEnterRoom)
- LiveVideoActivity.this.volumeProgressBar
- .setProgress(LiveVideoActivity.this.anychat
- .GetUserSpeakVolume(userID));
- // 判断是否需要隐藏退出按钮
- Date now = new Date();
- long interval = now.getTime()
- - LiveVideoActivity.this.mLastShowQuitBtnTime.getTime();
- if (LiveVideoActivity.this.bShowQuitBtn && (interval > 2000)) {
- LiveVideoActivity.this.bShowQuitBtn = false;
- quitBtn.setVisibility(View.GONE);
- }
- super.handleMessage(msg);
- }
- };
- }
- // 判断视频打开状态,如果没有打开,则找一个适合的用户
- private void CheckVideoStatus() {
- if (bVideoOpened || !bSuccessEnterRoom || bOnPaused)
- return;
-
- // 打开本地的视频,进入房间成功已经进行了打开操作,这儿只需要设置显示
- if (anychat.GetCameraState(userID) == 2
- && anychat.GetUserVideoWidth(userID) != 0) {
- SurfaceHolder holder = videoView.getHolder();
- holder.setFormat(PixelFormat.RGB_565);
- holder.setFixedSize(anychat.GetUserVideoWidth(userID),
- anychat.GetUserVideoHeight(userID));
- Surface s = holder.getSurface();
- anychat.SetVideoPos(userID, s, 0, 0, 0, 0);
- bVideoOpened = true;
- }
- }
- private void InitialSDK() {
- anychat = new AnyChatCoreSDK();
- anychat.SetBaseEvent(this);
- //初始化sdk参数,移植于Hall
- applyVideoConfig();
- if (configEntity.useARMv6Lib != 0)
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEARMV6LIB, 1);
- anychat.InitSDK(android.os.Build.VERSION.SDK_INT, 0);
-
- this.anychat.Connect("192.168.0.2", 8906);
-
- // 启动AnyChat传感器监听
- anychat.mSensorHelper.InitSensor(this);
- }
- private void InitialLayout() {
- this.setContentView(R.layout.live_video_room);
- this.setTitle("本地视频上传");
- videoView = (SurfaceView) findViewById(R.id.surface_video);
- SurfaceHolder holder = videoView.getHolder();
- holder.setKeepScreenOn(true);
- videoView.setOnClickListener(listener);
- if (configEntity.videoOverlay == 1)
- holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- // 如果是采用Java视频采集,则需要设置Surface的CallBack
- if (AnyChatCoreSDK
- .GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA)
- Log.e(TAG, "addCallback");
-
- videoView.getHolder().addCallback(AnyChatCoreSDK.mCameraHelper);
-
- mCameraSwitchImage = (ImageView) findViewById(R.id.image_switch_camera);
- mCameraSwitchImage.setOnClickListener(listener);
- quitBtn = (Button) findViewById(R.id.btn_back);
- quitBtn.setOnClickListener(listener);
- volumeProgressBar = (ProgressBar) findViewById(R.id.progress_volume);
- // 判断是否显示本地摄像头切换图标
- if (AnyChatCoreSDK
- .GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
- if (AnyChatCoreSDK.mCameraHelper.GetCameraNumber() > 1)
- mCameraSwitchImage.setVisibility(View.VISIBLE);
- } else {
- String[] strVideoCaptures = anychat.EnumVideoCapture();
- if (strVideoCaptures != null && strVideoCaptures.length > 1)
- mCameraSwitchImage.setVisibility(View.VISIBLE);
- }
- }
- OnClickListener listener = new OnClickListener() {
- public void onClick(View v) {
- if (v == quitBtn) {
- mTimer.cancel();
- if (bVideoOpened) {
- anychat.UserCameraControl(userID, 0);
- anychat.UserSpeakControl(userID, 0);
- bVideoOpened = false;
- }
- finish();
- } else if (v == videoView) {
- bShowQuitBtn = true;
- mLastShowQuitBtnTime = new Date();
- quitBtn.setVisibility(View.VISIBLE);
- } else if (v == mCameraSwitchImage) // 前后摄像头切换事件
- {
- // 如果是采用Java视频采集,则在Java层进行摄像头切换
- if (AnyChatCoreSDK
- .GetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER) == AnyChatDefine.VIDEOCAP_DRIVER_JAVA) {
- AnyChatCoreSDK.mCameraHelper.SwitchCamera();
- return;
- }
- String strVideoCaptures[] = anychat.EnumVideoCapture();
- if (strVideoCaptures == null)
- return;
- String temp = anychat.GetCurVideoCapture();
- for (int i = 0; i < strVideoCaptures.length; i++) {
- if (!temp.equals(strVideoCaptures[i])) {
- anychat.UserCameraControl(-1, 0);
- bVideoOpened = false;
- anychat.SelectVideoCapture(strVideoCaptures[i]);
- anychat.UserCameraControl(-1, 1);
- break;
- }
- }
- }
- }
- };
- protected void onRestart() {
- super.onRestart();
- anychat.UserCameraControl(userID, 1);
- anychat.UserSpeakControl(userID, 1);
- bOnPaused = false;
- }
- protected void onResume() {
- super.onResume();
- }
- protected void onPause() {
- super.onPause();
- bOnPaused = true;
- if (bVideoOpened) {
- anychat.UserCameraControl(userID, 0);
- anychat.UserSpeakControl(userID, 0);
- bVideoOpened = false;
- }
- }
- protected void onDestroy() {
- mTimer.cancel();
- anychat.LeaveRoom(-1);
- anychat.mSensorHelper.DestroySensor();
- super.onDestroy();
- finish();
- }
- @Override
- public void OnAnyChatConnectMessage(boolean bSuccess) {
- Log.d(TAG, "Connect " + bSuccess);
- //成功后登录
- if (bSuccess) {
- this.anychat.Login("android", "");
- }
- }
- @Override
- public void OnAnyChatEnterRoomMessage(int dwRoomId, int dwErrorCode) {
- Log.e(TAG, "EnterRoom in "+ dwRoomId +"dwErrorCode"+ dwErrorCode);
- if (dwErrorCode == 0) {
- bSuccessEnterRoom = true;
- anychat.UserCameraControl(userID, 1);
- anychat.UserSpeakControl(userID, 1);
- }
- }
- @Override
- public void OnAnyChatLinkCloseMessage(int dwErrorCode) {
- // 网络连接断开之后,上层需要主动关闭已经打开的音视频设备
- if (bVideoOpened) {
-
- anychat.UserCameraControl(userID, 0);
- anychat.UserSpeakControl(userID, 0);
-
- bVideoOpened = false;
- }
- }
- @Override
- public void OnAnyChatLoginMessage(int dwUserId, int dwErrorCode) {
- Log.e(TAG, "Login with "+ dwUserId +"dwErrorCode"+ dwErrorCode);
- //默认房间4
- anychat.EnterRoom(4, "");
- }
- @Override
- public void OnAnyChatOnlineUserMessage(int dwUserNum, int dwRoomId) {
- }
- @Override
- public void OnAnyChatUserAtRoomMessage(int dwUserId, boolean bEnter) {
- }
-
- /**
- * 初始化参数
- */
- private void applyVideoConfig() {
- configEntity = LoadConfig(this);
- if(configEntity.configMode == 1) // 自定义视频参数配置
- {
- // 设置本地视频编码的码率(如果码率为0,则表示使用质量优先模式)
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_BITRATECTRL, configEntity.videoBitrate);
- if(configEntity.videoBitrate==0)
- {
- // 设置本地视频编码的质量
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_QUALITYCTRL, configEntity.videoQuality);
- }
- // 设置本地视频编码的帧率
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FPSCTRL, configEntity.videoFps);
- // 设置本地视频编码的关键帧间隔
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_GOPCTRL, configEntity.videoFps*4);
- // 设置本地视频采集分辨率
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_WIDTHCTRL, configEntity.resolution_width);
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_HEIGHTCTRL, configEntity.resolution_height);
- // 设置视频编码预设参数(值越大,编码质量越高,占用CPU资源也会越高)
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_PRESETCTRL, configEntity.videoPreset);
- }
- // 让视频参数生效
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_APPLYPARAM, configEntity.configMode);
- // P2P设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_NETWORK_P2PPOLITIC, configEntity.enableP2P);
- // 本地视频Overlay模式设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_OVERLAY, configEntity.videoOverlay);
- // 回音消除设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_ECHOCTRL, configEntity.enableAEC);
- // 平台硬件编码设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_CORESDK_USEHWCODEC, configEntity.useHWCodec);
- // 视频旋转模式设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_ROTATECTRL, configEntity.videorotatemode);
- // 视频采集驱动设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_CAPDRIVER, configEntity.videoCapDriver);
- // 本地视频采集偏色修正设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_FIXCOLORDEVIA, configEntity.fixcolordeviation);
- // 视频显示驱动设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_DRIVERCTRL, configEntity.videoShowDriver);
- // 音频播放驱动设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_PLAYDRVCTRL, configEntity.audioPlayDriver);
- // 音频采集驱动设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_AUDIO_RECORDDRVCTRL, configEntity.audioRecordDriver);
- // 视频GPU渲染设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_VIDEOSHOW_GPUDIRECTRENDER, configEntity.videoShowGPURender);
- // 本地视频自动旋转设置
- AnyChatCoreSDK.SetSDKOptionInt(AnyChatDefine.BRAC_SO_LOCALVIDEO_AUTOROTATION, configEntity.videoAutoRotation);
- }
-
-
- /**调用设置
- * @param context
- * @return
- */
- private ConfigEntity LoadConfig(Context context) {
- ConfigEntity configEntity = new ConfigEntity();
- SharedPreferences share = context.getSharedPreferences("perference", Context.MODE_WORLD_WRITEABLE);
- configEntity.ip = share.getString("ip", "192.168.0.2");
- configEntity.port = share.getInt("port", 8906);
-
- configEntity.configMode = share.getInt("configMode", ConfigEntity.VIDEO_MODE_CUSTOMCONFIG);
- configEntity.resolution_width = share.getInt("resolution_width", 320);
- configEntity.resolution_height = share.getInt("resolution_height", 240);
- configEntity.videoBitrate = share.getInt("videoBitrate", 150*1000);
- configEntity.videoFps = share.getInt("videoFps", 10);
- configEntity.videoQuality = share.getInt("videoQuality", ConfigEntity.VIDEO_QUALITY_GOOD);
- configEntity.videoPreset = share.getInt("videoPreset", 3);
- configEntity.videoOverlay = share.getInt("videoOverlay", 1);
- configEntity.videorotatemode = share.getInt("VideoRotateMode", 0);
- configEntity.videoCapDriver = share.getInt("VideoCapDriver", AnyChatDefine.VIDEOCAP_DRIVER_JAVA);
- configEntity.fixcolordeviation = share.getInt("FixColorDeviation", 0);
- configEntity.videoShowGPURender = share.getInt("videoShowGPURender", 0);
- configEntity.videoAutoRotation = share.getInt("videoAutoRotation", 1);
- configEntity.enableP2P = share.getInt("enableP2P", 1);
- configEntity.useARMv6Lib = share.getInt("useARMv6Lib", 0);
- configEntity.enableAEC = share.getInt("enableAEC", 1);
- configEntity.useHWCodec = share.getInt("useHWCodec", 0);
- configEntity.videoShowDriver = share.getInt("videoShowDriver", AnyChatDefine.VIDEOSHOW_DRIVER_JAVA);
- configEntity.audioPlayDriver = share.getInt("audioPlayDriver", AnyChatDefine.AUDIOPLAY_DRIVER_JAVA);
- configEntity.audioRecordDriver = share.getInt("audioRecordDriver", AnyChatDefine.AUDIOREC_DRIVER_JAVA);
- return configEntity;
- }
- }
复制代码 接收端显示如下
|
|