Lines Matching refs:iodev
42 static void cras_iodev_alloc_dsp(struct cras_iodev *iodev);
68 static int cras_iodev_start(struct cras_iodev *iodev)
71 if (!cras_iodev_is_open(iodev))
73 if (!iodev->start) {
76 iodev->info.name);
79 rc = iodev->start(iodev);
82 iodev->state = CRAS_IODEV_STATE_NORMAL_RUN;
123 * iodev[in] - The output device.
260 static size_t get_best_rate(struct cras_iodev *iodev, size_t rrate)
265 if (iodev->supported_rates[0] == 0) /* No rates supported */
268 for (i = 0, best = 0; iodev->supported_rates[i] != 0; i++) {
269 if (rrate == iodev->supported_rates[i] &&
272 if (best == 0 && (rrate % iodev->supported_rates[i] == 0 ||
273 iodev->supported_rates[i] % rrate == 0))
274 best = iodev->supported_rates[i];
279 return iodev->supported_rates[0];
288 static size_t get_best_channel_count(struct cras_iodev *iodev, size_t count)
293 assert(iodev->supported_channel_counts[0] != 0);
295 for (i = 0; iodev->supported_channel_counts[i] != 0; i++) {
296 if (iodev->supported_channel_counts[i] == count)
303 for (i = 0; iodev->supported_channel_counts[i] != 0; i++) {
304 if (iodev->supported_channel_counts[i] ==
309 return iodev->supported_channel_counts[0];
314 static snd_pcm_format_t get_best_pcm_format(struct cras_iodev *iodev,
319 for (i = 0; iodev->supported_formats[i] != 0; i++) {
320 if (fmt == iodev->supported_formats[i])
324 return iodev->supported_formats[0];
327 /* Set default channel layout to an iodev. */
328 static void set_default_channel_layout(struct cras_iodev *iodev)
334 default_layout[i] = i < iodev->format->num_channels ? i : -1;
336 cras_audio_format_set_channel_layout(iodev->format, default_layout);
337 cras_audio_format_set_channel_layout(iodev->ext_format, default_layout);
340 /* Applies the DSP to the samples for the iodev if applicable. */
341 static void apply_dsp(struct cras_iodev *iodev, uint8_t *buf, size_t frames)
346 ctx = iodev->dsp_context;
361 static void cras_iodev_free_dsp(struct cras_iodev *iodev)
363 if (iodev->dsp_context) {
364 cras_dsp_context_free(iodev->dsp_context);
365 iodev->dsp_context = NULL;
371 static inline void adjust_dev_channel_for_dsp(const struct cras_iodev *iodev)
373 struct cras_dsp_context *ctx = iodev->dsp_context;
378 if (iodev->direction == CRAS_STREAM_OUTPUT) {
379 iodev->format->num_channels =
381 iodev->ext_format->num_channels =
384 iodev->format->num_channels =
386 iodev->ext_format->num_channels =
398 static void update_channel_layout(struct cras_iodev *iodev)
408 if (iodev->direction == CRAS_STREAM_OUTPUT &&
409 iodev->format->num_channels <= 2) {
410 set_default_channel_layout(iodev);
414 if (iodev->update_channel_layout == NULL)
417 rc = iodev->update_channel_layout(iodev);
419 set_default_channel_layout(iodev);
422 iodev->ext_format,
423 iodev->format->channel_layout);
427 int cras_iodev_set_format(struct cras_iodev *iodev,
436 if (iodev->format == NULL) {
437 iodev->format = malloc(sizeof(struct cras_audio_format));
438 iodev->ext_format = malloc(sizeof(struct cras_audio_format));
439 if (!iodev->format || !iodev->ext_format)
441 *iodev->format = *fmt;
442 *iodev->ext_format = *fmt;
444 if (iodev->update_supported_formats) {
445 rc = iodev->update_supported_formats(iodev);
455 actual_rate = get_best_rate(iodev, fmt->frame_rate);
456 iodev->format->frame_rate = actual_rate;
457 iodev->ext_format->frame_rate = actual_rate;
459 cras_iodev_alloc_dsp(iodev);
460 if (iodev->dsp_context)
461 adjust_dev_channel_for_dsp(iodev);
463 actual_num_channels = get_best_channel_count(iodev,
464 iodev->format->num_channels);
465 actual_format = get_best_pcm_format(iodev, fmt->format);
472 iodev->format->format = actual_format;
473 iodev->ext_format->format = actual_format;
474 if (iodev->format->num_channels != actual_num_channels) {
476 iodev->format->num_channels = actual_num_channels;
477 iodev->ext_format->num_channels = actual_num_channels;
478 cras_iodev_free_dsp(iodev);
481 update_channel_layout(iodev);
483 if (!iodev->rate_est)
484 iodev->rate_est = rate_estimator_create(
489 rate_estimator_reset_rate(iodev->rate_est, actual_rate);
495 free(iodev->format);
496 free(iodev->ext_format);
497 iodev->format = NULL;
498 iodev->ext_format = NULL;
502 void cras_iodev_update_dsp(struct cras_iodev *iodev)
506 if (!iodev->dsp_context)
509 cras_dsp_set_variable_string(iodev->dsp_context, "dsp_name",
510 iodev->dsp_name ? : "");
512 if (iodev->active_node && iodev->active_node->left_right_swapped)
515 cras_dsp_set_variable_boolean(iodev->dsp_context, "swap_lr_disabled",
518 cras_dsp_load_pipeline(iodev->dsp_context);
522 int cras_iodev_dsp_set_swap_mode_for_node(struct cras_iodev *iodev,
535 if (iodev->active_node == node)
536 cras_iodev_update_dsp(iodev);
540 void cras_iodev_free_format(struct cras_iodev *iodev)
542 free(iodev->format);
543 free(iodev->ext_format);
544 iodev->format = NULL;
545 iodev->ext_format = NULL;
549 void cras_iodev_init_audio_area(struct cras_iodev *iodev,
552 if (iodev->area)
553 cras_iodev_free_audio_area(iodev);
555 iodev->area = cras_audio_area_create(num_channels);
556 cras_audio_area_config_channels(iodev->area, iodev->format);
559 void cras_iodev_free_audio_area(struct cras_iodev *iodev)
561 if (!iodev->area)
564 cras_audio_area_destroy(iodev->area);
565 iodev->area = NULL;
568 void cras_iodev_free_resources(struct cras_iodev *iodev)
570 cras_iodev_free_dsp(iodev);
571 rate_estimator_destroy(iodev->rate_est);
572 if (iodev->ramp)
573 cras_ramp_destroy(iodev->ramp);
576 static void cras_iodev_alloc_dsp(struct cras_iodev *iodev)
580 if (iodev->direction == CRAS_STREAM_OUTPUT)
585 cras_iodev_free_dsp(iodev);
586 iodev->dsp_context = cras_dsp_context_new(iodev->format->frame_rate,
588 cras_iodev_update_dsp(iodev);
694 void cras_iodev_add_node(struct cras_iodev *iodev, struct cras_ionode *node)
696 DL_APPEND(iodev->nodes, node);
700 void cras_iodev_rm_node(struct cras_iodev *iodev, struct cras_ionode *node)
702 DL_DELETE(iodev->nodes, node);
706 void cras_iodev_set_active_node(struct cras_iodev *iodev,
709 iodev->active_node = node;
710 cras_iodev_list_notify_active_node_changed(iodev->direction);
713 float cras_iodev_get_software_volume_scaler(struct cras_iodev *iodev)
718 iodev, cras_system_get_volume());
720 if (iodev->active_node && iodev->active_node->softvol_scalers)
721 return iodev->active_node->softvol_scalers[volume];
725 float cras_iodev_get_software_gain_scaler(const struct cras_iodev *iodev) {
727 if (cras_iodev_software_volume_needed(iodev)) {
729 iodev, cras_system_get_capture_gain());
735 int cras_iodev_add_stream(struct cras_iodev *iodev,
739 DL_APPEND(iodev->streams, stream);
741 if (!iodev->buf_state)
742 iodev->buf_state = buffer_share_create(iodev->buffer_size);
743 buffer_share_add_id(iodev->buf_state, stream->stream->stream_id, NULL);
745 iodev->min_cb_level = MIN(iodev->min_cb_level, cb_threshold);
746 iodev->max_cb_level = MAX(iodev->max_cb_level, cb_threshold);
750 struct dev_stream *cras_iodev_rm_stream(struct cras_iodev *iodev,
756 unsigned int old_min_cb_level = iodev->min_cb_level;
758 iodev->min_cb_level = iodev->buffer_size / 2;
759 iodev->max_cb_level = 0;
760 DL_FOREACH(iodev->streams, out) {
762 buffer_share_rm_id(iodev->buf_state,
765 DL_DELETE(iodev->streams, out);
769 iodev->min_cb_level = MIN(iodev->min_cb_level, cb_threshold);
770 iodev->max_cb_level = MAX(iodev->max_cb_level, cb_threshold);
773 if (!iodev->streams) {
774 buffer_share_destroy(iodev->buf_state);
775 iodev->buf_state = NULL;
776 iodev->min_cb_level = old_min_cb_level;
780 if ((iodev->direction == CRAS_STREAM_OUTPUT) &&
781 (iodev->state == CRAS_IODEV_STATE_NORMAL_RUN))
782 cras_iodev_no_stream_playback_transition(iodev, 1);
787 unsigned int cras_iodev_stream_offset(struct cras_iodev *iodev,
790 return buffer_share_id_offset(iodev->buf_state,
794 void cras_iodev_stream_written(struct cras_iodev *iodev,
798 buffer_share_offset_update(iodev->buf_state,
802 unsigned int cras_iodev_all_streams_written(struct cras_iodev *iodev)
804 if (!iodev->buf_state)
806 return buffer_share_get_new_write_point(iodev->buf_state);
809 unsigned int cras_iodev_max_stream_offset(const struct cras_iodev *iodev)
814 DL_FOREACH(iodev->streams, curr) {
816 buffer_share_id_offset(iodev->buf_state,
823 int cras_iodev_open(struct cras_iodev *iodev, unsigned int cb_level)
827 rc = iodev->open_dev(iodev);
832 iodev->min_cb_level = MIN(iodev->buffer_size / 2, cb_level);
833 iodev->max_cb_level = 0;
835 iodev->reset_request_pending = 0;
836 iodev->state = CRAS_IODEV_STATE_OPEN;
838 if (iodev->direction == CRAS_STREAM_OUTPUT) {
841 if (iodev->start)
842 iodev->state = CRAS_IODEV_STATE_OPEN;
844 iodev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
849 iodev->state = CRAS_IODEV_STATE_NORMAL_RUN;
855 enum CRAS_IODEV_STATE cras_iodev_state(const struct cras_iodev *iodev)
857 return iodev->state;
860 int cras_iodev_close(struct cras_iodev *iodev)
863 if (!cras_iodev_is_open(iodev))
866 rc = iodev->close_dev(iodev);
869 iodev->state = CRAS_IODEV_STATE_CLOSE;
870 if (iodev->ramp)
871 cras_ramp_reset(iodev->ramp);
875 int cras_iodev_put_input_buffer(struct cras_iodev *iodev, unsigned int nframes)
877 rate_estimator_add_frames(iodev->rate_est, -nframes);
878 return iodev->put_buffer(iodev, nframes);
881 int cras_iodev_put_output_buffer(struct cras_iodev *iodev, uint8_t *frames,
884 const struct cras_audio_format *fmt = iodev->format;
893 int software_volume_needed = cras_iodev_software_volume_needed(iodev);
895 if (iodev->pre_dsp_hook)
896 iodev->pre_dsp_hook(frames, nframes, iodev->ext_format,
897 iodev->pre_dsp_hook_cb_data);
899 if (iodev->ramp) {
900 ramp_action = cras_ramp_get_current_action(iodev->ramp);
905 if (output_should_mute(iodev) &&
910 apply_dsp(iodev, frames, nframes);
912 if (iodev->post_dsp_hook)
913 iodev->post_dsp_hook(frames, nframes, fmt,
914 iodev->post_dsp_hook_cb_data);
919 cras_iodev_get_software_volume_scaler(iodev);
937 cras_ramp_update_ramped_frames(iodev->ramp, nframes);
949 iodev->format,
952 rate_estimator_add_frames(iodev->rate_est, nframes);
953 return iodev->put_buffer(iodev, nframes);
956 int cras_iodev_get_input_buffer(struct cras_iodev *iodev,
960 const struct cras_audio_format *fmt = iodev->format;
966 rc = iodev->get_buffer(iodev, area, frames);
982 apply_dsp(iodev, hw_buffer, *frames); /* TODO-applied 2x */
987 int cras_iodev_get_output_buffer(struct cras_iodev *iodev,
994 rc = iodev->get_buffer(iodev, area, frames);
1004 int cras_iodev_update_rate(struct cras_iodev *iodev, unsigned int level,
1007 return rate_estimator_check(iodev->rate_est, level, level_tstamp);
1010 int cras_iodev_reset_rate_estimator(const struct cras_iodev *iodev)
1012 rate_estimator_reset_rate(iodev->rate_est,
1013 iodev->ext_format->frame_rate);
1017 double cras_iodev_get_est_rate_ratio(const struct cras_iodev *iodev)
1019 return rate_estimator_get_rate(iodev->rate_est) /
1020 iodev->ext_format->frame_rate;
1023 int cras_iodev_get_dsp_delay(const struct cras_iodev *iodev)
1029 ctx = iodev->dsp_context;
1043 int cras_iodev_frames_queued(struct cras_iodev *iodev,
1048 rc = iodev->frames_queued(iodev, hw_tstamp);
1049 if (rc < 0 || iodev->direction == CRAS_STREAM_INPUT)
1052 if (rc < iodev->min_buffer_level)
1055 return rc - iodev->min_buffer_level;
1058 int cras_iodev_buffer_avail(struct cras_iodev *iodev, unsigned hw_level)
1060 if (iodev->direction == CRAS_STREAM_INPUT)
1063 if (hw_level + iodev->min_buffer_level > iodev->buffer_size)
1066 return iodev->buffer_size - iodev->min_buffer_level - hw_level;
1069 void cras_iodev_register_pre_dsp_hook(struct cras_iodev *iodev,
1073 iodev->pre_dsp_hook = loop_cb;
1074 iodev->pre_dsp_hook_cb_data = cb_data;
1077 void cras_iodev_register_post_dsp_hook(struct cras_iodev *iodev,
1081 iodev->post_dsp_hook = loop_cb;
1082 iodev->post_dsp_hook_cb_data = cb_data;
1191 unsigned int cras_iodev_get_num_underruns(const struct cras_iodev *iodev)
1193 if (iodev->get_num_underruns)
1194 return iodev->get_num_underruns(iodev);
1198 unsigned int cras_iodev_get_num_severe_underruns(const struct cras_iodev *iodev)
1200 if (iodev->get_num_severe_underruns)
1201 return iodev->get_num_severe_underruns(iodev);
1205 int cras_iodev_reset_request(struct cras_iodev* iodev)
1217 if (iodev->reset_request_pending)
1219 iodev->reset_request_pending = 1;
1220 return cras_device_monitor_reset_device(iodev);
1280 int cras_iodev_set_mute(struct cras_iodev* iodev)
1282 if (!cras_iodev_is_open(iodev))
1285 if (iodev->set_mute)
1286 iodev->set_mute(iodev);