blob: 0afef1e927e575c390e3f0ba29ae3e3547dde2d1 [file] [log] [blame]
Wei Jia53692fa2017-12-11 10:33:46 -08001/*
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/mediaplayer_common.h>
21
22#include <arpa/inet.h>
23
24#include <media/AudioResamplerPublic.h>
25#include <media/BufferingSettings.h>
Wei Jia51b69562018-02-05 16:17:13 -080026#include <mediaplayer2/MediaPlayer2EngineClient.h>
27#include <mediaplayer2/MediaPlayer2Engine.h>
Wei Jia53692fa2017-12-11 10:33:46 -080028
29#include <utils/Condition.h>
30#include <utils/KeyedVector.h>
31#include <utils/String8.h>
32#include <utils/ThreadDefs.h>
33
Wei Jia53692fa2017-12-11 10:33:46 -080034namespace android {
35
36struct AVSyncSettings;
Wei Jia28288fb2017-12-15 13:45:29 -080037struct ANativeWindowWrapper;
Wei Jiac5c79da2017-12-21 18:03:05 -080038class DataSource;
Wei Jiac2636032018-02-01 09:15:25 -080039struct DataSourceDesc;
Wei Jiac5c79da2017-12-21 18:03:05 -080040struct MediaHTTPService;
Wei Jia53692fa2017-12-11 10:33:46 -080041
42enum media2_event_type {
43 MEDIA2_NOP = 0, // interface test message
44 MEDIA2_PREPARED = 1,
45 MEDIA2_PLAYBACK_COMPLETE = 2,
46 MEDIA2_BUFFERING_UPDATE = 3,
47 MEDIA2_SEEK_COMPLETE = 4,
48 MEDIA2_SET_VIDEO_SIZE = 5,
49 MEDIA2_STARTED = 6,
50 MEDIA2_PAUSED = 7,
51 MEDIA2_STOPPED = 8,
52 MEDIA2_SKIPPED = 9,
53 MEDIA2_NOTIFY_TIME = 98,
54 MEDIA2_TIMED_TEXT = 99,
55 MEDIA2_ERROR = 100,
56 MEDIA2_INFO = 200,
57 MEDIA2_SUBTITLE_DATA = 201,
58 MEDIA2_META_DATA = 202,
59 MEDIA2_DRM_INFO = 210,
60 MEDIA2_AUDIO_ROUTING_CHANGED = 10000,
61};
62
63// Generic error codes for the media player framework. Errors are fatal, the
64// playback must abort.
65//
66// Errors are communicated back to the client using the
67// MediaPlayer2Listener::notify method defined below.
68// In this situation, 'notify' is invoked with the following:
69// 'msg' is set to MEDIA_ERROR.
70// 'ext1' should be a value from the enum media2_error_type.
71// 'ext2' contains an implementation dependant error code to provide
72// more details. Should default to 0 when not used.
73//
74// The codes are distributed as follow:
75// 0xx: Reserved
76// 1xx: Android Player errors. Something went wrong inside the MediaPlayer2.
77// 2xx: Media errors (e.g Codec not supported). There is a problem with the
78// media itself.
79// 3xx: Runtime errors. Some extraordinary condition arose making the playback
80// impossible.
81//
82enum media2_error_type {
83 // 0xx
84 MEDIA2_ERROR_UNKNOWN = 1,
85 // 1xx
Wei Jia72bf2a02018-02-06 15:29:23 -080086 // MEDIA2_ERROR_SERVER_DIED = 100,
Wei Jia53692fa2017-12-11 10:33:46 -080087 // 2xx
88 MEDIA2_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK = 200,
89 // 3xx
Wei Jia72bf2a02018-02-06 15:29:23 -080090 MEDIA2_ERROR_FAILED_TO_SET_DATA_SOURCE = 300,
Wei Jia53692fa2017-12-11 10:33:46 -080091};
92
93
94// Info and warning codes for the media player framework. These are non fatal,
95// the playback is going on but there might be some user visible issues.
96//
97// Info and warning messages are communicated back to the client using the
98// MediaPlayer2Listener::notify method defined below. In this situation,
99// 'notify' is invoked with the following:
100// 'msg' is set to MEDIA_INFO.
101// 'ext1' should be a value from the enum media2_info_type.
102// 'ext2' contains an implementation dependant info code to provide
103// more details. Should default to 0 when not used.
104//
105// The codes are distributed as follow:
106// 0xx: Reserved
107// 7xx: Android Player info/warning (e.g player lagging behind.)
108// 8xx: Media info/warning (e.g media badly interleaved.)
109//
110enum media2_info_type {
111 // 0xx
112 MEDIA2_INFO_UNKNOWN = 1,
113 // The player was started because it was used as the next player for another
114 // player, which just completed playback
115 MEDIA2_INFO_STARTED_AS_NEXT = 2,
116 // The player just pushed the very first video frame for rendering
Wei Jia57aeffd2018-02-15 16:01:14 -0800117 MEDIA2_INFO_VIDEO_RENDERING_START = 3,
118 // The player just pushed the very first audio frame for rendering
119 MEDIA2_INFO_AUDIO_RENDERING_START = 4,
120 // The player just completed the playback of this data source
121 MEDIA2_INFO_PLAYBACK_COMPLETE = 5,
122 // The player just completed the playback of the full play list
123 MEDIA2_INFO_PLAYLIST_END = 6,
124
125 //1xx
126 // The player just prepared a data source.
127 MEDIA2_INFO_PREPARED = 100,
128 // The player just completed a call play().
129 MEDIA2_INFO_COMPLETE_CALL_PLAY = 101,
130 // The player just completed a call pause().
131 MEDIA2_INFO_COMPLETE_CALL_PAUSE = 102,
132 // The player just completed a call seekTo.
133 MEDIA2_INFO_COMPLETE_CALL_SEEK = 103,
134
Wei Jia53692fa2017-12-11 10:33:46 -0800135 // 7xx
136 // The video is too complex for the decoder: it can't decode frames fast
137 // enough. Possibly only the audio plays fine at this stage.
138 MEDIA2_INFO_VIDEO_TRACK_LAGGING = 700,
139 // MediaPlayer2 is temporarily pausing playback internally in order to
140 // buffer more data.
141 MEDIA2_INFO_BUFFERING_START = 701,
142 // MediaPlayer2 is resuming playback after filling buffers.
143 MEDIA2_INFO_BUFFERING_END = 702,
144 // Bandwidth in recent past
145 MEDIA2_INFO_NETWORK_BANDWIDTH = 703,
146
147 // 8xx
148 // Bad interleaving means that a media has been improperly interleaved or not
149 // interleaved at all, e.g has all the video samples first then all the audio
150 // ones. Video is playing but a lot of disk seek may be happening.
151 MEDIA2_INFO_BAD_INTERLEAVING = 800,
152 // The media is not seekable (e.g live stream).
153 MEDIA2_INFO_NOT_SEEKABLE = 801,
154 // New media metadata is available.
155 MEDIA2_INFO_METADATA_UPDATE = 802,
156 // Audio can not be played.
157 MEDIA2_INFO_PLAY_AUDIO_ERROR = 804,
158 // Video can not be played.
159 MEDIA2_INFO_PLAY_VIDEO_ERROR = 805,
160
161 //9xx
162 MEDIA2_INFO_TIMED_TEXT_ERROR = 900,
163};
164
165
166
167enum media_player2_states {
168 MEDIA_PLAYER2_STATE_ERROR = 0,
169 MEDIA_PLAYER2_IDLE = 1 << 0,
170 MEDIA_PLAYER2_INITIALIZED = 1 << 1,
171 MEDIA_PLAYER2_PREPARING = 1 << 2,
172 MEDIA_PLAYER2_PREPARED = 1 << 3,
173 MEDIA_PLAYER2_STARTED = 1 << 4,
174 MEDIA_PLAYER2_PAUSED = 1 << 5,
175 MEDIA_PLAYER2_STOPPED = 1 << 6,
176 MEDIA_PLAYER2_PLAYBACK_COMPLETE = 1 << 7
177};
178
179// Keep KEY_PARAMETER_* in sync with MediaPlayer2.java.
180// The same enum space is used for both set and get, in case there are future keys that
181// can be both set and get. But as of now, all parameters are either set only or get only.
182enum media2_parameter_keys {
183 // Streaming/buffering parameters
184 MEDIA2_KEY_PARAMETER_CACHE_STAT_COLLECT_FREQ_MS = 1100, // set only
185
186 // Return a Parcel containing a single int, which is the channel count of the
187 // audio track, or zero for error (e.g. no audio track) or unknown.
188 MEDIA2_KEY_PARAMETER_AUDIO_CHANNEL_COUNT = 1200, // get only
189
190 // Playback rate expressed in permille (1000 is normal speed), saved as int32_t, with negative
191 // values used for rewinding or reverse playback.
192 MEDIA2_KEY_PARAMETER_PLAYBACK_RATE_PERMILLE = 1300, // set only
193
194 // Set a Parcel containing the value of a parcelled Java AudioAttribute instance
195 MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400 // set only
196};
197
198// Keep INVOKE_ID_* in sync with MediaPlayer2.java.
199enum media_player2_invoke_ids {
200 MEDIA_PLAYER2_INVOKE_ID_GET_TRACK_INFO = 1,
201 MEDIA_PLAYER2_INVOKE_ID_ADD_EXTERNAL_SOURCE = 2,
202 MEDIA_PLAYER2_INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3,
203 MEDIA_PLAYER2_INVOKE_ID_SELECT_TRACK = 4,
204 MEDIA_PLAYER2_INVOKE_ID_UNSELECT_TRACK = 5,
205 MEDIA_PLAYER2_INVOKE_ID_SET_VIDEO_SCALING_MODE = 6,
206 MEDIA_PLAYER2_INVOKE_ID_GET_SELECTED_TRACK = 7
207};
208
209// ----------------------------------------------------------------------------
210// ref-counted object for callbacks
211class MediaPlayer2Listener: virtual public RefBase
212{
213public:
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800214 virtual void notify(int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800215};
216
Wei Jia53692fa2017-12-11 10:33:46 -0800217class MediaPlayer2 : public MediaPlayer2EngineClient
218{
219public:
220 MediaPlayer2();
221 ~MediaPlayer2();
222 void disconnect();
223
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800224 status_t getSrcId(int64_t *srcId);
Wei Jiac2636032018-02-01 09:15:25 -0800225 status_t setDataSource(const sp<DataSourceDesc> &dsd);
Wei Jia57aeffd2018-02-15 16:01:14 -0800226 status_t prepareNextDataSource(const sp<DataSourceDesc> &dsd);
227 status_t playNextDataSource(int64_t srcId);
Wei Jia28288fb2017-12-15 13:45:29 -0800228 status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800229 status_t setListener(const sp<MediaPlayer2Listener>& listener);
230 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
231 status_t setBufferingSettings(const BufferingSettings& buffering);
Wei Jia53692fa2017-12-11 10:33:46 -0800232 status_t prepareAsync();
233 status_t start();
234 status_t stop();
235 status_t pause();
236 bool isPlaying();
237 status_t setPlaybackSettings(const AudioPlaybackRate& rate);
238 status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
239 status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint);
240 status_t getSyncSettings(
241 AVSyncSettings* sync /* nonnull */,
242 float* videoFps /* nonnull */);
243 status_t getVideoWidth(int *w);
244 status_t getVideoHeight(int *h);
245 status_t seekTo(
246 int msec,
247 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
248 status_t notifyAt(int64_t mediaTimeUs);
249 status_t getCurrentPosition(int *msec);
250 status_t getDuration(int *msec);
251 status_t reset();
252 status_t setAudioStreamType(audio_stream_type_t type);
253 status_t getAudioStreamType(audio_stream_type_t *type);
254 status_t setLooping(int loop);
255 bool isLooping();
256 status_t setVolume(float leftVolume, float rightVolume);
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800257 void notify(int64_t srcId, int msg, int ext1, int ext2,
258 const Parcel *obj = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800259 status_t invoke(const Parcel& request, Parcel *reply);
260 status_t setMetadataFilter(const Parcel& filter);
261 status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
262 status_t setAudioSessionId(audio_session_t sessionId);
263 audio_session_t getAudioSessionId();
264 status_t setAuxEffectSendLevel(float level);
265 status_t attachAuxEffect(int effectId);
266 status_t setParameter(int key, const Parcel& request);
267 status_t getParameter(int key, Parcel* reply);
Wei Jia53692fa2017-12-11 10:33:46 -0800268 status_t setNextMediaPlayer(const sp<MediaPlayer2>& player);
269
Wei Jia53692fa2017-12-11 10:33:46 -0800270 // Modular DRM
271 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
272 status_t releaseDrm();
273 // AudioRouting
274 status_t setOutputDevice(audio_port_handle_t deviceId);
275 audio_port_handle_t getRoutedDeviceId();
276 status_t enableAudioDeviceCallback(bool enabled);
277
278private:
279 void clear_l();
280 status_t seekTo_l(int msec, MediaPlayer2SeekMode mode);
281 status_t prepareAsync_l();
282 status_t getDuration_l(int *msec);
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800283 status_t attachNewPlayer(const sp<MediaPlayer2Engine>& player, long srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800284 status_t reset_l();
Wei Jia53692fa2017-12-11 10:33:46 -0800285 status_t checkStateForKeySet_l(int key);
286
287 sp<MediaPlayer2Engine> mPlayer;
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800288 int64_t mSrcId;
Wei Jia53692fa2017-12-11 10:33:46 -0800289 thread_id_t mLockThreadId;
290 Mutex mLock;
291 Mutex mNotifyLock;
Wei Jia53692fa2017-12-11 10:33:46 -0800292 sp<MediaPlayer2Listener> mListener;
293 void* mCookie;
294 media_player2_states mCurrentState;
295 int mCurrentPosition;
296 MediaPlayer2SeekMode mCurrentSeekMode;
297 int mSeekPosition;
298 MediaPlayer2SeekMode mSeekMode;
Wei Jia53692fa2017-12-11 10:33:46 -0800299 audio_stream_type_t mStreamType;
300 Parcel* mAudioAttributesParcel;
301 bool mLoop;
302 float mLeftVolume;
303 float mRightVolume;
304 int mVideoWidth;
305 int mVideoHeight;
306 audio_session_t mAudioSessionId;
307 float mSendLevel;
Wei Jia53692fa2017-12-11 10:33:46 -0800308};
309
310}; // namespace android
311
312#endif // ANDROID_MEDIAPLAYER2_H