diff --git a/ap2_buffered_audio_processor.c b/ap2_buffered_audio_processor.c index 0ef4beef..70e478e5 100644 --- a/ap2_buffered_audio_processor.c +++ b/ap2_buffered_audio_processor.c @@ -92,16 +92,17 @@ void rtp_buffered_audio_cleanup_handler(__attribute__((unused)) void *arg) { debug(2, "Buffered Audio Receiver Cleanup Start."); rtsp_conn_info *conn = (rtsp_conn_info *)arg; close(conn->buffered_audio_socket); - debug(1, "Connection %d: closing TCP Buffered Audio port: %u.", conn->connection_number, + debug(3, "Connection %d: closing TCP Buffered Audio port: %u.", conn->connection_number, conn->local_buffered_audio_port); conn->buffered_audio_socket = 0; - debug(1, "Connection %d: rtp_buffered_audio_processor exit.", conn->connection_number); + debug(2, "Connection %d: rtp_buffered_audio_processor exit.", conn->connection_number); } void *rtp_buffered_audio_processor(void *arg) { rtsp_conn_info *conn = (rtsp_conn_info *)arg; -#include - debug(1, "Connection %d: rtp_buffered_audio_processor PID %d start", conn->connection_number, syscall(SYS_gettid)); + // #include + // debug(1, "Connection %d: rtp_buffered_audio_processor PID %d start", conn->connection_number, + syscall(SYS_gettid)); conn->incoming_ssrc = 0; // reset conn->resampler_ssrc = 0; @@ -125,8 +126,8 @@ void *rtp_buffered_audio_processor(void *arg) { buffered_tcp_desc *buffered_audio = malloc(sizeof(buffered_tcp_desc)); if (buffered_audio == NULL) debug(1, "cannot allocate a buffered_tcp_desc!"); - // initialise the - + // initialise the + memset(buffered_audio, 0, sizeof(buffered_tcp_desc)); pthread_cleanup_push(malloc_cleanup, &buffered_audio); @@ -587,7 +588,8 @@ void *rtp_buffered_audio_processor(void *arg) { } } } while (finished == 0); - debug(1, "Connection %d: rtp_buffered_audio_processor PID %d exiting", conn->connection_number, syscall(SYS_gettid)); + // debug(1, "Connection %d: rtp_buffered_audio_processor PID %d exiting", conn->connection_number, + // syscall(SYS_gettid)); pthread_cleanup_pop(1); // buffered_tcp_reader thread creation pthread_cleanup_pop(1); // buffer malloc pthread_cleanup_pop(1); // not_full_cv @@ -596,6 +598,7 @@ void *rtp_buffered_audio_processor(void *arg) { pthread_cleanup_pop(1); // descriptor malloc pthread_cleanup_pop(1); // pthread_t malloc pthread_cleanup_pop(1); // do the cleanup. - debug(1, "Connection %d: rtp_buffered_audio_processor PID %d finish", conn->connection_number, syscall(SYS_gettid)); + // debug(1, "Connection %d: rtp_buffered_audio_processor PID %d finish", conn->connection_number, + // syscall(SYS_gettid)); pthread_exit(NULL); } diff --git a/player.c b/player.c index 1b76331b..fbcb80e1 100644 --- a/player.c +++ b/player.c @@ -3183,12 +3183,6 @@ int stuff_buffer_soxr_32(int32_t *inptr, int length, sps_format_t l_output_forma } #endif -void player_thread_initial_cleanup_handler(__attribute__((unused)) void *arg) { - rtsp_conn_info *conn = (rtsp_conn_info *)arg; - debug(3, "Connection %d: player thread main loop exit via player_thread_initial_cleanup_handler.", - conn->connection_number); -} - char line_of_stats[1024]; int statistics_row; // statistics_line 0 means print the headings; anything else 1 means print the // values. Set to 0 the first time out. @@ -3314,7 +3308,6 @@ void player_thread_cleanup_handler(void *arg) { #ifdef CONFIG_AIRPLAY_2 if (conn->airplay_type == ap_2) { debug(2, "Cancelling AP2 timing, control and audio threads..."); - if (conn->airplay_stream_type == realtime_stream) { debug(2, "Connection %d: Delete Realtime Audio Stream thread", conn->connection_number); pthread_cancel(conn->rtp_realtime_audio_thread); @@ -3337,7 +3330,6 @@ void player_thread_cleanup_handler(void *arg) { debug(2, "Connection %d: Delete AirPlay 2 Control thread", conn->connection_number); pthread_cancel(conn->rtp_ap2_control_thread); pthread_join(conn->rtp_ap2_control_thread, NULL); - } else { debug(2, "Cancelling AP1-compatible timing, control and audio threads..."); #else @@ -3358,10 +3350,10 @@ void player_thread_cleanup_handler(void *arg) { debug(3, "Join audio thread."); pthread_join(conn->rtp_audio_thread, NULL); debug(3, "Audio thread terminated."); + #ifdef CONFIG_AIRPLAY_2 } - ptp_send_control_message_string("T"); - // reset_anchor_info(conn); + ptp_send_control_message_string("E"); #endif if (conn->outbuf) { @@ -3373,11 +3365,6 @@ void player_thread_cleanup_handler(void *arg) { conn->tbuf = NULL; } - // if (conn->statistics) { - // free(conn->statistics); - // conn->statistics = NULL; - // } - free_audio_buffers(conn); if (conn->stream.type == ast_apple_lossless) { #ifdef CONFIG_APPLE_ALAC @@ -3385,15 +3372,16 @@ void player_thread_cleanup_handler(void *arg) { apple_alac_terminate(); } #endif + #ifdef CONFIG_HAMMERTON if (config.decoder_in_use == 1 << decoder_hammerton) { alac_free(conn->decoder_info); } #endif } - // no need to flush the FFMPEG decoder... conn->rtp_running = 0; + pthread_setcancelstate(oldState, NULL); debug(2, "Connection %d: player terminated.", conn->connection_number); } @@ -3420,7 +3408,6 @@ void *player_thread_func(void *arg) { 0; // initialised to avoid a "possibly uninitialised" warning int previous_frames_played_valid = 0; - // pthread_cleanup_push(player_thread_initial_cleanup_handler, arg); conn->latency_warning_issued = 0; // be permitted to generate a warning each time a play is attempted conn->packet_count = 0; diff --git a/rtsp.c b/rtsp.c index ae169ecb..224af845 100644 --- a/rtsp.c +++ b/rtsp.c @@ -430,12 +430,13 @@ int get_play_lock(rtsp_conn_info *conn, int allow_session_interruption) { if (principal_conn->fd > 0) { debug(1, - "Connection %d: get_play_lock forced termination in favour of connection %d. Closing RTSP connection socket %d: " + "Connection %d: get_play_lock forced termination in favour of connection %d. Closing " + "RTSP connection socket %d: " "from %s:%u to self at " "%s:%u.", - principal_conn->connection_number, conn->connection_number, principal_conn->fd, principal_conn->client_ip_string, - principal_conn->client_rtsp_port, principal_conn->self_ip_string, - principal_conn->self_rtsp_port); + principal_conn->connection_number, conn->connection_number, principal_conn->fd, + principal_conn->client_ip_string, principal_conn->client_rtsp_port, + principal_conn->self_ip_string, principal_conn->self_rtsp_port); close(principal_conn->fd); // principal_conn->fd = 0; } @@ -2538,6 +2539,7 @@ void handle_options(rtsp_conn_info *conn, __attribute__((unused)) rtsp_message * "OPTIONS, POST, GET, PUT"); } +/* void teardown_phase_one(rtsp_conn_info *conn) { // this can be called more than once on the same connection -- // by the player itself but also by the play session being killed @@ -2632,6 +2634,7 @@ void teardown_phase_two(rtsp_conn_info *conn) { } pthread_cleanup_pop(1); // release the principal_conn lock } +*/ void handle_teardown_2(rtsp_conn_info *conn, __attribute__((unused)) rtsp_message *req, rtsp_message *resp) { @@ -2644,41 +2647,9 @@ void handle_teardown_2(rtsp_conn_info *conn, __attribute__((unused)) rtsp_messag player_stop(conn); // this nulls the player_thread and cancels the threads... activity_monitor_signify_activity(0); // inactive, and should be after command_stop() } - /* - // msg_add_header(resp, "Connection", "close"); - plist_t messagePlist = plist_from_rtsp_content(req); - if (messagePlist != NULL) { - // now see if the incoming plist contains a "streams" array - plist_t streams = plist_dict_get_item(messagePlist, "streams"); - - if (streams) { - debug(1, "Connection %d: TEARDOWN %s Close the stream.", conn->connection_number, - get_category_string(conn->airplay_stream_category)); - // we are being asked to close a stream - - if (conn->player_thread) { - player_stop(conn); // this nulls the player_thread and cancels the threads... - activity_monitor_signify_activity(0); // inactive, and should be after command_stop() - } - - plist_free(streams); - debug(3, "Connection %d: TEARDOWN %s Close the stream complete", conn->connection_number, - get_category_string(conn->airplay_stream_category)); - } else { - debug(1, "Connection %d: TEARDOWN %s Close the connection.", conn->connection_number, - get_category_string(conn->airplay_stream_category)); - // teardown_phase_one(conn); // try to do phase one anyway - // teardown_phase_two(conn); - } - - plist_free(messagePlist); - resp->respcode = 200; - } else { - debug(1, "Connection %d: missing plist!", conn->connection_number); - resp->respcode = 451; // don't know what to do here - } - */ resp->respcode = 200; + msg_add_header(resp, "Connection", "close"); + // debug(1,"Bogus exit for valgrind -- remember to comment it out!."); // sps_shutdown(TOE_normal); // ask for a normal exit } @@ -3120,6 +3091,12 @@ void handle_setup_2(rtsp_conn_info *conn, rtsp_message *req, rtsp_message *resp) debug_log_rtsp_message(3, "SETUP (AirPlay 2) SETUP with streams incoming message", req); if (conn->airplay_stream_category == ptp_stream) { + if (conn->player_thread) { + debug(1, "stopping a running player during setup phase 2"); + player_stop(conn); // this nulls the player_thread and cancels the threads... + activity_monitor_signify_activity(0); // inactive, and should be after command_stop() + } + ptp_send_control_message_string( "B"); // signify clock dependability period is "B"eginning (or continuing) plist_t stream0 = plist_array_get_item(streams, 0); @@ -3295,13 +3272,10 @@ void handle_setup_2(rtsp_conn_info *conn, rtsp_message *req, rtsp_message *resp) activity_monitor_signify_activity(1); debug(1, "Connection %d: create rtp_buffered_audio_thread", conn->connection_number); - + named_pthread_create_with_priority(&conn->rtp_buffered_audio_thread, 2, &rtp_buffered_audio_processor, (void *)conn, "ap2_bat_%d", conn->connection_number); - - - usleep(1000000); plist_dict_set_item(stream0dict, "type", plist_new_uint(103)); plist_dict_set_item(stream0dict, "dataPort",