Skip to content

Commit

Permalink
Merge pull request #85 from ksvc/v3.0.2-29799
Browse files Browse the repository at this point in the history
update to v3.0.2-29799
  • Loading branch information
xingkai509 authored Feb 8, 2018
2 parents 1e5cfbc + 51e54db commit 82dc0bb
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public class AnimatedImageCapture {
private Bitmap mTempBitmap;
private Timer mTimer;
private CloseableReference<? extends CloseableImage> mCloseableReference;
private final Object mLock = new Object();

private ImgTexSrcPin mImgTexSrcPin;

public AnimatedImageCapture(GLRender glRender) {
mImgTexSrcPin = new ImgTexSrcPin(glRender);
mImgTexSrcPin.setUseSyncMode(true);
}

public SrcPin<ImgTexFrame> getSrcPin() {
Expand Down Expand Up @@ -80,13 +82,15 @@ private String assets2Asset(String url) {
}

public void stop() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
if (mCloseableReference != null) {
CloseableReference.closeSafely(mCloseableReference);
mCloseableReference = null;
synchronized (mLock) {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
if (mCloseableReference != null) {
CloseableReference.closeSafely(mCloseableReference);
mCloseableReference = null;
}
}
mImgTexSrcPin.updateFrame(null, false);
}
Expand All @@ -97,78 +101,81 @@ public void release() {
}

private void updateFrame() {
if (mCloseableReference == null) {
return;
}
try {
CloseableImage closeableImage = mCloseableReference.get();
if (closeableImage instanceof CloseableBitmap) {
Bitmap bitmap = ((CloseableBitmap) closeableImage).getUnderlyingBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
mBitmap = Bitmap.createBitmap(bitmap);
mImgTexSrcPin.updateFrame(mBitmap, false);
}
synchronized (mLock) {
if (mCloseableReference == null) {
return;
}
try {
CloseableImage closeableImage = mCloseableReference.get();
if (closeableImage instanceof CloseableBitmap) {
Bitmap bitmap = ((CloseableBitmap) closeableImage).getUnderlyingBitmap();
if (bitmap != null && !bitmap.isRecycled()) {
mBitmap = Bitmap.createBitmap(bitmap);
mImgTexSrcPin.updateFrame(mBitmap, false);
}
return;
}

if (!(closeableImage instanceof CloseableAnimatedImage)) {
return;
}
AnimatedImage animatedImage = ((CloseableAnimatedImage) closeableImage).getImage();
int w = animatedImage.getWidth();
int h = animatedImage.getHeight();
if (mBitmap == null || mBitmap.isRecycled()) {
Log.d(TAG, "Got animated image " + w + "x" + h);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mTempBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
if (mIndex >= animatedImage.getFrameCount()) {
mRepeatCount++;
mIndex = 0;
int loopCount = animatedImage.getLoopCount();
if (loopCount > 0 && mRepeatCount >= loopCount) {
Log.d(TAG, "repeat ended, repeat times: " + mRepeatCount);
if (!(closeableImage instanceof CloseableAnimatedImage)) {
return;
}
}
AnimatedImage animatedImage = ((CloseableAnimatedImage) closeableImage).getImage();
int w = animatedImage.getWidth();
int h = animatedImage.getHeight();
if (mBitmap == null || mBitmap.isRecycled()) {
Log.d(TAG, "Got animated image " + w + "x" + h);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mTempBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
if (mIndex >= animatedImage.getFrameCount()) {
mRepeatCount++;
mIndex = 0;
int loopCount = animatedImage.getLoopCount();
if (loopCount > 0 && mRepeatCount >= loopCount) {
Log.d(TAG, "repeat ended, repeat times: " + mRepeatCount);
return;
}
}

AnimatedImageFrame imageFrame = animatedImage.getFrame(mIndex);
int duration = imageFrame.getDurationMs();
AnimatedDrawableFrameInfo frameInfo = animatedImage.getFrameInfo(mIndex);
imageFrame.renderFrame(frameInfo.width, frameInfo.height, mTempBitmap);
if (VERBOSE) {
Log.d(TAG, "blend: " + frameInfo.blendOperation.name() +
" dispose: " + frameInfo.disposalMethod.name() +
" x=" + frameInfo.xOffset + " y=" + frameInfo.yOffset +
" " + frameInfo.width + "x" + frameInfo.height);
}
if (frameInfo.blendOperation == AnimatedDrawableFrameInfo.BlendOperation.NO_BLEND) {
mBitmap.eraseColor(Color.TRANSPARENT);
}
Rect srcRect = new Rect(0, 0, frameInfo.width, frameInfo.height);
Rect dstRect = new Rect(frameInfo.xOffset, frameInfo.yOffset,
frameInfo.xOffset + frameInfo.width, frameInfo.yOffset + frameInfo.height);
mCanvas.drawBitmap(mTempBitmap, srcRect, dstRect, null);
if (VERBOSE) {
Log.d(TAG, "frame " + mIndex + " got, duration=" + duration);
}
mImgTexSrcPin.updateFrame(mBitmap, false);
imageFrame.dispose();
AnimatedImageFrame imageFrame = animatedImage.getFrame(mIndex);
int duration = imageFrame.getDurationMs();
AnimatedDrawableFrameInfo frameInfo = animatedImage.getFrameInfo(mIndex);
imageFrame.renderFrame(frameInfo.width, frameInfo.height, mTempBitmap);
if (VERBOSE) {
Log.d(TAG, "blend: " + frameInfo.blendOperation.name() +
" dispose: " + frameInfo.disposalMethod.name() +
" x=" + frameInfo.xOffset + " y=" + frameInfo.yOffset +
" " + frameInfo.width + "x" + frameInfo.height);
}
if (frameInfo.blendOperation == AnimatedDrawableFrameInfo.BlendOperation.NO_BLEND) {
mBitmap.eraseColor(Color.TRANSPARENT);
}
Rect srcRect = new Rect(0, 0, frameInfo.width, frameInfo.height);
Rect dstRect = new Rect(frameInfo.xOffset, frameInfo.yOffset,
frameInfo.xOffset + frameInfo.width,
frameInfo.yOffset + frameInfo.height);
mCanvas.drawBitmap(mTempBitmap, srcRect, dstRect, null);
if (VERBOSE) {
Log.d(TAG, "frame " + mIndex + " got, duration=" + duration);
}
mImgTexSrcPin.updateFrame(mBitmap, false);
imageFrame.dispose();

mIndex++;
if (mLastDate == null) {
mLastDate = new Date();
}
mLastDate = new Date(mLastDate.getTime() + duration);
mTimer.schedule(new TimerTask() {
@Override
public void run() {
updateFrame();
mIndex++;
if (mLastDate == null) {
mLastDate = new Date();
}
}, mLastDate);
} catch (Exception e) {
e.printStackTrace();
mLastDate = new Date(mLastDate.getTime() + duration);
mTimer.schedule(new TimerTask() {
@Override
public void run() {
updateFrame();
}
}, mLastDate);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Expand All @@ -182,25 +189,27 @@ public void onNewResultImpl(
return;
}

// get ref
mCloseableReference = dataSource.getResult();

// reset
mIndex = 0;
mRepeatCount = 0;
mLastDate = null;
mBitmap = null;
mTempBitmap = null;
mCanvas = null;

// create timer
mTimer = new Timer("AnimatedImage");
mTimer.schedule(new TimerTask() {
@Override
public void run() {
updateFrame();
}
}, 0);
synchronized (mLock) {
// get ref
mCloseableReference = dataSource.getResult();

// reset
mIndex = 0;
mRepeatCount = 0;
mLastDate = null;
mBitmap = null;
mTempBitmap = null;
mCanvas = null;

// create timer
mTimer = new Timer("AnimatedImage");
mTimer.schedule(new TimerTask() {
@Override
public void run() {
updateFrame();
}
}, 0);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
* Audio streaming demo fragment.
Expand All @@ -35,15 +36,23 @@ public class AudioDemoFragment extends DemoFragment {
@BindView(R.id.print_debug_info)
protected CheckBox mShowDebugInfoCheckBox;

protected Unbinder mUnbinder;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.audio_demo_fragment, container, false);
ButterKnife.bind(this, view);
mUnbinder = ButterKnife.bind(this, view);
return view;
}

@Override
public void onDestroyView() {
super.onDestroyView();
mUnbinder.unbind();
}

protected void loadParams(AudioStreamingActivity.AudioStreamConfig config, String url) {
// initial value
config.mAudioKBitrate = 48;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
* Fragment for audio settings.
Expand All @@ -36,6 +37,8 @@ public class AudioFuncFragment extends Fragment {
@BindView(R.id.audio_ld)
protected CheckBox mAudioLDCB;

protected Unbinder mUnbinder;

protected StdCameraActivity mActivity;
protected KSYStreamer mStreamer;
protected String mBgmPath;
Expand All @@ -50,13 +53,19 @@ public class AudioFuncFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.audio_func_fragment, container, false);
ButterKnife.bind(this, view);
mUnbinder = ButterKnife.bind(this, view);
mActivity = (StdCameraActivity) getActivity();
mStreamer = mActivity.mStreamer;
mBgmPath = mActivity.mSdcardPath + "/test.mp3";
return view;
}

@Override
public void onDestroyView() {
super.onDestroyView();
mUnbinder.unbind();
}

@Override
public void onPause() {
Log.d(TAG, "onPause");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ protected void onStreamerInfo(int what, int msg1, int msg2) {
break;
case StreamerConstants.KSY_STREAMER_FRAME_SEND_SLOW:
Log.d(TAG, "KSY_STREAMER_FRAME_SEND_SLOW " + msg1 + "ms");
Toast.makeText(AudioStreamingActivity.this, "Network not good!",
Toast.makeText(getApplicationContext(), "Network not good!",
Toast.LENGTH_SHORT).show();
break;
case StreamerConstants.KSY_STREAMER_OPEN_FILE_SUCCESS:
Expand All @@ -327,7 +327,7 @@ protected void onStreamerError(int what, int msg1, int msg2) {
switch (what) {
case StreamerConstants.KSY_STREAMER_AUDIO_RECORDER_ERROR_START_FAILED:
case StreamerConstants.KSY_STREAMER_AUDIO_RECORDER_ERROR_UNKNOWN:
Toast.makeText(AudioStreamingActivity.this,
Toast.makeText(getApplicationContext(),
"Audio capture failed!",
Toast.LENGTH_SHORT).show();
stopStream();
Expand Down Expand Up @@ -385,7 +385,7 @@ protected void startWithPermCheck(boolean startRecord) {
if (audioPerm != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
Log.e(TAG, "No AudioRecord permission, please check");
Toast.makeText(this, "No AudioRecord permission, please check",
Toast.makeText(getApplicationContext(), "No AudioRecord permission, please check",
Toast.LENGTH_LONG).show();
} else {
mDelayStartRecord = startRecord;
Expand Down Expand Up @@ -419,7 +419,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String permissi
}
} else {
Log.e(TAG, "No AudioRecord permission");
Toast.makeText(this, "No AudioRecord permission",
Toast.makeText(getApplicationContext(), "No AudioRecord permission",
Toast.LENGTH_LONG).show();
}
break;
Expand Down
Loading

0 comments on commit 82dc0bb

Please sign in to comment.