James Dong | 199d1c1 | 2011-03-17 11:48:13 -0700 | [diff] [blame] | 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 | |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 17 | #include <binder/ProcessState.h> |
| 18 | |
| 19 | #include <media/IStreamSource.h> |
| 20 | #include <media/mediaplayer.h> |
| 21 | #include <media/stagefright/foundation/ADebug.h> |
Andreas Huber | ae9d507 | 2010-12-06 10:36:06 -0800 | [diff] [blame] | 22 | #include <media/stagefright/foundation/AMessage.h> |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 23 | |
| 24 | #include <binder/IServiceManager.h> |
| 25 | #include <media/IMediaPlayerService.h> |
| 26 | #include <surfaceflinger/ISurfaceComposer.h> |
| 27 | #include <surfaceflinger/SurfaceComposerClient.h> |
| 28 | |
| 29 | #include <fcntl.h> |
| 30 | |
| 31 | using namespace android; |
| 32 | |
| 33 | struct MyStreamSource : public BnStreamSource { |
| 34 | // Caller retains ownership of fd. |
| 35 | MyStreamSource(int fd); |
| 36 | |
| 37 | virtual void setListener(const sp<IStreamListener> &listener); |
| 38 | virtual void setBuffers(const Vector<sp<IMemory> > &buffers); |
| 39 | |
| 40 | virtual void onBufferAvailable(size_t index); |
| 41 | |
| 42 | protected: |
| 43 | virtual ~MyStreamSource(); |
| 44 | |
| 45 | private: |
| 46 | int mFd; |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 47 | off64_t mFileSize; |
| 48 | int64_t mNextSeekTimeUs; |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 49 | |
| 50 | sp<IStreamListener> mListener; |
| 51 | Vector<sp<IMemory> > mBuffers; |
| 52 | |
| 53 | DISALLOW_EVIL_CONSTRUCTORS(MyStreamSource); |
| 54 | }; |
| 55 | |
| 56 | MyStreamSource::MyStreamSource(int fd) |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 57 | : mFd(fd), |
| 58 | mFileSize(0), |
Andreas Huber | 54e6649 | 2010-12-23 10:27:40 -0800 | [diff] [blame] | 59 | mNextSeekTimeUs(-1) { // ALooper::GetNowUs() + 5000000ll) { |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 60 | CHECK_GE(fd, 0); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 61 | |
| 62 | mFileSize = lseek64(fd, 0, SEEK_END); |
| 63 | lseek64(fd, 0, SEEK_SET); |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | MyStreamSource::~MyStreamSource() { |
| 67 | } |
| 68 | |
| 69 | void MyStreamSource::setListener(const sp<IStreamListener> &listener) { |
| 70 | mListener = listener; |
| 71 | } |
| 72 | |
| 73 | void MyStreamSource::setBuffers(const Vector<sp<IMemory> > &buffers) { |
| 74 | mBuffers = buffers; |
| 75 | } |
| 76 | |
| 77 | void MyStreamSource::onBufferAvailable(size_t index) { |
| 78 | CHECK_LT(index, mBuffers.size()); |
Andreas Huber | a158746 | 2010-12-15 15:17:42 -0800 | [diff] [blame] | 79 | |
| 80 | if (mNextSeekTimeUs >= 0 && mNextSeekTimeUs <= ALooper::GetNowUs()) { |
| 81 | off64_t offset = (off64_t)(((float)rand() / RAND_MAX) * mFileSize * 0.8); |
| 82 | offset = (offset / 188) * 188; |
| 83 | |
| 84 | lseek(mFd, offset, SEEK_SET); |
| 85 | |
| 86 | mListener->issueCommand( |
| 87 | IStreamListener::DISCONTINUITY, false /* synchronous */); |
| 88 | |
| 89 | mNextSeekTimeUs = -1; |
| 90 | mNextSeekTimeUs = ALooper::GetNowUs() + 5000000ll; |
| 91 | } |
| 92 | |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 93 | sp<IMemory> mem = mBuffers.itemAt(index); |
| 94 | |
| 95 | ssize_t n = read(mFd, mem->pointer(), mem->size()); |
| 96 | if (n <= 0) { |
Andreas Huber | ae9d507 | 2010-12-06 10:36:06 -0800 | [diff] [blame] | 97 | mListener->issueCommand(IStreamListener::EOS, false /* synchronous */); |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 98 | } else { |
| 99 | mListener->queueBuffer(index, n); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | //////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | struct MyClient : public BnMediaPlayerClient { |
| 106 | MyClient() |
| 107 | : mEOS(false) { |
| 108 | } |
| 109 | |
Gloria Wang | 162ee49 | 2011-04-11 17:23:27 -0700 | [diff] [blame] | 110 | virtual void notify(int msg, int ext1, int ext2, const Parcel *obj) { |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 111 | Mutex::Autolock autoLock(mLock); |
| 112 | |
| 113 | if (msg == MEDIA_ERROR || msg == MEDIA_PLAYBACK_COMPLETE) { |
| 114 | mEOS = true; |
| 115 | mCondition.signal(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void waitForEOS() { |
| 120 | Mutex::Autolock autoLock(mLock); |
| 121 | while (!mEOS) { |
| 122 | mCondition.wait(mLock); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | protected: |
| 127 | virtual ~MyClient() { |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | Mutex mLock; |
| 132 | Condition mCondition; |
| 133 | |
| 134 | bool mEOS; |
| 135 | |
| 136 | DISALLOW_EVIL_CONSTRUCTORS(MyClient); |
| 137 | }; |
| 138 | |
| 139 | int main(int argc, char **argv) { |
| 140 | android::ProcessState::self()->startThreadPool(); |
| 141 | |
| 142 | if (argc != 2) { |
| 143 | fprintf(stderr, "Usage: %s filename\n", argv[0]); |
| 144 | return 1; |
| 145 | } |
| 146 | |
| 147 | sp<SurfaceComposerClient> composerClient = new SurfaceComposerClient; |
| 148 | CHECK_EQ(composerClient->initCheck(), (status_t)OK); |
| 149 | |
| 150 | sp<SurfaceControl> control = |
| 151 | composerClient->createSurface( |
Andreas Huber | 52b52cd | 2010-11-23 11:41:34 -0800 | [diff] [blame] | 152 | String8("A Surface"), |
| 153 | 0, |
| 154 | 1280, |
| 155 | 800, |
| 156 | PIXEL_FORMAT_RGB_565, |
| 157 | 0); |
| 158 | |
| 159 | CHECK(control != NULL); |
| 160 | CHECK(control->isValid()); |
| 161 | |
| 162 | CHECK_EQ(composerClient->openTransaction(), (status_t)OK); |
| 163 | CHECK_EQ(control->setLayer(30000), (status_t)OK); |
| 164 | CHECK_EQ(control->show(), (status_t)OK); |
| 165 | CHECK_EQ(composerClient->closeTransaction(), (status_t)OK); |
| 166 | |
| 167 | sp<Surface> surface = control->getSurface(); |
| 168 | CHECK(surface != NULL); |
| 169 | |
| 170 | sp<IServiceManager> sm = defaultServiceManager(); |
| 171 | sp<IBinder> binder = sm->getService(String16("media.player")); |
| 172 | sp<IMediaPlayerService> service = interface_cast<IMediaPlayerService>(binder); |
| 173 | |
| 174 | CHECK(service.get() != NULL); |
| 175 | |
| 176 | int fd = open(argv[1], O_RDONLY); |
| 177 | |
| 178 | if (fd < 0) { |
| 179 | fprintf(stderr, "Failed to open file '%s'.", argv[1]); |
| 180 | return 1; |
| 181 | } |
| 182 | |
| 183 | sp<MyClient> client = new MyClient; |
| 184 | |
| 185 | sp<IMediaPlayer> player = |
| 186 | service->create(getpid(), client, new MyStreamSource(fd), 0); |
| 187 | |
| 188 | if (player != NULL) { |
| 189 | player->setVideoSurface(surface); |
| 190 | player->start(); |
| 191 | |
| 192 | client->waitForEOS(); |
| 193 | |
| 194 | player->stop(); |
| 195 | } else { |
| 196 | fprintf(stderr, "failed to instantiate player.\n"); |
| 197 | } |
| 198 | |
| 199 | close(fd); |
| 200 | fd = -1; |
| 201 | |
| 202 | composerClient->dispose(); |
| 203 | |
| 204 | return 0; |
| 205 | } |