1 /*
2  * Copyright (C) 2010 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 <media/MediaPlayerInterface.h>
18 
19 #include <media/MediaMetricsItem.h>
20 #include <media/stagefright/foundation/ABase.h>
21 
22 namespace android {
23 
24 struct ALooper;
25 struct MediaClock;
26 struct NuPlayer;
27 
28 struct NuPlayerDriver : public MediaPlayerInterface {
29     explicit NuPlayerDriver(pid_t pid);
30 
31     virtual status_t initCheck();
32 
33     virtual status_t setUID(uid_t uid);
34 
35     virtual status_t setDataSource(
36             const sp<IMediaHTTPService> &httpService,
37             const char *url,
38             const KeyedVector<String8, String8> *headers);
39 
40     virtual status_t setDataSource(int fd, int64_t offset, int64_t length);
41 
42     virtual status_t setDataSource(const sp<IStreamSource> &source);
43 
44     virtual status_t setDataSource(const sp<DataSource>& dataSource);
45 
46     virtual status_t setVideoSurfaceTexture(
47             const sp<IGraphicBufferProducer> &bufferProducer);
48 
49     virtual status_t getBufferingSettings(
50             BufferingSettings* buffering /* nonnull */) override;
51     virtual status_t setBufferingSettings(const BufferingSettings& buffering) override;
52 
53     virtual status_t prepare();
54     virtual status_t prepareAsync();
55     virtual status_t start();
56     virtual status_t stop();
57     virtual status_t pause();
58     virtual bool isPlaying();
59     virtual status_t setPlaybackSettings(const AudioPlaybackRate &rate);
60     virtual status_t getPlaybackSettings(AudioPlaybackRate *rate);
61     virtual status_t setSyncSettings(const AVSyncSettings &sync, float videoFpsHint);
62     virtual status_t getSyncSettings(AVSyncSettings *sync, float *videoFps);
63     virtual status_t seekTo(
64             int msec, MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
65     virtual status_t getCurrentPosition(int *msec);
66     virtual status_t getDuration(int *msec);
67     virtual status_t reset();
68     virtual status_t notifyAt(int64_t mediaTimeUs) override;
69     virtual status_t setLooping(int loop);
70     virtual player_type playerType();
71     virtual status_t invoke(const Parcel &request, Parcel *reply);
72     virtual void setAudioSink(const sp<AudioSink> &audioSink);
73     virtual status_t setParameter(int key, const Parcel &request);
74     virtual status_t getParameter(int key, Parcel *reply);
75 
76     virtual status_t getMetadata(
77             const media::Metadata::Filter& ids, Parcel *records);
78 
79     virtual status_t dump(int fd, const Vector<String16> &args) const;
80 
81     void notifySetDataSourceCompleted(status_t err);
82     void notifyPrepareCompleted(status_t err);
83     void notifyResetComplete();
84     void notifySetSurfaceComplete();
85     void notifyDuration(int64_t durationUs);
86     void notifyMorePlayingTimeUs(int64_t timeUs);
87     void notifyMoreRebufferingTimeUs(int64_t timeUs);
88     void notifyRebufferingWhenExit(bool status);
89     void notifySeekComplete();
90     void notifySeekComplete_l();
91     void notifyListener(int msg, int ext1 = 0, int ext2 = 0, const Parcel *in = NULL);
92     void notifyFlagsChanged(uint32_t flags);
93 
94     // Modular DRM
95     virtual status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t> &drmSessionId);
96     virtual status_t releaseDrm();
97 
98 protected:
99     virtual ~NuPlayerDriver();
100 
101 private:
102     enum State {
103         STATE_IDLE,
104         STATE_SET_DATASOURCE_PENDING,
105         STATE_UNPREPARED,
106         STATE_PREPARING,
107         STATE_PREPARED,
108         STATE_RUNNING,
109         STATE_PAUSED,
110         STATE_RESET_IN_PROGRESS,
111         STATE_STOPPED,                  // equivalent to PAUSED
112         STATE_STOPPED_AND_PREPARING,    // equivalent to PAUSED, but seeking
113         STATE_STOPPED_AND_PREPARED,     // equivalent to PAUSED, but seek complete
114     };
115 
116     std::string stateString(State state);
117 
118     mutable Mutex mLock;
119     Condition mCondition;
120 
121     State mState;
122 
123     bool mIsAsyncPrepare;
124     status_t mAsyncResult;
125 
126     // The following are protected through "mLock"
127     // >>>
128     bool mSetSurfaceInProgress;
129     int64_t mDurationUs;
130     int64_t mPositionUs;
131     bool mSeekInProgress;
132     int64_t mPlayingTimeUs;
133     int64_t mRebufferingTimeUs;
134     int32_t mRebufferingEvents;
135     bool mRebufferingAtExit;
136     // <<<
137 
138     sp<ALooper> mLooper;
139     const sp<MediaClock> mMediaClock;
140     const sp<NuPlayer> mPlayer;
141     sp<AudioSink> mAudioSink;
142     uint32_t mPlayerFlags;
143 
144     mediametrics::Item *mMetricsItem;
145     mutable Mutex mMetricsLock;
146     uid_t mClientUid;
147 
148     bool mAtEOS;
149     bool mLooping;
150     bool mAutoLoop;
151 
152 
153     void updateMetrics(const char *where);
154     void logMetrics(const char *where);
155 
156     status_t prepare_l();
157     status_t start_l();
158     void notifyListener_l(int msg, int ext1 = 0, int ext2 = 0, const Parcel *in = NULL);
159 
160     DISALLOW_EVIL_CONSTRUCTORS(NuPlayerDriver);
161 };
162 
163 }  // namespace android
164 
165 
166