/android/author/PVMediaRecorder.cpp

https://github.com/Jib-BAOSP/platform_external_opencore · C++ · 387 lines · 320 code · 48 blank · 19 comment · 84 complexity · 6680d8c731cd38ce24ab62ce1c893ad8 MD5 · raw file

  1. /*
  2. * Copyright 2008, HTC Inc.
  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. //#define LOG_NDEBUG 0
  17. #define LOG_TAG "PVMediaRecorder"
  18. #include <utils/Log.h>
  19. #include <media/PVMediaRecorder.h>
  20. #include <camera/ICamera.h>
  21. #include "authordriver.h"
  22. namespace android {
  23. PVMediaRecorder::PVMediaRecorder()
  24. {
  25. LOGV("constructor");
  26. mAuthorDriverWrapper = new AuthorDriverWrapper();
  27. }
  28. PVMediaRecorder::~PVMediaRecorder()
  29. {
  30. LOGV("destructor");
  31. delete mAuthorDriverWrapper;
  32. }
  33. status_t PVMediaRecorder::init()
  34. {
  35. LOGV("init");
  36. if (mAuthorDriverWrapper == NULL) {
  37. LOGE("author driver wrapper is not initialized yet");
  38. return UNKNOWN_ERROR;
  39. }
  40. author_command *ac = new author_command(AUTHOR_INIT);
  41. if (ac == NULL) {
  42. LOGE("failed to construct an author command");
  43. return UNKNOWN_ERROR;
  44. }
  45. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  46. }
  47. status_t PVMediaRecorder::setAudioSource(audio_source as)
  48. {
  49. LOGV("setAudioSource(%d)", as);
  50. if (mAuthorDriverWrapper == NULL) {
  51. LOGE("author driver wrapper is not initialized yet");
  52. return UNKNOWN_ERROR;
  53. }
  54. set_audio_source_command *ac = new set_audio_source_command();
  55. if (ac == NULL) {
  56. LOGE("failed to construct an author command");
  57. return UNKNOWN_ERROR;
  58. }
  59. ac->as = as;
  60. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  61. }
  62. status_t PVMediaRecorder::setVideoSource(video_source vs)
  63. {
  64. LOGV("setVideoSource(%d)", vs);
  65. if (mAuthorDriverWrapper == NULL) {
  66. LOGE("author driver wrapper is not initialized yet");
  67. return UNKNOWN_ERROR;
  68. }
  69. set_video_source_command *ac = new set_video_source_command();
  70. if (ac == NULL) {
  71. LOGE("failed to construct an author command");
  72. return UNKNOWN_ERROR;
  73. }
  74. ac->vs = vs;
  75. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  76. }
  77. status_t PVMediaRecorder::setOutputFormat(output_format of)
  78. {
  79. LOGV("setOutputFormat(%d)", of);
  80. if (mAuthorDriverWrapper == NULL) {
  81. LOGE("author driver wrapper is not initialized yet");
  82. return UNKNOWN_ERROR;
  83. }
  84. set_output_format_command *ac = new set_output_format_command();
  85. if (ac == NULL) {
  86. LOGE("failed to construct an author command");
  87. return UNKNOWN_ERROR;
  88. }
  89. ac->of = of;
  90. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  91. }
  92. status_t PVMediaRecorder::setOutputFile(const char *path)
  93. {
  94. LOGV("setOutputFile(%s)", path);
  95. if (mAuthorDriverWrapper == NULL) {
  96. LOGE("author driver wrapper is not initialized yet");
  97. return NO_INIT;
  98. }
  99. // use file descriptor interface
  100. int fd = open(path, O_RDWR | O_CREAT );
  101. if (-1 == fd) {
  102. LOGE("Ln %d open() error %d", __LINE__, fd);
  103. return -errno;
  104. }
  105. return setOutputFile(fd, 0, 0);
  106. }
  107. status_t PVMediaRecorder::setOutputFile(int fd, int64_t offset, int64_t length)
  108. {
  109. LOGV("setOutputFile(%d, %lld, %lld)", fd, offset, length);
  110. set_output_file_command *ac = new set_output_file_command();
  111. if (ac == NULL) {
  112. LOGE("failed to construct an author command");
  113. return NO_MEMORY;
  114. }
  115. ac->fd = fd;
  116. ac->offset = offset;
  117. ac->length = length;
  118. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  119. }
  120. status_t PVMediaRecorder::setParameters(const String8& params) {
  121. LOGV("setParameters(%s)", params.string());
  122. set_parameters_command *command = new set_parameters_command(params);
  123. if (command == NULL) {
  124. LOGE("failed to construct an author command");
  125. return NO_MEMORY;
  126. }
  127. return mAuthorDriverWrapper->enqueueCommand(
  128. command, NULL /* completion_func */, NULL /* completion_cookie */);
  129. }
  130. status_t PVMediaRecorder::setAudioEncoder(audio_encoder ae)
  131. {
  132. LOGV("setAudioEncoder(%d)", ae);
  133. if (mAuthorDriverWrapper == NULL) {
  134. LOGE("author driver wrapper is not initialized yet");
  135. return UNKNOWN_ERROR;
  136. }
  137. set_audio_encoder_command *ac = new set_audio_encoder_command();
  138. if (ac == NULL) {
  139. LOGE("failed to construct an author command");
  140. return UNKNOWN_ERROR;
  141. }
  142. ac->ae = ae;
  143. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  144. }
  145. status_t PVMediaRecorder::setVideoEncoder(video_encoder ve)
  146. {
  147. LOGV("setVideoEncoder(%d)", ve);
  148. if (mAuthorDriverWrapper == NULL) {
  149. LOGE("author driver wrapper is not initialized yet");
  150. return UNKNOWN_ERROR;
  151. }
  152. set_video_encoder_command *ac = new set_video_encoder_command();
  153. if (ac == NULL) {
  154. LOGE("failed to construct an author command");
  155. return UNKNOWN_ERROR;
  156. }
  157. ac->ve = ve;
  158. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  159. }
  160. status_t PVMediaRecorder::setVideoFrameRate(int frames_per_second)
  161. {
  162. LOGV("setVideoFrameRate(%d)", frames_per_second);
  163. if (mAuthorDriverWrapper == NULL) {
  164. LOGE("author driver wrapper is not initialized yet");
  165. return UNKNOWN_ERROR;
  166. }
  167. set_video_frame_rate_command *ac = new set_video_frame_rate_command();
  168. if (ac == NULL) {
  169. LOGE("failed to construct an author command");
  170. return UNKNOWN_ERROR;
  171. }
  172. ac->rate = frames_per_second;
  173. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  174. }
  175. status_t PVMediaRecorder::setVideoSize(int width, int height)
  176. {
  177. LOGV("setVideoSize(%d, %d)", width, height);
  178. if (mAuthorDriverWrapper == NULL) {
  179. LOGE("author driver wrapper is not initialized yet");
  180. return UNKNOWN_ERROR;
  181. }
  182. set_video_size_command *ac = new set_video_size_command();
  183. if (ac == NULL) {
  184. LOGE("failed to construct an author command");
  185. return UNKNOWN_ERROR;
  186. }
  187. ac->width = width;
  188. ac->height = height;
  189. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  190. }
  191. status_t PVMediaRecorder::setCamera(const sp<ICamera>& camera)
  192. {
  193. LOGV("setCamera(%p)", camera.get());
  194. if (mAuthorDriverWrapper == NULL) {
  195. LOGE("author driver wrapper is not initialized yet");
  196. return UNKNOWN_ERROR;
  197. }
  198. set_camera_command *ac = new set_camera_command();
  199. if (ac == NULL) {
  200. LOGE("failed to construct an author command");
  201. return UNKNOWN_ERROR;
  202. }
  203. ac->camera = camera;
  204. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  205. }
  206. status_t PVMediaRecorder::setPreviewSurface(const sp<ISurface>& surface)
  207. {
  208. LOGV("setPreviewSurface(%p)", surface.get());
  209. if (mAuthorDriverWrapper == NULL) {
  210. LOGE("author driver wrapper is not initialized yet");
  211. return UNKNOWN_ERROR;
  212. }
  213. set_preview_surface_command *ac = new set_preview_surface_command();
  214. if (ac == NULL) {
  215. LOGE("failed to construct an author command");
  216. return UNKNOWN_ERROR;
  217. }
  218. ac->surface = surface;
  219. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  220. }
  221. status_t PVMediaRecorder::getMaxAmplitude(int *max)
  222. {
  223. LOGV("getMaxAmplitude");
  224. if (mAuthorDriverWrapper == NULL) {
  225. LOGE("author driver wrapper is not initialized yet");
  226. return UNKNOWN_ERROR;
  227. }
  228. return mAuthorDriverWrapper->getMaxAmplitude(max);
  229. }
  230. status_t PVMediaRecorder::prepare()
  231. {
  232. LOGV("prepare");
  233. if (mAuthorDriverWrapper == NULL) {
  234. LOGE("author driver wrapper is not initialized yet");
  235. return UNKNOWN_ERROR;
  236. }
  237. author_command *ac = new author_command(AUTHOR_PREPARE);
  238. if (ac == NULL) {
  239. LOGE("failed to construct an author command");
  240. return UNKNOWN_ERROR;
  241. }
  242. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  243. }
  244. status_t PVMediaRecorder::start()
  245. {
  246. LOGV("start");
  247. if (mAuthorDriverWrapper == NULL) {
  248. LOGE("author driver wrapper is not initialized yet");
  249. return UNKNOWN_ERROR;
  250. }
  251. author_command *ac = new author_command(AUTHOR_START);
  252. if (ac == NULL) {
  253. LOGE("failed to construct an author command");
  254. return UNKNOWN_ERROR;
  255. }
  256. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  257. }
  258. // Make sure that stop also calls PV author engine's Reset()
  259. // and Close() so that its internal state is maintained correctly
  260. status_t PVMediaRecorder::stop()
  261. {
  262. LOGV("stop");
  263. status_t ret = doStop();
  264. if (OK != ret)
  265. LOGE("stop failed");
  266. ret = reset();
  267. if (OK != ret)
  268. LOGE("reset failed");
  269. ret = close();
  270. if (OK != ret)
  271. LOGE("close failed");
  272. return ret;
  273. }
  274. status_t PVMediaRecorder::doStop()
  275. {
  276. LOGV("doStop");
  277. if (mAuthorDriverWrapper == NULL) {
  278. LOGE("author driver wrapper is not initialized yet");
  279. return UNKNOWN_ERROR;
  280. }
  281. author_command *ac = new author_command(AUTHOR_STOP);
  282. if (ac == NULL) {
  283. LOGE("failed to construct an author command");
  284. return UNKNOWN_ERROR;
  285. }
  286. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  287. }
  288. status_t PVMediaRecorder::reset()
  289. {
  290. LOGV("reset");
  291. if (mAuthorDriverWrapper == NULL) {
  292. LOGE("author driver wrapper is not initialized yet");
  293. return UNKNOWN_ERROR;
  294. }
  295. author_command *ac = new author_command(AUTHOR_RESET);
  296. if (ac == NULL) {
  297. LOGE("failed to construct an author command");
  298. return UNKNOWN_ERROR;
  299. }
  300. status_t ret = mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  301. if (ret != OK) {
  302. LOGE("failed to do reset(%d)", ret);
  303. return UNKNOWN_ERROR;
  304. }
  305. ret = mAuthorDriverWrapper->enqueueCommand(new author_command(AUTHOR_REMOVE_VIDEO_SOURCE), 0, 0);
  306. if (ret != OK) {
  307. LOGE("failed to remove video source(%d)", ret);
  308. return UNKNOWN_ERROR;
  309. }
  310. ret = mAuthorDriverWrapper->enqueueCommand(new author_command(AUTHOR_REMOVE_AUDIO_SOURCE), 0, 0);
  311. if (ret != OK) {
  312. LOGE("failed to remove audio source(%d)", ret);
  313. return UNKNOWN_ERROR;
  314. }
  315. return ret;
  316. }
  317. status_t PVMediaRecorder::close()
  318. {
  319. LOGV("close");
  320. if (mAuthorDriverWrapper == NULL) {
  321. LOGE("author driver wrapper is not initialized yet");
  322. return UNKNOWN_ERROR;
  323. }
  324. author_command *ac = new author_command(AUTHOR_CLOSE);
  325. if (ac == NULL) {
  326. LOGE("failed to construct an author command");
  327. return UNKNOWN_ERROR;
  328. }
  329. return mAuthorDriverWrapper->enqueueCommand(ac, 0, 0);
  330. }
  331. status_t PVMediaRecorder::setListener(const sp<IMediaPlayerClient>& listener) {
  332. LOGV("setListener");
  333. if (mAuthorDriverWrapper == NULL) {
  334. LOGE("author driver wrapper is not initialized yet");
  335. return UNKNOWN_ERROR;
  336. }
  337. return mAuthorDriverWrapper->setListener(listener);
  338. }
  339. }; // namespace android