Wei Jia | 53692fa | 2017-12-11 10:33:46 -0800 | [diff] [blame^] | 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 | #ifndef ANDROID_FRAMEWORKS_BASE_MEDIA_LIBMEDIA_TESTPLAYERSTUB_H__ |
| 18 | #define ANDROID_FRAMEWORKS_BASE_MEDIA_LIBMEDIA_TESTPLAYERSTUB_H__ |
| 19 | |
| 20 | #include <media/MediaPlayer2Interface.h> |
| 21 | #include <utils/Errors.h> |
| 22 | |
| 23 | namespace android { |
| 24 | class MediaPlayer2Base; // in media/MediaPlayer2Interface.h |
| 25 | |
| 26 | // Wrapper around a test media player that gets dynamically loaded. |
| 27 | // |
| 28 | // The URL passed to setDataSource has this format: |
| 29 | // |
| 30 | // test:<name of the .so>?url=<url for the real setDataSource impl.> |
| 31 | // |
| 32 | // e.g: |
| 33 | // test:invoke_test_media_player.so?url=http://youtube.com/ |
| 34 | // test:invoke_test_media_player.so?url=speedtest |
| 35 | // |
| 36 | // TestPlayerStub::setDataSource loads the library in the test url. 2 |
| 37 | // entry points with C linkage are expected. One to create the test |
| 38 | // player and one to destroy it. |
| 39 | // |
| 40 | // extern "C" android::MediaPlayer2Base* newPlayer(); |
| 41 | // extern "C" android::status_t deletePlayer(android::MediaPlayer2Base *p); |
| 42 | // |
| 43 | // Once the test player has been loaded, its setDataSource |
| 44 | // implementation is called with the value of the 'url' parameter. |
| 45 | // |
| 46 | // typical usage in a java test: |
| 47 | // ============================ |
| 48 | // |
| 49 | // MediaPlayer2 p = new MediaPlayer2(); |
| 50 | // p.setDataSource("test:invoke_mock_media_player.so?url=http://youtube.com"); |
| 51 | // p.prepare(); |
| 52 | // ... |
| 53 | // p.release(); |
| 54 | |
| 55 | class TestPlayerStub : public MediaPlayer2Interface { |
| 56 | public: |
| 57 | typedef MediaPlayer2Base* (*NEW_PLAYER)(); |
| 58 | typedef status_t (*DELETE_PLAYER)(MediaPlayer2Base *); |
| 59 | |
| 60 | TestPlayerStub(); |
| 61 | virtual ~TestPlayerStub(); |
| 62 | |
| 63 | // Called right after the constructor. Check if the current build |
| 64 | // allows test players. |
| 65 | virtual status_t initCheck(); |
| 66 | |
| 67 | // @param url Should be a test url. See class comment. |
| 68 | virtual status_t setDataSource( |
| 69 | const sp<MediaHTTPService> &httpService, |
| 70 | const char* url, |
| 71 | const KeyedVector<String8, String8> *headers); |
| 72 | |
| 73 | // Test player for a file descriptor source is not supported. |
| 74 | virtual status_t setDataSource(int, int64_t, int64_t) { |
| 75 | return INVALID_OPERATION; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | // All the methods below wrap the mPlayer instance. |
| 80 | virtual status_t setVideoSurfaceTexture( |
| 81 | const android::sp<android::IGraphicBufferProducer>& st) { |
| 82 | return mPlayer->setVideoSurfaceTexture(st); |
| 83 | } |
| 84 | virtual status_t prepare() {return mPlayer->prepare();} |
| 85 | virtual status_t prepareAsync() {return mPlayer->prepareAsync();} |
| 86 | virtual status_t start() {return mPlayer->start();} |
| 87 | virtual status_t stop() {return mPlayer->stop();} |
| 88 | virtual status_t pause() {return mPlayer->pause();} |
| 89 | virtual bool isPlaying() {return mPlayer->isPlaying();} |
| 90 | virtual status_t seekTo( |
| 91 | int msec, |
| 92 | MediaPlayer2SeekMode mode = MediaPlayer2SeekMode::SEEK_PREVIOUS_SYNC) { |
| 93 | return mPlayer->seekTo(msec, mode); |
| 94 | } |
| 95 | virtual status_t getCurrentPosition(int *p) { |
| 96 | return mPlayer->getCurrentPosition(p); |
| 97 | } |
| 98 | virtual status_t getDuration(int *d) {return mPlayer->getDuration(d);} |
| 99 | virtual status_t reset() {return mPlayer->reset();} |
| 100 | virtual status_t setLooping(int b) {return mPlayer->setLooping(b);} |
| 101 | virtual player2_type playerType() {return mPlayer->playerType();} |
| 102 | virtual status_t invoke(const android::Parcel& in, android::Parcel *out) { |
| 103 | return mPlayer->invoke(in, out); |
| 104 | } |
| 105 | virtual status_t setParameter(int key, const Parcel &request) { |
| 106 | return mPlayer->setParameter(key, request); |
| 107 | } |
| 108 | virtual status_t getParameter(int key, Parcel *reply) { |
| 109 | return mPlayer->getParameter(key, reply); |
| 110 | } |
| 111 | |
| 112 | |
| 113 | // @return true if the current build is 'eng' or 'test' and the |
| 114 | // url's scheme is 'test:' |
| 115 | static bool canBeUsed(const char *url); |
| 116 | |
| 117 | private: |
| 118 | // Release the player, dlclose the library. |
| 119 | status_t resetInternal(); |
| 120 | status_t parseUrl(); |
| 121 | |
| 122 | char *mUrl; // test:foo.so?url=http://bar |
| 123 | char *mFilename; // foo.so |
| 124 | char *mContentUrl; // http://bar |
| 125 | void *mHandle; // returned by dlopen |
| 126 | NEW_PLAYER mNewPlayer; |
| 127 | DELETE_PLAYER mDeletePlayer; |
| 128 | MediaPlayer2Base *mPlayer; // wrapped player |
| 129 | }; |
| 130 | |
| 131 | } // namespace android |
| 132 | |
| 133 | #endif // ANDROID_FRAMEWORKS_BASE_MEDIA_LIBMEDIA_TESTPLAYERSTUB_H__ |