void NodeHarnessWin::MessageReceived(BMessage *msg) { status_t err; switch (msg->what) { case BUTTON_CONNECT: mIsConnected = true; // set the button states appropriately mConnectButton->SetEnabled(false); mStartButton->SetEnabled(true); // set up the node network { BMediaRoster* r = BMediaRoster::Roster(); // find a node that can handle an audio file #if TEST_WITH_AUDIO entry_ref inRef; dormant_node_info info; ::get_ref_for_path("/boot/optional/sound/virtual (void)", &inRef); err = r->SniffRef(inRef, B_BUFFER_PRODUCER | B_FILE_INTERFACE, &info); ErrorCheck(err, "couldn't find file reader node\n"); err = r->InstantiateDormantNode(info, &mConnection.producer, B_FLAVOR_IS_LOCAL); ErrorCheck(err, "couldn't instantiate file reader node\n"); bigtime_t dummy_length; // output = media length; we don't use it err = r->SetRefFor(mConnection.producer, inRef, false, &dummy_length); ErrorCheck(err, "unable to SetRefFor() to read that sound file!\n"); #else r->GetVideoInput(&mConnection.producer); #endif entry_ref logRef; ::get_ref_for_path("/tmp/node_log", &logRef); mLogNode = new LoggingConsumer(logRef); err = r->RegisterNode(mLogNode); ErrorCheck(err, "unable to register LoggingConsumer node!\n"); // make sure the Media Roster knows that we're using the node r->GetNodeFor(mLogNode->Node().node, &mConnection.consumer); // trim down the log's verbosity a touch mLogNode->SetEnabled(LOG_HANDLE_EVENT, false); // fire off a window with the LoggingConsumer's controls in it BParameterWeb* web; r->GetParameterWebFor(mConnection.consumer, &web); BView* view = BMediaTheme::ViewFor(web); BWindow* win = new BWindow(BRect(250, 200, 300, 300), "Controls", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS); win->AddChild(view); win->ResizeTo(view->Bounds().Width(), view->Bounds().Height()); win->Show(); // set the nodes' time sources r->GetTimeSource(&mTimeSource); r->SetTimeSourceFor(mConnection.consumer.node, mTimeSource.node); r->SetTimeSourceFor(mConnection.producer.node, mTimeSource.node); // got the nodes; now we find the endpoints of the connection media_input logInput; media_output soundOutput; int32 count; err = r->GetFreeOutputsFor(mConnection.producer, &soundOutput, 1, &count); ErrorCheck(err, "unable to get a free output from the producer node"); err = r->GetFreeInputsFor(mConnection.consumer, &logInput, 1, &count); ErrorCheck(err, "unable to get a free input to the LoggingConsumer"); // fill in the rest of the Connection object mConnection.source = soundOutput.source; mConnection.destination = logInput.destination; // got the endpoints; now we connect it! media_format format; #if TEST_WITH_AUDIO format.type = B_MEDIA_RAW_AUDIO; // !!! hmmm.. how to fully wildcard this? format.u.raw_audio = media_raw_audio_format::wildcard; #else format.type = B_MEDIA_RAW_VIDEO; // !!! hmmm.. how to fully wildcard this? format.u.raw_video = media_raw_video_format::wildcard; #endif err = r->Connect(mConnection.source, mConnection.destination, &format, &soundOutput, &logInput); ErrorCheck(err, "unable to connect nodes"); mConnection.format = format; // for video input, we need to set the downstream latency for record -> playback bigtime_t latency; r->GetLatencyFor(mConnection.producer, &latency); printf("Setting producer run mode latency to %" B_PRIdBIGTIME "\n", latency); r->SetProducerRunModeDelay(mConnection.producer, latency + 6000); // preroll first, to be a good citizen r->PrerollNode(mConnection.consumer); r->PrerollNode(mConnection.producer); // start the LoggingConsumer and leave it running BTimeSource* ts = r->MakeTimeSourceFor(mTimeSource); r->StartNode(mConnection.consumer, ts->Now()); ts->Release(); } break; case BUTTON_START: mStartButton->SetEnabled(false); mStopButton->SetEnabled(true); // start the consumer running { bigtime_t latency; BMediaRoster* r = BMediaRoster::Roster(); BTimeSource* ts = r->MakeTimeSourceFor(mConnection.consumer); r->GetLatencyFor(mConnection.producer, &latency); r->StartNode(mConnection.producer, ts->Now() + latency); ts->Release(); mIsRunning = true; } break; case BUTTON_STOP: StopNodes(); break; default: BWindow::MessageReceived(msg); break; } }
void NodeHarnessWin::MessageReceived(BMessage *msg) { status_t err; switch (msg->what) { case BUTTON_CONNECT: mIsConnected = true; // set the button states appropriately mConnectButton->SetEnabled(false); mStartButton->SetEnabled(true); // set up the node network { BMediaRoster* r = BMediaRoster::Roster(); // connect to the mixer err = r->GetAudioMixer(&mConnection.consumer); ErrorCheck(err, "unable to get the system mixer"); // fire off a window with the ToneProducer's controls in it BParameterWeb* web; r->GetParameterWebFor(mConnection.producer, &web); BView* view = BMediaTheme::ViewFor(web); BWindow* win = new BWindow(BRect(250, 200, 300, 300), "Controls", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS); win->AddChild(view); win->ResizeTo(view->Bounds().Width(), view->Bounds().Height()); win->Show(); // set the producer's time source to be the "default" time source, which // the Mixer uses too. r->GetTimeSource(&mTimeSource); r->SetTimeSourceFor(mConnection.producer.node, mTimeSource.node); // got the nodes; now we find the endpoints of the connection media_input mixerInput; media_output soundOutput; int32 count = 1; err = r->GetFreeOutputsFor(mConnection.producer, &soundOutput, 1, &count); ErrorCheck(err, "unable to get a free output from the producer node"); count = 1; err = r->GetFreeInputsFor(mConnection.consumer, &mixerInput, 1, &count); ErrorCheck(err, "unable to get a free input to the mixer"); // got the endpoints; now we connect it! media_format format; format.type = B_MEDIA_RAW_AUDIO; format.u.raw_audio = media_raw_audio_format::wildcard; err = r->Connect(soundOutput.source, mixerInput.destination, &format, &soundOutput, &mixerInput); ErrorCheck(err, "unable to connect nodes"); // the inputs and outputs might have been reassigned during the // nodes' negotiation of the Connect(). That's why we wait until // after Connect() finishes to save their contents. mConnection.format = format; mConnection.source = soundOutput.source; mConnection.destination = mixerInput.destination; // Set an appropriate run mode for the producer r->SetRunModeNode(mConnection.producer, BMediaNode::B_INCREASE_LATENCY); } break; case BUTTON_START: mStartButton->SetEnabled(false); mStopButton->SetEnabled(true); // start the producer running { BMediaRoster* r = BMediaRoster::Roster(); BTimeSource* ts = r->MakeTimeSourceFor(mConnection.producer); if (!ts) { fprintf(stderr, "* ERROR - MakeTimeSourceFor(producer) returned NULL!\n"); exit(1); } // make sure we give the producer enough time to run buffers through // the node chain, otherwise it'll start up already late bigtime_t latency = 0; r->GetLatencyFor(mConnection.producer, &latency); r->StartNode(mConnection.producer, ts->Now() + latency); ts->Release(); mIsRunning = true; } break; case BUTTON_STOP: StopNodes(); break; default: BWindow::MessageReceived(msg); break; } }
status_t CodyCam::_SetUpNodes() { status_t status = B_OK; /* find the media roster */ fMediaRoster = BMediaRoster::Roster(&status); if (status != B_OK) { ErrorAlert("Cannot find the media roster", status, fWindow); return status; } /* find the time source */ status = fMediaRoster->GetTimeSource(&fTimeSourceNode); if (status != B_OK) { ErrorAlert("Cannot get a time source", status, fWindow); return status; } /* find a video producer node */ INFO("CodyCam acquiring VideoInput node\n"); status = fMediaRoster->GetVideoInput(&fProducerNode); if (status != B_OK) { ErrorAlert("Cannot find a video source. You need a webcam to use CodyCam.", status, fWindow); return status; } /* create the video consumer node */ fVideoConsumer = new VideoConsumer("CodyCam", ((VideoWindow*)fWindow)->VideoView(), ((VideoWindow*)fWindow)->StatusLine(), NULL, 0); if (!fVideoConsumer) { ErrorAlert("Cannot create a video window", B_ERROR, fWindow); return B_ERROR; } /* register the node */ status = fMediaRoster->RegisterNode(fVideoConsumer); if (status != B_OK) { ErrorAlert("Cannot register the video window", status, fWindow); return status; } fPort = fVideoConsumer->ControlPort(); /* find free producer output */ int32 cnt = 0; status = fMediaRoster->GetFreeOutputsFor(fProducerNode, &fProducerOut, 1, &cnt, B_MEDIA_RAW_VIDEO); if (status != B_OK || cnt < 1) { status = B_RESOURCE_UNAVAILABLE; ErrorAlert("Cannot find an available video stream", status, fWindow); return status; } /* find free consumer input */ cnt = 0; status = fMediaRoster->GetFreeInputsFor(fVideoConsumer->Node(), &fConsumerIn, 1, &cnt, B_MEDIA_RAW_VIDEO); if (status != B_OK || cnt < 1) { status = B_RESOURCE_UNAVAILABLE; ErrorAlert("Can't find an available connection to the video window", status, fWindow); return status; } /* Connect The Nodes!!! */ media_format format; format.type = B_MEDIA_RAW_VIDEO; media_raw_video_format vid_format = {0, 1, 0, 239, B_VIDEO_TOP_LEFT_RIGHT, 1, 1, {B_RGB32, VIDEO_SIZE_X, VIDEO_SIZE_Y, VIDEO_SIZE_X * 4, 0, 0}}; format.u.raw_video = vid_format; /* connect producer to consumer */ status = fMediaRoster->Connect(fProducerOut.source, fConsumerIn.destination, &format, &fProducerOut, &fConsumerIn); if (status != B_OK) { ErrorAlert("Cannot connect the video source to the video window", status); return status; } /* set time sources */ status = fMediaRoster->SetTimeSourceFor(fProducerNode.node, fTimeSourceNode.node); if (status != B_OK) { ErrorAlert("Cannot set the time source for the video source", status); return status; } status = fMediaRoster->SetTimeSourceFor(fVideoConsumer->ID(), fTimeSourceNode.node); if (status != B_OK) { ErrorAlert("Cannot set the time source for the video window", status); return status; } /* figure out what recording delay to use */ bigtime_t latency = 0; status = fMediaRoster->GetLatencyFor(fProducerNode, &latency); status = fMediaRoster->SetProducerRunModeDelay(fProducerNode, latency); /* start the nodes */ bigtime_t initLatency = 0; status = fMediaRoster->GetInitialLatencyFor(fProducerNode, &initLatency); if (status < B_OK) { ErrorAlert("Error getting initial latency for the capture node", status); return status; } initLatency += estimate_max_scheduling_latency(); BTimeSource* timeSource = fMediaRoster->MakeTimeSourceFor(fProducerNode); bool running = timeSource->IsRunning(); /* workaround for people without sound cards */ /* because the system time source won't be running */ bigtime_t real = BTimeSource::RealTime(); if (!running) { status = fMediaRoster->StartTimeSource(fTimeSourceNode, real); if (status != B_OK) { timeSource->Release(); ErrorAlert("Cannot start time source!", status); return status; } status = fMediaRoster->SeekTimeSource(fTimeSourceNode, 0, real); if (status != B_OK) { timeSource->Release(); ErrorAlert("Cannot seek time source!", status); return status; } } bigtime_t perf = timeSource->PerformanceTimeFor(real + latency + initLatency); timeSource->Release(); /* start the nodes */ status = fMediaRoster->StartNode(fProducerNode, perf); if (status != B_OK) { ErrorAlert("Cannot start the video source", status); return status; } status = fMediaRoster->StartNode(fVideoConsumer->Node(), perf); if (status != B_OK) { ErrorAlert("Cannot start the video window", status); return status; } return status; }
status_t NodeManager::_StartNodes() { status_t status = B_NO_INIT; if (!fMediaRoster) return status; // begin mucking with the media roster if (!fMediaRoster->Lock()) return B_ERROR; bigtime_t latency = 0; bigtime_t initLatency = 0; if (fVideoProducer && fVideoConsumer) { // figure out what recording delay to use status = fMediaRoster->GetLatencyFor(fVideoConnection.producer, &latency); if (status < B_OK) { print_error("error getting latency for video producer", status); } else TRACE("video latency: %Ld\n", latency); status = fMediaRoster->SetProducerRunModeDelay( fVideoConnection.producer, latency); if (status < B_OK) { print_error("error settings run mode delay for video producer", status); } // start the nodes status = fMediaRoster->GetInitialLatencyFor( fVideoConnection.producer, &initLatency); if (status < B_OK) { print_error("error getting initial latency for video producer", status); } } initLatency += estimate_max_scheduling_latency(); if (fAudioProducer) { // TODO: was this supposed to be added to initLatency?!? bigtime_t audioLatency = 0; status = fMediaRoster->GetLatencyFor(fAudioConnection.producer, &audioLatency); TRACE("audio latency: %Ld\n", audioLatency); } BTimeSource* timeSource; if (fVideoProducer) { timeSource = fMediaRoster->MakeTimeSourceFor( fVideoConnection.producer); } else { timeSource = fMediaRoster->MakeTimeSourceFor( fAudioConnection.producer); } bool running = timeSource->IsRunning(); // workaround for people without sound cards // because the system time source won't be running bigtime_t real = BTimeSource::RealTime(); if (!running) { status = fMediaRoster->StartTimeSource(fTimeSource, real); if (status != B_OK) { timeSource->Release(); print_error("cannot start time source!", status); return status; } status = fMediaRoster->SeekTimeSource(fTimeSource, 0, real); if (status != B_OK) { timeSource->Release(); print_error("cannot seek time source!", status); return status; } } bigtime_t perf = timeSource->PerformanceTimeFor(real + latency + initLatency); timeSource->Release(); // start the nodes if (fVideoProducer && fVideoConsumer) { status = fMediaRoster->StartNode(fVideoConnection.consumer, perf); if (status != B_OK) { print_error("Can't start the video consumer", status); return status; } status = fMediaRoster->StartNode(fVideoConnection.producer, perf); if (status != B_OK) { print_error("Can't start the video producer", status); return status; } } if (fAudioProducer) { status = fMediaRoster->StartNode(fAudioConnection.producer, perf); if (status != B_OK) { print_error("Can't start the audio producer", status); return status; } } fPerformanceTimeBase = perf; // done mucking with the media roster fMediaRoster->Unlock(); return status; }