Home | History | Annotate | Download | only in mediaplayer2
      1 /*
      2  * Copyright 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef ANDROID_MEDIAPLAYER2_H
     18 #define ANDROID_MEDIAPLAYER2_H
     19 
     20 #include <media/AVSyncSettings.h>
     21 #include <media/AudioResamplerPublic.h>
     22 #include <media/BufferingSettings.h>
     23 #include <media/mediaplayer_common.h>
     24 #include <mediaplayer2/MediaPlayer2Interface.h>
     25 #include <mediaplayer2/MediaPlayer2Types.h>
     26 #include <mediaplayer2/JObjectHolder.h>
     27 
     28 #include <jni.h>
     29 #include <utils/Errors.h>
     30 #include <utils/Mutex.h>
     31 #include <utils/RefBase.h>
     32 #include <utils/String16.h>
     33 #include <utils/Vector.h>
     34 #include <system/audio-base.h>
     35 
     36 #include "jni.h"
     37 
     38 namespace android {
     39 
     40 struct ANativeWindowWrapper;
     41 struct DataSourceDesc;
     42 class MediaPlayer2AudioOutput;
     43 
     44 // ref-counted object for callbacks
     45 class MediaPlayer2Listener: virtual public RefBase
     46 {
     47 public:
     48     virtual void notify(int64_t srcId, int msg, int ext1, int ext2,
     49             const PlayerMessage *obj = NULL) = 0;
     50 };
     51 
     52 class MediaPlayer2 : public MediaPlayer2InterfaceListener
     53 {
     54 public:
     55     ~MediaPlayer2();
     56 
     57     static sp<MediaPlayer2> Create(int32_t sessionId, jobject context);
     58     static status_t DumpAll(int fd, const Vector<String16>& args);
     59 
     60             void            disconnect();
     61 
     62             status_t        getSrcId(int64_t *srcId);
     63             status_t        setDataSource(const sp<DataSourceDesc> &dsd);
     64             status_t        prepareNextDataSource(const sp<DataSourceDesc> &dsd);
     65             status_t        playNextDataSource(int64_t srcId);
     66             status_t        setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww);
     67             status_t        setListener(const sp<MediaPlayer2Listener>& listener);
     68             status_t        getBufferingSettings(BufferingSettings* buffering /* nonnull */);
     69             status_t        setBufferingSettings(const BufferingSettings& buffering);
     70             status_t        prepareAsync();
     71             status_t        start();
     72             status_t        pause();
     73             bool            isPlaying();
     74             mediaplayer2_states getState();
     75             status_t        setPlaybackSettings(const AudioPlaybackRate& rate);
     76             status_t        getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
     77             status_t        setSyncSettings(const AVSyncSettings& sync, float videoFpsHint);
     78             status_t        getSyncSettings(
     79                                     AVSyncSettings* sync /* nonnull */,
     80                                     float* videoFps /* nonnull */);
     81             status_t        getVideoWidth(int *w);
     82             status_t        getVideoHeight(int *h);
     83             status_t        seekTo(
     84                     int64_t msec,
     85                     MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
     86             status_t        notifyAt(int64_t mediaTimeUs);
     87             status_t        getCurrentPosition(int64_t *msec);
     88             status_t        getDuration(int64_t srcId, int64_t *msec);
     89             status_t        reset();
     90             status_t        setAudioStreamType(audio_stream_type_t type);
     91             status_t        getAudioStreamType(audio_stream_type_t *type);
     92             status_t        setLooping(int loop);
     93             bool            isLooping();
     94             status_t        setVolume(float volume);
     95             void            notify(int64_t srcId, int msg, int ext1, int ext2,
     96                                    const PlayerMessage *obj = NULL);
     97             status_t        invoke(const PlayerMessage &request, PlayerMessage *reply);
     98             status_t        setAudioSessionId(int32_t sessionId);
     99             int32_t         getAudioSessionId();
    100             status_t        setAuxEffectSendLevel(float level);
    101             status_t        attachAuxEffect(int effectId);
    102             status_t        setAudioAttributes(const jobject attributes);
    103             jobject         getAudioAttributes();
    104             status_t        getParameter(int key, Parcel* reply);
    105             status_t        getMetrics(char **buffer, size_t *length);
    106 
    107             // Modular DRM
    108             status_t        prepareDrm(int64_t srcId,
    109                                        const uint8_t uuid[16],
    110                                        const Vector<uint8_t>& drmSessionId);
    111             status_t        releaseDrm(int64_t srcId);
    112             // AudioRouting
    113             status_t        setPreferredDevice(jobject device);
    114             jobject         getRoutedDevice();
    115             status_t        addAudioDeviceCallback(jobject routingDelegate);
    116             status_t        removeAudioDeviceCallback(jobject listener);
    117 
    118             status_t        dump(int fd, const Vector<String16>& args);
    119 
    120 private:
    121     MediaPlayer2(int32_t sessionId, jobject context);
    122     bool init();
    123 
    124     // Disconnect from the currently connected ANativeWindow.
    125     void disconnectNativeWindow_l();
    126 
    127     status_t setAudioAttributes_l(const jobject attributes);
    128 
    129     void clear_l();
    130     status_t seekTo_l(int64_t msec, MediaPlayer2SeekMode mode);
    131     status_t prepareAsync_l();
    132     status_t getDuration_l(int64_t *msec);
    133     status_t reset_l();
    134     status_t checkState_l();
    135 
    136     pid_t                       mPid;
    137     uid_t                       mUid;
    138     sp<MediaPlayer2Interface>   mPlayer;
    139     sp<MediaPlayer2AudioOutput> mAudioOutput;
    140     int64_t                     mSrcId;
    141     thread_id_t                 mLockThreadId;
    142     mutable Mutex               mLock;
    143     Mutex                       mNotifyLock;
    144     sp<MediaPlayer2Listener>    mListener;
    145     media_player2_internal_states mCurrentState;
    146     bool                        mTransitionToNext;
    147     int64_t                     mCurrentPosition;
    148     MediaPlayer2SeekMode        mCurrentSeekMode;
    149     int64_t                     mSeekPosition;
    150     MediaPlayer2SeekMode        mSeekMode;
    151     audio_stream_type_t         mStreamType;
    152     bool                        mLoop;
    153     float                       mVolume;
    154     int                         mVideoWidth;
    155     int                         mVideoHeight;
    156     int32_t                     mAudioSessionId;
    157     sp<JObjectHolder>           mAudioAttributes;
    158     sp<JObjectHolder>           mContext;
    159     float                       mSendLevel;
    160     sp<ANativeWindowWrapper>    mConnectedWindow;
    161 };
    162 
    163 }; // namespace android
    164 
    165 #endif // ANDROID_MEDIAPLAYER2_H
    166