歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 移植FFmpeg到Android ics

移植FFmpeg到Android ics

日期:2017/3/1 10:24:25   编辑:Linux編程

想到把FFmpeg移植到Android上面

網上有現成的2.2的移植,可以下載下來,鏈接https://github.com/havlenapetr,裡面的ffmpeg和framework下面的libaudio和libviedo兩個so,就是全部的東西。

看過一些東西,都說不開放某些代碼,挺沒勁的,人家都已經放出全部代碼了,有什麼藏匿的。

app下面的代碼幾乎不需要修改,就是一些編譯的錯誤,就是framework下面的本地適配需要需要改一下,適應一下ics,就是surface.cpp這個文件。

  1. /*
  2. * Copyright (C) 2009 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. #include <android/surface.h>
  17. #include <surfaceflinger/Surface.h>
  18. #include <utils/Log.h>
  19. #include <SkBitmap.h>
  20. #include <SkCanvas.h>
  21. #define TAG "SurfaceWrapper"
  22. using namespace android;
  23. static Surface *sSurface;
  24. static SkBitmap sBitmapClient;
  25. static SkBitmap sBitmapSurface;
  26. static Surface *getNativeSurface (JNIEnv * env, jobject jsurface)
  27. {
  28. jclass clazz = env->FindClass ("android/view/Surface");
  29. jfieldID field_surface = env->GetFieldID (clazz, "mNativeSurface", "I");
  30. if (field_surface == NULL)
  31. {
  32. return NULL;
  33. }
  34. return (Surface *) env->GetIntField (jsurface, field_surface);
  35. }
  36. static int initBitmap (SkBitmap * bitmap, int format, int width, int height, bool allocPixels)
  37. {
  38. switch (format)
  39. {
  40. case PIXEL_FORMAT_RGBA_8888:
  41. bitmap->setConfig (SkBitmap::kARGB_8888_Config, width, height);
  42. break;
  43. case PIXEL_FORMAT_RGBA_4444:
  44. bitmap->setConfig (SkBitmap::kARGB_4444_Config, width, height);
  45. break;
  46. case PIXEL_FORMAT_RGB_565:
  47. bitmap->setConfig (SkBitmap::kRGB_565_Config, width, height);
  48. break;
  49. case PIXEL_FORMAT_A_8:
  50. bitmap->setConfig (SkBitmap::kA8_Config, width, height);
  51. break;
  52. default:
  53. bitmap->setConfig (SkBitmap::kNo_Config, width, height);
  54. break;
  55. }
  56. if (allocPixels)
  57. {
  58. bitmap->setIsOpaque (true);
  59. //-- alloc array of pixels
  60. if (!bitmap->allocPixels ())
  61. {
  62. return -1;
  63. }
  64. }
  65. return 0;
  66. }
  67. int AndroidSurface_register (JNIEnv * env, jobject jsurface)
  68. {
  69. __android_log_print (ANDROID_LOG_INFO, TAG, "registering video surface");
  70. sSurface = getNativeSurface (env, jsurface);
  71. if (sSurface == NULL)
  72. {
  73. return ANDROID_SURFACE_RESULT_JNI_EXCEPTION;
  74. }
  75. __android_log_print (ANDROID_LOG_INFO, TAG, "registered");
  76. return ANDROID_SURFACE_RESULT_SUCCESS;
  77. }
  78. int AndroidSurface_getPixels (int width, int height, void **pixels)
  79. {
  80. __android_log_print (ANDROID_LOG_INFO, TAG, "getting surface's pixels %ix%i", width, height);
  81. if (sSurface == NULL)
  82. {
  83. return ANDROID_SURFACE_RESULT_JNI_EXCEPTION;
  84. }
  85. if (initBitmap (&sBitmapClient, PIXEL_FORMAT_RGB_565, width, height, true) < 0)
  86. {
  87. return ANDROID_SURFACE_RESULT_COULDNT_INIT_BITMAP_CLIENT;
  88. }
  89. *pixels = sBitmapClient.getPixels ();
  90. __android_log_print (ANDROID_LOG_INFO, TAG, "getted");
  91. return ANDROID_SURFACE_RESULT_SUCCESS;
  92. }
  93. static void doUpdateSurface ()
  94. {
  95. SkCanvas canvas (sBitmapSurface);
  96. SkRect surface_sBitmapClient;
  97. SkRect surface_sBitmapSurface;
  98. SkMatrix matrix;
  99. surface_sBitmapSurface.set (0, 0, sBitmapSurface.width (), sBitmapSurface.height ());
  100. surface_sBitmapClient.set (0, 0, sBitmapClient.width (), sBitmapClient.height ());
  101. matrix.setRectToRect (surface_sBitmapClient, surface_sBitmapSurface, SkMatrix::kFill_ScaleToFit);
  102. canvas.drawBitmapMatrix (sBitmapClient, matrix);
  103. }
  104. static int prepareSurfaceBitmap (Surface::SurfaceInfo * info)
  105. {
  106. if (initBitmap (&sBitmapSurface, info->format, info->w, info->h, false) < 0)
  107. {
  108. return -1;
  109. }
  110. sBitmapSurface.setPixels (info->bits);
  111. return 0;
  112. }
  113. int AndroidSurface_updateSurface ()
  114. {
  115. static Surface::SurfaceInfo surfaceInfo;
  116. if (sSurface == NULL)
  117. {
  118. return ANDROID_SURFACE_RESULT_JNI_EXCEPTION;
  119. }
  120. if (!Surface::isValid (sSurface))
  121. {
  122. return ANDROID_SURFACE_RESULT_NOT_VALID;
  123. }
  124. if (sSurface->lock (&surfaceInfo,NULL) < 0)
  125. {
  126. return ANDROID_SURFACE_RESULT_COULDNT_LOCK;
  127. }
  128. if (prepareSurfaceBitmap (&surfaceInfo) < 0)
  129. {
  130. return ANDROID_SURFACE_RESULT_COULDNT_INIT_BITMAP_SURFACE;
  131. }
  132. doUpdateSurface ();
  133. if (sSurface->unlockAndPost () < 0)
  134. {
  135. return ANDROID_SURFACE_RESULT_COULDNT_UNLOCK_AND_POST;
  136. }
  137. return ANDROID_SURFACE_RESULT_SUCCESS;
  138. }
  139. int AndroidSurface_unregister ()
  140. {
  141. __android_log_print (ANDROID_LOG_INFO, TAG, "unregistering video surface");
  142. __android_log_print (ANDROID_LOG_INFO, TAG, "unregistered");
  143. return ANDROID_SURFACE_RESULT_SUCCESS;
  144. }

改完以後,編譯出來的apk就是可以播放的,但是效果相當的差,問題太多了,需要時間來分析一下。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved