mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	module-rtp: move stream init to specific media types
Move the stream function setup to a stream specific method. Keep a separate stream format, that can be different later from the rtp format once we add encoding. Rename some methods to make them more unique.
This commit is contained in:
		
							parent
							
								
									35829468f8
								
							
						
					
					
						commit
						bcc052f5f1
					
				
					 3 changed files with 49 additions and 35 deletions
				
			
		| 
						 | 
					@ -2,7 +2,7 @@
 | 
				
			||||||
/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans <wim.taymans@gmail.com> */
 | 
					/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans <wim.taymans@gmail.com> */
 | 
				
			||||||
/* SPDX-License-Identifier: MIT */
 | 
					/* SPDX-License-Identifier: MIT */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void process_audio_playback(void *data)
 | 
					static void rtp_audio_process_playback(void *data)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = data;
 | 
						struct impl *impl = data;
 | 
				
			||||||
	struct pw_buffer *buf;
 | 
						struct pw_buffer *buf;
 | 
				
			||||||
| 
						 | 
					@ -94,7 +94,7 @@ static void process_audio_playback(void *data)
 | 
				
			||||||
	pw_stream_queue_buffer(impl->stream, buf);
 | 
						pw_stream_queue_buffer(impl->stream, buf);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int receive_rtp_audio(struct impl *impl, uint8_t *buffer, ssize_t len)
 | 
					static int rtp_audio_receive(struct impl *impl, uint8_t *buffer, ssize_t len)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct rtp_header *hdr;
 | 
						struct rtp_header *hdr;
 | 
				
			||||||
	ssize_t hlen, plen;
 | 
						ssize_t hlen, plen;
 | 
				
			||||||
