int main(int argc, char *argv[])
{
	pthread_mutex_init(&sleepMutex, NULL);
	pthread_cond_init(&sleepCond, NULL);

	signal(SIGINT, sigfunc);
	signal(SIGTERM, sigfunc);
	signal(SIGHUP, sigfunc);

	BMDConfig config;
	if (!config.ParseArguments(argc, argv))
	{
		config.DisplayUsage(1);
		return 1;
	}

	TestPattern generator(&config);

	if (!generator.Init())
		return 1;

	return 0;
}
int main(int argc, char *argv[]) {
  HRESULT result;
  int exitStatus = 1;
  int idx;

  IDeckLinkIterator* deckLinkIterator = NULL;
  IDeckLink* deckLink = NULL;

  IDeckLinkAttributes* deckLinkAttributes = NULL;
  bool formatDetectionSupported;

  IDeckLinkDisplayModeIterator* displayModeIterator = NULL;
  IDeckLinkDisplayMode* displayMode = NULL;
  char* displayModeName = NULL;
  BMDDisplayModeSupport displayModeSupported;

  DeckLinkCaptureDelegate* delegate = NULL;

  pthread_mutex_init(&g_sleepMutex, NULL);
  pthread_cond_init(&g_sleepCond, NULL);

  signal(SIGINT, sigfunc);
  signal(SIGTERM, sigfunc);
  signal(SIGHUP, sigfunc);

  // Network
  g_video_sock = socket(AF_INET, SOCK_STREAM, 0);
  g_video_addr.sin_family = AF_INET;
  g_video_addr.sin_port = htons(62310);
  g_video_addr.sin_addr.s_addr = inet_addr("192.168.100.31");
  connect(g_video_sock, (struct sockaddr *)&g_video_addr, sizeof(g_video_addr));

  g_audio_sock = socket(AF_INET, SOCK_STREAM, 0);
  g_audio_addr.sin_family = AF_INET;
  g_audio_addr.sin_port = htons(62311);
  g_audio_addr.sin_addr.s_addr = inet_addr("192.168.100.31");
  connect(g_audio_sock, (struct sockaddr *)&g_audio_addr, sizeof(g_audio_addr));

  // Process the command line arguments
  if (!g_config.ParseArguments(argc, argv)) {
    g_config.DisplayUsage(exitStatus);
    goto bail;
  }

  // Get the DeckLink device
  deckLinkIterator = CreateDeckLinkIteratorInstance();
  if (!deckLinkIterator) {
    fprintf(stderr, "This application requires the DeckLink drivers installed.\n");
    goto bail;
  }

  idx = g_config.m_deckLinkIndex;

  while ((result = deckLinkIterator->Next(&deckLink)) == S_OK) {
    if (idx == 0)
      break;
    --idx;

    deckLink->Release();
  }

  if (result != S_OK || deckLink == NULL) {
    fprintf(stderr, "Unable to get DeckLink device %u\n", g_config.m_deckLinkIndex);
    goto bail;
  }

  // Get the input (capture) interface of the DeckLink device
  result = deckLink->QueryInterface(IID_IDeckLinkInput, (void**)&g_deckLinkInput);
  if (result != S_OK)
    goto bail;

  // Get the display mode
  if (g_config.m_displayModeIndex == -1) {
    // Check the card supports format detection
    result = deckLink->QueryInterface(IID_IDeckLinkAttributes, (void**)&deckLinkAttributes);
    if (result == S_OK) {
      result = deckLinkAttributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &formatDetectionSupported);
      if (result != S_OK || !formatDetectionSupported) {
        fprintf(stderr, "Format detection is not supported on this device\n");
        goto bail;
      }
    }

    g_config.m_inputFlags |= bmdVideoInputEnableFormatDetection;

    // Format detection still needs a valid mode to start with
    idx = 0;
  } else {
    idx = g_config.m_displayModeIndex;
  }

  result = g_deckLinkInput->GetDisplayModeIterator(&displayModeIterator);
  if (result != S_OK) {
    goto bail;
  }

  while ((result = displayModeIterator->Next(&displayMode)) == S_OK) {
    if (idx == 0) {
      break;
    }
    --idx;

    displayMode->Release();
  }

  if (result != S_OK || displayMode == NULL) {
    fprintf(stderr, "Unable to get display mode %d\n", g_config.m_displayModeIndex);
    goto bail;
  }

  // Get display mode name
  result = displayMode->GetName((const char**)&displayModeName);
  if (result != S_OK) {
    displayModeName = (char *)malloc(32);
    snprintf(displayModeName, 32, "[index %d]", g_config.m_displayModeIndex);
  }

  // Check display mode is supported with given options
  result = g_deckLinkInput->DoesSupportVideoMode(displayMode->GetDisplayMode(), g_config.m_pixelFormat, bmdVideoInputFlagDefault, &displayModeSupported, NULL);
  if (result != S_OK)
    goto bail;

  if (displayModeSupported == bmdDisplayModeNotSupported)
  {
    fprintf(stderr, "The display mode %s is not supported with the selected pixel format\n", displayModeName);
    goto bail;
  }

  if (g_config.m_inputFlags & bmdVideoInputDualStream3D)
  {
    if (!(displayMode->GetFlags() & bmdDisplayModeSupports3D))
    {
      fprintf(stderr, "The display mode %s is not supported with 3D\n", displayModeName);
      goto bail;
    }
  }

  // Print the selected configuration
  g_config.DisplayConfiguration();

  // Configure the capture callback
  delegate = new DeckLinkCaptureDelegate();
  g_deckLinkInput->SetCallback(delegate);

  // Open output files
  // if (g_config.m_videoOutputFile != NULL)
  // {
  //   g_videoOutputFile = open(g_config.m_videoOutputFile, O_WRONLY|O_CREAT|O_TRUNC, 0664);
  //   if (g_videoOutputFile < 0)
  //   {
  //     fprintf(stderr, "Could not open video output file \"%s\"\n", g_config.m_videoOutputFile);
  //     goto bail;
  //   }
  // }
  //
  // if (g_config.m_audioOutputFile != NULL)
  // {
  //   g_audioOutputFile = open(g_config.m_audioOutputFile, O_WRONLY|O_CREAT|O_TRUNC, 0664);
  //   if (g_audioOutputFile < 0)
  //   {
  //     fprintf(stderr, "Could not open audio output file \"%s\"\n", g_config.m_audioOutputFile);
  //     goto bail;
  //   }
  // }

  // Block main thread until signal occurs
  while (!g_do_exit)
  {
    // Start capturing
    result = g_deckLinkInput->EnableVideoInput(displayMode->GetDisplayMode(), g_config.m_pixelFormat, g_config.m_inputFlags);
    if (result != S_OK)
    {
      fprintf(stderr, "Failed to enable video input. Is another application using the card?\n");
      goto bail;
    }

    result = g_deckLinkInput->EnableAudioInput(bmdAudioSampleRate48kHz, g_config.m_audioSampleDepth, g_config.m_audioChannels);
    if (result != S_OK)
      goto bail;

    result = g_deckLinkInput->StartStreams();
    if (result != S_OK)
      goto bail;

    // All Okay.
    exitStatus = 0;

    pthread_mutex_lock(&g_sleepMutex);
    pthread_cond_wait(&g_sleepCond, &g_sleepMutex);
    pthread_mutex_unlock(&g_sleepMutex);

    fprintf(stderr, "Stopping Capture\n");
    g_deckLinkInput->StopStreams();
    g_deckLinkInput->DisableAudioInput();
    g_deckLinkInput->DisableVideoInput();
  }

