1 /* 2 * Copyright (C) 2008, The Android Open Source Project 3 * Copyright (C) 2008 HTC Inc. 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 18 /* authordriver.h 19 * 20 * The glue between the Android MediaRecorder and PVAuthorInterface 21 */ 22 23 #ifndef _AUTHORDRIVER_PRIV_H 24 #define _AUTHORDRIVER_PRIV_H 25 26 #include <utils/Log.h> 27 #include <utils/threads.h> 28 #include <utils/List.h> 29 #include <utils/Errors.h> 30 31 #include <camera/ICamera.h> 32 33 34 #include <media/mediarecorder.h> 35 36 #include "oscl_scheduler.h" 37 #include "oscl_scheduler_ao.h" 38 #include "oscl_exception.h" 39 #include "pvlogger.h" 40 #include "pvlogger_file_appender.h" 41 #include "pvlogger_stderr_appender.h" 42 #include "pvlogger_time_and_id_layout.h" 43 #include "pvauthorenginefactory.h" 44 #include "pvauthorengineinterface.h" 45 #include "pv_engine_observer.h" 46 #include "pvmf_errorinfomessage_extension.h" 47 #include "oscl_mem.h" 48 #include "oscl_mem_audit.h" 49 #include "oscl_error.h" 50 #include "oscl_utf8conv.h" 51 #include "oscl_string_utils.h" 52 #include "android_camera_input.h" 53 #include "android_audio_input.h" 54 #include "pvmf_media_input_node_factory.h" 55 #include "pvmf_fileoutput_factory.h" 56 #include "pvmf_node_interface.h" 57 #include "pvmp4h263encextension.h" 58 #include "pvmp4ffcn_clipconfig.h" 59 #include "pvmf_fileoutput_config.h" 60 #ifndef PVMF_FILEOUTPUT_CONFIG_H_INCLUDED 61 #include "pvmf_fileoutput_config.h" 62 #endif 63 #ifndef PVMF_AUDIO_ENCNODE_EXTENSION_H_INCLUDED 64 #include "pvmf_audio_encnode_extension.h" 65 #endif 66 67 // FIXME: 68 // Platform-specic and temporal workaround to prevent video size 69 // from being set too large 70 71 #define ANDROID_MAX_ENCODED_FRAME_WIDTH 352 72 #define ANDROID_MAX_ENCODED_FRAME_HEIGHT 288 73 #define ANDROID_MIN_ENCODED_FRAME_WIDTH 176 74 #define ANDROID_MIN_ENCODED_FRAME_HEIGHT 144 75 76 #define ANDROID_MIN_FRAME_RATE_FPS 5 77 #define ANDROID_MAX_FRAME_RATE_FPS 20 78 79 static const int32 DEFAULT_VIDEO_FRAME_RATE = 20; 80 static const int32 DEFAULT_VIDEO_WIDTH = 176; 81 static const int32 DEFAULT_VIDEO_HEIGHT = 144; 82 83 static const int32 MIN_VIDEO_BITRATE_SETTING = 192000; 84 static const int32 MAX_VIDEO_BITRATE_SETTING = 420000; 85 static const int32 MAX_AUDIO_BITRATE_SETTING = 320000; // Max bitrate?? 86 static const int32 MIN_AUDIO_BITRATE_SETTING = 1; // Min bitrate?? 87 static const int32 DEFAULT_AUDIO_BITRATE_SETTING = 64000; // Default for all the other audio 88 static const PVMF_GSMAMR_Rate DEFAULT_AMR_NARROW_BAND_BITRATE_SETTING = GSM_AMR_12_2; 89 static const PVMF_GSMAMR_Rate DEFAULT_AMR_WIDE_BAND_BITRATE_SETTING = GSM_AMR_23_85; 90 91 typedef struct AMR_BITRATE_MAPPING 92 { 93 int32 bitrate; 94 PVMF_GSMAMR_Rate actual; 95 } AMR_BITRATE_MAPPING; 96 97 static const uint32 AMR_BITRATE_MAX_NUMBER_OF_ROWS = 10; 98 static const AMR_BITRATE_MAPPING AMR_BITRATE_MAPPING_ARRAY[AMR_BITRATE_MAX_NUMBER_OF_ROWS][2] = { 99 {{1,DEFAULT_AMR_NARROW_BAND_BITRATE_SETTING}, {1, DEFAULT_AMR_WIDE_BAND_BITRATE_SETTING}}, // default values 100 {{4950, GSM_AMR_4_75}, {7725, GSM_AMR_6_60}}, 101 {{5525, GSM_AMR_5_15}, {10750, GSM_AMR_8_85}}, 102 {{6300, GSM_AMR_5_90}, {13450, GSM_AMR_12_65}}, 103 {{7050, GSM_AMR_6_70}, {15050, GSM_AMR_14_25}}, 104 {{7625, GSM_AMR_7_40}, {17050, GSM_AMR_15_85}}, 105 {{9075, GSM_AMR_7_95}, {19050, GSM_AMR_18_25}}, 106 {{11200, GSM_AMR_10_2}, {21450, GSM_AMR_19_85}}, 107 {{(MAX_AUDIO_BITRATE_SETTING+1),GSM_AMR_12_2}, {23450, GSM_AMR_23_05}}, 108 {{(MAX_AUDIO_BITRATE_SETTING+1),GSM_AMR_12_2},{(MAX_AUDIO_BITRATE_SETTING+1), GSM_AMR_23_85}}}; 109 110 namespace android { 111 112 template<class DestructClass> 113 class LogAppenderDestructDealloc : public OsclDestructDealloc 114 { 115 public: 116 virtual void destruct_and_dealloc(OsclAny *ptr) 117 { 118 delete((DestructClass*)ptr); 119 } 120 }; 121 122 // Commands that MediaAuthor sends to the AuthorDriver. 123 // 124 enum author_command_type { 125 AUTHOR_INIT = 1, 126 AUTHOR_SET_CAMERA, 127 AUTHOR_SET_VIDEO_SOURCE, 128 AUTHOR_SET_AUDIO_SOURCE, 129 AUTHOR_SET_OUTPUT_FORMAT, 130 AUTHOR_SET_VIDEO_ENCODER, 131 AUTHOR_SET_AUDIO_ENCODER, 132 AUTHOR_SET_VIDEO_SIZE, 133 AUTHOR_SET_VIDEO_FRAME_RATE, 134 AUTHOR_SET_PREVIEW_SURFACE, 135 AUTHOR_SET_OUTPUT_FILE, 136 AUTHOR_SET_PARAMETERS, 137 AUTHOR_PREPARE, 138 AUTHOR_START, 139 AUTHOR_STOP, 140 AUTHOR_RESET, 141 AUTHOR_CLOSE, 142 AUTHOR_REMOVE_VIDEO_SOURCE, 143 AUTHOR_REMOVE_AUDIO_SOURCE, 144 AUTHOR_QUIT = 100 145 }; 146 147 struct author_command 148 { 149 author_command(author_command_type which) { 150 this->which = which; 151 } 152 153 virtual ~author_command() {} 154 155 author_command_type which; 156 media_completion_f comp; 157 void *cookie; 158 }; 159 160 struct set_audio_source_command : author_command 161 { 162 set_audio_source_command() : author_command(AUTHOR_SET_AUDIO_SOURCE) {}; 163 audio_source as; 164 }; 165 166 struct set_video_source_command : author_command 167 { 168 set_video_source_command() : author_command(AUTHOR_SET_VIDEO_SOURCE) {}; 169 video_source vs; 170 }; 171 172 struct set_output_format_command : author_command 173 { 174 set_output_format_command() : author_command(AUTHOR_SET_OUTPUT_FORMAT) {}; 175 output_format of; 176 }; 177 178 struct set_audio_encoder_command : author_command 179 { 180 set_audio_encoder_command() : author_command(AUTHOR_SET_AUDIO_ENCODER) {}; 181 audio_encoder ae; 182 }; 183 184 struct set_video_encoder_command : author_command 185 { 186 set_video_encoder_command() : author_command(AUTHOR_SET_VIDEO_ENCODER) {}; 187 video_encoder ve; 188 }; 189 190 struct set_output_file_command : author_command 191 { 192 set_output_file_command() : author_command(AUTHOR_SET_OUTPUT_FILE) {}; 193 int fd; 194 int64_t offset; 195 int64_t length; 196 }; 197 struct set_video_size_command : author_command 198 { 199 set_video_size_command() : author_command(AUTHOR_SET_VIDEO_SIZE) {}; 200 int width; 201 int height; 202 }; 203 204 struct set_video_frame_rate_command : author_command 205 { 206 set_video_frame_rate_command() : author_command(AUTHOR_SET_VIDEO_FRAME_RATE) {}; 207 int rate; 208 }; 209 210 struct set_preview_surface_command : author_command 211 { 212 set_preview_surface_command() : author_command(AUTHOR_SET_PREVIEW_SURFACE) {}; 213 sp<ISurface> surface; 214 }; 215 216 struct set_camera_command : author_command 217 { 218 set_camera_command() : author_command(AUTHOR_SET_CAMERA) {}; 219 sp<ICamera> camera; 220 }; 221 222 struct set_parameters_command : author_command 223 { 224 set_parameters_command(const String8& params) 225 : author_command(AUTHOR_SET_PARAMETERS), 226 mParams(params) { 227 } 228 229 const String8& params() const { return mParams; } 230 231 private: 232 String8 mParams; 233 234 // Disallow copying and assignment. 235 set_parameters_command(const set_parameters_command&); 236 set_parameters_command& operator=(const set_parameters_command&); 237 }; 238 239 class MediaProfiles; 240 241 class AuthorDriver : 242 public OsclActiveObject, 243 public PVCommandStatusObserver, 244 public PVInformationalEventObserver, 245 public PVErrorEventObserver 246 { 247 public: 248 AuthorDriver(); 249 ~AuthorDriver(); 250 251 author_command *dequeueCommand(); 252 status_t enqueueCommand(author_command *ec, media_completion_f comp, void *cookie); 253 254 // Dequeues a command from MediaRecorder and gives it to PVAuthorEngine. 255 void Run(); 256 257 // Handlers for the various commands we can accept. 258 void commandFailed(author_command *ac); 259 void handleInit(author_command *ac); 260 void handleSetAudioSource(set_audio_source_command *ac); 261 void handleSetCamera(set_camera_command *ac); 262 void handleSetVideoSource(set_video_source_command *ac); 263 void handleSetOutputFormat(set_output_format_command *ac); 264 void handleSetAudioEncoder(set_audio_encoder_command *ac); 265 void handleSetVideoEncoder(set_video_encoder_command *ac); 266 void handleSetVideoSize(set_video_size_command *ac); 267 void handleSetVideoFrameRate(set_video_frame_rate_command *ac); 268 void handleSetPreviewSurface(set_preview_surface_command *ac); 269 void handleSetOutputFile(set_output_file_command *ac); 270 void handleSetParameters(set_parameters_command *ac); 271 void handlePrepare(author_command *ac); 272 void handleStart(author_command *ac); 273 void handleStop(author_command *ac); 274 void handleReset(author_command *ac); 275 void handleClose(author_command *ac); 276 void handleQuit(author_command *ac); 277 278 void endOfData(); 279 280 void CommandCompleted(const PVCmdResponse& aResponse); 281 void HandleErrorEvent(const PVAsyncErrorEvent& aEvent); 282 void HandleInformationalEvent(const PVAsyncInformationalEvent& aEvent); 283 284 status_t getMaxAmplitude(int *max); 285 PVAEState getAuthorEngineState(); 286 status_t setListener(const sp<IMediaPlayerClient>& listener); 287 288 private: 289 // Finish up a non-async command in such a way that 290 // the event loop will keep running. 291 void FinishNonAsyncCommand(author_command *ec); 292 293 // remove references to configurations 294 void removeConfigRefs(author_command *ac); 295 296 // remove input video or audio source 297 void handleRemoveVideoSource(author_command *ac); 298 void handleRemoveAudioSource(author_command *ac); 299 300 // Release resources acquired in a recording session 301 // Can be called only in the IDLE state of the authoring engine 302 void doCleanUp(); 303 304 // Starts the PV scheduler thread. 305 static int startAuthorThread(void *cookie); 306 int authorThread(); 307 308 static void media_track_added(status_t s, void *cookie); 309 310 // Callback for synchronous commands. 311 static void syncCompletion(status_t s, void *cookie); 312 313 // Limit either the duration of the recording or the resulting file size 314 // If "limit_is_duration" is true, "limit" holds the maximum duration in 315 // milliseconds, otherwise "limit" holds the maximum filesize in bytes. 316 PVMFStatus setMaxDurationOrFileSize(int64_t limit, bool limit_is_duration); 317 318 // Used to set the sampling rate of the audio source 319 PVMFStatus setParamAudioSamplingRate(int64_t aSamplingRate); 320 321 // Used to set the number of channels of the audio source 322 PVMFStatus setParamAudioNumberOfChannels(int64_t aNumberOfChannels); 323 324 // Used for setting the audio encoding bitrate 325 PVMFStatus setParamAudioEncodingBitrate(int64_t aAudioBitrate); 326 327 PVMFStatus setParameter(const String8 &key, const String8 &value); 328 329 // Has no effect if called after video encoder is set 330 PVMFStatus setParamVideoEncodingBitrate(int64_t aVideoBitrate); 331 332 // Clips the intended video encoding bit rate, frame rate, frame size 333 // (width and height) so that it is within the supported range. 334 void clipVideoBitrate(); 335 void clipVideoFrameRate(); 336 void clipVideoFrameSize(); 337 void clipVideoFrameWidth(); 338 void clipVideoFrameHeight(); 339 340 // Clips the intended AAC audio bitrate so that it is in the supported range 341 void clipAACAudioBitrate(); 342 343 // Used to map the incoming bitrate to the closest AMR bitrate 344 bool MapAMRBitrate(int32 aAudioBitrate, PVMF_GSMAMR_Rate &anAMRBitrate); 345 346 PVAuthorEngineInterface *mAuthor; 347 348 PvmiMIOControl *mVideoInputMIO; 349 PVMFNodeInterface *mVideoNode; 350 sp<AndroidAudioInput> mAudioInputMIO; 351 PVMFNodeInterface *mAudioNode; 352 353 void *mSelectedComposer; 354 PVInterface *mComposerConfig; 355 PVInterface *mVideoEncoderConfig; 356 PVInterface *mAudioEncoderConfig; 357 358 int mVideoWidth; 359 int mVideoHeight; 360 int mVideoFrameRate; 361 //int mVideoBitRate; 362 video_encoder mVideoEncoder; 363 output_format mOutputFormat; 364 365 //int mAudioBitRate; 366 audio_encoder mAudioEncoder; 367 368 // Semaphore used for synchronous commands. 369 OsclSemaphore *mSyncSem; 370 // Status cached by syncCompletion for synchronous commands. 371 status_t mSyncStatus; 372 373 // Command queue and its lock. 374 List<author_command *> mCommandQueue; 375 Mutex mQueueLock; 376 377 sp<ICamera> mCamera; 378 sp<IMediaPlayerClient> mListener; 379 380 int32 mSamplingRate; 381 int32 mNumberOfChannels; 382 int32 mAudio_bitrate_setting; 383 int32 mVideo_bitrate_setting; 384 385 FILE* ifpOutput; 386 MediaProfiles *mMediaProfiles; 387 }; 388 389 class AuthorDriverWrapper 390 { 391 public: 392 AuthorDriverWrapper(); 393 ~AuthorDriverWrapper(); 394 status_t enqueueCommand(author_command *ec, media_completion_f comp, void *cookie); 395 status_t getMaxAmplitude(int *max); 396 status_t setListener(const sp<IMediaPlayerClient>& listener); 397 398 private: 399 void resetAndClose(); 400 401 AuthorDriver *mAuthorDriver; 402 }; 403 404 }; // namespace android 405 406 #endif // _AUTHORDRIVER_PRIV_H 407 408