Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【1300开源版本】video组件新增防盗链属性(rfc:#0004) #642

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ public interface Style {
String OBJECT_FIT = "objectFit";
String ALT_OBJECT_FIT = "altObjectFit";
String SRC = "src";
String MARK = "mark";
String SOURCE = "source";
String ALT = "alt";
String QUALITY = "quality";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/*
* Copyright (c) 2021, the hapjs-platform Project Contributors
* Copyright (c) 2021-present, the hapjs-platform Project Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.hapjs.widgets.video;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,6 +27,7 @@ public class DefaultPlayerManager implements PlayerManager {
private Context mContext;
private int mPlayerCountLimit = DEFAULT_PLAYER_COUNT_LIMIT;
private List<PlayerHolder> mInstances;
private static final String TAG = "DefaultPlayerManager";

private DefaultPlayerManager() {
mInstances = new ArrayList<>(DEFAULT_PLAYER_COUNT_LIMIT);
Expand Down Expand Up @@ -65,7 +69,7 @@ public <P extends IMediaPlayer> P obtainPlayer(PlayerProxy target) {
}

trimToSize(mPlayerCountLimit);
PlayerHolder holder = findAvaiableIdlePlayer();
PlayerHolder holder = findAvaiableIdlePlayer(target);
if (holder != null) {
// 有可用的空闲player
holder.mTarget.unbind();
Expand Down Expand Up @@ -98,11 +102,19 @@ private boolean hasLimit() {
return mPlayerCountLimit != PLAYER_COUNT_UNLIMITED;
}

private PlayerHolder findAvaiableIdlePlayer() {
private PlayerHolder findAvaiableIdlePlayer(PlayerProxy target) {
int size = mInstances.size();
for (int i = 0; i < size; i++) {
PlayerHolder holder = mInstances.get(i);
Player player = holder.mPlayer;
if (null != player && null != target) {
String mark = player.getMark();
String targetMark = target.getMark();
if (!TextUtils.isEmpty(targetMark) && !targetMark.equals(mark)) {
Log.w(TAG, "findAvaiableIdlePlayer targetMark mark not same.");
continue;
}
}
int targetState = player.getTargetState();
int currentState = player.getCurrentState();
if (targetState == currentState
Expand All @@ -117,7 +129,7 @@ private PlayerHolder findAvaiableIdlePlayer() {
}

private PlayerHolder createInternal(PlayerProxy target) {
Player player = new ExoPlayer(mContext);
Player player = new ExoPlayer(mContext, (null != target ? target.getMark() : null));
PlayerHolder holder = new PlayerHolder(player, target);
mInstances.add(holder);
return holder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.google.android.exoplayer2.util.Util;
import org.hapjs.component.constants.Attributes;

import java.util.HashMap;

public class ExoPlayer extends Player
implements SimpleExoPlayer.VideoListener,
com.google.android.exoplayer2.Player.EventListener {
Expand All @@ -56,14 +58,17 @@ public class ExoPlayer extends Player
private boolean isBuffering;
private SuspendLoadControl mSuspendLoadControl;

public ExoPlayer(@NonNull Context context) {
public ExoPlayer(@NonNull Context context, String mark) {
super(context);
mManifestDataSourceFactory =
new DefaultDataSourceFactory(context, Util.getUserAgent(context, "default"));
new DefaultDataSourceFactory(
context, Util.getUserAgent(context, TextUtils.isEmpty(mark) ? "default" : mark));
mMediaDataSourceFactory =
new DefaultDataSourceFactory(
context, Util.getUserAgent(context, "default"),
context,
Util.getUserAgent(context, TextUtils.isEmpty(mark) ? "default" : mark),
new DefaultBandwidthMeter());
mMark = mark;
}

@Override
Expand Down Expand Up @@ -406,7 +411,9 @@ public void onPlayerError(ExoPlaybackException e) {
if (e != null) {
what = e.type;
}
notifyError(what, -1);
HashMap<String, Object> datas = new HashMap<>();
datas.put(Attributes.Style.MARK, !TextUtils.isEmpty(mMark) ? mMark : "");
notifyError(what, -1, datas);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.IntDef;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;

public interface IMediaPlayer {
Expand Down Expand Up @@ -62,6 +63,10 @@ public interface IMediaPlayer {

void setMuted(boolean muted);

void setMark(String mark);

String getMark();

long getCurrentPosition();

long getDuration();
Expand Down Expand Up @@ -121,7 +126,7 @@ public interface IMediaPlayer {
interface EventListener {
void onLoadingChanged(IMediaPlayer player, boolean isLoading);

boolean onError(IMediaPlayer player, @MediaErrorType int what, int extra);
boolean onError(IMediaPlayer player, @MediaErrorType int what, int extra, HashMap<String, Object> datas);

void onPlayerStateChanged(IMediaPlayer player, @PlayerState int playbackState);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import android.view.TextureView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.hapjs.common.executors.Executors;
Expand Down Expand Up @@ -46,6 +48,7 @@ public abstract class Player implements IMediaPlayer {
private boolean mMuted;
private boolean mAutoPlay;
private boolean mSuspendBuffer = false;
protected String mMark;

protected Player(@NonNull Context context) {
mApplicationContext = context.getApplicationContext();
Expand Down Expand Up @@ -246,6 +249,16 @@ public boolean isSuspendBuffer() {
return mSuspendBuffer;
}

public String getMark() {
return mMark;
}

//no use now
@Override
public void setMark(String mark) {
mMark = mark;
}

@Override
public void setSuspendBuffer(boolean suspendBuffer) {
if (mSuspendBuffer == suspendBuffer) {
Expand Down Expand Up @@ -409,9 +422,9 @@ private void notifyStateChanged(@PlayerState int state) {
}
}

protected void notifyError(int what, int extra) {
protected void notifyError(int what, int extra, HashMap<String, Object> datas) {
if (mEventListener != null) {
mEventListener.onError(this, what, extra);
mEventListener.onError(this, what, extra, datas);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PlayerProxy<P extends IMediaPlayer> implements IMediaPlayer {
private float mSpeed = 1.0f;
public long mCachedPosition = -1;
private boolean mSuspendBuffer = false;
private String mMark;

@Override
public void prepare() {
Expand Down Expand Up @@ -154,6 +155,14 @@ public void setMuted(boolean muted) {
}
}

public String getMark() {
return mMark;
}

public void setMark(String mark) {
this.mMark = mark;
}

@Override
public long getCurrentPosition() {
if (mPlayer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import org.hapjs.runtime.HapEngine;
import org.hapjs.widgets.view.video.FlexVideoView;

import static org.hapjs.component.constants.Attributes.Style.MARK;

@WidgetAnnotation(
name = Video.WIDGET_NAME,
methods = {
Expand Down Expand Up @@ -216,6 +218,10 @@ protected boolean setAttribute(String key, Object attribute) {
String uri = Attributes.getString(attribute);
setVideoURI(uri);
return true;
case MARK:
String mark = Attributes.getString(attribute);
setVideoMark(mark);
return true;
case Attributes.Style.AUTO_PLAY:
boolean autoPlay = Attributes.getBoolean(attribute, false);
setAutoPlay(autoPlay);
Expand Down Expand Up @@ -299,9 +305,12 @@ protected boolean addEvent(String event) {
mHost.setOnErrorListener(
new FlexVideoView.OnErrorListener() {
@Override
public boolean onError(int what, int extra) {
public boolean onError(int what, int extra, HashMap<String, Object> datas) {
Log.w(TAG, "Error, what:" + what + " extra:" + extra);
Map<String, Object> params = new HashMap();
if (null != datas) {
params.putAll(datas);
}
params.put("what", what);
params.put("extra", extra);
mCallback
Expand Down Expand Up @@ -476,6 +485,13 @@ public void setVideoURI(String uri) {
NetworkReportManager.getInstance().reportNetwork(NetworkReportManager.KEY_VIDEO, uri.toString());
}

public void setVideoMark(String mark) {
if (mHost == null) {
return;
}
mHost.setMark(mark);
}

public void setAutoPlay(boolean autoPlay) {
mAutoPlay = autoPlay;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand All @@ -40,6 +41,7 @@
import com.facebook.yoga.YogaNode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -97,6 +99,7 @@ public class FlexVideoView extends FrameLayout
private Uri mPosterUri;
private Boolean mMuted;
private String mPlayCount;
private String mMark;
private float mSpeed = Video.SPEED_DEFAULT;
private OnErrorListener mOnErrorListener;
private OnIdleListener mOnIdleListener;
Expand Down Expand Up @@ -176,6 +179,20 @@ private void initBorderRadiusParams() {
}
}

public String getMark() {
return mMark;
}

public void setMark(String mark) {
this.mMark = mark;
if (null != mPlayer) {
String curMark = mPlayer.getMark();
if (!TextUtils.isEmpty(mMark) && !mMark.equals(curMark)) {
releasePlayer();
}
}
}

@Override
protected void onDraw(Canvas canvas) {
if (isRoundedBorders()) {
Expand Down Expand Up @@ -534,13 +551,13 @@ public void onPlayerStateChanged(IMediaPlayer player, int playbackState) {
}
}

protected boolean onError(int what, int extra) {
protected boolean onError(int what, int extra, HashMap<String, Object> datas) {
if (mOnTimeUpdateListener != null) {
mTimeUpdateHandler.removeMessages(MSG_TIME_UPDATE);
}

if (mOnErrorListener != null) {
mOnErrorListener.onError(what, extra);
mOnErrorListener.onError(what, extra, datas);
}

switchKeepScreenOnFlagsByState(Player.STATE_ERROR);
Expand All @@ -557,8 +574,8 @@ protected boolean onError(int what, int extra) {
}

@Override
public boolean onError(IMediaPlayer player, int what, int extra) {
return onError(what, extra);
public boolean onError(IMediaPlayer player, int what, int extra, HashMap<String, Object> datas) {
return onError(what, extra, datas);
}

@Override
Expand Down Expand Up @@ -1317,6 +1334,7 @@ public void initPlayer() {
mPlayer.setEventListener(this);
mControlsManager.attachPlayer(mPlayer);
}
mPlayer.setMark(mMark);
}

public void setSpeed(float speed) {
Expand All @@ -1327,7 +1345,7 @@ public void setSpeed(float speed) {
}

public interface OnErrorListener {
boolean onError(int what, int extra);
boolean onError(int what, int extra, HashMap<String, Object> datas);
}

public interface OnIdleListener {
Expand Down
Loading