• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef AUDIO_SESSION_DEVICE_INFO_H
16 #define AUDIO_SESSION_DEVICE_INFO_H
17 
18 #include "audio_device_descriptor.h"
19 #include "audio_device_info.h"
20 
21 namespace OHOS {
22 namespace AudioStandard {
23 
24 /**
25  * Enumerates the recommend action when device changed.
26  * @since 20
27  */
28 enum class OutputDeviceChangeRecommendedAction {
29     /**
30      * No special recommendations, the playback can be continue or not.
31      */
32     RECOMMEND_TO_CONTINUE = 0,
33     /**
34      * Recommend to stop the playback.
35      */
36     RECOMMEND_TO_STOP = 1,
37 };
38 
39 /**
40  * Audio session device change info.
41  * @since 20
42  */
43 struct CurrentOutputDeviceChangedEvent : public Parcelable {
44     /**
45      * Audio device descriptors after changed.
46      * @since 20
47      */
48     std::vector<std::shared_ptr<AudioDeviceDescriptor>> devices;
49     /**
50      * Audio device changed reason.
51      * @since 20
52      */
53     AudioStreamDeviceChangeReason changeReason;
54     /**
55      * Recommend action when device changed.
56      * @since 20
57      */
58     OutputDeviceChangeRecommendedAction recommendedAction;
59     static constexpr int32_t DEVICE_CHANGE_VALID_SIZE = 128;
60 
MarshallingCurrentOutputDeviceChangedEvent61     bool Marshalling(Parcel &parcel) const override
62     {
63         parcel.WriteInt32(static_cast<int32_t>(changeReason));
64         parcel.WriteInt32(static_cast<int32_t>(recommendedAction));
65         int32_t size = static_cast<int32_t>(devices.size());
66         parcel.WriteInt32(size);
67         for (int i = 0; i < size; i++) {
68             if (devices[i] != nullptr) {
69                 devices[i]->Marshalling(parcel);
70             }
71         }
72         return true;
73     }
74 
UnmarshallingCurrentOutputDeviceChangedEvent75     static CurrentOutputDeviceChangedEvent *Unmarshalling(Parcel &parcel)
76     {
77         auto event = new(std::nothrow) CurrentOutputDeviceChangedEvent();
78         if (event == nullptr) {
79             return nullptr;
80         }
81 
82         event->changeReason = static_cast<AudioStreamDeviceChangeReason>(parcel.ReadInt32());
83         event->recommendedAction = static_cast<OutputDeviceChangeRecommendedAction>(parcel.ReadInt32());
84         int32_t size = parcel.ReadInt32();
85         if (size < 0 || size >= DEVICE_CHANGE_VALID_SIZE) {
86             delete event;
87             return nullptr;
88         }
89         for (int32_t i = 0; i < size; i++) {
90             auto device = AudioDeviceDescriptor::Unmarshalling(parcel);
91             if (device != nullptr) {
92                 event->devices.emplace_back(std::shared_ptr<AudioDeviceDescriptor>(device));
93             }
94         }
95         return event;
96     }
97 };
98 } // namespace AudioStandard
99 } // namespace OHOS
100 #endif // AUDIO_SESSION_DEVICE_INFO_H
101