1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PLAYER_H 17 #define PLAYER_H 18 19 #include <cstdint> 20 #ifdef SUPPORT_AUDIO_ONLY 21 #else 22 #include "surface.h" 23 #endif 24 #include "format.h" 25 #include "media_data_source.h" 26 27 namespace OHOS { 28 namespace Media { 29 class PlayerKeys { 30 public: 31 static constexpr std::string_view PLAYER_STATE_CHANGED_REASON = "state_changed_reason"; 32 static constexpr std::string_view PLAYER_VOLUME_LEVEL = "volume_level"; 33 static constexpr std::string_view PLAYER_TRACK_INDEX = "track_index"; 34 static constexpr std::string_view PLAYER_TRACK_TYPE = "track_type"; 35 static constexpr std::string_view PLAYER_WIDTH = "width"; 36 static constexpr std::string_view PLAYER_HEIGHT = "height"; 37 static constexpr std::string_view PLAYER_MIME = "codec_mime"; 38 static constexpr std::string_view PLAYER_BITRATE = "bitrate"; 39 static constexpr std::string_view PLAYER_FRAMERATE = "frame_rate"; 40 static constexpr std::string_view PLAYER_LANGUGAE = "language_code"; 41 static constexpr std::string_view PLAYER_SAMPLE_RATE = "sample_rate"; 42 static constexpr std::string_view PLAYER_CHANNELS = "channel_count"; 43 static constexpr std::string_view PLAYER_BUFFERING_START = "buffering_start"; 44 static constexpr std::string_view PLAYER_BUFFERING_END = "buffering_end"; 45 static constexpr std::string_view PLAYER_BUFFERING_PERCENT = "buffering_percent"; 46 static constexpr std::string_view PLAYER_CACHED_DURATION = "cached_duration"; 47 static constexpr std::string_view CONTENT_TYPE = "content_type"; 48 static constexpr std::string_view STREAM_USAGE = "stream_usage"; 49 static constexpr std::string_view RENDERER_FLAG = "renderer_flag"; 50 static constexpr std::string_view VIDEO_SCALE_TYPE = "video_scale_type"; 51 static constexpr std::string_view AUDIO_INTERRUPT_MODE = "audio_interrupt_mode"; 52 static constexpr std::string_view AUDIO_INTERRUPT_TYPE = "audio_interrupt_type"; 53 static constexpr std::string_view AUDIO_INTERRUPT_FORCE = "audio_interrupt_force"; 54 static constexpr std::string_view AUDIO_INTERRUPT_HINT = "audio_interrupt_hint"; 55 }; 56 57 enum BufferingInfoType : int32_t { 58 /* begin to b buffering */ 59 BUFFERING_START = 1, 60 /* end to buffering */ 61 BUFFERING_END = 2, 62 /* buffering percent */ 63 BUFFERING_PERCENT = 3, 64 /* cached duration in milliseconds */ 65 CACHED_DURATION = 4, 66 }; 67 68 enum PlayerErrorType : int32_t { 69 /* Valid error, error code reference defined in media_errors.h */ 70 PLAYER_ERROR, 71 /* Unknown error */ 72 PLAYER_ERROR_UNKNOWN, 73 /* extend error type start,The extension error type agreed upon by the plug-in and 74 the application will be transparently transmitted by the service. */ 75 PLAYER_ERROR_EXTEND_START = 0X10000, 76 }; 77 78 enum PlayerMessageType : int32_t { 79 /* unknown info */ 80 PLAYER_INFO_UNKNOWN = 0, 81 /* first video frame start to render. */ 82 PLAYER_INFO_VIDEO_RENDERING_START, 83 /* network bandwidth, uint is KB and passed by "extra"(arg 2). */ 84 PLAYER_INFO_NETWORK_BANDWIDTH, 85 /* not fatal errors accured, errorcode see "media_errors.h" and passed by "extra"(arg 2). */ 86 PLAYER_INFO_WARNING, 87 /* system new info type should be added here. 88 extend start. App and plugins or PlayerEngine extended info type start. */ 89 PLAYER_INFO_EXTEND_START = 0X1000, 90 }; 91 92 enum PlayerOnInfoType : int32_t { 93 /* return the message when seeking done. */ 94 INFO_TYPE_SEEKDONE = 1, 95 /* return the message when speeding done. */ 96 INFO_TYPE_SPEEDDONE, 97 /* return the message when select bitrate done */ 98 INFO_TYPE_BITRATEDONE, 99 /* return the message when playback is end of steam. */ 100 INFO_TYPE_EOS, 101 /* return the message when PlayerStates changed. */ 102 INFO_TYPE_STATE_CHANGE, 103 /* return the current posion of playback automatically. */ 104 INFO_TYPE_POSITION_UPDATE, 105 /* return the playback message. */ 106 INFO_TYPE_MESSAGE, 107 /* return the message when volume changed. */ 108 INFO_TYPE_VOLUME_CHANGE, 109 /* return the message when video size is first known or updated. */ 110 INFO_TYPE_RESOLUTION_CHANGE, 111 /* return multiqueue buffering time. */ 112 INFO_TYPE_BUFFERING_UPDATE, 113 /* return hls bitrate. 114 Bitrate is to convert data into uint8_t array storage, 115 which needs to be forcibly converted to uint32_t through offset access. */ 116 INFO_TYPE_BITRATE_COLLECT, 117 /* return the message when audio focus changed. */ 118 INFO_TYPE_INTERRUPT_EVENT, 119 /* return the message when PlayerStates changed by audio. */ 120 INFO_TYPE_STATE_CHANGE_BY_AUDIO, 121 /* return the message with extra information in format. */ 122 INFO_TYPE_EXTRA_FORMAT, 123 /* return the duration of playback. */ 124 INFO_TYPE_DURATION_UPDATE, 125 }; 126 127 enum PlayerStates : int32_t { 128 /* error states */ 129 PLAYER_STATE_ERROR = 0, 130 /* idle states */ 131 PLAYER_IDLE = 1, 132 /* initialized states(Internal states) */ 133 PLAYER_INITIALIZED = 2, 134 /* preparing states(Internal states) */ 135 PLAYER_PREPARING = 3, 136 /* prepared states */ 137 PLAYER_PREPARED = 4, 138 /* started states */ 139 PLAYER_STARTED = 5, 140 /* paused states */ 141 PLAYER_PAUSED = 6, 142 /* stopped states */ 143 PLAYER_STOPPED = 7, 144 /* Play to the end states */ 145 PLAYER_PLAYBACK_COMPLETE = 8, 146 /* released states */ 147 PLAYER_RELEASED = 9, 148 }; 149 150 enum PlayerSeekMode : int32_t { 151 /* sync to keyframes after the time point. */ 152 SEEK_NEXT_SYNC = 0, 153 /* sync to keyframes before the time point. */ 154 SEEK_PREVIOUS_SYNC, 155 /* sync to closest keyframes. */ 156 SEEK_CLOSEST_SYNC, 157 /* seek to frames closest the time point. */ 158 SEEK_CLOSEST, 159 }; 160 161 enum PlaybackRateMode : int32_t { 162 /* Video playback at 0.75x normal speed */ 163 SPEED_FORWARD_0_75_X, 164 /* Video playback at normal speed */ 165 SPEED_FORWARD_1_00_X, 166 /* Video playback at 1.25x normal speed */ 167 SPEED_FORWARD_1_25_X, 168 /* Video playback at 1.75x normal speed */ 169 SPEED_FORWARD_1_75_X, 170 /* Video playback at 2.0x normal speed */ 171 SPEED_FORWARD_2_00_X, 172 }; 173 174 enum VideoScaleType : int32_t { 175 /** 176 * The content is stretched to the fit the display surface rendering area. When 177 * the aspect ratio of the content is not same as the display surface, the aspect 178 * of the content is not maintained. This is the default scale type. 179 */ 180 VIDEO_SCALE_TYPE_FIT = 0, 181 182 /** 183 * The content is stretched to the fit the display surface rendering area. When 184 * the aspect ratio of the content is not the same as the display surface, content's 185 * aspect ratio is maintained and the content is cropped to fit the display surface. 186 */ 187 VIDEO_SCALE_TYPE_FIT_CROP, 188 }; 189 190 class PlayerCallback { 191 public: 192 virtual ~PlayerCallback() = default; 193 /** 194 * Called when an error occurred for versions older than api9 195 * 196 * @param errorType Error type. For details, see {@link PlayerErrorType}. 197 * @param errorCode Error code. 198 */ OnError(PlayerErrorType errorType,int32_t errorCode)199 __attribute__((deprecated)) virtual void OnError(PlayerErrorType errorType, int32_t errorCode) 200 { 201 (void)errorType; 202 (void)errorCode; 203 } 204 205 /** 206 * Called when a player message or alarm is received. 207 * 208 * @param type Indicates the information type. For details, see {@link PlayerOnInfoType}. 209 * @param extra Indicates other information, for example, the start time position of a playing file. 210 * @param infoBody According to the info type, the information carrier passed.Is an optional parameter. 211 */ 212 virtual void OnInfo(PlayerOnInfoType type, int32_t extra, const Format &infoBody) = 0; 213 214 /** 215 * Called when an error occurred for versions above api9 216 * 217 * @param errorCode Error code. 218 * @param errorMsg Error message. 219 */ OnError(int32_t errorCode,const std::string & errorMsg)220 virtual void OnError(int32_t errorCode, const std::string &errorMsg) 221 { 222 (void)errorCode; 223 (void)errorMsg; 224 } 225 }; 226 227 class Player { 228 public: 229 virtual ~Player() = default; 230 231 /** 232 * @brief Sets the playback source for the player. The corresponding source can be http url 233 * 234 * @param url Indicates the playback source. 235 * @return Returns {@link MSERR_OK} if the url is set successfully; returns an error code defined 236 * in {@link media_errors.h} otherwise. 237 * @since 1.0 238 * @version 1.0 239 */ 240 virtual int32_t SetSource(const std::string &url) = 0; 241 242 /** 243 * @brief Sets the playback media data source for the player. 244 * 245 * @param dataSrc Indicates the media data source. in {@link media_data_source.h} 246 * @return Returns {@link MSERR_OK} if the mediadatasource is set successfully; returns an error code defined 247 * in {@link media_errors.h} otherwise. 248 * @since 1.0 249 * @version 1.0 250 */ 251 virtual int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) = 0; 252 253 /** 254 * @brief Sets the playback media file descriptor source for the player. 255 * 256 * @param fd Indicates the file descriptor of media source. 257 * @param offset Indicates the offset of media source in file descriptor. 258 * @param size Indicates the size of media source. 259 * @return Returns {@link MSERR_OK} if the fd source is set successfully; returns an error code defined 260 * in {@link media_errors.h} otherwise. 261 * @since 1.0 262 * @version 1.0 263 */ 264 virtual int32_t SetSource(int32_t fd, int64_t offset = 0, int64_t size = 0) = 0; 265 266 /** 267 * @brief Start playback. 268 * 269 * This function must be called after {@link Prepare}. If the player state is <b>Prepared</b>, 270 * this function is called to start playback. 271 * 272 * @return Returns {@link MSERR_OK} if the playback is started; otherwise returns an error code defined 273 * in {@link media_errors.h} otherwise. 274 * @since 1.0 275 * @version 1.0 276 */ 277 virtual int32_t Play() = 0; 278 279 /** 280 * @brief Prepares the playback environment and buffers media data asynchronous. 281 * 282 * This function must be called after {@link SetSource}. 283 * 284 * @return Returns {@link MSERR_OK} if the playback is prepared; returns an error code defined 285 * in {@link media_errors.h} otherwise. 286 * @since 1.0 287 * @version 1.0 288 */ 289 __attribute__((deprecated)) virtual int32_t Prepare() = 0; 290 291 /** 292 * @brief Prepare the playback environment and buffers media data asynchronous. 293 * 294 * This function must be called after {@link SetSource}. 295 * 296 * @return Returns {@link MSERR_OK} if the playback is preparing; returns an error code defined 297 * in {@link media_errors.h} otherwise. 298 * @since 1.0 299 * @version 1.0 300 */ 301 virtual int32_t PrepareAsync() = 0; 302 303 /** 304 * @brief Pauses playback. 305 * 306 * @return Returns {@link MSERR_OK} if the playback is paused; returns an error code defined 307 * in {@link media_errors.h} otherwise. 308 * @since 1.0 309 * @version 1.0 310 */ 311 virtual int32_t Pause() = 0; 312 313 /** 314 * @brief Stop playback. 315 * 316 * @return Returns {@link MSERR_OK} if the playback is stopped; returns an error code defined 317 * in {@link media_errors.h} otherwise. 318 * @since 1.0 319 * @version 1.0 320 */ 321 virtual int32_t Stop() = 0; 322 323 /** 324 * @brief Restores the player to the initial state. 325 * 326 * After the function is called, add a playback source by calling {@link SetSource}, 327 * call {@link Play} to start playback again after {@link Prepare} is called. 328 * 329 * @return Returns {@link MSERR_OK} if the playback is reset; returns an error code defined 330 * in {@link media_errors.h} otherwise. 331 * @since 1.0 332 * @version 1.0 333 */ 334 virtual int32_t Reset() = 0; 335 336 /** 337 * @brief Releases player resources async 338 * 339 * Asynchronous release guarantees the performance 340 * but cannot ensure whether the surfacebuffer is released. 341 * The caller needs to ensure the life cycle security of the sufrace 342 * 343 * @return Returns {@link MSERR_OK} if the playback is released; returns an error code defined 344 * in {@link media_errors.h} otherwise. 345 * @since 1.0 346 * @version 1.0 347 */ 348 virtual int32_t Release() = 0; 349 350 /** 351 * @brief Sets the volume of the player. 352 * 353 * This function can be used during playback or pause. The value <b>0</b> indicates no sound, 354 * and <b>1</b> indicates the original volume. If no audio device is started or no audio 355 * stream exists, the value <b>-1</b> is returned. 356 * 357 * @param leftVolume Indicates the target volume of the left audio channel to set, 358 * ranging from 0 to 1. each step is 0.01. 359 * @param rightVolume Indicates the target volume of the right audio channel to set, 360 * ranging from 0 to 1. each step is 0.01. 361 * @return Returns {@link MSERR_OK} if the volume is set; returns an error code defined 362 * in {@link media_errors.h} otherwise. 363 * @since 1.0 364 * @version 1.0 365 */ 366 virtual int32_t SetVolume(float leftVolume, float rightVolume) = 0; 367 368 /** 369 * @brief Changes the playback position. 370 * 371 * This function can be used during play or pause. 372 * 373 * @param mSeconds Indicates the target playback position, accurate to milliseconds. 374 * @param mode Indicates the player seek mode. For details, see {@link PlayerSeekMode}. 375 * @return Returns {@link MSERR_OK} if the seek is done; returns an error code defined 376 * in {@link media_errors.h} otherwise. 377 * @since 1.0 378 * @version 1.0 379 */ 380 virtual int32_t Seek(int32_t mSeconds, PlayerSeekMode mode) = 0; 381 382 /** 383 * @brief Obtains the playback position, accurate to millisecond. 384 * 385 * @param currentTime Indicates the playback position. 386 * @return Returns {@link MSERR_OK} if the current position is get; returns an error code defined 387 * in {@link media_errors.h} otherwise. 388 * @since 1.0 389 * @version 1.0 390 */ 391 virtual int32_t GetCurrentTime(int32_t ¤tTime) = 0; 392 393 /** 394 * @brief Obtains the video track info, contains mimeType, bitRate, width, height, frameRata. 395 * 396 * @param video track info vec. 397 * @return Returns {@link MSERR_OK} if the track info is get; returns an error code defined 398 * in {@link media_errors.h} otherwise. 399 * @since 1.0 400 * @version 1.0 401 */ 402 virtual int32_t GetVideoTrackInfo(std::vector<Format> &videoTrack) = 0; 403 404 /** 405 * @brief Obtains the audio track info, contains mimeType, bitRate, sampleRate, channels, language. 406 * 407 * @param audio track info vec. 408 * @return Returns {@link MSERR_OK} if the track info is get; returns an error code defined 409 * in {@link media_errors.h} otherwise. 410 * @since 1.0 411 * @version 1.0 412 */ 413 virtual int32_t GetAudioTrackInfo(std::vector<Format> &audioTrack) = 0; 414 415 /** 416 * @brief get the video width. 417 * 418 * @return Returns width if success; else returns 0 419 * @since 1.0 420 * @version 1.0 421 */ 422 virtual int32_t GetVideoWidth() = 0; 423 424 /** 425 * @brief get the video height. 426 * 427 * @return Returns height if success; else returns 0 428 * @since 1.0 429 * @version 1.0 430 */ 431 virtual int32_t GetVideoHeight() = 0; 432 433 /** 434 * @brief Obtains the total duration of media files, accurate to milliseconds. 435 * 436 * @param duration Indicates the total duration of media files. 437 * @return Returns {@link MSERR_OK} if the current duration is get; returns an error code defined 438 * in {@link media_errors.h} otherwise. 439 * @since 1.0 440 * @version 1.0 441 */ 442 virtual int32_t GetDuration(int32_t &duration) = 0; 443 444 /** 445 * @brief set the player playback rate 446 * 447 * @param mode the rate mode {@link PlaybackRateMode} which can set. 448 * @return Returns {@link MSERR_OK} if the playback rate is set successful; returns an error code defined 449 * in {@link media_errors.h} otherwise. 450 * @since 1.0 451 * @version 1.0 452 */ 453 virtual int32_t SetPlaybackSpeed(PlaybackRateMode mode) = 0; 454 455 /** 456 * @brief get the current player playback rate 457 * 458 * @param mode the rate mode {@link PlaybackRateMode} which can get. 459 * @return Returns {@link MSERR_OK} if the current player playback rate is get; returns an error code defined 460 * in {@link media_errors.h} otherwise. 461 * @since 1.0 462 * @version 1.0 463 */ 464 virtual int32_t GetPlaybackSpeed(PlaybackRateMode &mode) = 0; 465 466 /** 467 * @brief set the bit rate use for hls player 468 * the playback bitrate expressed in bits per second, expressed in bits per second, 469 * which is only valid for HLS protocol network flow. By default, 470 * the player will select the appropriate bit rate and speed according to the network connection. 471 * report the effective bit rate linked list by "INFO_TYPE_BITRATE_COLLECT" 472 * set and select the specified bit rate, and select the bit rate that is less than and closest 473 * to the specified bit rate for playback. When ready, read it to query the currently selected bit rate. 474 * @param bitRate the bit rate, The unit is bps. 475 * @return Returns {@link MSERR_OK} if the bit rate is set successfully; returns an error code defined 476 * in {@link media_errors.h} otherwise. 477 * @since 1.0 478 * @version 1.0 479 */ 480 virtual int32_t SelectBitRate(uint32_t bitRate) = 0; 481 482 #ifdef SUPPORT_AUDIO_ONLY 483 #else 484 /** 485 * @brief Method to set the surface. 486 * 487 * @param surface pointer of the surface. 488 * @return Returns {@link MSERR_OK} if the surface is set; returns an error code defined 489 * in {@link media_errors.h} otherwise. 490 * @since 1.0 491 * @version 1.0 492 */ 493 virtual int32_t SetVideoSurface(sptr<Surface> surface) = 0; 494 #endif 495 496 /** 497 * @brief Checks whether the player is playing. 498 * 499 * @return Returns true if the playback is playing; false otherwise. 500 * @since 1.0 501 * @version 1.0 502 */ 503 virtual bool IsPlaying() = 0; 504 505 /** 506 * @brief Returns the value whether single looping is enabled or not . 507 * 508 * @return Returns true if the playback is single looping; false otherwise. 509 * @since 1.0 510 * @version 1.0 511 */ 512 virtual bool IsLooping() = 0; 513 514 /** 515 * @brief Enables single looping of the media playback. 516 * 517 * @return Returns {@link MSERR_OK} if the single looping is set; returns an error code defined 518 * in {@link media_errors.h} otherwise. 519 * @since 1.0 520 * @version 1.0 521 */ 522 virtual int32_t SetLooping(bool loop) = 0; 523 524 /** 525 * @brief Method to set player callback. 526 * 527 * @param callback object pointer. 528 * @return Returns {@link MSERR_OK} if the playercallback is set; returns an error code defined 529 * in {@link media_errors.h} otherwise. 530 * @since 1.0 531 * @version 1.0 532 */ 533 virtual int32_t SetPlayerCallback(const std::shared_ptr<PlayerCallback> &callback) = 0; 534 535 /** 536 * @brief Sets an extended parameter for player 537 * 538 * @param format Indicates the string key and value. For details, see {@link Format} 539 * @return Returns {@link MSERR_OK} if the parameters are set; returns an error code defined 540 * in {@link media_errors.h} otherwise. 541 * @since 1.0 542 * @version 1.0 543 */ 544 virtual int32_t SetParameter(const Format ¶m) = 0; 545 546 /** 547 * @brief Releases player resources sync 548 * 549 * Synchronous release ensures effective release of surfacebuffer 550 * but this interface will take a long time (when the engine is not idle state) 551 * requiring the caller to design an asynchronous mechanism by itself 552 * 553 * @return Returns {@link MSERR_OK} if the playback is released; returns an error code defined 554 * in {@link media_errors.h} otherwise. 555 * @since 1.0 556 * @version 1.0 557 */ 558 virtual int32_t ReleaseSync() = 0; 559 }; 560 561 class __attribute__((visibility("default"))) PlayerFactory { 562 public: 563 #ifdef UNSUPPORT_PLAYER CreatePlayer()564 static std::shared_ptr<Player> CreatePlayer() 565 { 566 return nullptr; 567 } 568 #else 569 static std::shared_ptr<Player> CreatePlayer(); 570 #endif 571 private: 572 PlayerFactory() = default; 573 ~PlayerFactory() = default; 574 }; 575 } // namespace Media 576 } // namespace OHOS 577 #endif // PLAYER_H 578