fix(phone): keep shared media alive and let unbound calls be hung up

With call waiting, one browser owns one media session and both calls share it.
Ending the first call removed that session unconditionally and cleared the
browser's lease, so the waiting call could no longer be hung up (403 control
lease does not own this call) or rejected (403 invalid media control lease).

- release the media only when no other live call still binds it;
- allow hanging up a call that no browser has bound yet (a ringing inbound
  call has no lease to compare against);
- allow rejecting such a call even when the caller's media is already gone,
  because dropping a ringing call needs no audio path.
This commit is contained in:
miaoguoqiang committed 2026-11-18 12:51:44 +08:00
1 parent b18c9017c7
commit 22fbbdcf64
3 files changed
+80 -11

No files matched your search

+56 -10
View File
@@ -134,28 +134,34 @@ func (s *Service) Answer(ctx context.Context, request ControlRequest) (CallView,
}
func (s *Service) Reject(request ControlRequest) error {
call, _, reservation, err := s.claimIncoming(request)
call, _, reservation, err := s.claimIncomingWithMedia(request, true)
if err != nil {
return err
}
defer s.releaseIncomingClaim(call)
defer s.releaseMediaReservation(reservation)
var pendingMediaDrop bool
s.mu.Lock()
if current := s.calls[request.CallID]; current != call || call.terminal {
s.mu.Unlock()
return errActiveCallNotFound
}
pendingMediaDrop, err := s.bindMediaLocked(request.CallID, reservation)
if err != nil {
s.mu.Unlock()
return err
// A reject with no media left is still a reject: the call is dropped at the
// modem, nothing needs an audio path.
if reservation != nil {
pendingMediaDrop, err = s.bindMediaLocked(request.CallID, reservation)
if err != nil {
s.mu.Unlock()
return err
}
call.owner, call.lease, call.mediaID = request.Owner, request.Lease, reservation.mediaID
call.view.MediaID = reservation.mediaID
}
call.userRejected = true
call.owner, call.lease, call.mediaID = request.Owner, request.Lease, reservation.mediaID
call.view.MediaID = reservation.mediaID
deviceID := call.view.DeviceID
mediaID := call.mediaID
s.mu.Unlock()
s.resumePendingMediaDrop(reservation.mediaID, pendingMediaDrop)
s.resumePendingMediaDrop(mediaID, pendingMediaDrop)
if err := s.gateway.RejectIncomingCall(voicehost.RejectRequest{
DeviceID: deviceID, CallID: request.CallID, StatusCode: 486,
}); err != nil {
@@ -257,9 +263,20 @@ func (s *Service) setCallHold(ctx context.Context, owner, callID, lease string,
}
func (s *Service) claimIncoming(request ControlRequest) (*activeCall, *MediaSession, *mediaReservation, error) {
return s.claimIncomingWithMedia(request, false)
}
// claimIncomingWithMedia claims a pending inbound call. When allowMissingMedia
// is set, a browser whose own media session was already released (the previous
// call ended and took it with it) can still reject the waiting call: rejecting
// needs no audio path, only the right to hang it up.
func (s *Service) claimIncomingWithMedia(request ControlRequest, allowMissingMedia bool) (*activeCall, *MediaSession, *mediaReservation, error) {
media, reservation, err := s.reserveControlledMedia(request.Owner, request.MediaID, request.Lease)
if err != nil {
return nil, nil, nil, err
if !allowMissingMedia {
return nil, nil, nil, err
}
return s.claimIncomingWithoutMedia(request, err)
}
s.mu.Lock()
call := s.calls[request.CallID]
@@ -282,6 +299,30 @@ func (s *Service) claimIncoming(request ControlRequest) (*activeCall, *MediaSess
return call, media, reservation, nil
}
// claimIncomingWithoutMedia handles the reject path when the caller's media is
// gone. It only applies to a call that no browser has bound yet; otherwise the
// original lease error stands and nobody can silently take over a controlled
// call.
func (s *Service) claimIncomingWithoutMedia(request ControlRequest, cause error) (*activeCall, *MediaSession, *mediaReservation, error) {
s.mu.Lock()
call := s.calls[request.CallID]
if call == nil || call.terminal || call.view.Direction != "inbound" || call.controlPending {
s.mu.Unlock()
return nil, nil, nil, errors.New("phone: pending incoming call not found")
}
if call.lease != "" {
s.mu.Unlock()
return nil, nil, nil, cause
}
call.controlPending = true
if call.autoAnswerTimer != nil {
call.autoAnswerTimer.Stop()
call.autoAnswerTimer = nil
}
s.mu.Unlock()
return call, nil, nil, nil
}
func (s *Service) releaseIncomingClaim(call *activeCall) {
if call == nil {
return
@@ -301,7 +342,12 @@ func (s *Service) controlledCall(owner, callID, lease string) (*activeCall, erro
return nil, errActiveCallNotFound
}
if call.owner != owner || !secureEqual(call.lease, lease) {
return nil, errors.New("phone: control lease does not own this call")
// An unbound call (typically a ringing inbound call nobody has answered)
// has no lease to compare against. Requiring one turns "hang up the
// waiting call" into a permanent 403 and leaves the call stuck.
if !(call.lease == "" && call.owner == "") {
return nil, errors.New("phone: control lease does not own this call")
}
}
return call, nil
}
+7 -1
View File
@@ -304,12 +304,18 @@ func (s *Service) finishCall(event voicehost.CallEvent) {
s.clearPendingMediaDropLocked(call.mediaID)
s.terminalSeen[event.CallID] = struct{}{}
mediaID := call.mediaID
// One browser owns one media session, and call waiting hands the same
// session to the second call. Ending the first call must not tear that
// session down while the other call still depends on it.
mediaShared := mediaID != "" && s.mediaUsedByOtherCallLocked(mediaID, call.view.CallID)
deviceID, peer, direction := call.view.DeviceID, call.view.Peer, call.view.Direction
s.mu.Unlock()
defer call.terminalOnce.Do(func() { close(call.terminalDone) })
s.stopMixedRecording(call)
if mediaID != "" {
if mediaID != "" && !mediaShared {
s.media.Remove(mediaID)
} else if mediaShared {
logger.Info("媒体仍被其他通话使用,保留会话", "media_id", mediaID, "ended_call", call.view.CallID)
}
s.mu.RLock()
record := call.record
+17
View File
@@ -75,3 +75,20 @@ func (s *Service) bindMediaLocked(callID string, reservation *mediaReservation)
s.mediaCalls[reservation.mediaID] = callID
return s.clearPendingMediaDropLocked(reservation.mediaID), nil
}
// mediaUsedByOtherCallLocked reports whether a live call still binds the given
// browser media. Callers must hold s.mu.
func (s *Service) mediaUsedByOtherCallLocked(mediaID, exceptCallID string) bool {
if mediaID == "" {
return false
}
for callID, call := range s.calls {
if callID == exceptCallID || call == nil || call.terminal {
continue
}
if call.mediaID == mediaID {
return true
}
}
return false
}