void CodyCam::MessageReceived(BMessage *message) { switch (message->what) { case msg_start: { BTimeSource* timeSource = fMediaRoster->MakeTimeSourceFor(fTimeSourceNode); bigtime_t real = BTimeSource::RealTime(); bigtime_t perf = timeSource->PerformanceTimeFor(real) + 10000; status_t status = fMediaRoster->StartNode(fProducerNode, perf); if (status != B_OK) ERROR("error starting producer!"); timeSource->Release(); break; } case msg_stop: fMediaRoster->StopNode(fProducerNode, 0, true); break; case msg_video: { if (fVideoControlWindow) { fVideoControlWindow->Activate(); break; } BParameterWeb* web = NULL; BView* view = NULL; media_node node = fProducerNode; status_t err = fMediaRoster->GetParameterWebFor(node, &web); if (err >= B_OK && web != NULL) { view = BMediaTheme::ViewFor(web); fVideoControlWindow = new ControlWindow( BRect(2 * WINDOW_OFFSET_X + WINDOW_SIZE_X, WINDOW_OFFSET_Y, 2 * WINDOW_OFFSET_X + WINDOW_SIZE_X + view->Bounds().right, WINDOW_OFFSET_Y + view->Bounds().bottom), view, node); fMediaRoster->StartWatching(BMessenger(NULL, fVideoControlWindow), node, B_MEDIA_WEB_CHANGED); fVideoControlWindow->Show(); } break; } case msg_about: (new BAlert("About CodyCam", "CodyCam\n\nThe Original BeOS webcam", "Close"))->Go(); break; case msg_control_win: // our control window is being asked to go away // set our pointer to NULL fVideoControlWindow = NULL; break; default: BApplication::MessageReceived(message); break; } }
status_t GameSoundBuffer::StartPlaying() { if (fIsPlaying) return EALREADY; BMediaRoster* roster = BMediaRoster::Roster(); BTimeSource* source = roster->MakeTimeSourceFor(fConnection->producer); // 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; status_t status = roster->GetLatencyFor(fConnection->producer, &latency); if (status == B_OK) status = roster->StartNode(fConnection->producer, source->Now() + latency); source->Release(); fIsPlaying = true; return status; }
void RouteAppNodeManager::nodeCreated( NodeRef* ref) { // prepare the log message BMessage logMsg(M_LOG); BString title = "Node '"; title << ref->name() << "' created"; logMsg.AddString("title", title); // create a default group for the node // [em 8feb00] NodeGroup* g = createGroup(ref->name()); if(ref->kind() & B_TIME_SOURCE) { // notify observers BMessage m(M_TIME_SOURCE_CREATED); m.AddInt32("nodeID", ref->id()); notify(&m); } // adopt node's time source if it's not the system clock (the default) // [em 20mar00] media_node systemClock; status_t err = roster->GetSystemTimeSource(&systemClock); if(err == B_OK) { BTimeSource* ts = roster->MakeTimeSourceFor(ref->node()); if(ts->Node() != systemClock) { g->setTimeSource(ts->Node()); logMsg.AddString("line", "Synced to system clock"); } ts->Release(); } g->addNode(ref); m_logTarget.SendMessage(&logMsg); }
status_t CodyCam::_SetUpNodes() { status_t status = B_OK; /* find the media roster */ fMediaRoster = BMediaRoster::Roster(&status); if (status != B_OK) { fWindow->ErrorAlert(B_TRANSLATE("Cannot find the media roster"), status); return status; } /* find the time source */ status = fMediaRoster->GetTimeSource(&fTimeSourceNode); if (status != B_OK) { fWindow->ErrorAlert(B_TRANSLATE("Cannot get a time source"), status); return status; } /* find a video producer node */ INFO("CodyCam acquiring VideoInput node\n"); status = fMediaRoster->GetVideoInput(&fProducerNode); if (status != B_OK) { fWindow->ErrorAlert(B_TRANSLATE("Cannot find a video source. You need " "a webcam to use CodyCam."), status); return status; } /* create the video consumer node */ fVideoConsumer = new VideoConsumer("CodyCam", ((VideoWindow*)fWindow)->VideoView(), ((VideoWindow*)fWindow)->StatusLine(), NULL, 0); if (!fVideoConsumer) { fWindow->ErrorAlert(B_TRANSLATE("Cannot create a video window"), B_ERROR); return B_ERROR; } /* register the node */ status = fMediaRoster->RegisterNode(fVideoConsumer); if (status != B_OK) { fWindow->ErrorAlert(B_TRANSLATE("Cannot register the video window"), status); 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; fWindow->ErrorAlert(B_TRANSLATE("Cannot find an available video stream"), status); 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; fWindow->ErrorAlert(B_TRANSLATE("Can't find an available connection to " "the video window"), status); 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) { fWindow->ErrorAlert(B_TRANSLATE("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) { fWindow->ErrorAlert(B_TRANSLATE("Cannot set the time source for the " "video source"), status); return status; } status = fMediaRoster->SetTimeSourceFor(fVideoConsumer->ID(), fTimeSourceNode.node); if (status != B_OK) { fWindow->ErrorAlert(B_TRANSLATE("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) { fWindow->ErrorAlert(B_TRANSLATE("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(); fWindow->ErrorAlert(B_TRANSLATE("Cannot start time source!"), status); return status; } status = fMediaRoster->SeekTimeSource(fTimeSourceNode, 0, real); if (status != B_OK) { timeSource->Release(); fWindow->ErrorAlert(B_TRANSLATE("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) { fWindow->ErrorAlert(B_TRANSLATE("Cannot start the video source"), status); return status; } status = fMediaRoster->StartNode(fVideoConsumer->Node(), perf); if (status != B_OK) { fWindow->ErrorAlert(B_TRANSLATE("Cannot start the video window"), status); return status; } return status; }
status_t NodeManager::_StartNodes() { status_t status = B_NO_INIT; if (!fMediaRoster) return status; 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; return status; }
status_t Controller::ConnectNodes() { status_t err; // dvb_node = gDeviceRoster->DeviceNode(fCurrentInterface); err = gMediaRoster->GetNodeFor(gDeviceRoster->DeviceNode(fCurrentInterface).node, &dvb_node); HandleError("GetNodeFor failed", err); video_window_node = fVideoNode->Node(); err = gMediaRoster->GetAudioMixer(&audio_mixer_node); HandleError("GetAudioMixer failed", err); media_input input; media_output output; media_format fmt; int32 count; // Connect audio err = gMediaRoster->GetFreeOutputsFor(dvb_node, &output, 1, &count, B_MEDIA_RAW_AUDIO); HandleError("Can't find free audio output", err); if (count < 1) HandleError("No free audio output", -1); err = gMediaRoster->GetFreeInputsFor(audio_mixer_node, &input, 1, &count, B_MEDIA_RAW_AUDIO); HandleError("Can't find free audio input", err); if (count < 1) HandleError("No free audio input", -1); memset(&fmt, 0, sizeof(fmt)); err = gMediaRoster->Connect(output.source, input.destination, &fmt, &audio_output, &audio_input); HandleError("Can't connect audio", err); // Connect video err = gMediaRoster->GetFreeOutputsFor(dvb_node, &output, 1, &count, B_MEDIA_RAW_VIDEO); HandleError("Can't find free video output", err); if (count < 1) HandleError("No free video output", -1); err = gMediaRoster->GetFreeInputsFor(video_window_node, &input, 1, &count, B_MEDIA_RAW_VIDEO); HandleError("Can't find free video input", err); if (count < 1) HandleError("No free video input", -1); color_space cspaces_overlay[] = { B_YCbCr422, B_RGB32, B_NO_COLOR_SPACE }; color_space cspaces_bitmap[] = { B_RGB32, B_NO_COLOR_SPACE }; if (gOverlayDisabled) { err = B_ERROR; } else { fVideoNode->SetOverlayEnabled(true); for (int i = 0; cspaces_overlay[i] != B_NO_COLOR_SPACE; i++) { printf("trying connect with colorspace 0x%08x\n", cspaces_overlay[i]); memset(&fmt, 0, sizeof(fmt)); fmt.type = B_MEDIA_RAW_VIDEO; fmt.u.raw_video.display.format = cspaces_overlay[i]; err = gMediaRoster->Connect(output.source, input.destination, &fmt, &video_output, &video_input); if (err == B_OK) break; } } if (err) { fVideoNode->SetOverlayEnabled(false); for (int i = 0; cspaces_bitmap[i] != B_NO_COLOR_SPACE; i++) { printf("trying connect with colorspace 0x%08x\n", cspaces_bitmap[i]); memset(&fmt, 0, sizeof(fmt)); fmt.type = B_MEDIA_RAW_VIDEO; fmt.u.raw_video.display.format = cspaces_bitmap[i]; err = gMediaRoster->Connect(output.source, input.destination, &fmt, &video_output, &video_input); if (err == B_OK) break; } } HandleError("Can't connect video", err); // set time sources err = gMediaRoster->GetTimeSource(&time_node); HandleError("Can't get time source", err); BTimeSource *ts = gMediaRoster->MakeTimeSourceFor(time_node); err = gMediaRoster->SetTimeSourceFor(dvb_node.node, time_node.node); HandleError("Can't set dvb time source", err); err = gMediaRoster->SetTimeSourceFor(audio_mixer_node.node, time_node.node); HandleError("Can't set audio mixer time source", err); err = gMediaRoster->SetTimeSourceFor(video_window_node.node, time_node.node); HandleError("Can't set video window time source", err); // Add a delay of (2 video frames) to the buffers send by the DVB node, // because as a capture device in B_RECORDING run mode it's supposed to // deliver buffers that were captured in the past (and does so). // It is important that the audio buffer size used by the connection with // the DVB node is smaller than this, optimum is the same length as one // video frame (40 ms). However, this is not yet guaranteed. err = gMediaRoster->SetProducerRunModeDelay(dvb_node, 80000); HandleError("Can't set DVB producer delay", err); bigtime_t start_time = ts->Now() + 50000; ts->Release(); // start nodes err = gMediaRoster->StartNode(dvb_node, start_time); HandleError("Can't start dvb node", err); err = gMediaRoster->StartNode(audio_mixer_node, start_time); HandleError("Can't start audio mixer node", err); err = gMediaRoster->StartNode(video_window_node, start_time); HandleError("Can't start video window node", err); printf("running...\n"); fConnected = true; return B_OK; }
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; } }