| 
						 | 
					@ -202,13 +202,12 @@ set_iovec(struct spa_ringbuffer *rbuf, void *buffer, uint32_t size,
 | 
				
			||||||
	iov[1].iov_base = buffer;
 | 
						iov[1].iov_base = buffer;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void flush_audio_packets(struct impl *impl)
 | 
					static void rtp_audio_flush_packets(struct impl *impl)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	int32_t avail;
 | 
						int32_t avail, tosend;
 | 
				
			||||||
	uint32_t stride, timestamp;
 | 
						uint32_t stride, timestamp;
 | 
				
			||||||
	struct iovec iov[3];
 | 
						struct iovec iov[3];
 | 
				
			||||||
	struct rtp_header header;
 | 
						struct rtp_header header;
 | 
				
			||||||
	int32_t tosend;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	avail = spa_ringbuffer_get_read_index(&impl->ring, ×tamp);
 | 
						avail = spa_ringbuffer_get_read_index(&impl->ring, ×tamp);
 | 
				
			||||||
	tosend = impl->psamples;
 | 
						tosend = impl->psamples;
 | 
				
			||||||
| 
						 | 
					@ -246,7 +245,7 @@ static void flush_audio_packets(struct impl *impl)
 | 
				
			||||||
	spa_ringbuffer_read_update(&impl->ring, timestamp);
 | 
						spa_ringbuffer_read_update(&impl->ring, timestamp);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void process_audio_capture(void *data)
 | 
					static void rtp_audio_process_capture(void *data)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = data;
 | 
						struct impl *impl = data;
 | 
				
			||||||
	struct pw_buffer *buf;
 | 
						struct pw_buffer *buf;
 | 
				
			||||||
| 
						 | 
					@ -299,5 +298,15 @@ static void process_audio_capture(void *data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	pw_stream_queue_buffer(impl->stream, buf);
 | 
						pw_stream_queue_buffer(impl->stream, buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flush_audio_packets(impl);
 | 
						rtp_audio_flush_packets(impl);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int rtp_audio_init(struct impl *impl, enum spa_direction direction)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (direction == SPA_DIRECTION_INPUT)
 | 
				
			||||||
 | 
							impl->stream_events.process = rtp_audio_process_capture;
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							impl->stream_events.process = rtp_audio_process_playback;
 | 
				
			||||||
 | 
						impl->receive_rtp = rtp_audio_receive;
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2,7 +2,7 @@
 | 
				
			||||||
/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans <wim.taymans@gmail.com> */
 | 
					/* SPDX-FileCopyrightText: Copyright © 2022 Wim Taymans <wim.taymans@gmail.com> */
 | 
				
			||||||
/* SPDX-License-Identifier: MIT */
 | 
					/* SPDX-License-Identifier: MIT */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void process_midi_playback(void *data)
 | 
					static void rtp_midi_process_playback(void *data)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = data;
 | 
						struct impl *impl = data;
 | 
				
			||||||
	struct pw_buffer *buf;
 | 
						struct pw_buffer *buf;
 | 
				
			||||||
| 
						 | 
					@ -150,7 +150,7 @@ static double get_time(struct impl *impl)
 | 
				
			||||||
	return t;
 | 
						return t;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int receive_midi(struct impl *impl, uint8_t *packet, uint32_t timestamp,
 | 
					static int rtp_midi_receive_midi(struct impl *impl, uint8_t *packet, uint32_t timestamp,
 | 
				
			||||||
		uint16_t seq, uint32_t payload_offset, uint32_t plen)
 | 
							uint16_t seq, uint32_t payload_offset, uint32_t plen)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	uint32_t write;
 | 
						uint32_t write;
 | 
				
			||||||
| 
						 | 
					@ -268,11 +268,10 @@ static int receive_midi(struct impl *impl, uint8_t *packet, uint32_t timestamp,
 | 
				
			||||||
	write += b.state.offset;
 | 
						write += b.state.offset;
 | 
				
			||||||
	spa_ringbuffer_write_update(&impl->ring, write);
 | 
						spa_ringbuffer_write_update(&impl->ring, write);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
	return 0;
 | 
						return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static int receive_rtp_midi(struct impl *impl, uint8_t *buffer, ssize_t len)
 | 
					static int rtp_midi_receive(struct impl *impl, uint8_t *buffer, ssize_t len)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct rtp_header *hdr;
 | 
						struct rtp_header *hdr;
 | 
				
			||||||
	ssize_t hlen;
 | 
						ssize_t hlen;
 | 
				
			||||||
| 
						 | 
					@ -308,7 +307,7 @@ static int receive_rtp_midi(struct impl *impl, uint8_t *buffer, ssize_t len)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	impl->receiving = true;
 | 
						impl->receiving = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return receive_midi(impl, buffer, timestamp, seq, hlen, len);
 | 
						return rtp_midi_receive_midi(impl, buffer, timestamp, seq, hlen, len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
short_packet:
 | 
					short_packet:
 | 
				
			||||||
	pw_log_warn("short packet received");
 | 
						pw_log_warn("short packet received");
 | 
				
			||||||
| 
						 | 
					@ -347,7 +346,7 @@ static int write_event(uint8_t *p, uint32_t value, void *ev, uint32_t size)
 | 
				
			||||||
        return count + size;
 | 
					        return count + size;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void flush_midi_packets(struct impl *impl,
 | 
					static void rtp_midi_flush_packets(struct impl *impl,
 | 
				
			||||||
		struct spa_pod_sequence *sequence, uint32_t timestamp, uint32_t rate)
 | 
							struct spa_pod_sequence *sequence, uint32_t timestamp, uint32_t rate)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct spa_pod_control *c;
 | 
						struct spa_pod_control *c;
 | 
				
			||||||
| 
						 | 
					@ -442,7 +441,7 @@ static void flush_midi_packets(struct impl *impl,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void process_midi_capture(void *data)
 | 
					static void rtp_midi_process_capture(void *data)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct impl *impl = data;
 | 
						struct impl *impl = data;
 | 
				
			||||||
	struct pw_buffer *buf;
 | 
						struct pw_buffer *buf;
 | 
				
			||||||
| 
						 | 
					@ -481,8 +480,18 @@ static void process_midi_capture(void *data)
 | 
				
			||||||
		impl->have_sync = true;
 | 
							impl->have_sync = true;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flush_midi_packets(impl, (struct spa_pod_sequence*)pod, timestamp, rate);
 | 
						rtp_midi_flush_packets(impl, (struct spa_pod_sequence*)pod, timestamp, rate);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
done:
 | 
					done:
 | 
				
			||||||
	pw_stream_queue_buffer(impl->stream, buf);
 | 
						pw_stream_queue_buffer(impl->stream, buf);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static int rtp_midi_init(struct impl *impl, enum spa_direction direction)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						if (direction == SPA_DIRECTION_INPUT)
 | 
				
			||||||
 | 
							impl->stream_events.process = rtp_midi_process_capture;
 | 
				
			||||||
 | 
						else
 | 
				
			||||||
 | 
							impl->stream_events.process = rtp_midi_process_playback;
 | 
				
			||||||
 | 
						impl->receive_rtp = rtp_midi_receive;
 | 
				
			||||||
 | 
						return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,6 +35,7 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
struct impl {
 | 
					struct impl {
 | 
				
			||||||
	struct spa_audio_info info;
 | 
						struct spa_audio_info info;
 | 
				
			||||||
 | 
						struct spa_audio_info stream_info;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct pw_stream *stream;
 | 
						struct pw_stream *stream;
 | 
				
			||||||
	struct spa_hook stream_listener;
 | 
						struct spa_hook stream_listener;
 | 
				
			||||||
| 
						 | 
					@ -290,20 +291,23 @@ struct rtp_stream *rtp_stream_new(struct pw_core *core,
 | 
				
			||||||
		goto out;
 | 
							goto out;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (impl->info.media_type) {
 | 
						switch (impl->info.media_subtype) {
 | 
				
			||||||
	case SPA_MEDIA_TYPE_audio:
 | 
						case SPA_MEDIA_SUBTYPE_raw:
 | 
				
			||||||
		parse_audio_info(props, &impl->info.info.raw);
 | 
							parse_audio_info(props, &impl->info.info.raw);
 | 
				
			||||||
 | 
							impl->stream_info = impl->info;
 | 
				
			||||||
		impl->format_info = find_audio_format_info(&impl->info);
 | 
							impl->format_info = find_audio_format_info(&impl->info);
 | 
				
			||||||
		if (impl->format_info == NULL) {
 | 
							if (impl->format_info == NULL) {
 | 
				
			||||||
			pw_log_error("unsupported audio format:%d channels:%d",
 | 
								pw_log_error("unsupported audio format:%d channels:%d",
 | 
				
			||||||
					impl->info.info.raw.format, impl->info.info.raw.channels);
 | 
										impl->stream_info.info.raw.format,
 | 
				
			||||||
 | 
										impl->stream_info.info.raw.channels);
 | 
				
			||||||
			res = -EINVAL;
 | 
								res = -EINVAL;
 | 
				
			||||||
			goto out;
 | 
								goto out;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		impl->stride = impl->format_info->size * impl->info.info.raw.channels;
 | 
							impl->stride = impl->format_info->size * impl->stream_info.info.raw.channels;
 | 
				
			||||||
		impl->rate = impl->info.info.raw.rate;
 | 
							impl->rate = impl->stream_info.info.raw.rate;
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case SPA_MEDIA_TYPE_application:
 | 
						case SPA_MEDIA_SUBTYPE_control:
 | 
				
			||||||
 | 
							impl->stream_info = impl->info;
 | 
				
			||||||
		impl->format_info = find_audio_format_info(&impl->info);
 | 
							impl->format_info = find_audio_format_info(&impl->info);
 | 
				
			||||||
		if (impl->format_info == NULL) {
 | 
							if (impl->format_info == NULL) {
 | 
				
			||||||
			res = -EINVAL;
 | 
								res = -EINVAL;
 | 
				
			||||||
| 
						 | 
					@ -401,27 +405,19 @@ struct rtp_stream *rtp_stream_new(struct pw_core *core,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	flags = PW_STREAM_FLAG_MAP_BUFFERS | PW_STREAM_FLAG_RT_PROCESS;
 | 
						flags = PW_STREAM_FLAG_MAP_BUFFERS | PW_STREAM_FLAG_RT_PROCESS;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (impl->info.media_type) {
 | 
						switch (impl->info.media_subtype) {
 | 
				
			||||||
	case SPA_MEDIA_TYPE_audio:
 | 
						case SPA_MEDIA_SUBTYPE_raw:
 | 
				
			||||||
		params[n_params++] = spa_format_audio_build(&b,
 | 
							params[n_params++] = spa_format_audio_build(&b,
 | 
				
			||||||
				SPA_PARAM_EnumFormat, &impl->info);
 | 
									SPA_PARAM_EnumFormat, &impl->stream_info);
 | 
				
			||||||
		if (direction == SPA_DIRECTION_INPUT)
 | 
					 | 
				
			||||||
			impl->stream_events.process = process_audio_capture;
 | 
					 | 
				
			||||||
		else
 | 
					 | 
				
			||||||
			impl->stream_events.process = process_audio_playback;
 | 
					 | 
				
			||||||
		impl->receive_rtp = receive_rtp_audio;
 | 
					 | 
				
			||||||
		flags |= PW_STREAM_FLAG_AUTOCONNECT;
 | 
							flags |= PW_STREAM_FLAG_AUTOCONNECT;
 | 
				
			||||||
 | 
							rtp_audio_init(impl, direction);
 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	case SPA_MEDIA_TYPE_application:
 | 
						case SPA_MEDIA_SUBTYPE_control:
 | 
				
			||||||
		params[n_params++] = spa_pod_builder_add_object(&b,
 | 
							params[n_params++] = spa_pod_builder_add_object(&b,
 | 
				
			||||||
                                SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
 | 
					                                SPA_TYPE_OBJECT_Format, SPA_PARAM_EnumFormat,
 | 
				
			||||||
                                SPA_FORMAT_mediaType,           SPA_POD_Id(SPA_MEDIA_TYPE_application),
 | 
					                                SPA_FORMAT_mediaType,           SPA_POD_Id(SPA_MEDIA_TYPE_application),
 | 
				
			||||||
                                SPA_FORMAT_mediaSubtype,        SPA_POD_Id(SPA_MEDIA_SUBTYPE_control));
 | 
					                                SPA_FORMAT_mediaSubtype,        SPA_POD_Id(SPA_MEDIA_SUBTYPE_control));
 | 
				
			||||||
		if (direction == SPA_DIRECTION_INPUT)
 | 
							rtp_midi_init(impl, direction);
 | 
				
			||||||
			impl->stream_events.process = process_midi_capture;
 | 
					 | 
				
			||||||
		else
 | 
					 | 
				
			||||||
			impl->stream_events.process = process_midi_playback;
 | 
					 | 
				
			||||||
		impl->receive_rtp = receive_rtp_midi;
 | 
					 | 
				
			||||||
		break;
 | 
							break;
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		res = -EINVAL;
 | 
							res = -EINVAL;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue