summaryrefslogtreecommitdiffstats
path: root/media/cast/test/simulator.cc
blob: 135d44bb45c85c9193e08dd213b19c94dc8494f3 (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Simulate end to end streaming.
//
// Input:
// --source=
//   WebM used as the source of video and audio frames.
// --output=
//   File path to writing out the raw event log of the simulation session.
// --sim-id=
//   Unique simulation ID.
//
// Output:
// - Raw event log of the simulation session tagged with the unique test ID,
//   written out to the specified file path.

#include "base/at_exit.h"
#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/memory_mapped_file.h"
#include "base/files/scoped_file.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/tick_clock.h"
#include "base/values.h"
#include "media/base/audio_bus.h"
#include "media/base/media.h"
#include "media/base/video_frame.h"
#include "media/cast/cast_config.h"
#include "media/cast/cast_environment.h"
#include "media/cast/cast_receiver.h"
#include "media/cast/cast_sender.h"
#include "media/cast/logging/encoding_event_subscriber.h"
#include "media/cast/logging/log_serializer.h"
#include "media/cast/logging/logging_defines.h"
#include "media/cast/logging/proto/raw_events.pb.h"
#include "media/cast/logging/raw_event_subscriber_bundle.h"
#include "media/cast/logging/simple_event_subscriber.h"
#include "media/cast/net/cast_transport_config.h"
#include "media/cast/net/cast_transport_defines.h"
#include "media/cast/net/cast_transport_sender.h"
#include "media/cast/net/cast_transport_sender_impl.h"
#include "media/cast/test/fake_media_source.h"
#include "media/cast/test/fake_single_thread_task_runner.h"
#include "media/cast/test/loopback_transport.h"
#include "media/cast/test/proto/network_simulation_model.pb.h"
#include "media/cast/test/skewed_tick_clock.h"
#include "media/cast/test/utility/audio_utility.h"
#include "media/cast/test/utility/default_config.h"
#include "media/cast/test/utility/test_util.h"
#include "media/cast/test/utility/udp_proxy.h"
#include "media/cast/test/utility/video_utility.h"

using media::cast::proto::IPPModel;
using media::cast::proto::NetworkSimulationModel;
using media::cast::proto::NetworkSimulationModelType;

namespace media {
namespace cast {
namespace {
const int kTargetDelay = 300;
const char kSourcePath[] = "source";
const char kModelPath[] = "model";
const char kOutputPath[] = "output";
const char kSimulationId[] = "sim-id";
const char kLibDir[] = "lib-dir";

void UpdateCastTransportStatus(CastTransportStatus status) {
  LOG(INFO) << "Cast transport status: " << status;
}

void AudioInitializationStatus(CastInitializationStatus status) {
  LOG(INFO) << "Audio status: " << status;
}

void VideoInitializationStatus(CastInitializationStatus status) {
  LOG(INFO) << "Video status: " << status;
}

void LogTransportEvents(const scoped_refptr<CastEnvironment>& env,
                        const std::vector<PacketEvent>& packet_events,
                        const std::vector<FrameEvent>& frame_events) {
  for (std::vector<media::cast::PacketEvent>::const_iterator it =
           packet_events.begin();
       it != packet_events.end();
       ++it) {
    env->Logging()->InsertPacketEvent(it->timestamp,
                                      it->type,
                                      it->media_type,
                                      it->rtp_timestamp,
                                      it->frame_id,
                                      it->packet_id,
                                      it->max_packet_id,
                                      it->size);
  }
  for (std::vector<media::cast::FrameEvent>::const_iterator it =
           frame_events.begin();
       it != frame_events.end();
       ++it) {
    if (it->type == FRAME_PLAYOUT) {
      env->Logging()->InsertFrameEventWithDelay(
          it->timestamp,
          it->type,
          it->media_type,
          it->rtp_timestamp,
          it->frame_id,
          it->delay_delta);
    } else {
      env->Logging()->InsertFrameEvent(
          it->timestamp,
          it->type,
          it->media_type,
          it->rtp_timestamp,
          it->frame_id);
    }
  }
}

void GotVideoFrame(
    int* counter,
    CastReceiver* cast_receiver,
    const scoped_refptr<media::VideoFrame>& video_frame,
    const base::TimeTicks& render_time,
    bool continuous) {
  ++*counter;
  cast_receiver->RequestDecodedVideoFrame(
      base::Bind(&GotVideoFrame, counter, cast_receiver));
}

void GotAudioFrame(
    int* counter,
    CastReceiver* cast_receiver,
    scoped_ptr<AudioBus> audio_bus,
    const base::TimeTicks& playout_time,
    bool is_continuous) {
  ++*counter;
  cast_receiver->RequestDecodedAudioFrame(
      base::Bind(&GotAudioFrame, counter, cast_receiver));
}

void AppendLog(EncodingEventSubscriber* subscriber,
               const std::string& extra_data,
               const base::FilePath& output_path) {
  media::cast::proto::LogMetadata metadata;
  metadata.set_extra_data(extra_data);

  media::cast::FrameEventList frame_events;
  media::cast::PacketEventList packet_events;
  subscriber->GetEventsAndReset(
      &metadata, &frame_events, &packet_events);
  media::cast::proto::GeneralDescription* gen_desc =
      metadata.mutable_general_description();
  gen_desc->set_product("Cast Simulator");
  gen_desc->set_product_version("0.1");

  scoped_ptr<char[]> serialized_log(new char[media::cast::kMaxSerializedBytes]);
  int output_bytes;
  bool success = media::cast::SerializeEvents(metadata,
                                              frame_events,
                                              packet_events,
                                              true,
                                              media::cast::kMaxSerializedBytes,
                                              serialized_log.get(),
                                              &output_bytes);

  if (!success) {
    LOG(ERROR) << "Failed to serialize log.";
    return;
  }

  if (AppendToFile(output_path, serialized_log.get(), output_bytes) == -1) {
    LOG(ERROR) << "Failed to append to log.";
  }
}

// Run simulation once.
//
// |output_path| is the path to write serialized log.
// |extra_data| is extra tagging information to write to log.
void RunSimulation(const base::FilePath& source_path,
                   const base::FilePath& output_path,
                   const std::string& extra_data,
                   const NetworkSimulationModel& model) {
  // Fake clock. Make sure start time is non zero.
  base::SimpleTestTickClock testing_clock;
  testing_clock.Advance(base::TimeDelta::FromSeconds(1));

  // Task runner.
  scoped_refptr<test::FakeSingleThreadTaskRunner> task_runner =
      new test::FakeSingleThreadTaskRunner(&testing_clock);
  base::ThreadTaskRunnerHandle task_runner_handle(task_runner);

  // CastEnvironments.
  scoped_refptr<CastEnvironment> sender_env =
      new CastEnvironment(
          scoped_ptr<base::TickClock>(
              new test::SkewedTickClock(&testing_clock)).Pass(),
          task_runner,
          task_runner,
          task_runner);
  scoped_refptr<CastEnvironment> receiver_env =
      new CastEnvironment(
          scoped_ptr<base::TickClock>(
              new test::SkewedTickClock(&testing_clock)).Pass(),
          task_runner,
          task_runner,
          task_runner);

  // Event subscriber. Store at most 1 hour of events.
  EncodingEventSubscriber audio_event_subscriber(AUDIO_EVENT,
                                                 100 * 60 * 60);
  EncodingEventSubscriber video_event_subscriber(VIDEO_EVENT,
                                                 30 * 60 * 60);
  sender_env->Logging()->AddRawEventSubscriber(&audio_event_subscriber);
  sender_env->Logging()->AddRawEventSubscriber(&video_event_subscriber);

  // Audio sender config.
  AudioSenderConfig audio_sender_config = GetDefaultAudioSenderConfig();
  audio_sender_config.target_playout_delay =
      base::TimeDelta::FromMilliseconds(kTargetDelay);

  // Audio receiver config.
  FrameReceiverConfig audio_receiver_config =
      GetDefaultAudioReceiverConfig();
  audio_receiver_config.rtp_max_delay_ms =
      audio_sender_config.target_playout_delay.InMilliseconds();

  // Video sender config.
  VideoSenderConfig video_sender_config = GetDefaultVideoSenderConfig();
  video_sender_config.max_bitrate = 4000000;
  video_sender_config.min_bitrate = 2000000;
  video_sender_config.start_bitrate = 4000000;
  video_sender_config.target_playout_delay =
      base::TimeDelta::FromMilliseconds(kTargetDelay);

  // Video receiver config.
  FrameReceiverConfig video_receiver_config =
      GetDefaultVideoReceiverConfig();
  video_receiver_config.rtp_max_delay_ms =
      video_sender_config.target_playout_delay.InMilliseconds();

  // Loopback transport.
  LoopBackTransport receiver_to_sender(receiver_env);
  LoopBackTransport sender_to_receiver(sender_env);

  // Cast receiver.
  scoped_ptr<CastReceiver> cast_receiver(
      CastReceiver::Create(receiver_env,
                           audio_receiver_config,
                           video_receiver_config,
                           &receiver_to_sender));

  // Cast sender and transport sender.
  scoped_ptr<CastTransportSender> transport_sender(
      new CastTransportSenderImpl(
          NULL,
          &testing_clock,
          net::IPEndPoint(),
          base::Bind(&UpdateCastTransportStatus),
          base::Bind(&LogTransportEvents, sender_env),
          base::TimeDelta::FromSeconds(1),
          task_runner,
          &sender_to_receiver));
  scoped_ptr<CastSender> cast_sender(
      CastSender::Create(sender_env, transport_sender.get()));

  // Build packet pipe.
  if (model.type() != media::cast::proto::INTERRUPTED_POISSON_PROCESS) {
    LOG(ERROR) << "Unknown model type " << model.type() << ".";
    return;
  }

  const IPPModel& ipp_model = model.ipp();

  std::vector<double> average_rates(ipp_model.average_rate_size());
  std::copy(ipp_model.average_rate().begin(), ipp_model.average_rate().end(),
      average_rates.begin());
  test::InterruptedPoissonProcess ipp(average_rates,
      ipp_model.coef_burstiness(), ipp_model.coef_variance(), 0);

  // Connect sender to receiver. This initializes the pipe.
  receiver_to_sender.Initialize(
      ipp.NewBuffer(128 * 1024), transport_sender->PacketReceiverForTesting(),
      task_runner, &testing_clock);
  sender_to_receiver.Initialize(
      ipp.NewBuffer(128 * 1024), cast_receiver->packet_receiver(), task_runner,
      &testing_clock);

  // Start receiver.
  int audio_frame_count = 0;
  int video_frame_count = 0;
  cast_receiver->RequestDecodedVideoFrame(
      base::Bind(&GotVideoFrame, &video_frame_count, cast_receiver.get()));
  cast_receiver->RequestDecodedAudioFrame(
      base::Bind(&GotAudioFrame, &audio_frame_count, cast_receiver.get()));

  FakeMediaSource media_source(task_runner,
                               &testing_clock,
                               video_sender_config);

  // Initializing audio and video senders.
  cast_sender->InitializeAudio(audio_sender_config,
                               base::Bind(&AudioInitializationStatus));
  cast_sender->InitializeVideo(media_source.get_video_config(),
                               base::Bind(&VideoInitializationStatus),
                               CreateDefaultVideoEncodeAcceleratorCallback(),
                               CreateDefaultVideoEncodeMemoryCallback());

  // Start sending.
  if (!source_path.empty()) {
    // 0 means using the FPS from the file.
    media_source.SetSourceFile(source_path, 0);
  }
  media_source.Start(cast_sender->audio_frame_input(),
                     cast_sender->video_frame_input());

  // Run for 3 minutes.
  base::TimeDelta elapsed_time;
  while (elapsed_time.InMinutes() < 3) {
    // Each step is 100us.
    base::TimeDelta step = base::TimeDelta::FromMicroseconds(100);
    task_runner->Sleep(step);
    elapsed_time += step;
  }

  LOG(INFO) << "Audio frame count: " << audio_frame_count;
  LOG(INFO) << "Video frame count: " << video_frame_count;
  LOG(INFO) << "Writing log: " << output_path.value();

  // Truncate file and then write serialized log.
  {
    base::ScopedFILE file(base::OpenFile(output_path, "wb"));
    if (!file.get()) {
      LOG(INFO) << "Cannot write to log.";
      return;
    }
  }
  AppendLog(&video_event_subscriber, extra_data, output_path);
  AppendLog(&audio_event_subscriber, extra_data, output_path);
}

NetworkSimulationModel DefaultModel() {
  NetworkSimulationModel model;
  model.set_type(cast::proto::INTERRUPTED_POISSON_PROCESS);
  IPPModel* ipp = model.mutable_ipp();
  ipp->set_coef_burstiness(0.609);
  ipp->set_coef_variance(4.1);

  ipp->add_average_rate(0.609);
  ipp->add_average_rate(0.495);
  ipp->add_average_rate(0.561);
  ipp->add_average_rate(0.458);
  ipp->add_average_rate(0.538);
  ipp->add_average_rate(0.513);
  ipp->add_average_rate(0.585);
  ipp->add_average_rate(0.592);
  ipp->add_average_rate(0.658);
  ipp->add_average_rate(0.556);
  ipp->add_average_rate(0.371);
  ipp->add_average_rate(0.595);
  ipp->add_average_rate(0.490);
  ipp->add_average_rate(0.980);
  ipp->add_average_rate(0.781);
  ipp->add_average_rate(0.463);

  return model;
}

bool IsModelValid(const NetworkSimulationModel& model) {
  if (!model.has_type())
    return false;
  NetworkSimulationModelType type = model.type();
  if (type == media::cast::proto::INTERRUPTED_POISSON_PROCESS) {
    if (!model.has_ipp())
      return false;
    const IPPModel& ipp = model.ipp();
    if (ipp.coef_burstiness() <= 0.0 || ipp.coef_variance() <= 0.0)
      return false;
    if (ipp.average_rate_size() == 0)
      return false;
    for (int i = 0; i < ipp.average_rate_size(); i++) {
      if (ipp.average_rate(i) <= 0.0)
        return false;
    }
  }

  return true;
}

NetworkSimulationModel LoadModel(const base::FilePath& model_path) {
  if (model_path.empty()) {
    LOG(ERROR) << "Model path not set.";
    return DefaultModel();
  }
  std::string model_str;
  if (!base::ReadFileToString(model_path, &model_str)) {
    LOG(ERROR) << "Failed to read model file.";
    return DefaultModel();
  }

  NetworkSimulationModel model;
  if (!model.ParseFromString(model_str)) {
    LOG(ERROR) << "Failed to parse model.";
    return DefaultModel();
  }
  if (!IsModelValid(model)) {
    LOG(ERROR) << "Invalid model.";
    return DefaultModel();
  }

  return model;
}

}  // namespace
}  // namespace cast
}  // namespace media

int main(int argc, char** argv) {
  base::AtExitManager at_exit;
  CommandLine::Init(argc, argv);
  InitLogging(logging::LoggingSettings());

  const CommandLine* cmd = CommandLine::ForCurrentProcess();
  base::FilePath media_path = cmd->GetSwitchValuePath(media::cast::kLibDir);
  if (media_path.empty()) {
    if (!PathService::Get(base::DIR_MODULE, &media_path)) {
      LOG(ERROR) << "Failed to load FFmpeg.";
      return 1;
    }
  }

  if (!media::InitializeMediaLibrary(media_path)) {
    LOG(ERROR) << "Failed to initialize FFmpeg.";
    return 1;
  }

  base::FilePath source_path = cmd->GetSwitchValuePath(
      media::cast::kSourcePath);
  base::FilePath output_path = cmd->GetSwitchValuePath(
      media::cast::kOutputPath);
  if (output_path.empty()) {
    base::GetTempDir(&output_path);
    output_path = output_path.AppendASCII("sim-events.gz");
  }
  std::string sim_id = cmd->GetSwitchValueASCII(media::cast::kSimulationId);

  NetworkSimulationModel model = media::cast::LoadModel(
      cmd->GetSwitchValuePath(media::cast::kModelPath));

  base::DictionaryValue values;
  values.SetBoolean("sim", true);
  values.SetString("sim-id", sim_id);

  std::string extra_data;
  base::JSONWriter::Write(&values, &extra_data);

  // Run.
  media::cast::RunSimulation(source_path, output_path, extra_data, model);
  return 0;
}