示例#1
0
status_t
GameSoundBuffer::Connect(media_node * consumer)
{
	BMediaRoster* roster = BMediaRoster::Roster();
	status_t err = roster->RegisterNode(fNode);
	
	if (err != B_OK) 
		return err;
	
	// make sure the Media Roster knows that we're using the node
	err = roster->GetNodeFor(fNode->Node().node, &fConnection->producer);

	if (err != B_OK)
        	return err;
	
	// connect to the mixer
	fConnection->consumer = *consumer;

	// set the producer's time source to be the "default" time source, which
	// the Mixer uses too.
	err = roster->GetTimeSource(&fConnection->timeSource);
	if (err != B_OK)
                return err;	

	err = roster->SetTimeSourceFor(fConnection->producer.node, fConnection->timeSource.node);
        if (err != B_OK)
                return err;
	// got the nodes; now we find the endpoints of the connection
	media_input mixerInput;
	media_output soundOutput;
	int32 count = 1;
	err = roster->GetFreeOutputsFor(fConnection->producer, &soundOutput, 1, &count);
	if (err != B_OK) 
		return err;
	count = 1;
	err = roster->GetFreeInputsFor(fConnection->consumer, &mixerInput, 1, &count);
	if (err != B_OK) 
		return err;

	// 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 = roster->Connect(soundOutput.source, mixerInput.destination, &format, &soundOutput, &mixerInput);
	if (err != B_OK) 
		return err;
	
	// 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.
	fConnection->format = format;
	fConnection->source = soundOutput.source;
	fConnection->destination = mixerInput.destination;

	fIsConnected = true;
	return B_OK;
}
示例#2
0
// -------------------------------------------------------- //
// implementation of BFileInterface
// -------------------------------------------------------- //
status_t MediaReader::SetRef(
				const entry_ref & file,
				bool create,
				bigtime_t * out_time)
{
	CALLED();

	status_t status = AbstractFileInterfaceNode::SetRef(file,B_READ_ONLY,create,out_time);
	if (status != B_OK) {
		PRINT("AbstractFileInterfaceNode::SetRef returned an error\n");
		return status;
	}

	if (output.destination == media_destination::null) {
		// reset the format, and set the requirements imposed by this file
		GetFormat(&output.format);
		AddRequirements(&output.format);
		return B_OK;
	}

	// if we are connected we may have to re-negotiate the connection
	media_format format;
	GetFormat(&format);
	AddRequirements(&format);
	if (format_is_acceptible(format,output.format)) {
		fprintf(stderr,"  compatible format = no re-negotiation necessary\n");
		return B_OK;
	}
	// first try the easy way : SORRY DEPRECATED into private :-(
	// this code from MediaWriter would be different for MediaReader even if it worked...
	//	int32 change_tag = NewChangeTag();
	//	status = this->BBufferConsumer::RequestFormatChange(output.source,output.destination,&format,&change_tag);
	//	if (status == B_OK) {
	//		fprintf(stderr,"  format change successful\n");
	//		return B_OK;
	//	}

	// okay, the hard way requires we get the MediaRoster
	BMediaRoster * roster = BMediaRoster::Roster(&status);
	if (roster == 0)
		return B_MEDIA_SYSTEM_FAILURE;

	if (status != B_OK)
		return status;

	// before disconnect one should always stop the nodes (bebook says)
	// requires run_state cast since the return type on RunState() is
	// wrong [int32]
	run_state destinationRunState = run_state(RunState());
	if (destinationRunState == BMediaEventLooper::B_STARTED)
		Stop(0,true); // stop us right now

	// should also stop the destination if it is running, but how?
	/*	BMediaNode destinationNode = ??
	run_state destinationRunState = destinationNode->??;
	status = destinationNode->StopNode(??,0,true);
	if (status != B_OK) {
		return status;
	}  */
	// we should disconnect right now
	media_destination outputDestination = output.destination;
	status = roster->Disconnect(output.source.id,output.source,
							    output.destination.id,output.destination);
	if (status != B_OK)
		return status;

	// if that went okay, we'll try reconnecting	
	media_output connectOutput;
	media_input connectInput;
	status = roster->Connect(output.source,outputDestination,
							 &format,&connectOutput,&connectInput);
	if (status != B_OK)
		return status;

	// now restart if necessary
	if (destinationRunState == BMediaEventLooper::B_STARTED) {
		Start(0);
	}							 
	return status;
}
示例#3
0
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;
	}
}
示例#4
0
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;
    }
}