summaryrefslogtreecommitdiff
path: root/video_engine/test/auto_test/source/vie_autotest_record.cc
blob: 1fb11ad141e7e471b0ada617ae150bcda3d50ae2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

//
// vie_autotest_record.cc
//
// This code is also used as sample code for ViE 3.0
//

#include <fstream>
#include <stdio.h>

#include "webrtc/common_types.h"
#include "webrtc/system_wrappers/interface/tick_util.h"
#include "webrtc/test/channel_transport/include/channel_transport.h"
#include "webrtc/video_engine/include/vie_base.h"
#include "webrtc/video_engine/include/vie_capture.h"
#include "webrtc/video_engine/include/vie_codec.h"
#include "webrtc/video_engine/include/vie_network.h"
#include "webrtc/video_engine/include/vie_render.h"
#include "webrtc/video_engine/include/vie_rtp_rtcp.h"
#include "webrtc/video_engine/test/auto_test/interface/vie_autotest.h"
#include "webrtc/video_engine/test/auto_test/interface/vie_autotest_defines.h"
#include "webrtc/video_engine/test/libvietest/include/tb_external_transport.h"
#include "webrtc/voice_engine/include/voe_base.h"
#include "webrtc/voice_engine/include/voe_network.h"
#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"

#define VCM_RED_PAYLOAD_TYPE            96
#define VCM_ULPFEC_PAYLOAD_TYPE         97
#define DEFAULT_AUDIO_PORT              11113
#define DEFAULT_AUDIO_CODEC             "ISAC"
#define DEFAULT_VIDEO_CODEC_WIDTH       640
#define DEFAULT_VIDEO_CODEC_HEIGHT      480
#define DEFAULT_VIDEO_CODEC_START_RATE  1000
#define DEFAULT_RECORDING_FOLDER        "RECORDING"
#define DEFAULT_RECORDING_AUDIO         "/audio_debug.aec"
#define DEFAULT_RECORDING_VIDEO         "/video_debug.yuv"
#define DEFAULT_RECORDING_AUDIO_RTP     "/audio_rtpdump.rtp"
#define DEFAULT_RECORDING_VIDEO_RTP     "/video_rtpdump.rtp"

bool GetAudioDevices(webrtc::VoEBase* voe_base,
                     webrtc::VoEHardware* voe_hardware,
                     char* recording_device_name,
                     int& recording_device_index,
                     char* playbackDeviceName,
                     int& playback_device_index);
bool GetAudioCodecRecord(webrtc::VoECodec* voe_codec,
                         webrtc::CodecInst& audio_codec);

int VideoEngineSampleRecordCode(void* window1, void* window2) {
  int error = 0;
  // Audio settings.
  int audio_tx_port = DEFAULT_AUDIO_PORT;
  int audio_rx_port = DEFAULT_AUDIO_PORT;
  webrtc::CodecInst audio_codec;
  int audio_channel = -1;
  int audio_capture_device_index = -1;
  int audio_playback_device_index = -1;
  const unsigned int KMaxDeviceNameLength = 128;
  const unsigned int KMaxUniqueIdLength = 256;
  char deviceName[KMaxDeviceNameLength];
  char audio_capture_device_name[KMaxUniqueIdLength] = "";
  char audio_playbackDeviceName[KMaxUniqueIdLength] = "";

  // Network settings.
  const char* ipAddress = "127.0.0.1";
  const int rtpPort = 6000;

  //
  // Create a VideoEngine instance
  //
  webrtc::VideoEngine* ptrViE = NULL;
  ptrViE = webrtc::VideoEngine::Create();
  if (ptrViE == NULL) {
    printf("ERROR in VideoEngine::Create\n");
    return -1;
  }

  error = ptrViE->SetTraceFilter(webrtc::kTraceAll);
  if (error == -1) {
    printf("ERROR in VideoEngine::SetTraceLevel\n");
    return -1;
  }

  std::string trace_file =
    ViETest::GetResultOutputPath() + "ViERecordCall_trace.txt";
  error = ptrViE->SetTraceFile(trace_file.c_str());
  if (error == -1) {
    printf("ERROR in VideoEngine::SetTraceFile\n");
    return -1;
  }

  //
  // Create a VoE instance
  //
  webrtc::VoiceEngine* voe = webrtc::VoiceEngine::Create();
  //
  // Init VideoEngine and create a channel
  //
  webrtc::ViEBase* ptrViEBase = webrtc::ViEBase::GetInterface(ptrViE);
  if (ptrViEBase == NULL) {
    printf("ERROR in ViEBase::GetInterface\n");
    return -1;
  }

  error = ptrViEBase->Init();
  if (error == -1) {
    printf("ERROR in ViEBase::Init\n");
    return -1;
  }

  webrtc::VoEBase* voe_base = webrtc::VoEBase::GetInterface(voe);
  if (voe_base == NULL) {
    printf("ERROR in VoEBase::GetInterface\n");
    return -1;
  }
  error = voe_base->Init();
  if (error == -1) {
    printf("ERROR in VoEBase::Init\n");
    return -1;
  }

  int videoChannel = -1;
  error = ptrViEBase->CreateChannel(videoChannel);
  if (error == -1) {
    printf("ERROR in ViEBase::CreateChannel\n");
    return -1;
  }

  webrtc::VoEHardware* voe_hardware =
    webrtc::VoEHardware::GetInterface(voe);
  webrtc::VoECodec* voe_codec = webrtc::VoECodec::GetInterface(voe);
  webrtc::VoEAudioProcessing* voe_apm =
       webrtc::VoEAudioProcessing::GetInterface(voe);
  webrtc::VoENetwork* voe_network =
    webrtc::VoENetwork::GetInterface(voe);

  // Get the audio device for the call.
  memset(audio_capture_device_name, 0, KMaxUniqueIdLength);
  memset(audio_playbackDeviceName, 0, KMaxUniqueIdLength);
  GetAudioDevices(voe_base, voe_hardware, audio_capture_device_name,
                  audio_capture_device_index, audio_playbackDeviceName,
                  audio_playback_device_index);

  // Get the audio codec for the call.
  memset(static_cast<void*>(&audio_codec), 0, sizeof(audio_codec));
  GetAudioCodecRecord(voe_codec, audio_codec);

  audio_channel = voe_base->CreateChannel();

  webrtc::scoped_ptr<webrtc::test::VoiceChannelTransport>
      voice_channel_transport(
          new webrtc::test::VoiceChannelTransport(voe_network, audio_channel));

  voice_channel_transport->SetSendDestination(ipAddress, audio_tx_port);
  voice_channel_transport->SetLocalReceiver(audio_rx_port);

  voe_hardware->SetRecordingDevice(audio_capture_device_index);
  voe_hardware->SetPlayoutDevice(audio_playback_device_index);
  voe_codec->SetSendCodec(audio_channel, audio_codec);
  voe_apm->SetAgcStatus(true, webrtc::kAgcDefault);
  voe_apm->SetNsStatus(true, webrtc::kNsHighSuppression);

  //
  // List available capture devices, allocate and connect.
  //
  webrtc::ViECapture* ptrViECapture =
      webrtc::ViECapture::GetInterface(ptrViE);
  if (ptrViECapture == NULL) {
    printf("ERROR in ViECapture::GetInterface\n");
    return -1;
  }

  webrtc::VoERTP_RTCP* ptrVoERtpRtcp =
    webrtc::VoERTP_RTCP::GetInterface(voe);
  if (ptrVoERtpRtcp == NULL) {
    printf("ERROR in VoERTP_RTCP::GetInterface\n");
    return -1;
  }

  memset(deviceName, 0, KMaxDeviceNameLength);
  char uniqueId[KMaxUniqueIdLength];
  memset(uniqueId, 0, KMaxUniqueIdLength);

  printf("Available capture devices:\n");
  int captureIdx = 0;
  for (captureIdx = 0;
       captureIdx < ptrViECapture->NumberOfCaptureDevices();
       captureIdx++) {
    memset(deviceName, 0, KMaxDeviceNameLength);
    memset(uniqueId, 0, KMaxUniqueIdLength);

    error = ptrViECapture->GetCaptureDevice(captureIdx, deviceName,
                                            KMaxDeviceNameLength, uniqueId,
                                            KMaxUniqueIdLength);
    if (error == -1) {
      printf("ERROR in ViECapture::GetCaptureDevice\n");
      return -1;
    }
    printf("\t %d. %s\n", captureIdx + 1, deviceName);
  }
  printf("\nChoose capture device: ");
#ifdef WEBRTC_ANDROID
  captureIdx = 0;
  printf("0\n");
#else
  if (scanf("%d", &captureIdx) != 1) {
    printf("Error in scanf()\n");
    return -1;
  }
  getchar();
  captureIdx = captureIdx - 1;  // Compensate for idx start at 1.
#endif
  error = ptrViECapture->GetCaptureDevice(captureIdx, deviceName,
                                          KMaxDeviceNameLength, uniqueId,
                                          KMaxUniqueIdLength);
  if (error == -1) {
    printf("ERROR in ViECapture::GetCaptureDevice\n");
    return -1;
  }

  int captureId = 0;
  error = ptrViECapture->AllocateCaptureDevice(uniqueId, KMaxUniqueIdLength,
                                               captureId);
  if (error == -1) {
    printf("ERROR in ViECapture::AllocateCaptureDevice\n");
    return -1;
  }

  error = ptrViECapture->ConnectCaptureDevice(captureId, videoChannel);
  if (error == -1) {
    printf("ERROR in ViECapture::ConnectCaptureDevice\n");
    return -1;
  }

  error = ptrViECapture->StartCapture(captureId);
  if (error == -1) {
    printf("ERROR in ViECapture::StartCapture\n");
    return -1;
  }

  //
  // RTP/RTCP settings
  //
  webrtc::ViERTP_RTCP* ptrViERtpRtcp =
      webrtc::ViERTP_RTCP::GetInterface(ptrViE);
  if (ptrViERtpRtcp == NULL) {
    printf("ERROR in ViERTP_RTCP::GetInterface\n");
    return -1;
  }

  error = ptrViERtpRtcp->SetRTCPStatus(videoChannel,
                                       webrtc::kRtcpCompound_RFC4585);
  if (error == -1) {
    printf("ERROR in ViERTP_RTCP::SetRTCPStatus\n");
    return -1;
  }

  error = ptrViERtpRtcp->SetKeyFrameRequestMethod(
      videoChannel, webrtc::kViEKeyFrameRequestPliRtcp);
  if (error == -1) {
    printf("ERROR in ViERTP_RTCP::SetKeyFrameRequestMethod\n");
    return -1;
  }

  error = ptrViERtpRtcp->SetRembStatus(videoChannel, true, true);
  if (error == -1) {
    printf("ERROR in ViERTP_RTCP::SetTMMBRStatus\n");
    return -1;
  }

  //
  // Set up rendering
  //
  webrtc::ViERender* ptrViERender = webrtc::ViERender::GetInterface(ptrViE);
  if (ptrViERender == NULL) {
    printf("ERROR in ViERender::GetInterface\n");
    return -1;
  }

  error = ptrViERender->AddRenderer(captureId, window1, 0, 0.0, 0.0, 1.0, 1.0);
  if (error == -1) {
    printf("ERROR in ViERender::AddRenderer\n");
    return -1;
  }

  error = ptrViERender->StartRender(captureId);
  if (error == -1) {
    printf("ERROR in ViERender::StartRender\n");
    return -1;
  }

  error = ptrViERender->AddRenderer(videoChannel, window2, 1, 0.0, 0.0, 1.0,
                                    1.0);
  if (error == -1) {
    printf("ERROR in ViERender::AddRenderer\n");
    return -1;
  }

  error = ptrViERender->StartRender(videoChannel);
  if (error == -1) {
    printf("ERROR in ViERender::StartRender\n");
    return -1;
  }

  //
  // Setup codecs
  //
  webrtc::ViECodec* ptrViECodec = webrtc::ViECodec::GetInterface(ptrViE);
  if (ptrViECodec == NULL) {
    printf("ERROR in ViECodec::GetInterface\n");
    return -1;
  }

  webrtc::VideoCodec videoCodec;
  memset(&videoCodec, 0, sizeof(webrtc::VideoCodec));
  int codecIdx = 0;

#ifdef WEBRTC_ANDROID
  codecIdx = 0;
  printf("0\n");
#else
  codecIdx = 0;  // Compensate for idx start at 1.
#endif

  error = ptrViECodec->GetCodec(codecIdx, videoCodec);
  if (error == -1) {
     printf("ERROR in ViECodec::GetCodec\n");
     return -1;
  }

  // Set spatial resolution option
  videoCodec.width = DEFAULT_VIDEO_CODEC_WIDTH;
  videoCodec.height = DEFAULT_VIDEO_CODEC_HEIGHT;

  // Set start bit rate
  videoCodec.startBitrate = DEFAULT_VIDEO_CODEC_START_RATE;

  error = ptrViECodec->SetSendCodec(videoChannel, videoCodec);
  if (error == -1) {
    printf("ERROR in ViECodec::SetSendCodec\n");
    return -1;
  }

  //
  // Address settings
  //
  webrtc::ViENetwork* ptrViENetwork =
      webrtc::ViENetwork::GetInterface(ptrViE);
  if (ptrViENetwork == NULL) {
    printf("ERROR in ViENetwork::GetInterface\n");
    return -1;
  }
  webrtc::test::VideoChannelTransport* video_channel_transport =
      new webrtc::test::VideoChannelTransport(ptrViENetwork, videoChannel);

  error = video_channel_transport->SetSendDestination(ipAddress, rtpPort);
  if (error == -1) {
    printf("ERROR in SetSendDestination\n");
    return -1;
  }
  error = video_channel_transport->SetLocalReceiver(rtpPort);
  if (error == -1) {
    printf("ERROR in SetLocalReceiver\n");
    return -1;
  }

  std::string str;
  int enable_labeling = 0;
  std::cout << std::endl;
  std::cout << "Do you want to label this recording?" << std::endl;
  std::cout << "0. No (default)." << std::endl;
  std::cout << "1. This call will be labeled on the fly." << std::endl;
  std::getline(std::cin, str);
  enable_labeling = atoi(str.c_str());

  uint32_t folder_time = static_cast<uint32_t>
    (webrtc::TickTime::MillisecondTimestamp());
  std::stringstream folder_time_str;
  folder_time_str <<  folder_time;
  const std::string folder_name = "recording" + folder_time_str.str();
  printf("recording name = %s\n", folder_name.c_str());
  // TODO(mikhal): use file_utils.
#ifdef WIN32
  _mkdir(folder_name.c_str());
#else
  mkdir(folder_name.c_str(), 0777);
#endif
  const std::string audio_filename =  folder_name + DEFAULT_RECORDING_AUDIO;
  const std::string video_filename =  folder_name + DEFAULT_RECORDING_VIDEO;
  const std::string audio_rtp_filename = folder_name +
    DEFAULT_RECORDING_AUDIO_RTP;
  const std::string video_rtp_filename = folder_name +
    DEFAULT_RECORDING_VIDEO_RTP;
  std::fstream timing;
  if (enable_labeling == 1) {
    std::cout << "Press enter to stamp current time."<< std::endl;
    std::string timing_file = folder_name + "/labeling.txt";
    timing.open(timing_file.c_str(), std::fstream::out | std::fstream::app);
  }
  printf("\nPress enter to start recording\n");
  std::getline(std::cin, str);
  printf("\nRecording started\n\n");

  error = ptrViEBase->StartReceive(videoChannel);
  if (error == -1) {
    printf("ERROR in ViENetwork::StartReceive\n");
    return -1;
  }

  error = ptrViEBase->StartSend(videoChannel);
  if (error == -1) {
    printf("ERROR in ViENetwork::StartSend\n");
    return -1;
  }
  error = voe_base->StartSend(audio_channel);
  if (error == -1) {
    printf("ERROR in VoENetwork::StartSend\n");
    return -1;
  }

  //  Engine started

  voe_apm->StartDebugRecording(audio_filename.c_str());
  ptrViECodec->StartDebugRecording(videoChannel, video_filename.c_str());
  ptrViERtpRtcp->StartRTPDump(videoChannel,
                              video_rtp_filename.c_str(), webrtc::kRtpOutgoing);
  ptrVoERtpRtcp->StartRTPDump(audio_channel,
                              audio_rtp_filename.c_str(), webrtc::kRtpOutgoing);
  printf("Press s + enter to stop...");
  int64_t clock_time;
  if (enable_labeling == 1) {
    clock_time = webrtc::TickTime::MillisecondTimestamp();
    timing << clock_time << std::endl;
  }
  char c = getchar();
  fflush(stdin);
  while (c != 's') {
    if (c == '\n' && enable_labeling == 1) {
      clock_time = webrtc::TickTime::MillisecondTimestamp();
      timing << clock_time << std::endl;
    }
    c = getchar();
  }
  if (enable_labeling == 1) {
    clock_time = webrtc::TickTime::MillisecondTimestamp();
    timing << clock_time << std::endl;
  }

  ptrViERtpRtcp->StopRTPDump(videoChannel, webrtc::kRtpOutgoing);
  ptrVoERtpRtcp->StopRTPDump(audio_channel, webrtc::kRtpOutgoing);
  voe_apm->StopDebugRecording();
  ptrViECodec->StopDebugRecording(videoChannel);
  if (enable_labeling == 1)
    timing.close();

  //  Recording finished. Tear down Video Engine.

  error = ptrViEBase->StopReceive(videoChannel);
  if (error == -1) {
    printf("ERROR in ViEBase::StopReceive\n");
    return -1;
  }

  error = ptrViEBase->StopSend(videoChannel);
  if (error == -1) {
    printf("ERROR in ViEBase::StopSend\n");
    return -1;
  }
  error = voe_base->StopSend(audio_channel);

  error = ptrViERender->StopRender(captureId);
  if (error == -1) {
    printf("ERROR in ViERender::StopRender\n");
    return -1;
  }

  error = ptrViERender->RemoveRenderer(captureId);
  if (error == -1) {
    printf("ERROR in ViERender::RemoveRenderer\n");
    return -1;
  }

  error = ptrViERender->StopRender(videoChannel);
  if (error == -1) {
    printf("ERROR in ViERender::StopRender\n");
    return -1;
  }

  error = ptrViERender->RemoveRenderer(videoChannel);
  if (error == -1) {
    printf("ERROR in ViERender::RemoveRenderer\n");
    return -1;
  }

  error = ptrViECapture->StopCapture(captureId);
  if (error == -1) {
    printf("ERROR in ViECapture::StopCapture\n");
    return -1;
  }

  error = ptrViECapture->DisconnectCaptureDevice(videoChannel);
  if (error == -1) {
    printf("ERROR in ViECapture::DisconnectCaptureDevice\n");
    return -1;
  }

  error = ptrViECapture->ReleaseCaptureDevice(captureId);
  if (error == -1) {
    printf("ERROR in ViECapture::ReleaseCaptureDevice\n");
    return -1;
  }

  error = ptrViEBase->DeleteChannel(videoChannel);
  if (error == -1) {
    printf("ERROR in ViEBase::DeleteChannel\n");
    return -1;
  }
  delete video_channel_transport;

  int remainingInterfaces = 0;
  remainingInterfaces = ptrViECodec->Release();
  remainingInterfaces += ptrViECapture->Release();
  remainingInterfaces += ptrViERtpRtcp->Release();
  remainingInterfaces += ptrViERender->Release();
  remainingInterfaces += ptrViENetwork->Release();
  remainingInterfaces += ptrViEBase->Release();
  if (remainingInterfaces > 0) {
    printf("ERROR: Could not release all interfaces\n");
    return -1;
  }
  bool deleted = webrtc::VideoEngine::Delete(ptrViE);
  if (deleted == false) {
    printf("ERROR in VideoEngine::Delete\n");
    return -1;
  }
  return 0;
}


