MetadataRetrieverClient.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. **
  3. ** Copyright (C) 2008 The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. //#define LOG_NDEBUG 0
  18. #define LOG_TAG "MetadataRetrieverClient"
  19. #include <utils/Log.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <dirent.h>
  23. #include <unistd.h>
  24. #include <string.h>
  25. #include <cutils/atomic.h>
  26. #include <cutils/properties.h>
  27. #include <binder/MemoryBase.h>
  28. #include <binder/MemoryHeapBase.h>
  29. #include <binder/IPCThreadState.h>
  30. #include <binder/IServiceManager.h>
  31. #include <media/DataSource.h>
  32. #include <media/IMediaHTTPService.h>
  33. #include <media/MediaMetadataRetrieverInterface.h>
  34. #include <media/MediaPlayerInterface.h>
  35. #include <media/stagefright/InterfaceUtils.h>
  36. #include <media/stagefright/Utils.h>
  37. #include <private/media/VideoFrame.h>
  38. #include "MetadataRetrieverClient.h"
  39. #include "StagefrightMetadataRetriever.h"
  40. #include "MediaPlayerFactory.h"
  41. namespace android {
  42. MetadataRetrieverClient::MetadataRetrieverClient(pid_t pid)
  43. {
  44. ALOGV("MetadataRetrieverClient constructor pid(%d)", pid);
  45. mPid = pid;
  46. mAlbumArt = NULL;
  47. mRetriever = NULL;
  48. }
  49. MetadataRetrieverClient::~MetadataRetrieverClient()
  50. {
  51. ALOGV("MetadataRetrieverClient destructor");
  52. disconnect();
  53. }
  54. status_t MetadataRetrieverClient::dump(int fd, const Vector<String16>& /*args*/)
  55. {
  56. const size_t SIZE = 256;
  57. char buffer[SIZE];
  58. String8 result;
  59. result.append(" MetadataRetrieverClient\n");
  60. snprintf(buffer, 255, " pid(%d)\n", mPid);
  61. result.append(buffer);
  62. write(fd, result.string(), result.size());
  63. write(fd, "\n", 1);
  64. return NO_ERROR;
  65. }
  66. void MetadataRetrieverClient::disconnect()
  67. {
  68. ALOGV("disconnect from pid %d", mPid);
  69. Mutex::Autolock lock(mLock);
  70. mRetriever.clear();
  71. mAlbumArt.clear();
  72. IPCThreadState::self()->flushCommands();
  73. }
  74. static sp<MediaMetadataRetrieverBase> createRetriever(player_type playerType)
  75. {
  76. sp<MediaMetadataRetrieverBase> p;
  77. switch (playerType) {
  78. case STAGEFRIGHT_PLAYER:
  79. case NU_PLAYER:
  80. {
  81. p = new StagefrightMetadataRetriever;
  82. break;
  83. }
  84. default:
  85. // TODO:
  86. // support for TEST_PLAYER
  87. ALOGE("player type %d is not supported", playerType);
  88. break;
  89. }
  90. if (p == NULL) {
  91. ALOGE("failed to create a retriever object");
  92. }
  93. return p;
  94. }
  95. status_t MetadataRetrieverClient::setDataSource(
  96. const sp<IMediaHTTPService> &httpService,
  97. const char *url,
  98. const KeyedVector<String8, String8> *headers)
  99. {
  100. ALOGV("setDataSource(%s)", url);
  101. Mutex::Autolock lock(mLock);
  102. if (url == NULL) {
  103. return UNKNOWN_ERROR;
  104. }
  105. // When asking the MediaPlayerFactory subsystem to choose a media player for
  106. // a given URL, a pointer to an outer IMediaPlayer can be passed to the
  107. // factory system to be taken into consideration along with the URL. In the
  108. // case of choosing an instance of a MediaPlayerBase for a
  109. // MetadataRetrieverClient, there is no outer IMediaPlayer which will
  110. // eventually encapsulate the result of this selection. In this case, just
  111. // pass NULL to getPlayerType to indicate that there is no outer
  112. // IMediaPlayer to consider during selection.
  113. player_type playerType =
  114. MediaPlayerFactory::getPlayerType(NULL /* client */, url);
  115. ALOGV("player type = %d", playerType);
  116. sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
  117. if (p == NULL) return NO_INIT;
  118. status_t ret = p->setDataSource(httpService, url, headers);
  119. if (ret == NO_ERROR) mRetriever = p;
  120. return ret;
  121. }
  122. status_t MetadataRetrieverClient::setDataSource(int fd, int64_t offset, int64_t length)
  123. {
  124. ALOGV("setDataSource fd=%d (%s), offset=%lld, length=%lld",
  125. fd, nameForFd(fd).c_str(), (long long) offset, (long long) length);
  126. Mutex::Autolock lock(mLock);
  127. struct stat sb;
  128. int ret = fstat(fd, &sb);
  129. if (ret != 0) {
  130. ALOGE("fstat(%d) failed: %d, %s", fd, ret, strerror(errno));
  131. return BAD_VALUE;
  132. }
  133. ALOGV("st_dev = %llu", static_cast<unsigned long long>(sb.st_dev));
  134. ALOGV("st_mode = %u", sb.st_mode);
  135. ALOGV("st_uid = %lu", static_cast<unsigned long>(sb.st_uid));
  136. ALOGV("st_gid = %lu", static_cast<unsigned long>(sb.st_gid));
  137. ALOGV("st_size = %llu", static_cast<unsigned long long>(sb.st_size));
  138. if (offset >= sb.st_size) {
  139. ALOGE("offset (%lld) bigger than file size (%llu)",
  140. (long long)offset, (unsigned long long)sb.st_size);
  141. return BAD_VALUE;
  142. }
  143. if (offset + length > sb.st_size) {
  144. length = sb.st_size - offset;
  145. ALOGV("calculated length = %lld", (long long)length);
  146. }
  147. player_type playerType =
  148. MediaPlayerFactory::getPlayerType(NULL /* client */,
  149. fd,
  150. offset,
  151. length);
  152. ALOGV("player type = %d", playerType);
  153. sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
  154. if (p == NULL) {
  155. return NO_INIT;
  156. }
  157. status_t status = p->setDataSource(fd, offset, length);
  158. if (status == NO_ERROR) mRetriever = p;
  159. return status;
  160. }
  161. status_t MetadataRetrieverClient::setDataSource(
  162. const sp<IDataSource>& source, const char *mime)
  163. {
  164. ALOGV("setDataSource(IDataSource)");
  165. Mutex::Autolock lock(mLock);
  166. sp<DataSource> dataSource = CreateDataSourceFromIDataSource(source);
  167. player_type playerType =
  168. MediaPlayerFactory::getPlayerType(NULL /* client */, dataSource);
  169. ALOGV("player type = %d", playerType);
  170. sp<MediaMetadataRetrieverBase> p = createRetriever(playerType);
  171. if (p == NULL) return NO_INIT;
  172. status_t ret = p->setDataSource(dataSource, mime);
  173. if (ret == NO_ERROR) mRetriever = p;
  174. return ret;
  175. }
  176. Mutex MetadataRetrieverClient::sLock;
  177. sp<IMemory> MetadataRetrieverClient::getFrameAtTime(
  178. int64_t timeUs, int option, int colorFormat, bool metaOnly)
  179. {
  180. ALOGV("getFrameAtTime: time(%lld us) option(%d) colorFormat(%d), metaOnly(%d)",
  181. (long long)timeUs, option, colorFormat, metaOnly);
  182. Mutex::Autolock lock(mLock);
  183. Mutex::Autolock glock(sLock);
  184. if (mRetriever == NULL) {
  185. ALOGE("retriever is not initialized");
  186. return NULL;
  187. }
  188. sp<IMemory> frame = mRetriever->getFrameAtTime(timeUs, option, colorFormat, metaOnly);
  189. if (frame == NULL) {
  190. ALOGE("failed to capture a video frame");
  191. return NULL;
  192. }
  193. return frame;
  194. }
  195. sp<IMemory> MetadataRetrieverClient::getImageAtIndex(
  196. int index, int colorFormat, bool metaOnly, bool thumbnail) {
  197. ALOGV("getImageAtIndex: index(%d) colorFormat(%d), metaOnly(%d) thumbnail(%d)",
  198. index, colorFormat, metaOnly, thumbnail);
  199. Mutex::Autolock lock(mLock);
  200. Mutex::Autolock glock(sLock);
  201. if (mRetriever == NULL) {
  202. ALOGE("retriever is not initialized");
  203. return NULL;
  204. }
  205. sp<IMemory> frame = mRetriever->getImageAtIndex(index, colorFormat, metaOnly, thumbnail);
  206. if (frame == NULL) {
  207. ALOGE("failed to extract image");
  208. return NULL;
  209. }
  210. return frame;
  211. }
  212. sp<IMemory> MetadataRetrieverClient::getImageRectAtIndex(
  213. int index, int colorFormat, int left, int top, int right, int bottom) {
  214. ALOGV("getImageRectAtIndex: index(%d) colorFormat(%d), rect {%d, %d, %d, %d}",
  215. index, colorFormat, left, top, right, bottom);
  216. Mutex::Autolock lock(mLock);
  217. Mutex::Autolock glock(sLock);
  218. if (mRetriever == NULL) {
  219. ALOGE("retriever is not initialized");
  220. return NULL;
  221. }
  222. sp<IMemory> frame = mRetriever->getImageRectAtIndex(
  223. index, colorFormat, left, top, right, bottom);
  224. if (frame == NULL) {
  225. ALOGE("failed to extract image");
  226. return NULL;
  227. }
  228. return frame;
  229. }
  230. status_t MetadataRetrieverClient::getFrameAtIndex(
  231. std::vector<sp<IMemory> > *frames,
  232. int frameIndex, int numFrames, int colorFormat, bool metaOnly) {
  233. ALOGV("getFrameAtIndex: frameIndex(%d), numFrames(%d), colorFormat(%d), metaOnly(%d)",
  234. frameIndex, numFrames, colorFormat, metaOnly);
  235. Mutex::Autolock lock(mLock);
  236. Mutex::Autolock glock(sLock);
  237. if (mRetriever == NULL) {
  238. ALOGE("retriever is not initialized");
  239. return INVALID_OPERATION;
  240. }
  241. status_t err = mRetriever->getFrameAtIndex(
  242. frames, frameIndex, numFrames, colorFormat, metaOnly);
  243. if (err != OK) {
  244. frames->clear();
  245. return err;
  246. }
  247. return OK;
  248. }
  249. sp<IMemory> MetadataRetrieverClient::extractAlbumArt()
  250. {
  251. ALOGV("extractAlbumArt");
  252. Mutex::Autolock lock(mLock);
  253. mAlbumArt.clear();
  254. if (mRetriever == NULL) {
  255. ALOGE("retriever is not initialized");
  256. return NULL;
  257. }
  258. MediaAlbumArt *albumArt = mRetriever->extractAlbumArt();
  259. if (albumArt == NULL) {
  260. ALOGE("failed to extract an album art");
  261. return NULL;
  262. }
  263. size_t size = sizeof(MediaAlbumArt) + albumArt->size();
  264. sp<MemoryHeapBase> heap = new MemoryHeapBase(size, 0, "MetadataRetrieverClient");
  265. if (heap == NULL) {
  266. ALOGE("failed to create MemoryDealer object");
  267. delete albumArt;
  268. return NULL;
  269. }
  270. mAlbumArt = new MemoryBase(heap, 0, size);
  271. if (mAlbumArt == NULL) {
  272. ALOGE("not enough memory for MediaAlbumArt size=%zu", size);
  273. delete albumArt;
  274. return NULL;
  275. }
  276. MediaAlbumArt::init((MediaAlbumArt *) mAlbumArt->pointer(),
  277. albumArt->size(), albumArt->data());
  278. delete albumArt; // We've taken our copy.
  279. return mAlbumArt;
  280. }
  281. const char* MetadataRetrieverClient::extractMetadata(int keyCode)
  282. {
  283. ALOGV("extractMetadata");
  284. Mutex::Autolock lock(mLock);
  285. if (mRetriever == NULL) {
  286. ALOGE("retriever is not initialized");
  287. return NULL;
  288. }
  289. return mRetriever->extractMetadata(keyCode);
  290. }
  291. }; // namespace android