push_video_stream_subscription_impl.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/video_capture/push_video_stream_subscription_impl.h"
  5. #include "base/bind.h"
  6. #include "base/callback_helpers.h"
  7. #include "services/video_capture/broadcasting_receiver.h"
  8. namespace video_capture {
  9. PushVideoStreamSubscriptionImpl::PushVideoStreamSubscriptionImpl(
  10. mojo::PendingReceiver<mojom::PushVideoStreamSubscription>
  11. subscription_receiver,
  12. mojo::PendingRemote<mojom::VideoFrameHandler> subscriber,
  13. const media::VideoCaptureParams& requested_settings,
  14. mojom::VideoSource::CreatePushSubscriptionCallback creation_callback,
  15. BroadcastingReceiver* broadcaster,
  16. mojo::Remote<mojom::Device>* device)
  17. : receiver_(this, std::move(subscription_receiver)),
  18. subscriber_(std::move(subscriber)),
  19. requested_settings_(requested_settings),
  20. creation_callback_(std::move(creation_callback)),
  21. broadcaster_(broadcaster),
  22. device_(device),
  23. status_(Status::kCreationCallbackNotYetRun),
  24. broadcaster_client_id_(0) {
  25. DCHECK(broadcaster_);
  26. DCHECK(device_);
  27. }
  28. PushVideoStreamSubscriptionImpl::~PushVideoStreamSubscriptionImpl() = default;
  29. void PushVideoStreamSubscriptionImpl::SetOnClosedHandler(
  30. base::OnceCallback<void(base::OnceClosure done_cb)> handler) {
  31. on_closed_handler_ = std::move(handler);
  32. receiver_.set_disconnect_handler(
  33. base::BindOnce(&PushVideoStreamSubscriptionImpl::OnConnectionLost,
  34. weak_factory_.GetWeakPtr()));
  35. }
  36. void PushVideoStreamSubscriptionImpl::OnDeviceStartSucceededWithSettings(
  37. const media::VideoCaptureParams& settings) {
  38. if (status_ != Status::kCreationCallbackNotYetRun) {
  39. // Creation callback has already been run from a previous device start.
  40. return;
  41. }
  42. mojom::CreatePushSubscriptionSuccessCode success_code =
  43. settings == requested_settings_
  44. ? mojom::CreatePushSubscriptionSuccessCode::
  45. kCreatedWithRequestedSettings
  46. : mojom::CreatePushSubscriptionSuccessCode::
  47. kCreatedWithDifferentSettings;
  48. std::move(creation_callback_)
  49. .Run(
  50. mojom::CreatePushSubscriptionResultCode::NewSuccessCode(success_code),
  51. settings);
  52. status_ = Status::kNotYetActivated;
  53. }
  54. void PushVideoStreamSubscriptionImpl::OnDeviceStartFailed(
  55. media::VideoCaptureError error) {
  56. DCHECK_NE(error, media::VideoCaptureError::kNone);
  57. if (status_ != Status::kCreationCallbackNotYetRun) {
  58. // Creation callback has already been run from a previous device start.
  59. return;
  60. }
  61. std::move(creation_callback_)
  62. .Run(mojom::CreatePushSubscriptionResultCode::NewErrorCode(error),
  63. requested_settings_);
  64. status_ = Status::kClosed;
  65. }
  66. void PushVideoStreamSubscriptionImpl::Activate() {
  67. if (status_ != Status::kNotYetActivated)
  68. return;
  69. broadcaster_client_id_ = broadcaster_->AddClient(
  70. std::move(subscriber_), requested_settings_.buffer_type);
  71. status_ = Status::kActive;
  72. }
  73. void PushVideoStreamSubscriptionImpl::Suspend(SuspendCallback callback) {
  74. if (status_ != Status::kActive)
  75. return;
  76. broadcaster_->SuspendClient(broadcaster_client_id_);
  77. status_ = Status::kSuspended;
  78. std::move(callback).Run();
  79. }
  80. void PushVideoStreamSubscriptionImpl::Resume() {
  81. if (status_ != Status::kSuspended)
  82. return;
  83. broadcaster_->ResumeClient(broadcaster_client_id_);
  84. status_ = Status::kActive;
  85. }
  86. void PushVideoStreamSubscriptionImpl::GetPhotoState(
  87. GetPhotoStateCallback callback) {
  88. switch (status_) {
  89. case Status::kCreationCallbackNotYetRun: // Fall through.
  90. case Status::kClosed:
  91. // Ignore the call.
  92. return;
  93. case Status::kNotYetActivated: // Fall through.
  94. case Status::kActive: // Fall through.
  95. case Status::kSuspended:
  96. (*device_)->GetPhotoState(std::move(callback));
  97. return;
  98. }
  99. }
  100. void PushVideoStreamSubscriptionImpl::SetPhotoOptions(
  101. media::mojom::PhotoSettingsPtr settings,
  102. SetPhotoOptionsCallback callback) {
  103. switch (status_) {
  104. case Status::kCreationCallbackNotYetRun: // Fall through.
  105. case Status::kClosed:
  106. // Ignore the call.
  107. return;
  108. case Status::kNotYetActivated: // Fall through.
  109. case Status::kActive: // Fall through.
  110. case Status::kSuspended:
  111. (*device_)->SetPhotoOptions(std::move(settings), std::move(callback));
  112. return;
  113. }
  114. }
  115. void PushVideoStreamSubscriptionImpl::TakePhoto(TakePhotoCallback callback) {
  116. switch (status_) {
  117. case Status::kCreationCallbackNotYetRun: // Fall through.
  118. case Status::kClosed:
  119. // Ignore the call.
  120. return;
  121. case Status::kNotYetActivated: // Fall through.
  122. case Status::kActive: // Fall through.
  123. case Status::kSuspended:
  124. (*device_)->TakePhoto(std::move(callback));
  125. return;
  126. }
  127. }
  128. void PushVideoStreamSubscriptionImpl::Close(CloseCallback callback) {
  129. switch (status_) {
  130. case Status::kCreationCallbackNotYetRun:
  131. case Status::kClosed:
  132. std::move(callback).Run();
  133. return;
  134. case Status::kActive: // Fall through.
  135. case Status::kSuspended:
  136. broadcaster_->RemoveClient(broadcaster_client_id_);
  137. status_ = Status::kClosed;
  138. if (on_closed_handler_)
  139. std::move(on_closed_handler_).Run(std::move(callback));
  140. return;
  141. case Status::kNotYetActivated:
  142. status_ = Status::kClosed;
  143. if (on_closed_handler_)
  144. std::move(on_closed_handler_).Run(std::move(callback));
  145. return;
  146. }
  147. }
  148. void PushVideoStreamSubscriptionImpl::OnConnectionLost() {
  149. if (on_closed_handler_)
  150. std::move(on_closed_handler_).Run(base::DoNothing());
  151. }
  152. void PushVideoStreamSubscriptionImpl::ProcessFeedback(
  153. const media::VideoCaptureFeedback& feedback) {
  154. switch (status_) {
  155. case Status::kCreationCallbackNotYetRun: // Fall through.
  156. case Status::kClosed:
  157. // Ignore the call.
  158. return;
  159. case Status::kNotYetActivated: // Fall through.
  160. case Status::kActive: // Fall through.
  161. case Status::kSuspended:
  162. (*device_)->ProcessFeedback(feedback);
  163. return;
  164. }
  165. }
  166. } // namespace video_capture