• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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
16import rpc from "@ohos.rpc"
17import avSession from '@ohos.multimedia.avsession';
18import backgroundTaskManager from '@ohos.backgroundTaskManager';
19import featureAbility from '@ohos.ability.featureAbility';
20import wantAgent from '@ohos.wantAgent';
21
22function startContinuousTask() {
23    let wantAgentInfo = {
24        wants: [
25            {
26                bundleName: "com.example.myapplication",
27                abilityName: "com.example.myapplication.ServiceAbility"
28            }
29        ],
30        operationType: wantAgent.OperationType.START_SERVICE,
31        requestCode: 0,
32        wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
33    };
34
35    wantAgent.getWantAgent(wantAgentInfo).then((wantAgentObj) => {
36        try {
37            backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
38            backgroundTaskManager.BackgroundMode.MULTI_DEVICE_CONNECTION, wantAgentObj).then(() => {
39                console.info(`Operation startBackgroundRunning succeeded`);
40            }).catch((error) => {
41                console.error(`Operation startBackgroundRunning failed. code is ${error.code}, message is ${error.message}`);
42            });
43        } catch (error) {
44            console.error(`Operation startBackgroundRunning failed. code is ${error.code}, message is ${error.message}`);
45        }
46    });
47}
48
49function stopContinuousTask() {
50    try {
51        backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext()).then(() => {
52            console.info(`Operation stopBackgroundRunning failed cause: ${err}`);
53        }) .catch((error) => {
54            console.error(`Operation stopBackgroundRunning failed. code is ${error.code}, message is ${error.message}`);
55        })
56    } catch (error) {
57        console.error(`Operation stopBackgroundRunning failed. code is ${error.code}, message is ${error.message}`);
58    }
59
60}
61
62export default {
63    onStart() {
64        console.info('AVSessionServer: onStart');
65        startContinuousTask();
66        console.info('AVSessionServer: startContinuousTask');
67    },
68    onStop() {
69        console.info('AVSessionServer: onStop');
70        stopContinuousTask();
71        console.info('AVSessionServer: stopContinuousTask');
72    },
73    onCommand(want, startId) {
74        console.info('AVSessionServer: onCommand, want: ' + JSON.stringify(want) + ', startId: ' + startId)
75    },
76    onConnect(want) {
77        console.info('AVSessionServer: service onConnect called.')
78        return new Stub("rpcTestAbility")
79    },
80    onDisconnect(want) {
81        console.info('AVSessionServer: service onDisConnect called.')
82    },
83    onReconnect(want) {
84        console.info('AVSessionServer: service onReConnect called.')
85    }
86}
87
88const CODE_CAST_AUDIO = 1;
89let descriptor;
90let controller;
91
92class Stub extends rpc.RemoteObject {
93    constructor(descriptor) {
94        super(descriptor);
95    }
96
97    async onRemoteMessageRequest(code, data, reply, option) {
98        try {
99            console.info(`AVSessionServer: onRemoteRequest: ${code}`);
100            switch (code) {
101                case CODE_CAST_AUDIO:
102                {
103                    console.info('AVSessionServer:case  CODE_CAST_AUDIO');
104                    await avSession.getAllSessionDescriptors().then((descriptors) => {
105                        console.info(descriptors.length);
106                        console.info('AVSessionServer: Get descriptors Successfully');
107                        if (descriptors.length === 0) {
108                            console.info('AVSessionServer: Get descriptors : Fail');
109                        }
110                        descriptor = descriptors[0];
111                    }).catch((err) => {
112                        console.info(`AVSessionServer: ${err.message}`);
113                        return false;
114                    });
115
116                    await avSession.createController(descriptor.sessionId).then((data) => {
117                        console.info('AVSessionServer: Create controller Successfully');
118                        controller = data;
119                    }).catch((err) => {
120                        console.info(`AVSessionServer: ${err.message}`);
121                        return false;
122                    });
123
124                    await controller.getAVMetadata().then((data) => {
125                        console.info(data.assetId);
126                        console.info(data.artist);
127                        console.info('AVSessionServer: Get Metadata');
128                        if (data.assetId === '121278' && data.artist === 'Eminem') {
129                            console.info(`AVSessionServer: Get Metadata is ${data}`);
130                            let writeResult = reply.writeString('case 1 get successfully');
131                            console.info(`AVSessionServer writeString result is ${writeResult}`);
132                            return true;
133                        }
134                    }).catch((err) => {
135                        console.info(`AVSessionServer: ${err.message}`);
136                        return false;
137                    });
138
139                }
140                default:
141                    console.error(`AVSessionServer: default case code is ${code}`);
142                    return super.onRemoteMessageRequest(code, data, reply, option);
143            }
144        } catch (error) {
145            console.info(`AVSessionServer: onRemoteRequest: ${error.message}`);
146        }
147        return false;
148    }
149}