1 /*
2  * Copyright (C) 2007 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_MEDIAPLAYER_H
18 #define ANDROID_MEDIAPLAYER_H
19 
20 #include <media/mediaplayer_common.h>
21 
22 #include <arpa/inet.h>
23 
24 #include <binder/IMemory.h>
25 
26 #include <media/AudioResamplerPublic.h>
27 #include <media/BufferingSettings.h>
28 #include <media/IMediaPlayerClient.h>
29 #include <media/IMediaPlayer.h>
30 #include <media/IMediaDeathNotifier.h>
31 #include <media/IStreamSource.h>
32 #include <android/content/AttributionSourceState.h>
33 
34 #include <utils/KeyedVector.h>
35 #include <utils/String8.h>
36 
37 #include <string>
38 
39 struct ANativeWindow;
40 
41 namespace android {
42 
43 struct AVSyncSettings;
44 class IGraphicBufferProducer;
45 class Surface;
46 
47 enum media_event_type {
48     MEDIA_NOP               = 0, // interface test message
49     MEDIA_PREPARED          = 1,
50     MEDIA_PLAYBACK_COMPLETE = 2,
51     MEDIA_BUFFERING_UPDATE  = 3,
52     MEDIA_SEEK_COMPLETE     = 4,
53     MEDIA_SET_VIDEO_SIZE    = 5,
54     MEDIA_STARTED           = 6,
55     MEDIA_PAUSED            = 7,
56     MEDIA_STOPPED           = 8,
57     MEDIA_SKIPPED           = 9,
58     MEDIA_NOTIFY_TIME       = 98,
59     MEDIA_TIMED_TEXT        = 99,
60     MEDIA_ERROR             = 100,
61     MEDIA_INFO              = 200,
62     MEDIA_SUBTITLE_DATA     = 201,
63     MEDIA_META_DATA         = 202,
64     MEDIA_DRM_INFO          = 210,
65     MEDIA_TIME_DISCONTINUITY = 211,
66     MEDIA_IMS_RX_NOTICE     = 300,
67     MEDIA_AUDIO_ROUTING_CHANGED = 10000,
68 };
69 
70 // Generic error codes for the media player framework.  Errors are fatal, the
71 // playback must abort.
72 //
73 // Errors are communicated back to the client using the
74 // MediaPlayerListener::notify method defined below.
75 // In this situation, 'notify' is invoked with the following:
76 //   'msg' is set to MEDIA_ERROR.
77 //   'ext1' should be a value from the enum media_error_type.
78 //   'ext2' contains an implementation dependant error code to provide
79 //          more details. Should default to 0 when not used.
80 //
81 // The codes are distributed as follow:
82 //   0xx: Reserved
83 //   1xx: Android Player errors. Something went wrong inside the MediaPlayer.
84 //   2xx: Media errors (e.g Codec not supported). There is a problem with the
85 //        media itself.
86 //   3xx: Runtime errors. Some extraordinary condition arose making the playback
87 //        impossible.
88 //
89 enum media_error_type {
90     // 0xx
91     MEDIA_ERROR_UNKNOWN = 1,
92     // 1xx
93     MEDIA_ERROR_SERVER_DIED = 100,
94     // 2xx
95     MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
96     // 3xx
97 };
98 
99 
100 // Info and warning codes for the media player framework.  These are non fatal,
101 // the playback is going on but there might be some user visible issues.
102 //
103 // Info and warning messages are communicated back to the client using the
104 // MediaPlayerListener::notify method defined below.  In this situation,
105 // 'notify' is invoked with the following:
106 //   'msg' is set to MEDIA_INFO.
107 //   'ext1' should be a value from the enum media_info_type.
108 //   'ext2' contains an implementation dependant info code to provide
109 //          more details. Should default to 0 when not used.
110 //
111 // The codes are distributed as follow:
112 //   0xx: Reserved
113 //   7xx: Android Player info/warning (e.g player lagging behind.)
114 //   8xx: Media info/warning (e.g media badly interleaved.)
115 //
116 enum media_info_type {
117     // 0xx
118     MEDIA_INFO_UNKNOWN = 1,
119     // The player was started because it was used as the next player for another
120     // player, which just completed playback
121     MEDIA_INFO_STARTED_AS_NEXT = 2,
122     // The player just pushed the very first video frame for rendering
123     MEDIA_INFO_RENDERING_START = 3,
124     // 7xx
125     // The video is too complex for the decoder: it can't decode frames fast
126     // enough. Possibly only the audio plays fine at this stage.
127     MEDIA_INFO_VIDEO_TRACK_LAGGING = 700,
128     // MediaPlayer is temporarily pausing playback internally in order to
129     // buffer more data.
130     MEDIA_INFO_BUFFERING_START = 701,
131     // MediaPlayer is resuming playback after filling buffers.
132     MEDIA_INFO_BUFFERING_END = 702,
133     // Bandwidth in recent past
134     MEDIA_INFO_NETWORK_BANDWIDTH = 703,
135 
136     // 8xx
137     // Bad interleaving means that a media has been improperly interleaved or not
138     // interleaved at all, e.g has all the video samples first then all the audio
139     // ones. Video is playing but a lot of disk seek may be happening.
140     MEDIA_INFO_BAD_INTERLEAVING = 800,
141     // The media is not seekable (e.g live stream).
142     MEDIA_INFO_NOT_SEEKABLE = 801,
143     // New media metadata is available.
144     MEDIA_INFO_METADATA_UPDATE = 802,
145     // Audio can not be played.
146     MEDIA_INFO_PLAY_AUDIO_ERROR = 804,
147     // Video can not be played.
148     MEDIA_INFO_PLAY_VIDEO_ERROR = 805,
149 
150     //9xx
151     MEDIA_INFO_TIMED_TEXT_ERROR = 900,
152 };
153 
154 
155 
156 enum media_player_states {
157     MEDIA_PLAYER_STATE_ERROR        = 0,
158     MEDIA_PLAYER_IDLE               = 1 << 0,
159     MEDIA_PLAYER_INITIALIZED        = 1 << 1,
160     MEDIA_PLAYER_PREPARING          = 1 << 2,
161     MEDIA_PLAYER_PREPARED           = 1 << 3,
162     MEDIA_PLAYER_STARTED            = 1 << 4,
163     MEDIA_PLAYER_PAUSED             = 1 << 5,
164     MEDIA_PLAYER_STOPPED            = 1 << 6,
165     MEDIA_PLAYER_PLAYBACK_COMPLETE  = 1 << 7
166 };
167 
168 // Keep KEY_PARAMETER_* in sync with MediaPlayer.java.
169 // The same enum space is used for both set and get, in case there are future keys that
170 // can be both set and get.  But as of now, all parameters are either set only or get only.
171 enum media_parameter_keys {
172     // Streaming/buffering parameters
173     KEY_PARAMETER_CACHE_STAT_COLLECT_FREQ_MS = 1100,            // set only
174 
175     // Return a Parcel containing a single int, which is the channel count of the
176     // audio track, or zero for error (e.g. no audio track) or unknown.
177     KEY_PARAMETER_AUDIO_CHANNEL_COUNT = 1200,                   // get only
178 
179     // Playback rate expressed in permille (1000 is normal speed), saved as int32_t, with negative
180     // values used for rewinding or reverse playback.
181     KEY_PARAMETER_PLAYBACK_RATE_PERMILLE = 1300,                // set only
182 
183     // Set a Parcel containing the value of a parcelled Java AudioAttribute instance
184     KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400,                       // set only
185 
186     // Set a Parcel containing the values of RTP attribute
187     KEY_PARAMETER_RTP_ATTRIBUTES = 2000                       // set only
188 };
189 
190 // Keep INVOKE_ID_* in sync with MediaPlayer.java.
191 enum media_player_invoke_ids {
192     INVOKE_ID_GET_TRACK_INFO = 1,
193     INVOKE_ID_ADD_EXTERNAL_SOURCE = 2,
194     INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3,
195     INVOKE_ID_SELECT_TRACK = 4,
196     INVOKE_ID_UNSELECT_TRACK = 5,
197     INVOKE_ID_SET_VIDEO_SCALING_MODE = 6,
198     INVOKE_ID_GET_SELECTED_TRACK = 7,
199     INVOKE_ID_SET_PLAYER_IID = 8,
200 };
201 
202 // ----------------------------------------------------------------------------
203 // ref-counted object for callbacks
204 class MediaPlayerListener: virtual public RefBase
205 {
206 public:
207     virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) = 0;
208 };
209 
210 struct IMediaHTTPService;
211 
212 class MediaPlayer : public BnMediaPlayerClient,
213                     public virtual IMediaDeathNotifier
214 {
215 public:
216     explicit MediaPlayer(const android::content::AttributionSourceState& mAttributionSource =
217         android::content::AttributionSourceState(),
218         audio_session_t sessionId = AUDIO_SESSION_ALLOCATE);
219     ~MediaPlayer();
220             void            died();
221             void            disconnect();
222 
223             status_t        setDataSource(
224                     const sp<IMediaHTTPService> &httpService,
225                     const char *url,
226                     const KeyedVector<String8, String8> *headers);
227 
228             status_t        setDataSource(int fd, int64_t offset, int64_t length);
229             status_t        setDataSource(const sp<IDataSource> &source);
230             status_t        setDataSource(const String8& rtpParams);
231             status_t        setVideoSurfaceTexture(
232                                     const sp<IGraphicBufferProducer>& bufferProducer);
233             status_t        setListener(const sp<MediaPlayerListener>& listener);
234             status_t        getBufferingSettings(BufferingSettings* buffering /* nonnull */);
235             status_t        setBufferingSettings(const BufferingSettings& buffering);
236             status_t        prepare();
237             status_t        prepareAsync();
238             status_t        start();
239             status_t        stop();
240             status_t        pause();
241             bool            isPlaying();
242             status_t        setPlaybackSettings(const AudioPlaybackRate& rate);
243             status_t        getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
244             status_t        setSyncSettings(const AVSyncSettings& sync, float videoFpsHint);
245             status_t        getSyncSettings(
246                                     AVSyncSettings* sync /* nonnull */,
247                                     float* videoFps /* nonnull */);
248             status_t        getVideoWidth(int *w);
249             status_t        getVideoHeight(int *h);
250             status_t        seekTo(
251                     int msec,
252                     MediaPlayerSeekMode mode = MediaPlayerSeekMode::SEEK_PREVIOUS_SYNC);
253             status_t        notifyAt(int64_t mediaTimeUs);
254             status_t        getCurrentPosition(int *msec);
255             status_t        getDuration(int *msec);
256             status_t        reset();
257             status_t        setAudioStreamType(audio_stream_type_t type);
258             status_t        getAudioStreamType(audio_stream_type_t *type);
259             status_t        setLooping(int loop);
260             bool            isLooping();
261             status_t        setVolume(float leftVolume, float rightVolume);
262             void            notify(int msg, int ext1, int ext2, const Parcel *obj = NULL);
263             status_t        invoke(const Parcel& request, Parcel *reply);
264             status_t        setMetadataFilter(const Parcel& filter);
265             status_t        getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
266             status_t        setAudioSessionId(audio_session_t sessionId);
267             audio_session_t getAudioSessionId();
268             status_t        setAuxEffectSendLevel(float level);
269             status_t        attachAuxEffect(int effectId);
270             status_t        setParameter(int key, const Parcel& request);
271             status_t        getParameter(int key, Parcel* reply);
272             status_t        setRetransmitEndpoint(const char* addrString, uint16_t port);
273             status_t        setNextMediaPlayer(const sp<MediaPlayer>& player);
274 
275             media::VolumeShaper::Status applyVolumeShaper(
276                                     const sp<media::VolumeShaper::Configuration>& configuration,
277                                     const sp<media::VolumeShaper::Operation>& operation);
278             sp<media::VolumeShaper::State> getVolumeShaperState(int id);
279             // Modular DRM
280             status_t        prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
281             status_t        releaseDrm();
282             // AudioRouting
283             status_t        setOutputDevice(audio_port_handle_t deviceId);
284             audio_port_handle_t getRoutedDeviceId();
285             status_t        enableAudioDeviceCallback(bool enabled);
286 
287 private:
288             void            clear_l();
289             status_t        seekTo_l(int msec, MediaPlayerSeekMode mode);
290             status_t        prepareAsync_l();
291             status_t        getDuration_l(int *msec);
292             status_t        attachNewPlayer(const sp<IMediaPlayer>& player);
293             status_t        reset_l();
294             status_t        doSetRetransmitEndpoint(const sp<IMediaPlayer>& player);
295             status_t        checkStateForKeySet_l(int key);
296 
297     sp<IMediaPlayer>            mPlayer;
298     thread_id_t                 mLockThreadId;
299     Mutex                       mLock;
300     Mutex                       mNotifyLock;
301     Condition                   mSignal;
302     sp<MediaPlayerListener>     mListener;
303     void*                       mCookie;
304     media_player_states         mCurrentState;
305     int                         mCurrentPosition;
306     MediaPlayerSeekMode         mCurrentSeekMode;
307     int                         mSeekPosition;
308     MediaPlayerSeekMode         mSeekMode;
309     bool                        mPrepareSync;
310     status_t                    mPrepareStatus;
311     audio_stream_type_t         mStreamType;
312     Parcel*                     mAudioAttributesParcel;
313     bool                        mLoop;
314     float                       mLeftVolume;
315     float                       mRightVolume;
316     int                         mVideoWidth;
317     int                         mVideoHeight;
318     audio_session_t             mAudioSessionId;
319     float                       mSendLevel;
320     struct sockaddr_in          mRetransmitEndpoint;
321     bool                        mRetransmitEndpointValid;
322     const android::content::AttributionSourceState mAttributionSource;
323 };
324 
325 }; // namespace android
326 
327 #endif // ANDROID_MEDIAPLAYER_H
328