Home | History | Annotate | Download | only in nuplayer2
      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 #include <mediaplayer2/MediaPlayer2Interface.h>
     18 
     19 #include <media/MediaMetrics.h>
     20 #include <media/stagefright/foundation/ABase.h>
     21 #include <mediaplayer2/JObjectHolder.h>
     22 
     23 namespace android {
     24 
     25 struct ALooper;
     26 struct MediaClock;
     27 struct NuPlayer2;
     28 
     29 struct NuPlayer2Driver : public MediaPlayer2Interface {
     30     explicit NuPlayer2Driver(pid_t pid, uid_t uid, const sp<JObjectHolder> &context);
     31 
     32     virtual status_t initCheck() override;
     33 
     34     virtual status_t setDataSource(const sp<DataSourceDesc> &dsd) override;
     35     virtual status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd) override;
     36     virtual status_t playNextDataSource(int64_t srcId) override;
     37 
     38     virtual status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper> &nww) override;
     39 
     40     virtual status_t getBufferingSettings(
     41             BufferingSettings* buffering /* nonnull */) override;
     42     virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
     43 
     44     virtual status_t prepareAsync() override;
     45     virtual status_t start() override;
     46     virtual status_t pause() override;
     47     virtual bool isPlaying() override;
     48     virtual status_t setPlaybackSettings(const AudioPlaybackRate &rate) override;
     49     virtual status_t getPlaybackSettings(AudioPlaybackRate *rate) override;
     50     virtual status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint) override;
     51     virtual status_t getSyncSettings(AVSyncSettings *sync, float *videoFps) override;
     52     virtual status_t seekTo(
     53             int64_t msec,
     54             MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) override;
     55     virtual status_t getCurrentPosition(int64_t *msec) override;
     56     virtual status_t getDuration(int64_t *msec) override;
     57     virtual status_t reset() override;
     58     virtual status_t notifyAt(int64_t mediaTimeUs) override;
     59     virtual status_t setLooping(int loop) override;
     60     virtual status_t invoke(const PlayerMessage &request, PlayerMessage *response) override;
     61     virtual void setAudioSink(const sp<AudioSink> &audioSink) override;
     62     virtual status_t setParameter(int key, const Parcel &request) override;
     63     virtual status_t getParameter(int key, Parcel *reply) override;
     64     virtual status_t getMetrics(char **buf, size_t *length) override;
     65 
     66     virtual status_t dump(int fd, const Vector<String16> &args) const override;
     67 
     68     virtual void onMessageReceived(const sp<AMessage> &msg) override;
     69 
     70     void notifySetDataSourceCompleted(int64_t srcId, status_t err);
     71     void notifyPrepareCompleted(int64_t srcId, status_t err);
     72     void notifyResetComplete(int64_t srcId);
     73     void notifySetSurfaceComplete(int64_t srcId);
     74     void notifyDuration(int64_t srcId, int64_t durationUs);
     75     void notifyMorePlayingTimeUs(int64_t srcId, int64_t timeUs);
     76     void notifyMoreRebufferingTimeUs(int64_t srcId, int64_t timeUs);
     77     void notifyRebufferingWhenExit(int64_t srcId, bool status);
     78     void notifySeekComplete(int64_t srcId);
     79     void notifyListener(int64_t srcId, int msg, int ext1 = 0, int ext2 = 0,
     80                         const PlayerMessage *in = NULL);
     81     void notifyFlagsChanged(int64_t srcId, uint32_t flags);
     82 
     83     // Modular DRM
     84     virtual status_t prepareDrm(
     85             int64_t srcId, const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
     86     virtual status_t releaseDrm(int64_t srcId);
     87 
     88 protected:
     89     virtual ~NuPlayer2Driver();
     90 
     91 private:
     92     enum State {
     93         STATE_IDLE,
     94         STATE_SET_DATASOURCE_PENDING,
     95         STATE_UNPREPARED,
     96         STATE_PREPARING,
     97         STATE_PREPARED,
     98         STATE_RUNNING,
     99         STATE_PAUSED,
    100         STATE_RESET_IN_PROGRESS,
    101     };
    102 
    103     std::string stateString(State state);
    104 
    105     enum {
    106         kWhatNotifyListener,
    107     };
    108 
    109     mutable Mutex mLock;
    110     Condition mCondition;
    111 
    112     State mState;
    113 
    114     status_t mAsyncResult;
    115 
    116     // The following are protected through "mLock"
    117     // >>>
    118     int64_t mSrcId;
    119     bool mSetSurfaceInProgress;
    120     int64_t mDurationUs;
    121     int64_t mPositionUs;
    122     bool mSeekInProgress;
    123     int64_t mPlayingTimeUs;
    124     int64_t mRebufferingTimeUs;
    125     int32_t mRebufferingEvents;
    126     bool mRebufferingAtExit;
    127     // <<<
    128 
    129     sp<ALooper> mLooper;
    130     sp<ALooper> mNuPlayer2Looper;
    131     const sp<MediaClock> mMediaClock;
    132     const sp<NuPlayer2> mPlayer;
    133     sp<AudioSink> mAudioSink;
    134     uint32_t mPlayerFlags;
    135 
    136     mediametrics_handle_t mMetricsHandle;
    137     int64_t mPlayerVersion;
    138     uid_t mClientUid;
    139 
    140     bool mAtEOS;
    141     bool mLooping;
    142     bool mAutoLoop;
    143 
    144     void updateMetrics(const char *where);
    145     void logMetrics(const char *where);
    146 
    147     status_t start_l();
    148     void notifyListener_l(int64_t srcId, int msg, int ext1 = 0, int ext2 = 0,
    149                           const PlayerMessage *in = NULL);
    150 
    151     DISALLOW_EVIL_CONSTRUCTORS(NuPlayer2Driver);
    152 };
    153 
    154 }  // namespace android
    155 
    156 
    157