// TODO(mikhal): Place above functionality under this class.
int ViEAutoTest::ViERecordCall() {
  ViETest::Log(" ");
  ViETest::Log("========================================");
  ViETest::Log(" ViE Record Call\n");

  if (VideoEngineSampleRecordCode(_window1, _window2) == 0) {
    ViETest::Log(" ");
    ViETest::Log(" ViE Autotest Record Call Done");
    ViETest::Log("========================================");
    ViETest::Log(" ");
    return 0;
  }

  ViETest::Log(" ");
  ViETest::Log(" ViE Autotest Record Call Failed");
  ViETest::Log("========================================");
  ViETest::Log(" ");
  return 1;
}

bool GetAudioCodecRecord(webrtc::VoECodec* voe_codec,
                         webrtc::CodecInst& audio_codec) {
  int error = 0;
  int number_of_errors = 0;
  memset(&audio_codec, 0, sizeof(webrtc::CodecInst));

  while (1) {
    int codec_idx = 0;
    int default_codec_idx = 0;
    for (codec_idx = 0; codec_idx < voe_codec->NumOfCodecs(); codec_idx++) {
      error = voe_codec->GetCodec(codec_idx, audio_codec);
      number_of_errors += ViETest::TestError(error == 0,
                                             "ERROR: %s at line %d",
                                             __FUNCTION__, __LINE__);

      // Test for default codec index.
      if (strcmp(audio_codec.plname, DEFAULT_AUDIO_CODEC) == 0) {
        default_codec_idx = codec_idx;
      }
    }
    error = voe_codec->GetCodec(default_codec_idx, audio_codec);
    number_of_errors += ViETest::TestError(error == 0,
                                           "ERROR: %s at line %d",
                                           __FUNCTION__, __LINE__);
    return true;
  }
  assert(false);
  return false;
}