diff --git a/frameworks/native/capi/player/native_avplayer.cpp b/frameworks/native/capi/player/native_avplayer.cpp index bb7f45519481662a254d666a9456f0593d0ce721..4ade9ba7c91191c0ec8ed9787fe7518625631b6e 100644 --- a/frameworks/native/capi/player/native_avplayer.cpp +++ b/frameworks/native/capi/player/native_avplayer.cpp @@ -269,6 +269,43 @@ private: Player_MediaKeySystemInfoCallback drmsysteminfocallback_ = nullptr; }; +class NativeAVDataSource : public OHOS::Media::IMediaDataSource { +public: + NativeAVDataSource(OH_AVDataSourceExt* dataSourceExt, void* userData) + : dataSourceExt_(dataSourceExt), userData_(userData) {} + + virtual ~NativeAVDataSource() = default; + + int32_t ReadAt(const std::shared_ptr &mem, uint32_t length, int64_t pos = -1) + { + std::shared_ptr buffer = AVBuffer::CreateAVBuffer( + mem->GetBase(), mem->GetSize(), mem->GetSize() + ); + OH_AVBuffer avBuffer(buffer); + return dataSourceExt_->readAt(&avBuffer, length, pos, userData_); + } + + int32_t GetSize(int64_t &size) + { + size = dataSourceExt_->size; + return 0; + } + + int32_t ReadAt(int64_t pos, uint32_t length, const std::shared_ptr &mem) + { + return ReadAt(mem, length, pos); + } + + int32_t ReadAt(uint32_t length, const std::shared_ptr &mem) + { + return ReadAt(mem, length); + } + +private: + OH_AVDataSourceExt* dataSourceExt_ = nullptr; + void* userData_ = nullptr; +}; + void PlayerObject::StartListenCurrentResource() { if (callback_ != nullptr) { @@ -1425,4 +1462,18 @@ OH_AVErrCode OH_AVPlayer_SetLoudnessGain(OH_AVPlayer *player, float loudnessGain int32_t ret = playerObj->player_->SetLoudnessGain(loudnessGain); CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_STATE, "player SetLoudnessGain failed"); return AV_ERR_OK; +} + +OH_AVErrCode OH_AVPlayer_SetDataSource(OH_AVPlayer *player, OH_AVDataSourceExt* datasrc, void* userData) +{ + CHECK_AND_RETURN_RET_LOG(player != nullptr, AV_ERR_INVALID_VAL, "input player is nullptr!"); + struct PlayerObject *playerObj = reinterpret_cast(player); + CHECK_AND_RETURN_RET_LOG(playerObj->player_ != nullptr, AV_ERR_INVALID_VAL, "player_ is null"); + CHECK_AND_RETURN_RET_LOG(datasrc != nullptr, AV_ERR_INVALID_VAL, "datasrc is null"); + CHECK_AND_RETURN_RET_LOG(datasrc->readAt != nullptr, AV_ERR_INVALID_VAL, "datasrc readAt is null"); + playerObj->StartListenCurrentResource(); + std::shared_ptr avDataSource = std::make_shared(datasrc, userData); + int32_t ret = playerObj->player_->SetSource(avDataSource); + CHECK_AND_RETURN_RET_LOG(ret == MSERR_OK, AV_ERR_INVALID_VAL, "player setDataSource failed"); + return AV_ERR_OK; } \ No newline at end of file diff --git a/interfaces/kits/c/avplayer.h b/interfaces/kits/c/avplayer.h index f4f5413781ad5b00e20e6d3b5691b12668033848..0f3ec8947bda8c318854dfcae58db311aacfa8fb 100644 --- a/interfaces/kits/c/avplayer.h +++ b/interfaces/kits/c/avplayer.h @@ -44,6 +44,7 @@ #include "native_averrors.h" #include "avplayer_base.h" #include "native_audiostream_base.h" +#include "native_avcodec_base.h" #ifdef __cplusplus @@ -588,6 +589,18 @@ OH_AVErrCode OH_AVPlayer_SetOnErrorCallback(OH_AVPlayer *player, OH_AVPlayerOnEr */ OH_AVErrCode OH_AVPlayer_SetLoudnessGain(OH_AVPlayer *player, float loudnessGain); +/** + * @brief Set the media source of the player. The data of this media source is provided by the application. + * @param {OH_AVPlayer*} player Pointer to an OH_AVPlayer instance + * @param {OH_AVDataSourceExt*} datasrc Pointer to an OH_AVDataSourceExt instance + * @param {void*} userData The handle passed in by the user is used to pass in the callback + * @return Function result code. + * {@link AV_ERR_OK} if the execution is successful. + * {@link AV_ERR_INVALID_VAL} if input player is nullptr or player setDataSource failed. + * @since 21 + */ +OH_AVErrCode OH_AVPlayer_SetDataSource(OH_AVPlayer *player, OH_AVDataSourceExt* datasrc, void* userData); + #ifdef __cplusplus } #endif