bail:
  if (g_videoOutputFile != 0)
    close(g_videoOutputFile);

  if (g_audioOutputFile != 0)
    close(g_audioOutputFile);

  if (displayModeName != NULL)
    free(displayModeName);

  if (displayMode != NULL)
    displayMode->Release();

  if (displayModeIterator != NULL)
    displayModeIterator->Release();

  if (g_deckLinkInput != NULL)
  {
    g_deckLinkInput->Release();
    g_deckLinkInput = NULL;
  }

  if (deckLinkAttributes != NULL)
    deckLinkAttributes->Release();

  if (deckLink != NULL)
    deckLink->Release();

  if (deckLinkIterator != NULL)
    deckLinkIterator->Release();

  close(g_video_sock);
  close(g_audio_sock);

  return exitStatus;
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    HRESULT							result;
    int								exitStatus = 1;
    int								idx;

    pthread_mutex_init(&g_sleepMutex, NULL);
    pthread_cond_init(&g_sleepCond, NULL);

    signal(SIGINT, sigfunc);
    signal(SIGTERM, sigfunc);
    signal(SIGHUP, sigfunc);

    // Process the command line arguments
    if (!g_config.ParseArguments(argc, argv))
    {
        g_config.DisplayUsage(exitStatus);
        cleanup();
        return exitStatus;
    }

    g_lcm = new lcm::LCM();
    g_conversionInst = CreateVideoConversionInstance();

    // Get the DeckLink device
    g_deckLinkIterator = CreateDeckLinkIteratorInstance();
    if (!g_deckLinkIterator)
    {
        fprintf(stderr, "This application requires the DeckLink drivers installed.\n");
        cleanup();
        return exitStatus;
    }

    idx = g_config.m_deckLinkIndex;

    while ((result = g_deckLinkIterator->Next(&g_deckLink)) == S_OK)
    {
        if (idx == 0)
            break;
        --idx;

        g_deckLink->Release();
    }

    if (result != S_OK || g_deckLink == NULL)
    {
        fprintf(stderr, "Unable to get DeckLink device %u\n", g_config.m_deckLinkIndex);
        cleanup();
        return exitStatus;
    }

    // Get the input (capture) interface of the DeckLink device
    result = g_deckLink->QueryInterface(IID_IDeckLinkInput, (void**)&g_deckLinkInput);
    if (result != S_OK)
    {
        cleanup();
        return exitStatus;
    }



    // Get the output (display) interface of the DeckLink device
    if (g_deckLink->QueryInterface(IID_IDeckLinkOutput, (void**)&g_deckLinkOutput) != S_OK)
    {
        cleanup();
        return exitStatus;
    }


    // Get the display mode
    if (g_config.m_displayModeIndex == -1)
    {
        // Check the card supports format detection
        result = g_deckLink->QueryInterface(IID_IDeckLinkAttributes, (void**)&g_deckLinkAttributes);
        if (result == S_OK)
        {
            result = g_deckLinkAttributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &g_formatDetectionSupported);
            if (result != S_OK || !g_formatDetectionSupported)
            {
                fprintf(stderr, "Format detection is not supported on this device\n");
                cleanup();
                return exitStatus;
            }
        }

        g_config.m_inputFlags |= bmdVideoInputEnableFormatDetection;

        // Format detection still needs a valid mode to start with
        idx = 0;
    }
    else
    {
        idx = g_config.m_displayModeIndex;
    }

    result = g_deckLinkInput->GetDisplayModeIterator(&g_displayModeIterator);
    if (result != S_OK)
    {
        cleanup();
        return exitStatus;
    }

    while ((result = g_displayModeIterator->Next(&g_displayMode)) == S_OK)
    {
        if (idx == 0)
            break;
        --idx;

        g_displayMode->Release();
    }

    if (result != S_OK || g_displayMode == NULL)
    {
        fprintf(stderr, "Unable to get display mode %d\n", g_config.m_displayModeIndex);
        cleanup();
        return exitStatus;
    }

    // Get display mode name
    result = g_displayMode->GetName((const char**)&g_displayModeName);
    if (result != S_OK)
    {
        g_displayModeName = (char *)malloc(32);
        snprintf(g_displayModeName, 32, "[index %d]", g_config.m_displayModeIndex);
    }

    // Check display mode is supported with given options
    result = g_deckLinkInput->DoesSupportVideoMode(g_displayMode->GetDisplayMode(), g_config.m_pixelFormat, bmdVideoInputFlagDefault, &g_displayModeSupported, NULL);
    if (result != S_OK)
    {
        cleanup();
        return exitStatus;
    }

    if (g_displayModeSupported == bmdDisplayModeNotSupported)
    {
        fprintf(stderr, "The display mode %s is not supported with the selected pixel format\n", g_displayModeName);
        cleanup();
        return exitStatus;
    }

    if (g_config.m_inputFlags & bmdVideoInputDualStream3D)
    {
        if (!(g_displayMode->GetFlags() & bmdDisplayModeSupports3D))
        {
            fprintf(stderr, "The display mode %s is not supported with 3D\n", g_displayModeName);
            cleanup();
            return exitStatus;
        }
    }

    // Print the selected configuration
    g_config.DisplayConfiguration();

    // Configure the capture callback
    g_delegate = new DeckLinkCaptureDelegate();
    g_deckLinkInput->SetCallback(g_delegate);

    // Open output files
    if (g_config.m_videoOutputFile != NULL)
    {
        g_videoOutputFile = open(g_config.m_videoOutputFile, O_WRONLY|O_CREAT|O_TRUNC, 0664);
        if (g_videoOutputFile < 0)
        {
            fprintf(stderr, "Could not open video output file \"%s\"\n", g_config.m_videoOutputFile);
            cleanup();
            return exitStatus;
        }
    }

    if (g_config.m_audioOutputFile != NULL)
    {
        g_audioOutputFile = open(g_config.m_audioOutputFile, O_WRONLY|O_CREAT|O_TRUNC, 0664);
        if (g_audioOutputFile < 0)
        {
            fprintf(stderr, "Could not open audio output file \"%s\"\n", g_config.m_audioOutputFile);
            cleanup();
            return exitStatus;
        }
    }

    // Block main thread until signal occurs
    while (!g_do_exit)
    {
        // Start capturing
        result = g_deckLinkInput->EnableVideoInput(g_displayMode->GetDisplayMode(), g_config.m_pixelFormat, g_config.m_inputFlags);
        if (result != S_OK)
        {
            fprintf(stderr, "Failed to enable video input. Is another application using the card?\n");
        }
        else
        {
            result = g_deckLinkInput->EnableAudioInput(bmdAudioSampleRate48kHz, g_config.m_audioSampleDepth, g_config.m_audioChannels);
            if (result == S_OK)
            {
                frameConsumer.Start();

                result = g_deckLinkInput->StartStreams();
                if (result == S_OK)
                {
                    // All Okay.
                    exitStatus = 0;

                    pthread_mutex_lock(&g_sleepMutex);
                    pthread_cond_wait(&g_sleepCond, &g_sleepMutex);
                    pthread_mutex_unlock(&g_sleepMutex);

                    fprintf(stderr, "Stopping Capture\n");
                    frameConsumer.Stop();
                    g_deckLinkInput->StopStreams();
                    g_deckLinkInput->DisableAudioInput();
                    g_deckLinkInput->DisableVideoInput();
                }
            }
        }
    }

    cleanup();

    return exitStatus;
}