blob: e9d6f847c2a82b0a164025df0a6cc0ee601f14fd [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
117 MEDIA2_INFO_RENDERING_START = 3,
118 // 7xx
119 // The video is too complex for the decoder: it can't decode frames fast
120 // enough. Possibly only the audio plays fine at this stage.
121 MEDIA2_INFO_VIDEO_TRACK_LAGGING = 700,
122 // MediaPlayer2 is temporarily pausing playback internally in order to
123 // buffer more data.
124 MEDIA2_INFO_BUFFERING_START = 701,
125 // MediaPlayer2 is resuming playback after filling buffers.
126 MEDIA2_INFO_BUFFERING_END = 702,
127 // Bandwidth in recent past
128 MEDIA2_INFO_NETWORK_BANDWIDTH = 703,
129
130 // 8xx
131 // Bad interleaving means that a media has been improperly interleaved or not
132 // interleaved at all, e.g has all the video samples first then all the audio
133 // ones. Video is playing but a lot of disk seek may be happening.
134 MEDIA2_INFO_BAD_INTERLEAVING = 800,
135 // The media is not seekable (e.g live stream).
136 MEDIA2_INFO_NOT_SEEKABLE = 801,
137 // New media metadata is available.
138 MEDIA2_INFO_METADATA_UPDATE = 802,
139 // Audio can not be played.
140 MEDIA2_INFO_PLAY_AUDIO_ERROR = 804,
141 // Video can not be played.
142 MEDIA2_INFO_PLAY_VIDEO_ERROR = 805,
143
144 //9xx
145 MEDIA2_INFO_TIMED_TEXT_ERROR = 900,
146};
147
148
149
150enum media_player2_states {
151 MEDIA_PLAYER2_STATE_ERROR = 0,
152 MEDIA_PLAYER2_IDLE = 1 << 0,
153 MEDIA_PLAYER2_INITIALIZED = 1 << 1,
154 MEDIA_PLAYER2_PREPARING = 1 << 2,
155 MEDIA_PLAYER2_PREPARED = 1 << 3,
156 MEDIA_PLAYER2_STARTED = 1 << 4,
157 MEDIA_PLAYER2_PAUSED = 1 << 5,
158 MEDIA_PLAYER2_STOPPED = 1 << 6,
159 MEDIA_PLAYER2_PLAYBACK_COMPLETE = 1 << 7
160};
161
162// Keep KEY_PARAMETER_* in sync with MediaPlayer2.java.
163// The same enum space is used for both set and get, in case there are future keys that
164// can be both set and get. But as of now, all parameters are either set only or get only.
165enum media2_parameter_keys {
166 // Streaming/buffering parameters
167 MEDIA2_KEY_PARAMETER_CACHE_STAT_COLLECT_FREQ_MS = 1100, // set only
168
169 // Return a Parcel containing a single int, which is the channel count of the
170 // audio track, or zero for error (e.g. no audio track) or unknown.
171 MEDIA2_KEY_PARAMETER_AUDIO_CHANNEL_COUNT = 1200, // get only
172
173 // Playback rate expressed in permille (1000 is normal speed), saved as int32_t, with negative
174 // values used for rewinding or reverse playback.
175 MEDIA2_KEY_PARAMETER_PLAYBACK_RATE_PERMILLE = 1300, // set only
176
177 // Set a Parcel containing the value of a parcelled Java AudioAttribute instance
178 MEDIA2_KEY_PARAMETER_AUDIO_ATTRIBUTES = 1400 // set only
179};
180
181// Keep INVOKE_ID_* in sync with MediaPlayer2.java.
182enum media_player2_invoke_ids {
183 MEDIA_PLAYER2_INVOKE_ID_GET_TRACK_INFO = 1,
184 MEDIA_PLAYER2_INVOKE_ID_ADD_EXTERNAL_SOURCE = 2,
185 MEDIA_PLAYER2_INVOKE_ID_ADD_EXTERNAL_SOURCE_FD = 3,
186 MEDIA_PLAYER2_INVOKE_ID_SELECT_TRACK = 4,
187 MEDIA_PLAYER2_INVOKE_ID_UNSELECT_TRACK = 5,
188 MEDIA_PLAYER2_INVOKE_ID_SET_VIDEO_SCALING_MODE = 6,
189 MEDIA_PLAYER2_INVOKE_ID_GET_SELECTED_TRACK = 7
190};
191
192// ----------------------------------------------------------------------------
193// ref-counted object for callbacks
194class MediaPlayer2Listener: virtual public RefBase
195{
196public:
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800197 virtual void notify(int64_t srcId, int msg, int ext1, int ext2, const Parcel *obj) = 0;
Wei Jia53692fa2017-12-11 10:33:46 -0800198};
199
Wei Jia53692fa2017-12-11 10:33:46 -0800200class MediaPlayer2 : public MediaPlayer2EngineClient
201{
202public:
203 MediaPlayer2();
204 ~MediaPlayer2();
205 void disconnect();
206
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800207 status_t getSrcId(int64_t *srcId);
Wei Jiac2636032018-02-01 09:15:25 -0800208 status_t setDataSource(const sp<DataSourceDesc> &dsd);
Wei Jia28288fb2017-12-15 13:45:29 -0800209 status_t setVideoSurfaceTexture(const sp<ANativeWindowWrapper>& nww);
Wei Jia53692fa2017-12-11 10:33:46 -0800210 status_t setListener(const sp<MediaPlayer2Listener>& listener);
211 status_t getBufferingSettings(BufferingSettings* buffering /* nonnull */);
212 status_t setBufferingSettings(const BufferingSettings& buffering);
213 status_t prepare();
214 status_t prepareAsync();
215 status_t start();
216 status_t stop();
217 status_t pause();
218 bool isPlaying();
219 status_t setPlaybackSettings(const AudioPlaybackRate& rate);
220 status_t getPlaybackSettings(AudioPlaybackRate* rate /* nonnull */);
221 status_t setSyncSettings(const AVSyncSettings& sync, float videoFpsHint);
222 status_t getSyncSettings(
223 AVSyncSettings* sync /* nonnull */,
224 float* videoFps /* nonnull */);
225 status_t getVideoWidth(int *w);
226 status_t getVideoHeight(int *h);
227 status_t seekTo(
228 int msec,
229 MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC);
230 status_t notifyAt(int64_t mediaTimeUs);
231 status_t getCurrentPosition(int *msec);
232 status_t getDuration(int *msec);
233 status_t reset();
234 status_t setAudioStreamType(audio_stream_type_t type);
235 status_t getAudioStreamType(audio_stream_type_t *type);
236 status_t setLooping(int loop);
237 bool isLooping();
238 status_t setVolume(float leftVolume, float rightVolume);
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800239 void notify(int64_t srcId, int msg, int ext1, int ext2,
240 const Parcel *obj = NULL);
Wei Jia53692fa2017-12-11 10:33:46 -0800241 status_t invoke(const Parcel& request, Parcel *reply);
242 status_t setMetadataFilter(const Parcel& filter);
243 status_t getMetadata(bool update_only, bool apply_filter, Parcel *metadata);
244 status_t setAudioSessionId(audio_session_t sessionId);
245 audio_session_t getAudioSessionId();
246 status_t setAuxEffectSendLevel(float level);
247 status_t attachAuxEffect(int effectId);
248 status_t setParameter(int key, const Parcel& request);
249 status_t getParameter(int key, Parcel* reply);
Wei Jia53692fa2017-12-11 10:33:46 -0800250 status_t setNextMediaPlayer(const sp<MediaPlayer2>& player);
251
Wei Jia53692fa2017-12-11 10:33:46 -0800252 // Modular DRM
253 status_t prepareDrm(const uint8_t uuid[16], const Vector<uint8_t>& drmSessionId);
254 status_t releaseDrm();
255 // AudioRouting
256 status_t setOutputDevice(audio_port_handle_t deviceId);
257 audio_port_handle_t getRoutedDeviceId();
258 status_t enableAudioDeviceCallback(bool enabled);
259
260private:
261 void clear_l();
262 status_t seekTo_l(int msec, MediaPlayer2SeekMode mode);
263 status_t prepareAsync_l();
264 status_t getDuration_l(int *msec);
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800265 status_t attachNewPlayer(const sp<MediaPlayer2Engine>& player, long srcId);
Wei Jia53692fa2017-12-11 10:33:46 -0800266 status_t reset_l();
Wei Jia53692fa2017-12-11 10:33:46 -0800267 status_t checkStateForKeySet_l(int key);
268
269 sp<MediaPlayer2Engine> mPlayer;
Wei Jiad2bb1bd2018-02-08 09:47:37 -0800270 int64_t mSrcId;
Wei Jia53692fa2017-12-11 10:33:46 -0800271 thread_id_t mLockThreadId;
272 Mutex mLock;
273 Mutex mNotifyLock;
274 Condition mSignal;
275 sp<MediaPlayer2Listener> mListener;
276 void* mCookie;
277 media_player2_states mCurrentState;
278 int mCurrentPosition;
279 MediaPlayer2SeekMode mCurrentSeekMode;
280 int mSeekPosition;
281 MediaPlayer2SeekMode mSeekMode;
282 bool mPrepareSync;
283 status_t mPrepareStatus;
284 audio_stream_type_t mStreamType;
285 Parcel* mAudioAttributesParcel;
286 bool mLoop;
287 float mLeftVolume;
288 float mRightVolume;
289 int mVideoWidth;
290 int mVideoHeight;
291 audio_session_t mAudioSessionId;
292 float mSendLevel;
Wei Jia53692fa2017-12-11 10:33:46 -0800293};
294
295}; // namespace android
296
297#endif // ANDROID_MEDIAPLAYER2_H