niklase@google.com | 77ae29b | 2011-05-30 11:22:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | // testAPI.cpp : Defines the entry point for the console application. |
| 12 | // |
| 13 | // NOTES: |
| 14 | // 1. MediaFile library and testAPI.cpp must be built in DEBUG mode for testing. |
| 15 | // |
| 16 | |
| 17 | #include <iostream> |
| 18 | #include <stdio.h> |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #ifdef WIN32 |
| 22 | #include <windows.h> |
| 23 | #include <tchar.h> |
| 24 | #endif |
| 25 | |
| 26 | #include "common_types.h" |
| 27 | #include "trace.h" |
| 28 | |
| 29 | #include "Engineconfigurations.h" |
| 30 | #include "media_file.h" |
| 31 | #include "file_player.h" |
| 32 | #include "file_recorder.h" |
| 33 | |
| 34 | |
| 35 | bool notify = false, playing = false, recording = false; |
| 36 | |
| 37 | // callback class for FileModule |
| 38 | class MyFileModuleCallback : public FileCallback |
| 39 | { |
| 40 | public: |
| 41 | virtual void PlayNotification( const WebRtc_Word32 id, |
| 42 | const WebRtc_UWord32 durationMs ) |
| 43 | { |
| 44 | printf("\tReceived PlayNotification from module %ld, durationMs = %ld\n", |
| 45 | id, durationMs); |
| 46 | notify = true; |
| 47 | }; |
| 48 | |
| 49 | virtual void RecordNotification( const WebRtc_Word32 id, |
| 50 | const WebRtc_UWord32 durationMs ) |
| 51 | { |
| 52 | printf("\tReceived RecordNotification from module %ld, durationMs = %ld\n", |
| 53 | id, durationMs); |
| 54 | notify = true; |
| 55 | }; |
| 56 | |
| 57 | virtual void PlayFileEnded(const WebRtc_Word32 id) |
| 58 | { |
| 59 | printf("\tReceived PlayFileEnded notification from module %ld.\n", id); |
| 60 | playing = false; |
| 61 | }; |
| 62 | |
| 63 | virtual void RecordFileEnded(const WebRtc_Word32 id) |
| 64 | { |
| 65 | printf("\tReceived RecordFileEnded notification from module %ld.\n", id); |
| 66 | recording = false; |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | // main test app |
| 71 | #ifdef WIN32 |
| 72 | int _tmain(int argc, _TCHAR* argv[]) |
| 73 | #else |
| 74 | int main(int /*argc*/, char** /*argv*/) |
| 75 | #endif |
| 76 | { |
| 77 | Trace::CreateTrace(); |
| 78 | Trace::SetTraceFile("testTrace.txt"); |
| 79 | Trace::SetEncryptedTraceFile("testTraceDebug.txt"); |
| 80 | |
| 81 | int playId = 1; |
| 82 | int recordId = 2; |
| 83 | |
| 84 | printf("Welcome to test of FilePlayer and FileRecorder\n"); |
| 85 | |
| 86 | |
| 87 | /////////////////////////////////////////////// |
| 88 | // |
| 89 | // avi test case 1 |
| 90 | // |
| 91 | /////////////////////////////////////////////// |
| 92 | |
| 93 | |
| 94 | // todo PW we need more AVI tests Mp4 |
| 95 | |
| 96 | { |
| 97 | FilePlayer& filePlayer(*FilePlayer::CreateFilePlayer(1, webrtc::kFileFormatAviFile)); |
| 98 | FileRecorder& fileRecorder(*FileRecorder::CreateFileRecorder(1, webrtc::kFileFormatAviFile)); |
| 99 | |
| 100 | const char* KFileName = "./tmpAviFileTestCase1_audioI420CIF30fps.avi"; |
| 101 | |
| 102 | printf("\tReading from an avi file and writing the information to another \n"); |
| 103 | printf("\tin the same format (I420 CIF 30fps) \n"); |
| 104 | printf("\t\t check file named %s\n", KFileName); |
| 105 | |
| 106 | assert(filePlayer.StartPlayingVideoFile( |
| 107 | "../../../MediaFile/main/test/files/aviTestCase1_audioI420CIF30fps.avi", |
| 108 | false, false) == 0); |
| 109 | |
| 110 | // init codecs |
| 111 | webrtc::VideoCodec videoCodec; |
| 112 | webrtc::VideoCodec recVideoCodec; |
| 113 | webrtc::CodecInst audioCodec; |
| 114 | assert(filePlayer.VideoCodec( videoCodec ) == 0); |
| 115 | assert(filePlayer.AudioCodec( audioCodec) == 0); |
| 116 | |
| 117 | recVideoCodec = videoCodec; |
| 118 | |
| 119 | assert( fileRecorder.StartRecordingVideoFile(KFileName, |
| 120 | audioCodec, |
| 121 | recVideoCodec) == 0); |
| 122 | |
| 123 | assert(fileRecorder.IsRecording()); |
| 124 | |
| 125 | WebRtc_UWord32 videoReadSize = static_cast<WebRtc_UWord32>( (videoCodec.width * videoCodec.height * 3.0) / 2.0); |
| 126 | |
| 127 | webrtc::VideoFrame videoFrame; |
| 128 | videoFrame.VerifyAndAllocate(videoReadSize); |
| 129 | |
| 130 | int frameCount = 0; |
| 131 | bool audioNotDone = true; |
| 132 | bool videoNotDone = true; |
| 133 | AudioFrame audioFrame; |
| 134 | |
| 135 | while( audioNotDone || videoNotDone) |
| 136 | { |
| 137 | if(filePlayer.TimeUntilNextVideoFrame() <= 0) |
| 138 | { |
| 139 | if(filePlayer.GetVideoFromFile( videoFrame) != 0) |
| 140 | { |
| 141 | // no more video frames |
| 142 | break; |
| 143 | } |
| 144 | frameCount++; |
| 145 | videoNotDone = ( videoFrame.Length() > 0); |
| 146 | videoFrame.SetRenderTime(TickTime::MillisecondTimestamp()); |
| 147 | if( videoNotDone) |
| 148 | { |
| 149 | assert(fileRecorder.RecordVideoToFile(videoFrame) == 0); |
| 150 | ::Sleep(10); |
| 151 | } |
| 152 | } |
| 153 | WebRtc_UWord32 decodedDataLengthInSamples; |
| 154 | if( 0 != filePlayer.Get10msAudioFromFile( audioFrame._payloadData, decodedDataLengthInSamples, audioCodec.plfreq)) |
| 155 | { |
| 156 | audioNotDone = false; |
| 157 | } else |
| 158 | { |
| 159 | audioFrame._frequencyInHz = filePlayer.Frequency(); |
| 160 | audioFrame._payloadDataLengthInSamples = (WebRtc_UWord16)decodedDataLengthInSamples; |
| 161 | fileRecorder.RecordAudioToFile(audioFrame, &TickTime::Now()); |
| 162 | } |
| 163 | } |
| 164 | ::Sleep(100); |
| 165 | assert(fileRecorder.StopRecording() == 0); |
| 166 | assert( !fileRecorder.IsRecording()); |
| 167 | assert(frameCount == 135); |
| 168 | printf("\tGenerated %s\n\n", KFileName); |
| 169 | } |
| 170 | /////////////////////////////////////////////// |
| 171 | // |
| 172 | // avi test case 2 |
| 173 | // |
| 174 | /////////////////////////////////////////////// |
| 175 | { |
| 176 | FilePlayer& filePlayer(*FilePlayer::CreateFilePlayer(2, webrtc::kFileFormatAviFile)); |
| 177 | FileRecorder& fileRecorder(*FileRecorder::CreateFileRecorder(2, webrtc::kFileFormatAviFile)); |
| 178 | |
| 179 | const char* KFileName = "./tmpAviFileTestCase2_audioI420CIF20fps.avi"; |
| 180 | |
| 181 | printf("\tWriting information to a avi file and check the written file by \n"); |
| 182 | printf("\treopening it and control codec information.\n"); |
| 183 | printf("\t\t check file named %s all frames should be light green.\n", KFileName); |
| 184 | // init codecs |
| 185 | webrtc::VideoCodec videoCodec; |
| 186 | webrtc::CodecInst audioCodec; |
| 187 | |
| 188 | memset(&videoCodec, 0, sizeof(videoCodec)); |
| 189 | |
| 190 | const char* KVideoCodecName = "I420"; |
| 191 | strcpy(videoCodec.plName, KVideoCodecName); |
| 192 | videoCodec.plType = 124; |
| 193 | videoCodec.maxFramerate = 20; |
| 194 | videoCodec.height = 288; |
| 195 | videoCodec.width = 352; |
| 196 | |
| 197 | const char* KAudioCodecName = "PCMU"; |
| 198 | strcpy(audioCodec.plname, KAudioCodecName); |
| 199 | audioCodec.pltype = 0; |
| 200 | audioCodec.plfreq = 8000; |
| 201 | audioCodec.pacsize = 80; |
| 202 | audioCodec.channels = 1; |
| 203 | audioCodec.rate = 64000; |
| 204 | |
| 205 | assert( fileRecorder.StartRecordingVideoFile( |
| 206 | KFileName, |
| 207 | audioCodec, |
| 208 | videoCodec) == 0); |
| 209 | |
| 210 | assert(fileRecorder.IsRecording()); |
| 211 | |
| 212 | const WebRtc_UWord32 KVideoWriteSize = static_cast< WebRtc_UWord32>( (videoCodec.width * videoCodec.height * 3) / 2); |
| 213 | webrtc::VideoFrame videoFrame; |
| 214 | |
| 215 | // 10 ms |
| 216 | AudioFrame audioFrame; |
| 217 | audioFrame._payloadDataLengthInSamples = audioCodec.plfreq/100; |
| 218 | memset(audioFrame._payloadData, 0, 2*audioFrame._payloadDataLengthInSamples); |
| 219 | audioFrame._frequencyInHz = 8000; |
| 220 | |
| 221 | // prepare the video frame |
| 222 | videoFrame.VerifyAndAllocate(KVideoWriteSize); |
| 223 | memset(videoFrame.Buffer(), 127, videoCodec.width * videoCodec.height); |
| 224 | memset(videoFrame.Buffer() +(videoCodec.width * videoCodec.height), 0, videoCodec.width * videoCodec.height/2); |
| 225 | videoFrame.SetLength(KVideoWriteSize); |
| 226 | videoFrame.SetHeight(videoCodec.height); |
| 227 | videoFrame.SetWidth(videoCodec.width); |
| 228 | |
| 229 | // write avi file, with 20 video frames |
| 230 | const int KWriteNumFrames = 20; |
| 231 | int writeFrameCount = 0; |
| 232 | while(writeFrameCount < KWriteNumFrames) |
| 233 | { |
| 234 | // add a video frame |
| 235 | assert(fileRecorder.RecordVideoToFile(videoFrame) == 0); |
| 236 | |
| 237 | // add 50 ms of audio |
| 238 | for(int i=0; i<5; i++) |
| 239 | { |
| 240 | assert( fileRecorder.RecordAudioToFile(audioFrame) == 0); |
| 241 | }// for i |
| 242 | writeFrameCount++; |
| 243 | } |
| 244 | ::Sleep(10); // enough tim eto write the queued data to the file |
| 245 | assert(writeFrameCount == 20); |
| 246 | assert(fileRecorder.StopRecording() == 0); |
| 247 | assert( ! fileRecorder.IsRecording()); |
| 248 | |
| 249 | assert(filePlayer.StartPlayingVideoFile(KFileName,false, false) == 0); |
| 250 | assert(filePlayer.IsPlayingFile( )); |
| 251 | |
| 252 | // compare codecs read from file to the ones used when writing the file |
| 253 | webrtc::VideoCodec readVideoCodec; |
| 254 | assert(filePlayer.VideoCodec( readVideoCodec ) == 0); |
| 255 | assert(strcmp(readVideoCodec.plName, videoCodec.plName) == 0); |
| 256 | assert(readVideoCodec.width == videoCodec.width); |
| 257 | assert(readVideoCodec.height == videoCodec.height); |
| 258 | assert(readVideoCodec.maxFramerate == videoCodec.maxFramerate); |
| 259 | |
| 260 | webrtc::CodecInst readAudioCodec; |
| 261 | assert(filePlayer.AudioCodec( readAudioCodec) == 0); |
| 262 | assert(strcmp(readAudioCodec.plname, audioCodec.plname) == 0); |
| 263 | assert(readAudioCodec.pltype == audioCodec.pltype); |
| 264 | assert(readAudioCodec.plfreq == audioCodec.plfreq); |
| 265 | assert(readAudioCodec.pacsize == audioCodec.pacsize); |
| 266 | assert(readAudioCodec.channels == audioCodec.channels); |
| 267 | assert(readAudioCodec.rate == audioCodec.rate); |
| 268 | |
| 269 | assert(filePlayer.StopPlayingFile() == 0); |
| 270 | assert( ! filePlayer.IsPlayingFile()); |
| 271 | printf("\tGenerated %s\n\n", KFileName); |
| 272 | } |
| 273 | /////////////////////////////////////////////// |
| 274 | // |
| 275 | // avi test case 3 |
| 276 | // |
| 277 | /////////////////////////////////////////////// |
| 278 | |
| 279 | { |
| 280 | FilePlayer& filePlayer(*FilePlayer::CreateFilePlayer(2, webrtc::kFileFormatAviFile)); |
| 281 | FileRecorder& fileRecorder(*FileRecorder::CreateFileRecorder(3, webrtc::kFileFormatAviFile)); |
| 282 | |
| 283 | printf("\tReading from an avi file and writing the information to another \n"); |
| 284 | printf("\tin a different format (H.263 CIF 30fps) \n"); |
| 285 | printf("\t\t check file named tmpAviFileTestCase1_audioH263CIF30fps.avi\n"); |
| 286 | |
| 287 | assert(filePlayer.StartPlayingVideoFile( |
| 288 | "../../../MediaFile/main/test/files/aviTestCase1_audioI420CIF30fps.avi", |
| 289 | false, |
| 290 | false) == 0); |
| 291 | |
| 292 | // init codecs |
| 293 | webrtc::VideoCodec videoCodec; |
| 294 | webrtc::VideoCodec recVideoCodec; |
| 295 | webrtc::CodecInst audioCodec; |
| 296 | assert(filePlayer.VideoCodec( videoCodec ) == 0); |
| 297 | assert(filePlayer.AudioCodec( audioCodec) == 0); |
| 298 | recVideoCodec = videoCodec; |
| 299 | |
| 300 | memcpy(recVideoCodec.plName, "H263",5); |
| 301 | recVideoCodec.startBitrate = 1000; |
| 302 | recVideoCodec.codecSpecific.H263.quality = 1; |
| 303 | recVideoCodec.plType = 34; |
| 304 | recVideoCodec.codecType = webrtc::kVideoCodecH263; |
| 305 | |
| 306 | assert( fileRecorder.StartRecordingVideoFile( |
| 307 | "./tmpAviFileTestCase1_audioH263CIF30fps.avi", |
| 308 | audioCodec, |
| 309 | recVideoCodec) == 0); |
| 310 | |
| 311 | assert(fileRecorder.IsRecording()); |
| 312 | |
| 313 | WebRtc_UWord32 videoReadSize = static_cast<WebRtc_UWord32>( (videoCodec.width * videoCodec.height * 3.0) / 2.0); |
| 314 | |
| 315 | webrtc::VideoFrame videoFrame; |
| 316 | videoFrame.VerifyAndAllocate(videoReadSize); |
| 317 | |
| 318 | int videoFrameCount = 0; |
| 319 | int audioFrameCount = 0; |
| 320 | bool audioNotDone = true; |
| 321 | bool videoNotDone = true; |
| 322 | AudioFrame audioFrame; |
| 323 | |
| 324 | while( audioNotDone || videoNotDone) |
| 325 | { |
| 326 | if(filePlayer.TimeUntilNextVideoFrame() <= 0) |
| 327 | { |
| 328 | if(filePlayer.GetVideoFromFile( videoFrame) != 0) |
| 329 | { |
| 330 | break; |
| 331 | } |
| 332 | videoFrameCount++; |
| 333 | videoNotDone = ( videoFrame.Length() > 0); |
| 334 | if( videoNotDone) |
| 335 | { |
| 336 | assert(fileRecorder.RecordVideoToFile(videoFrame) == 0); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | WebRtc_UWord32 decodedDataLengthInSamples; |
| 341 | if( 0 != filePlayer.Get10msAudioFromFile( audioFrame._payloadData, decodedDataLengthInSamples, audioCodec.plfreq)) |
| 342 | { |
| 343 | audioNotDone = false; |
| 344 | |
| 345 | } else |
| 346 | { |
| 347 | ::Sleep(5); |
| 348 | audioFrame._frequencyInHz = filePlayer.Frequency(); |
| 349 | audioFrame._payloadDataLengthInSamples = (WebRtc_UWord16)decodedDataLengthInSamples; |
| 350 | assert(0 == fileRecorder.RecordAudioToFile(audioFrame)); |
| 351 | |
| 352 | audioFrameCount++; |
| 353 | } |
| 354 | } |
| 355 | assert(videoFrameCount == 135); |
| 356 | assert(audioFrameCount == 446); // we will start & stop with a video frame |
| 357 | |
| 358 | assert(fileRecorder.StopRecording() == 0); |
| 359 | assert( !fileRecorder.IsRecording()); |
| 360 | printf("\tGenerated ./tmpAviFileTestCase1_audioH263CIF30fps.avi\n\n"); |
| 361 | } |
| 362 | |
| 363 | |
| 364 | printf("\nTEST completed.\n"); |
| 365 | |
| 366 | Trace::ReturnTrace(); |
| 367 | return 0; |
| 368 | } |