<commit_before>// Copyright (c) 2013 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gflags/gflags.h>
#include <iostream>

#include "app/fixed_key_encryption_flags.h"
#include "app/muxer_flags.h"
#include "app/widevine_encryption_flags.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "media/base/demuxer.h"
#include "media/base/fixed_encryptor_source.h"
#include "media/base/widevine_encryptor_source.h"
#include "media/base/media_stream.h"
#include "media/base/muxer_options.h"
#include "media/base/request_signer.h"
#include "media/base/stream_info.h"
#include "media/file/file.h"
#include "media/mp4/mp4_muxer.h"

DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info.");

namespace {
const char kUsage[] =
    "Packager driver program. Sample Usage:\n%s <input> [flags]";
}  // namespace

namespace media {

void DumpStreamInfo(const std::vector<MediaStream*>& streams) {
  std::cout << "Found " << streams.size() << " stream(s)." << std::endl;
  for (size_t i = 0; i < streams.size(); ++i)
    std::cout << "Stream [" << i << "] " << streams[i]->ToString() << std::endl;
}

// Create and initialize encryptor source.
scoped_ptr<EncryptorSource> CreateEncryptorSource() {
  scoped_ptr<EncryptorSource> encryptor_source;
  if (FLAGS_enable_widevine_encryption) {
    std::string rsa_private_key;
    if (!File::ReadFileToString(FLAGS_signing_key_path.c_str(),
                                &rsa_private_key)) {
      LOG(ERROR) << "Failed to read from rsa_key_file.";
      return scoped_ptr<EncryptorSource>();
    }

    scoped_ptr<RequestSigner> signer(
        RsaRequestSigner::CreateSigner(FLAGS_signer, rsa_private_key));
    if (!signer) {
      LOG(ERROR) << "Cannot create signer object from "
                 << FLAGS_signing_key_path;
      return scoped_ptr<EncryptorSource>();
    }

    WidevineEncryptorSource::TrackType track_type =
        WidevineEncryptorSource::GetTrackTypeFromString(FLAGS_track_type);
    if (track_type == WidevineEncryptorSource::TRACK_TYPE_UNKNOWN) {
      LOG(ERROR) << "Unknown track_type specified.";
      return scoped_ptr<EncryptorSource>();
    }

    encryptor_source.reset(new WidevineEncryptorSource(
        FLAGS_server_url, FLAGS_content_id, track_type, signer.Pass()));
  } else if (FLAGS_enable_fixed_key_encryption) {
    encryptor_source.reset(
        new FixedEncryptorSource(FLAGS_key_id, FLAGS_key, FLAGS_pssh));
  }

  if (encryptor_source) {
    Status status = encryptor_source->Initialize();
    if (!status.ok()) {
      LOG(ERROR) << "Encryptor source failed to initialize: "
                 << status.ToString();
      return scoped_ptr<EncryptorSource>();
    }
  }
  return encryptor_source.Pass();
}

bool GetMuxerOptions(MuxerOptions* muxer_options) {
  DCHECK(muxer_options);

  muxer_options->single_segment = FLAGS_single_segment;
  muxer_options->segment_duration = FLAGS_segment_duration;
  muxer_options->fragment_duration = FLAGS_fragment_duration;
  muxer_options->segment_sap_aligned = FLAGS_segment_sap_aligned;
  muxer_options->fragment_sap_aligned = FLAGS_fragment_sap_aligned;
  muxer_options->num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx;
  muxer_options->output_file_name = FLAGS_output;
  muxer_options->segment_template = FLAGS_segment_template;
  muxer_options->temp_file_name = FLAGS_temp_file;

  // Create a temp file if needed.
  if (muxer_options->single_segment && muxer_options->temp_file_name.empty()) {
    base::FilePath path;
    if (!file_util::CreateTemporaryFile(&path)) {
      LOG(ERROR) << "Failed to create a temporary file.";
      return false;
    }
    muxer_options->temp_file_name = path.value();
  }
  return true;
}

MediaStream* FindFirstStreamOfType(const std::vector<MediaStream*>& streams,
                                   StreamType stream_type) {
  typedef std::vector<MediaStream*>::const_iterator StreamIterator;
  for (StreamIterator it = streams.begin(); it != streams.end(); ++it) {
    if ((*it)->info()->stream_type() == stream_type)
      return *it;
  }
  return NULL;
}
MediaStream* FindFirstVideoStream(const std::vector<MediaStream*>& streams) {
  return FindFirstStreamOfType(streams, kStreamVideo);
}
MediaStream* FindFirstAudioStream(const std::vector<MediaStream*>& streams) {
  return FindFirstStreamOfType(streams, kStreamAudio);
}

bool AddStreamToMuxer(const std::vector<MediaStream*>& streams, Muxer* muxer) {
  DCHECK(muxer);

  if (!FLAGS_video && !FLAGS_audio) {
    LOG(ERROR) << "Required: add_video_stream or add_audio_stream.";
    return false;
  }
<commit_msg>Fix a misspelling of the flags name.<commit_after>// Copyright (c) 2013 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <gflags/gflags.h>
#include <iostream>

#include "app/fixed_key_encryption_flags.h"
#include "app/muxer_flags.h"
#include "app/widevine_encryption_flags.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "media/base/demuxer.h"
#include "media/base/fixed_encryptor_source.h"
#include "media/base/widevine_encryptor_source.h"
#include "media/base/media_stream.h"
#include "media/base/muxer_options.h"
#include "media/base/request_signer.h"
#include "media/base/stream_info.h"
#include "media/file/file.h"
#include "media/mp4/mp4_muxer.h"

DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info.");

namespace {
const char kUsage[] =
    "Packager driver program. Sample Usage:\n%s <input> [flags]";
}  // namespace

namespace media {

void DumpStreamInfo(const std::vector<MediaStream*>& streams) {
  std::cout << "Found " << streams.size() << " stream(s)." << std::endl;
  for (size_t i = 0; i < streams.size(); ++i)
    std::cout << "Stream [" << i << "] " << streams[i]->ToString() << std::endl;
}

// Create and initialize encryptor source.
scoped_ptr<EncryptorSource> CreateEncryptorSource() {
  scoped_ptr<EncryptorSource> encryptor_source;
  if (FLAGS_enable_widevine_encryption) {
    std::string rsa_private_key;
    if (!File::ReadFileToString(FLAGS_signing_key_path.c_str(),
                                &rsa_private_key)) {
      LOG(ERROR) << "Failed to read from rsa_key_file.";
      return scoped_ptr<EncryptorSource>();
    }

    scoped_ptr<RequestSigner> signer(
        RsaRequestSigner::CreateSigner(FLAGS_signer, rsa_private_key));
    if (!signer) {
      LOG(ERROR) << "Cannot create signer object from "
                 << FLAGS_signing_key_path;
      return scoped_ptr<EncryptorSource>();
    }

    WidevineEncryptorSource::TrackType track_type =
        WidevineEncryptorSource::GetTrackTypeFromString(FLAGS_track_type);
    if (track_type == WidevineEncryptorSource::TRACK_TYPE_UNKNOWN) {
      LOG(ERROR) << "Unknown track_type specified.";
      return scoped_ptr<EncryptorSource>();
    }

    encryptor_source.reset(new WidevineEncryptorSource(
        FLAGS_server_url, FLAGS_content_id, track_type, signer.Pass()));
  } else if (FLAGS_enable_fixed_key_encryption) {
    encryptor_source.reset(
        new FixedEncryptorSource(FLAGS_key_id, FLAGS_key, FLAGS_pssh));
  }

  if (encryptor_source) {
    Status status = encryptor_source->Initialize();
    if (!status.ok()) {
      LOG(ERROR) << "Encryptor source failed to initialize: "
                 << status.ToString();
      return scoped_ptr<EncryptorSource>();
    }
  }
  return encryptor_source.Pass();
}

bool GetMuxerOptions(MuxerOptions* muxer_options) {
  DCHECK(muxer_options);

  muxer_options->single_segment = FLAGS_single_segment;
  muxer_options->segment_duration = FLAGS_segment_duration;
  muxer_options->fragment_duration = FLAGS_fragment_duration;
  muxer_options->segment_sap_aligned = FLAGS_segment_sap_aligned;
  muxer_options->fragment_sap_aligned = FLAGS_fragment_sap_aligned;
  muxer_options->num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx;
  muxer_options->output_file_name = FLAGS_output;
  muxer_options->segment_template = FLAGS_segment_template;
  muxer_options->temp_file_name = FLAGS_temp_file;

  // Create a temp file if needed.
  if (muxer_options->single_segment && muxer_options->temp_file_name.empty()) {
    base::FilePath path;
    if (!file_util::CreateTemporaryFile(&path)) {
      LOG(ERROR) << "Failed to create a temporary file.";
      return false;
    }
    muxer_options->temp_file_name = path.value();
  }
  return true;
}

MediaStream* FindFirstStreamOfType(const std::vector<MediaStream*>& streams,
                                   StreamType stream_type) {
  typedef std::vector<MediaStream*>::const_iterator StreamIterator;
  for (StreamIterator it = streams.begin(); it != streams.end(); ++it) {
    if ((*it)->info()->stream_type() == stream_type)
      return *it;
  }
  return NULL;
}
MediaStream* FindFirstVideoStream(const std::vector<MediaStream*>& streams) {
  return FindFirstStreamOfType(streams, kStreamVideo);
}
MediaStream* FindFirstAudioStream(const std::vector<MediaStream*>& streams) {
  return FindFirstStreamOfType(streams, kStreamAudio);
}

bool AddStreamToMuxer(const std::vector<MediaStream*>& streams, Muxer* muxer) {
  DCHECK(muxer);

  if (!FLAGS_video && !FLAGS_audio) {
    LOG(ERROR) << "Required: --audio or --video.";
    return false;
  }

{'fraction_non_alphanumeric': 0.1240038253108065, 'fraction_numerical': 0.0014876208691956222, 'mean_word_length': 4.523474178403756, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '469733', 'n_tokens_mistral': 3209, 'n_tokens_neox': 3110, 'n_words': 584}