void
StreamCoderTest :: testSetCodec()
{
  LoggerStack stack;
  stack.setGlobalLevel(Logger::LEVEL_WARN, false);

  h->setupReading(h->SAMPLE_FILE);

  coder = h->coders[0];
  RefPointer<ICodec> codec = h->codecs[0];

  // The helper, the coder, and me
  VS_TUT_ENSURE_EQUALS("wrong codec ref count",
      codec->getCurrentRefCount(),
      3);

  coder->setCodec(0);
  // The helper, and me
  VS_TUT_ENSURE_EQUALS("wrong codec ref count",
      codec->getCurrentRefCount(),
      2);

  coder->setCodec(ICodec::CODEC_ID_NELLYMOSER);
  codec = coder->getCodec();
  VS_TUT_ENSURE("could not find NELLYMOSER codec", codec);

  // The coder, and me
  VS_TUT_ENSURE_EQUALS("codec refcount wrong",
      codec->getCurrentRefCount(),
      2);
  // Just the helper
  VS_TUT_ENSURE_EQUALS("codec refcount wrong",
      h->codecs[0]->getCurrentRefCount(),
      1);

  codec = ICodec::findDecodingCodecByName("aac");
  VS_TUT_ENSURE("could not find ac3 decoder", codec);
  VS_TUT_ENSURE_EQUALS("ac3 codec refcount wrong",
      codec->getCurrentRefCount(),
      1);
  coder->setCodec(codec.value());
  VS_TUT_ENSURE_EQUALS("ac3 codec refcount wrong after setting",
      codec->getCurrentRefCount(),
      2);

  // This should fail because it's an Encoder, not a Decoder
  RefPointer<ICodec> encodec = h->codecs[0];

  encodec = ICodec::findEncodingCodecByName("libmp3lame");

  VS_TUT_ENSURE("could not find libmp3lame encoder", codec);
  coder->setSampleRate(22050);
  coder->setCodec(encodec.value());
  codec = coder->getCodec();
  VS_TUT_ENSURE_EQUALS("should allow wrong direction", codec.value(), encodec.value());
  int retval = coder->open();
  VS_TUT_ENSURE("should succeed even though wrong direction", retval >= 0);
  (void) retval; // in release compiles VS_TUT_ENSURE is removed and so -Werror produces errors about unused retvals
  coder->close();
}
void
StreamCoderTest :: testGetters()
{
  int refcount=0;
  h->setupReading(h->SAMPLE_FILE);

  for(int i = 0; i< h->num_streams; i++)
  {
    RefPointer<IStream> stream;
    RefPointer<ICodec> codec;
    RefPointer<IRational> rational;
    stream = h->streams[i];
    coder = h->coders[i];

    VS_TUT_ENSURE_EQUALS("invalid direction", coder->getDirection(),
        IStreamCoder::DECODING);

    codec = coder->getCodec();
    refcount = codec->getCurrentRefCount();
    {
      LoggerStack stack;
      stack.setGlobalLevel(Logger::LEVEL_ERROR, false);
      coder->setCodec(codec.value());
    }
    // ensure that one release and one acquire happens
    VS_TUT_ENSURE_EQUALS("invalid releasing or acquiring of codec",
        codec->getCurrentRefCount(),
        refcount);

    VS_TUT_ENSURE_EQUALS("wrong codec type",
        codec->getType(),
        h->expected_codec_types[i]);
    VS_TUT_ENSURE_EQUALS("wrong codec id",
        codec->getID(),
        h->expected_codec_ids[i]);

    if (codec->getType() == ICodec::CODEC_TYPE_AUDIO)
    {
      if (h->expected_sample_rate)
        VS_TUT_ENSURE_EQUALS("unexpected sample rate",
            coder->getSampleRate(),
            h->expected_sample_rate
        );
      if (h->expected_channels)
        VS_TUT_ENSURE_EQUALS("unexpected sample rate",
            coder->getChannels(),
            h->expected_channels
        );

    } else if (codec->getType() == ICodec::CODEC_TYPE_VIDEO)
    {
      if (h->expected_width)
        VS_TUT_ENSURE_EQUALS("unexpected width",
            coder->getWidth(),
            h->expected_width
        );
      if (h->expected_height)
              VS_TUT_ENSURE_EQUALS("unexpected height",
                  coder->getHeight(),
                  h->expected_height
              );
      if (h->expected_gops)
        VS_TUT_ENSURE_EQUALS("unexpected group of pictures",
            coder->getNumPicturesInGroupOfPictures(),
            h->expected_gops
        );
      if (h->expected_pixel_format != IPixelFormat::NONE)
        VS_TUT_ENSURE_EQUALS("unexpected group of pictures",
            coder->getPixelType(),
            h->expected_pixel_format
        );
      if (h->expected_time_base)
      {
        rational = coder->getTimeBase();
        VS_TUT_ENSURE_DISTANCE("unexpected time base",
            rational->getDouble(),
            h->expected_time_base,
            0.0001
        );
        rational = stream->getTimeBase();
        VS_TUT_ENSURE_DISTANCE("unexpected time base",
            rational->getDouble(),
            h->expected_time_base,
            0.0001
        );

      }
    }
    else
    {
      VS_LOG_ERROR("Unexpected type of codec");
      TS_ASSERT(false);
    }
  }
}